Skip to content

Commit 7b172ca

Browse files
authored
[feat] [sdk] bump @cozeloop/ai to 0.0.6 (#14)
* feat(cozeloop-ai): prompt hub support jinja2 template type * test(cozeloop-ai): add prompt/utils and tracer/adapt test cases * fix(cozeloop-ai): nunjucks cjs in esm * ci(workflow): add permissions * feat(example): add with-jinja * feat(cozeloop-ai): bump to 0.0.6 * fix: lic header
1 parent 1df768d commit 7b172ca

19 files changed

Lines changed: 377 additions & 90 deletions

File tree

.github/workflows/ci.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ on:
66
pull_request:
77
branches: ['main']
88

9+
permissions:
10+
contents: read
11+
pull-requests: read
12+
913
jobs:
1014
build:
1115
name: Build and Test

.github/workflows/issue-sync.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ on:
44
issues:
55
types: ['opened', 'reopened']
66

7+
permissions:
8+
contents: read
9+
pull-requests: read
10+
711
jobs:
812
sync:
913
name: Sync Issues

.github/workflows/license-check.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ on:
55
branches: ['ci/**', 'main']
66
pull_request:
77
branches: ['main']
8-
workflow_dispatch:
8+
9+
permissions:
10+
contents: read
11+
pull-requests: read
912

1013
jobs:
1114
license-check:

.github/workflows/pr-sync.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ on:
44
pull_request_target:
55
types: ['opened', 'reopened', 'closed']
66

7+
permissions:
8+
contents: read
9+
pull-requests: read
10+
711
jobs:
812
sync:
913
name: Send Lark Message

.github/workflows/semantic-pull-request.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ name: Semantic Pull Request
22

33
on:
44
pull_request:
5-
types:
6-
- opened
7-
- reopened
8-
- edited
5+
types: ['opened', 'reopened', 'edited']
6+
7+
permissions:
8+
contents: read
9+
pull-requests: read
910

1011
concurrency:
1112
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event.number }}

common/config/rush/pnpm-lock.yaml

Lines changed: 43 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/cozeloop-ai-node/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { config } from 'dotenv';
22

33
import { run as runTrace } from './tracer';
4-
import { run as runPromptHub } from './prompt/hub';
4+
import { run as runPromptHub } from './prompt';
55
import { run as runOAuthJwt } from './auth/oauth-jwt';
66
import { run as runApiClient } from './api/api-client';
77

@@ -25,6 +25,8 @@ async function run() {
2525
.catch(e => console.error(`❌ ${task.name} error=${e}`));
2626

2727
await Promise.all(tasks.map(it => runTask(it)));
28+
29+
process.exit(0);
2830
}
2931

3032
config();

examples/cozeloop-ai-node/src/prompt/hub.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ export async function run() {
1414

1515
// 1. getPrompt
1616
const key = 'loop';
17-
const version = '0.0.3';
17+
const version = '0.0.1';
1818
const prompt = await hub.getPrompt(key, version);
1919
// {
20-
// workspace_id: '7306823955623854124',
20+
// workspace_id: '7308703665823416358',
2121
// prompt_key: 'loop',
22-
// version: '0.0.3',
22+
// version: '0.0.1',
2323
// prompt_template: {
2424
// template_type: 'normal',
2525
// messages: [
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { run as runWithJinja } from './with-jinja';
2+
import { run as runBasic } from './hub';
3+
4+
export async function run() {
5+
await Promise.all([runBasic(), runWithJinja()]);
6+
7+
process.exit(0);
8+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/* eslint-disable max-len -- skip */
2+
import assert from 'node:assert';
3+
4+
import { type Message, type PromptVariables, PromptHub } from '@cozeloop/ai';
5+
6+
export async function run() {
7+
const hub = new PromptHub({
8+
/** workspace id, use process.env.COZELOOP_WORKSPACE_ID when unprovided */
9+
// workspaceId: 'your_workspace_id',
10+
apiClient: {
11+
// baseURL: 'api_base_url',
12+
// token: 'your_api_token',
13+
},
14+
});
15+
16+
// 1. getPrompt
17+
const key = 'loop12';
18+
const version = '0.0.3';
19+
const prompt = await hub.getPrompt(key, version);
20+
// {
21+
// workspace_id: '7308703665823416358',
22+
// prompt_key: 'loop12',
23+
// version: '0.0.3',
24+
// prompt_template: {
25+
// template_type: 'normal',
26+
// messages: [
27+
// {
28+
// role: 'system',
29+
// content:
30+
// '{# 注释:这是一个 Jinja2 模板示例 #}\n标题: {{ title | default("默认标题") }}\n\n{%- if user.is_authenticated %}\n你好, {{ user.name }}!\n{%- else %}\n请登录。\n{%- endif %}\n\n项目列表:\n{%- for item in items %}\n - {{ loop.index }}: {{ item.name | upper }}\n{%- else %}\n - 暂无项目。\n{%- endfor %}\n\n原始HTML: {{ "<strong>不转义</strong>" | safe }}\n\n{# 定义一个宏 #}\n{% macro greet(person) %}\nHello, {{ person }}!\n{% endmacro %}\n{{ greet(user.name) }}\n\n项目总数: {{ items | length }}',
31+
// },
32+
// { role: 'placeholder', content: 'pl' },
33+
// ],
34+
// },
35+
// llm_config: { max_tokens: 1000, top_p: 1, temperature: 0.7 },
36+
// tools: [],
37+
// }
38+
39+
assert.strictEqual(prompt?.prompt_key, key);
40+
assert.strictEqual(prompt.version, version);
41+
42+
// 2. formatPrompt with variables
43+
const placeholderMessages: Message[] = [
44+
{ role: 'assistant', content: 'Hello!' },
45+
{ role: 'user', content: 'Hello!' },
46+
];
47+
const variables: PromptVariables = {
48+
title: '示例标题',
49+
user: {
50+
is_authenticated: true,
51+
name: '张三',
52+
},
53+
items: [{ name: '项目一' }, { name: '项目二' }, { name: '项目三' }],
54+
pl: placeholderMessages,
55+
};
56+
const messages = hub.formatPrompt(prompt, variables);
57+
// [
58+
// {
59+
// role: 'system',
60+
// content:
61+
// '\n标题: 示例标题\n你好, 张三!\n\n项目列表:\n - 1: 项目一\n - 2: 项目二\n - 3: 项目三\n\n原始HTML: <strong>不转义</strong>\n\n\n\n\nHello, 张三!\n\n\n项目总数: 3',
62+
// },
63+
// { role: 'assistant', content: 'Hello!' },
64+
// { role: 'user', content: 'Hello!' },
65+
// ];
66+
67+
assert.ok(messages.length);
68+
}

0 commit comments

Comments
 (0)