update
This commit is contained in:
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
node_modules
|
||||||
|
|
||||||
|
.env
|
||||||
|
!.env*.example
|
||||||
20
package.json
Normal file
20
package.json
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
{
|
||||||
|
"name": "test_router_prompts",
|
||||||
|
"version": "0.0.1",
|
||||||
|
"description": "",
|
||||||
|
"main": "index.js",
|
||||||
|
"scripts": {
|
||||||
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
|
},
|
||||||
|
"keywords": [],
|
||||||
|
"author": "abearxiong <xiongxiao@xiongxiao.me> (https://www.xiongxiao.me)",
|
||||||
|
"license": "MIT",
|
||||||
|
"packageManager": "pnpm@10.16.1",
|
||||||
|
"type": "module",
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/node": "^24.9.1"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"dotenv": "^17.2.3"
|
||||||
|
}
|
||||||
|
}
|
||||||
39
pnpm-lock.yaml
generated
Normal file
39
pnpm-lock.yaml
generated
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
lockfileVersion: '9.0'
|
||||||
|
|
||||||
|
settings:
|
||||||
|
autoInstallPeers: true
|
||||||
|
excludeLinksFromLockfile: false
|
||||||
|
|
||||||
|
importers:
|
||||||
|
|
||||||
|
.:
|
||||||
|
dependencies:
|
||||||
|
dotenv:
|
||||||
|
specifier: ^17.2.3
|
||||||
|
version: 17.2.3
|
||||||
|
devDependencies:
|
||||||
|
'@types/node':
|
||||||
|
specifier: ^24.9.1
|
||||||
|
version: 24.9.1
|
||||||
|
|
||||||
|
packages:
|
||||||
|
|
||||||
|
'@types/node@24.9.1':
|
||||||
|
resolution: {integrity: sha512-QoiaXANRkSXK6p0Duvt56W208du4P9Uye9hWLWgGMDTEoKPhuenzNcC4vGUmrNkiOKTlIrBoyNQYNpSwfEZXSg==}
|
||||||
|
|
||||||
|
dotenv@17.2.3:
|
||||||
|
resolution: {integrity: sha512-JVUnt+DUIzu87TABbhPmNfVdBDt18BLOWjMUFJMSi/Qqg7NTYtabbvSNJGOJ7afbRuv9D/lngizHtP7QyLQ+9w==}
|
||||||
|
engines: {node: '>=12'}
|
||||||
|
|
||||||
|
undici-types@7.16.0:
|
||||||
|
resolution: {integrity: sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw==}
|
||||||
|
|
||||||
|
snapshots:
|
||||||
|
|
||||||
|
'@types/node@24.9.1':
|
||||||
|
dependencies:
|
||||||
|
undici-types: 7.16.0
|
||||||
|
|
||||||
|
dotenv@17.2.3: {}
|
||||||
|
|
||||||
|
undici-types@7.16.0: {}
|
||||||
86
src/index.ts
Normal file
86
src/index.ts
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
import dotenv from 'dotenv';
|
||||||
|
import path from 'path';
|
||||||
|
dotenv.config();
|
||||||
|
|
||||||
|
const token = process.env.BAILIAN_API_KEY || '';
|
||||||
|
|
||||||
|
const chat = async (message: { role: string, content: string }[]) => {
|
||||||
|
const baseURL = 'https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions';
|
||||||
|
|
||||||
|
return fetch(baseURL, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
'Authorization': `Bearer ${token}`
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
model: 'qwen-turbo-latest',
|
||||||
|
messages: message,
|
||||||
|
response_format: {
|
||||||
|
type: 'json_object'
|
||||||
|
},
|
||||||
|
stream: false
|
||||||
|
})
|
||||||
|
}).then(res => res.json());
|
||||||
|
};
|
||||||
|
|
||||||
|
const fn1 = {
|
||||||
|
id: 'get_weather',
|
||||||
|
description: '获取天气',
|
||||||
|
fn: async (params: { location: string }) => {
|
||||||
|
return `当前${params.location}的天气是:晴,25度,湿度60%。`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const fn2 = {
|
||||||
|
id: 'get_time',
|
||||||
|
description: '获取时间',
|
||||||
|
fn: async (params: { timezone: string }) => {
|
||||||
|
return `当前${params.timezone}的时间是:2024-06-01 10:00 AM。`;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const fns = [fn1, fn2];
|
||||||
|
const callPrompst = `你是一个函数调用助手,可以根据用户的需求选择合适的函数进行调用,并返回 JSON 数据, 格式为 { id: string, payload: {} } ,函数列表如下:
|
||||||
|
|
||||||
|
${fns.map(fn => `id: ${fn.id}
|
||||||
|
description: ${fn.description}`).join('\n\n')}
|
||||||
|
`;
|
||||||
|
|
||||||
|
const pickJson = (text: string) => {
|
||||||
|
const jsonStart = text.indexOf('{');
|
||||||
|
const jsonEnd = text.lastIndexOf('}');
|
||||||
|
if (jsonStart !== -1 && jsonEnd !== -1) {
|
||||||
|
const jsonString = text.substring(jsonStart, jsonEnd + 1);
|
||||||
|
try {
|
||||||
|
return JSON.parse(jsonString);
|
||||||
|
} catch (e) {
|
||||||
|
console.error('JSON 解析错误:', e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
const main = async () => {
|
||||||
|
const userMessage = '请告诉我北京的天气。';
|
||||||
|
|
||||||
|
const messages = [
|
||||||
|
{ role: 'system', content: callPrompst },
|
||||||
|
{ role: 'user', content: userMessage }
|
||||||
|
];
|
||||||
|
// 获取函数调用建议
|
||||||
|
const response1 = await chat(messages);
|
||||||
|
const message1 = response1.choices[0].message;
|
||||||
|
console.log('AI Response:', message1);
|
||||||
|
const json = pickJson(message1.content);
|
||||||
|
console.log('Parsed JSON:', json);
|
||||||
|
|
||||||
|
const fn = fns.find(f => f.id === json?.id);
|
||||||
|
if (fn) {
|
||||||
|
const fnResult = await fn.fn({ location: '北京', timezone: '纽约' });
|
||||||
|
console.log(`Function ${fn.id} result:`, fnResult);
|
||||||
|
} else {
|
||||||
|
console.log('No matching function found.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
main();
|
||||||
Reference in New Issue
Block a user