|
| 1 | +import assert from 'node:assert'; |
| 2 | + |
| 3 | +import { PromptAsAService } from '@cozeloop/ai'; |
| 4 | + |
| 5 | +async function runWithNormal() { |
| 6 | + const model = new PromptAsAService({ |
| 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 | + prompt: { |
| 14 | + prompt_key: 'CozeLoop_Travel_Master', |
| 15 | + version: '0.0.2', |
| 16 | + }, |
| 17 | + }); |
| 18 | + |
| 19 | + // 1. model.invoke |
| 20 | + const reply = await model.invoke({ |
| 21 | + messages: [{ role: 'user', content: '帮我规划轻松旅行' }], |
| 22 | + variables: { |
| 23 | + departure: '北京', |
| 24 | + destination: '上海', |
| 25 | + people_num: 2, |
| 26 | + days_num: 1, |
| 27 | + travel_theme: '亲子', |
| 28 | + }, |
| 29 | + }); |
| 30 | + |
| 31 | + assert(reply?.message); |
| 32 | + assert(reply.usage); |
| 33 | + assert.strictEqual(reply.finish_reason, 'stop'); |
| 34 | + |
| 35 | + // 2. model.stream |
| 36 | + const replyStream = await model.stream({ |
| 37 | + messages: [{ role: 'user', content: '帮我规划轻松旅行' }], |
| 38 | + variables: { |
| 39 | + departure: '北京', |
| 40 | + destination: '上海', |
| 41 | + people_num: 2, |
| 42 | + days_num: 1, |
| 43 | + travel_theme: '亲子', |
| 44 | + }, |
| 45 | + }); |
| 46 | + |
| 47 | + for await (const chunk of replyStream) { |
| 48 | + assert(chunk); |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +async function runWithJinja() { |
| 53 | + const model = new PromptAsAService({ |
| 54 | + /** workspace id, use process.env.COZELOOP_WORKSPACE_ID when unprovided */ |
| 55 | + // workspaceId: 'your_workspace_id', |
| 56 | + apiClient: { |
| 57 | + // baseURL: 'api_base_url', |
| 58 | + // token: 'your_api_token', |
| 59 | + }, |
| 60 | + prompt: { |
| 61 | + prompt_key: 'loop12', |
| 62 | + version: '0.0.5', |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + // 1. model.invoke |
| 67 | + const reply = await model.invoke({ |
| 68 | + messages: [{ role: 'user', content: '总结模板内容' }], |
| 69 | + variables: { |
| 70 | + title: 'Title', |
| 71 | + user: { |
| 72 | + is_authenticated: false, |
| 73 | + name: 'Loop', |
| 74 | + }, |
| 75 | + items: [{ name: 'fish' }], |
| 76 | + place: [{ role: 'assistant', content: '好的' }], |
| 77 | + }, |
| 78 | + }); |
| 79 | + |
| 80 | + assert(reply?.message); |
| 81 | + assert(reply.usage); |
| 82 | + assert.strictEqual(reply.finish_reason, 'stop'); |
| 83 | +} |
| 84 | + |
| 85 | +async function runWithMultiPart() { |
| 86 | + const model = new PromptAsAService({ |
| 87 | + /** workspace id, use process.env.COZELOOP_WORKSPACE_ID when unprovided */ |
| 88 | + // workspaceId: 'your_workspace_id', |
| 89 | + apiClient: { |
| 90 | + // baseURL: 'api_base_url', |
| 91 | + // token: 'your_api_token', |
| 92 | + }, |
| 93 | + prompt: { |
| 94 | + prompt_key: 'loop', |
| 95 | + version: '0.0.3', |
| 96 | + }, |
| 97 | + }); |
| 98 | + |
| 99 | + const replyStream = await model.stream({ |
| 100 | + messages: [{ role: 'user', content: 'respond in 50 words' }], |
| 101 | + variables: { |
| 102 | + var1: 'sports', |
| 103 | + placeholder1: { role: 'assistant', content: 'go on' }, |
| 104 | + var2: 'how to play football', |
| 105 | + img1: [ |
| 106 | + { type: 'text', text: 'text1' }, |
| 107 | + { |
| 108 | + type: 'image_url', |
| 109 | + image_url: { |
| 110 | + url: 'https://tinypng.com/static/images/george-anim/large_george_x2.webp', |
| 111 | + }, |
| 112 | + }, |
| 113 | + ], |
| 114 | + }, |
| 115 | + }); |
| 116 | + |
| 117 | + for await (const chunk of replyStream) { |
| 118 | + assert(chunk); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +export async function run() { |
| 123 | + await Promise.all([ |
| 124 | + runWithNormal(), // 普通模板 |
| 125 | + runWithJinja(), // Jinja2 模板 |
| 126 | + runWithMultiPart(), // 多模态变量 |
| 127 | + ]); |
| 128 | + |
| 129 | + process.exit(); |
| 130 | +} |
| 131 | + |
| 132 | +run(); |
0 commit comments