46 lines
1.5 KiB
TypeScript
46 lines
1.5 KiB
TypeScript
|
||
import { schema, app, cnb, models } from '@/app.ts'
|
||
import z from 'zod';
|
||
import { runAgent } from '@kevisual/ai/agent'
|
||
import dayjs from 'dayjs';
|
||
|
||
app.route({
|
||
path: 'flowme-life',
|
||
key: 'chat',
|
||
description: `聊天接口, 对自己的数据进行操作,参数是 question或messages,question是用户的提问,messages是对话消息列表,优先级高于 question`,
|
||
middleware: ['auth']
|
||
, metadata: {
|
||
args: {
|
||
question: z.string().describe('用户的提问'),
|
||
messages: z.any().optional().describe('对话消息列表,优先级高于 question'),
|
||
}
|
||
}
|
||
}).define(async (ctx) => {
|
||
const question = ctx.query.question || '';
|
||
const _messages = ctx.query.messages;
|
||
const token = ctx.query.token || '';
|
||
if (!question && !_messages) {
|
||
ctx.throw(400, '缺少参数 question 或 messages');
|
||
}
|
||
const routes = ctx.app.getList().filter(r => r.path.startsWith('flowme-life') && r.key !== 'chat');
|
||
const currentTime = dayjs().toISOString();
|
||
const messages = _messages || [
|
||
{
|
||
"role": "system" as const,
|
||
"content": `你是我的智能助手,协助我操作我的数据, 请根据我的提问选择合适的接口进行调用。当前时间是 ${currentTime}。`
|
||
},
|
||
{
|
||
"role": "user" as const,
|
||
"content": question
|
||
}
|
||
]
|
||
const res = await runAgent({
|
||
app: app,
|
||
messages: messages,
|
||
languageModel: cnb(models['auto']),
|
||
// query: 'WHERE path LIKE 'flowme-life%',
|
||
routes,
|
||
token,
|
||
});
|
||
ctx.body = res
|
||
}).addTo(app); |