新增调度器模块

This commit is contained in:
maojindao55
2025-02-24 19:39:20 +08:00
parent b52f2acfd5
commit a9542f1bc5
2 changed files with 39 additions and 28 deletions

View File

@@ -16,7 +16,7 @@ interface MessageHistory {
export async function onRequestPost({ env, request }) {
try {
const { message, history, availableAIs } = await request.json();
const selectedAIs = await scheduleAIResponses(message, history, availableAIs);
const selectedAIs = await scheduleAIResponses(message, history, availableAIs, env);
return Response.json({
selectedAIs: selectedAIs
@@ -30,36 +30,43 @@ export async function onRequestPost({ env, request }) {
}
}
async function analyzeMessageWithAI(message: string, allTags: string[]): Promise<string[]> {
async function analyzeMessageWithAI(message: string, allTags: string[], env: any, history: MessageHistory[] = []): Promise<string[]> {
const shedulerAI = shedulerAICharacter(message, allTags);
const modelConfig = modelConfigs.find(config => config.model === shedulerAI.model);
const apiKey = env[modelConfig.apiKey];
if (!apiKey) {
throw new Error(`${modelConfig.model} 的API密钥未配置`);
}
const openai = new OpenAI({
apiKey: modelConfig.apiKey,
baseURL: modelConfig.baseURL, // DeepSeek API 的基础URL
});
const prompt = shedulerAI.custom_prompt;
try {
const completion = await openai.chat.completions.create({
model: shedulerAI.model,
messages: [
{ role: "user", content: prompt }
],
apiKey: apiKey,
baseURL: modelConfig.baseURL,
});
const matchedTags = completion.choices[0].message.content?.split(',').map(tag => tag.trim()) || [];
return matchedTags;
} catch (error) {
console.error('AI分析失败:', error);
return [];
}
const prompt = shedulerAI.custom_prompt;
try {
const completion = await openai.chat.completions.create({
model: shedulerAI.model,
messages: [
{ role: "user", content: prompt },
...history.slice(-10), // 添加历史消息
{ role: "user", content: message }
],
});
const matchedTags = completion.choices[0].message.content?.split(',').map(tag => tag.trim()) || [];
return matchedTags;
} catch (error) {
console.error('AI分析失败:', error);
return [];
}
}
async function scheduleAIResponses(
message: string,
history: MessageHistory[],
availableAIs: AICharacter[]
availableAIs: AICharacter[],
env: any
): Promise<string[]> {
// 1. 收集所有可用的标签
const allTags = new Set<string>();
@@ -68,8 +75,12 @@ async function scheduleAIResponses(
});
// 2. 使用AI模型分析消息并匹配标签
const matchedTags = await analyzeMessageWithAI(message, Array.from(allTags));
const matchedTags = await analyzeMessageWithAI(message, Array.from(allTags), env, history);
console.log('matchedTags', matchedTags, allTags);
//如果含有"文字游戏"标签,则需要全员参与
if (matchedTags.includes("文字游戏")) {
return availableAIs.map(ai => ai.id);
}
// 3. 计算每个AI的匹配分数
const aiScores = new Map<string, number>();
const messageLC = message.toLowerCase();
@@ -110,6 +121,7 @@ async function scheduleAIResponses(
// 5. 如果没有匹配到任何AI随机选择1-2个
if (sortedAIs.length === 0) {
console.log('没有匹配到任何AI随机选择1-2个');
const maxResponders = Math.min(2, availableAIs.length);
const numResponders = Math.floor(Math.random() * maxResponders) + 1;

View File

@@ -46,9 +46,8 @@ export function shedulerAICharacter(message: string, allTags: string[]): AIChara
personality: "sheduler",
model: modelConfigs[4].model,
avatar: "",
custom_prompt: `作为一个语义分析专家,请分析以下用户消息,并从给定的标签列表中选择最相关的标签(最多选择3个
custom_prompt: `你是一个语义分析专家,你在一个聊天群里,请分析用户消息和上下文内容,并从给定的标签列表中选择最相关的标签(1-3个
请只返回标签列表,用逗号分隔,不要有其他解释。
用户消息:"${message}"
可选标签:${allTags.join(', ')}
回复格式示例标签1,标签2,标签3`
}
@@ -114,7 +113,7 @@ export function generateAICharacters(groupName: string): AICharacter[] {
model: modelConfigs[3].model,
avatar: "/img/doubao_new.png",
custom_prompt: `你是一个名叫"豆包"的硅基生命体,你当前在一个叫"${groupName}" 的聊天群里`,
tags: ["生活助手", "语音交互", "学生党福音", "娱乐利器", "抖音生态"]
tags: ["生活助手", "文字游戏", "学生党福音", "娱乐利器", "抖音生态"]
},
{
id: 'ai6',
@@ -123,7 +122,7 @@ export function generateAICharacters(groupName: string): AICharacter[] {
model: modelConfigs[0].model,
avatar: "/img/qwen.jpg",
custom_prompt: `你是一个名叫"千问"的硅基生命体,你当前在一个叫"${groupName}" 的聊天群里`,
tags: ["广告文案", "行业应用", "分析数据", "企业级AI", "阿里云生态"]
tags: ["广告文案", "行业应用", "分析数据","文字游戏", "企业级AI", "阿里云生态"]
},
{
id: 'ai7',
@@ -132,7 +131,7 @@ export function generateAICharacters(groupName: string): AICharacter[] {
model: modelConfigs[1].model,
avatar: "/img/ds.svg",
custom_prompt: `你是一个名叫"DeepSeek"的硅基生命体,你当前在一个叫"${groupName}" 的聊天群里`,
tags: ["深度推理", "编程神器", "中文优化", "性价比之王", "开源先锋"]
tags: ["深度推理", "编程神器", "中文优化", "文字游戏"]
}
];
}