feat: 添加cnb智能对话接口,支持用户提问和消息列表处理
This commit is contained in:
36
agent/routes/chat/chat.ts
Normal file
36
agent/routes/chat/chat.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { runAgent } from '@kevisual/ai/agent'
|
||||
import { app, notCNBCheck, cnbAi } from '../../app.ts';
|
||||
import z from 'zod';
|
||||
|
||||
app.route({
|
||||
path: 'cnb',
|
||||
key: 'chat',
|
||||
description: 'cnb智能对话接口',
|
||||
middleware: ['auth'],
|
||||
metadata: {
|
||||
args: {
|
||||
question: z.string().describe('用户输入的问题'),
|
||||
messages: z.array(z.object({
|
||||
role: z.enum(['user', 'assistant']).describe('消息角色,user表示用户输入,assistant表示助手回复'),
|
||||
content: z.string().describe('消息内容')
|
||||
})).describe('对话消息列表,按照时间顺序排列,包含用户和助手的历史消息')
|
||||
}
|
||||
}
|
||||
}).define(async (ctx) => {
|
||||
// notCNBCheck(ctx);
|
||||
if (!ctx.args.question && !ctx.args.messages) {
|
||||
ctx.throw(400, '缺少必要参数,必须提供question或messages');
|
||||
return;
|
||||
}
|
||||
const messages = ctx.args.messages || [{
|
||||
role: 'user',
|
||||
content: ctx.args.question
|
||||
}]
|
||||
const result = await runAgent({
|
||||
app,
|
||||
messages: messages,
|
||||
languageModel: cnbAi('auto'),
|
||||
token: ctx.query?.token as string,
|
||||
});
|
||||
ctx.body = result;
|
||||
}).addTo(app);
|
||||
Reference in New Issue
Block a user