Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ on:
pull_request:
branches: ['main']

permissions:
contents: read
pull-requests: read

jobs:
build:
name: Build and Test
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/issue-sync.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ on:
issues:
types: ['opened', 'reopened']

permissions:
contents: read
pull-requests: read

jobs:
sync:
name: Sync Issues
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/license-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ on:
branches: ['ci/**', 'main']
pull_request:
branches: ['main']
workflow_dispatch:

permissions:
contents: read
pull-requests: read

jobs:
license-check:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/pr-sync.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ on:
pull_request_target:
types: ['opened', 'reopened', 'closed']

permissions:
contents: read
pull-requests: read

jobs:
sync:
name: Send Lark Message
Expand Down
9 changes: 5 additions & 4 deletions .github/workflows/semantic-pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ name: Semantic Pull Request

on:
pull_request:
types:
- opened
- reopened
- edited
types: ['opened', 'reopened', 'edited']

permissions:
contents: read
pull-requests: read

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event.number }}
Expand Down
43 changes: 43 additions & 0 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion examples/cozeloop-ai-node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { config } from 'dotenv';

import { run as runTrace } from './tracer';
import { run as runPromptHub } from './prompt/hub';
import { run as runPromptHub } from './prompt';
import { run as runOAuthJwt } from './auth/oauth-jwt';
import { run as runApiClient } from './api/api-client';

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

await Promise.all(tasks.map(it => runTask(it)));

process.exit(0);
}

config();
Expand Down
6 changes: 3 additions & 3 deletions examples/cozeloop-ai-node/src/prompt/hub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ export async function run() {

// 1. getPrompt
const key = 'loop';
const version = '0.0.3';
const version = '0.0.1';
const prompt = await hub.getPrompt(key, version);
// {
// workspace_id: '7306823955623854124',
// workspace_id: '7308703665823416358',
// prompt_key: 'loop',
// version: '0.0.3',
// version: '0.0.1',
// prompt_template: {
// template_type: 'normal',
// messages: [
Expand Down
8 changes: 8 additions & 0 deletions examples/cozeloop-ai-node/src/prompt/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { run as runWithJinja } from './with-jinja';
import { run as runBasic } from './hub';

export async function run() {
await Promise.all([runBasic(), runWithJinja()]);

process.exit(0);
}
68 changes: 68 additions & 0 deletions examples/cozeloop-ai-node/src/prompt/with-jinja.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/* eslint-disable max-len -- skip */
import assert from 'node:assert';

import { type Message, type PromptVariables, PromptHub } from '@cozeloop/ai';

export async function run() {
const hub = new PromptHub({
/** workspace id, use process.env.COZELOOP_WORKSPACE_ID when unprovided */
// workspaceId: 'your_workspace_id',
apiClient: {
// baseURL: 'api_base_url',
// token: 'your_api_token',
},
});

// 1. getPrompt
const key = 'loop12';
const version = '0.0.3';
const prompt = await hub.getPrompt(key, version);
// {
// workspace_id: '7308703665823416358',
// prompt_key: 'loop12',
// version: '0.0.3',
// prompt_template: {
// template_type: 'normal',
// messages: [
// {
// role: 'system',
// content:
// '{# 注释:这是一个 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 }}',
// },
// { role: 'placeholder', content: 'pl' },
// ],
// },
// llm_config: { max_tokens: 1000, top_p: 1, temperature: 0.7 },
// tools: [],
// }

assert.strictEqual(prompt?.prompt_key, key);
assert.strictEqual(prompt.version, version);

// 2. formatPrompt with variables
const placeholderMessages: Message[] = [
{ role: 'assistant', content: 'Hello!' },
{ role: 'user', content: 'Hello!' },
];
const variables: PromptVariables = {
title: '示例标题',
user: {
is_authenticated: true,
name: '张三',
},
items: [{ name: '项目一' }, { name: '项目二' }, { name: '项目三' }],
pl: placeholderMessages,
};
const messages = hub.formatPrompt(prompt, variables);
// [
// {
// role: 'system',
// content:
// '\n标题: 示例标题\n你好, 张三!\n\n项目列表:\n - 1: 项目一\n - 2: 项目二\n - 3: 项目三\n\n原始HTML: <strong>不转义</strong>\n\n\n\n\nHello, 张三!\n\n\n项目总数: 3',
// },
// { role: 'assistant', content: 'Hello!' },
// { role: 'user', content: 'Hello!' },
// ];

assert.ok(messages.length);
}
5 changes: 4 additions & 1 deletion packages/cozeloop-ai/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# 🕗 Change Log - @cozeloop/ai
# 🕗 ChangeLog - @cozeloop/ai

## 0.0.6
* PromptHub: support prompt with Jinja2 template type

## 0.0.5
* fix: variable_defs may be undefined (by @othorizon)
Expand Down
Loading
Loading