|
| 1 | +import assert from 'node:assert'; |
| 2 | + |
| 3 | +import { type PromptVariables, PromptHub } from '@cozeloop/ai'; |
| 4 | + |
| 5 | +export async function run() { |
| 6 | + const hub = new PromptHub({ |
| 7 | + /** workspace id, use process.env.COZELOOP_WORKSPACE_ID when unprovided */ |
| 8 | + // workspaceId: 'your_workspace_id', |
| 9 | + apiClient: { |
| 10 | + // baseURL: 'api_base_url', |
| 11 | + // token: 'your_api_token', |
| 12 | + }, |
| 13 | + }); |
| 14 | + |
| 15 | + // 1. getPrompt |
| 16 | + const key = 'loop'; |
| 17 | + const version = '0.0.3'; |
| 18 | + const prompt = await hub.getPrompt(key, version); |
| 19 | + // { |
| 20 | + // workspace_id: '7308703665823416358', |
| 21 | + // prompt_key: 'loop', |
| 22 | + // version: '0.0.3', |
| 23 | + // prompt_template: { |
| 24 | + // template_type: 'normal', |
| 25 | + // messages: [ |
| 26 | + // { |
| 27 | + // role: 'system', |
| 28 | + // content: 'You are a helpful bot, the conversation topic is {{var1}}.', |
| 29 | + // }, |
| 30 | + // { role: 'placeholder', content: 'placeholder1' }, |
| 31 | + // { content: 'My question is {{var2}}', role: 'user' }, |
| 32 | + // { role: 'placeholder', content: 'placeholder2' }, |
| 33 | + // { |
| 34 | + // role: 'user', |
| 35 | + // content: '', |
| 36 | + // parts: [ |
| 37 | + // { type: 'text', text: 'text2{{var2}}\n' }, |
| 38 | + // { type: 'multi_part_variable', text: 'img1' }, |
| 39 | + // { type: 'text', text: '\ntext3{{var3}}' }, |
| 40 | + // ], |
| 41 | + // }, |
| 42 | + // ], |
| 43 | + // variable_defs: [ |
| 44 | + // { key: 'var1', desc: '', type: 'string' }, |
| 45 | + // { key: 'var2', desc: '', type: 'string' }, |
| 46 | + // { key: 'var3', desc: '', type: 'string' }, |
| 47 | + // { key: 'img1', desc: '', type: 'multi_part' }, |
| 48 | + // { key: 'placeholder1', desc: '', type: 'placeholder' }, |
| 49 | + // { desc: '', type: 'placeholder', key: 'placeholder2' }, |
| 50 | + // ], |
| 51 | + // }, |
| 52 | + // llm_config: { |
| 53 | + // temperature: 1, |
| 54 | + // max_tokens: 4096, |
| 55 | + // top_p: 0.7, |
| 56 | + // frequency_penalty: 0, |
| 57 | + // }, |
| 58 | + // } |
| 59 | + |
| 60 | + assert.strictEqual(prompt?.prompt_key, key); |
| 61 | + assert.strictEqual(prompt.version, version); |
| 62 | + |
| 63 | + // 2. formatPrompt with variables |
| 64 | + const variables: PromptVariables = { |
| 65 | + var1: 'value_of_var1', |
| 66 | + var2: 'value_of_var2', |
| 67 | + var3: 'value_of_var3', |
| 68 | + placeholder1: { role: 'assistant', content: 'user' }, |
| 69 | + img1: [ |
| 70 | + { type: 'text', text: 'text' }, |
| 71 | + { |
| 72 | + type: 'image_url', |
| 73 | + image_url: { url: 'https://expample.com/dot.png' }, |
| 74 | + }, |
| 75 | + ], |
| 76 | + }; |
| 77 | + const messages = hub.formatPrompt(prompt, variables); |
| 78 | + // [ |
| 79 | + // { |
| 80 | + // role: 'system', |
| 81 | + // content: |
| 82 | + // 'You are a helpful bot, the conversation topic is value_of_var1.', |
| 83 | + // }, |
| 84 | + // { |
| 85 | + // role: 'assistant', |
| 86 | + // content: 'user', |
| 87 | + // }, |
| 88 | + // { |
| 89 | + // role: 'user', |
| 90 | + // content: 'My question is value_of_var2', |
| 91 | + // }, |
| 92 | + // { |
| 93 | + // role: 'user', |
| 94 | + // content: '', |
| 95 | + // parts: [ |
| 96 | + // { |
| 97 | + // type: 'text', |
| 98 | + // text: 'text2value_of_var2\n', |
| 99 | + // }, |
| 100 | + // { |
| 101 | + // type: 'text', |
| 102 | + // text: 'text', |
| 103 | + // }, |
| 104 | + // { |
| 105 | + // type: 'image_url', |
| 106 | + // image_url: { |
| 107 | + // url: 'https://expample.com/dot.png', |
| 108 | + // }, |
| 109 | + // }, |
| 110 | + // { |
| 111 | + // type: 'text', |
| 112 | + // text: '\ntext3value_of_var3', |
| 113 | + // }, |
| 114 | + // ], |
| 115 | + // }, |
| 116 | + // ] |
| 117 | + assert.ok(messages.length); |
| 118 | +} |
| 119 | + |
| 120 | +run(); |
0 commit comments