This commit is contained in:
2025-06-21 18:21:49 +08:00
parent a76b506327
commit da6d4041ad
9 changed files with 121 additions and 12 deletions

View File

@@ -0,0 +1,80 @@
import { agent } from '@/agent/agent.ts';
import { ai } from '../ai.ts';
import { getJsonFromString } from './content.ts';
export type Category = {
category: string;
description: string;
};
export const categoryList: Category[] = [
{
category: 'daily_poetry',
description: '获取一篇今日诗词',
},
{
category: 'irony',
description: '反讽一下',
},
{
category: 'random_beautiful_text',
description: '随机生成一段优美的文字',
},
{
category: 'random_joke',
description: '随机生成一个笑话',
},
{
category: '',
description: '教教我,怎么做'
},
{
category: 'default',
description: '默认分类,无法识别的请求',
},
];
agent
.route({
path: 'analyze',
key: 'category',
description: '分析文本内容判断文本的请求分类。比如1. 获取一篇今日诗词。2. 反讽一下 3. 随机生成一段优美的文字。',
})
.define(async (ctx) => {
const text = ctx.query?.text || '';
const prompt = `
请分析以下文本内容判断文本的请求分类并返回一个JSON对象。
识别的分类包括:
${categoryList.map((item) => `- ${item.category}: ${item.description}`).join('\n')}
返回内容示例:
\`\`\`json
{
"category": "daily_poetry"
}
\`\`\`
<context>
${text}
</context>
`;
const res = await ai
.chat(
[
{
role: 'user',
content: prompt,
},
],
{
enable_thinking: false,
},
)
.catch((error) => {
ctx.throw(500, 'AI 服务错误: ' + error.status);
return error;
});
const content = res.choices?.[0]?.message?.content || '';
const jsonResponse = getJsonFromString(content);
const category = jsonResponse?.category || 'default';
ctx.body = { category };
})
.addTo(agent);

View File

@@ -1,7 +1,7 @@
import { agent } from '@/agent/agent.ts';
import { ai } from '../ai.ts';
import { logger } from '@/agent/logger.ts';
const getJsonFromString = (str: string) => {
export const getJsonFromString = (str: string) => {
// 尝试从字符串中提取JSON对象
try {
const jsonMatch = str.match(/```json\s*([\s\S]*?)\s*```/);

View File

@@ -1,5 +1,7 @@
import { agent } from './agent.ts';
import './analyze/content.ts';
import './analyze/category.ts';
import './fix/prompt.ts';
import './xhs.ts';

View File

@@ -0,0 +1,21 @@
import { agent } from '../index.ts';
const main = async () => {
const text1 = '解答一下这个笔记。';
const text2 = '分析一下这个图片';
const text3 = '这个视频介绍的是什么';
const text4 = '评价一下这个评论。';
const text5 = '关于这个评论。';
const text6 = '1+1=';
const text7 = '反讽一下';
const text8 = '随机生成一段优美的文字';
const res = await agent.call({
path: 'analyze',
key: 'category',
payload: {
text: text1,
},
});
console.log('analyze category res', res.code, 'content', res.body);
};
main();

View File

@@ -3,13 +3,16 @@
import { QueryRouterServer } from '@kevisual/router';
import { redis } from '@/modules/redis.ts';
import { Queue } from 'bullmq';
import { app as xhsApp } from '@kevisual/xhs/index';
import { app as xhsApp, xhsServices, xhsRootClient, XhsClient } from '@kevisual/xhs/index';
import { nanoid } from 'nanoid';
export const XHS_GET_UNREAD = 'unread_count';
export const XHS_QUEUE_NAME = 'XHS_QUEUE';
import { config } from '../modules/config.ts';
const xhsClient: XhsClient = xhsRootClient;
xhsClient.setCookie(config.XHS_ROOT_COOKIE || '');
console.log('XHS_ROOT_COOKIE', xhsClient.cookie);
export const taskApp = new QueryRouterServer();
export { xhsApp };
export { xhsApp, xhsServices, xhsRootClient };
export const queue = new Queue(XHS_QUEUE_NAME, {
connection: redis,
});