增加 openai 的模型兼容
This commit is contained in:
@@ -1,8 +1,14 @@
|
||||
import { Type } from "@google/genai";
|
||||
import { ModelOption, AnalysisResult, ExpertResult, ReviewResult } from '../../types';
|
||||
import { ModelOption, AnalysisResult, ExpertResult, ReviewResult, ApiProvider } from '../../types';
|
||||
import { cleanJsonString } from '../../utils';
|
||||
import { MANAGER_SYSTEM_PROMPT, MANAGER_REVIEW_SYSTEM_PROMPT } from './prompts';
|
||||
import { withRetry } from '../utils/retry';
|
||||
import { generateContent as generateOpenAIContent } from './openaiClient';
|
||||
import { getAIProvider } from '../../api';
|
||||
|
||||
const isGoogleProvider = (ai: any): boolean => {
|
||||
return ai?.models?.generateContent !== undefined;
|
||||
};
|
||||
|
||||
export const executeManagerAnalysis = async (
|
||||
ai: any,
|
||||
@@ -11,57 +17,86 @@ export const executeManagerAnalysis = async (
|
||||
context: string,
|
||||
budget: number
|
||||
): Promise<AnalysisResult> => {
|
||||
const managerSchema = {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
thought_process: { type: Type.STRING, description: "Brief explanation of why these supplementary experts were chosen." },
|
||||
experts: {
|
||||
type: Type.ARRAY,
|
||||
items: {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
role: { type: Type.STRING },
|
||||
description: { type: Type.STRING },
|
||||
temperature: { type: Type.NUMBER },
|
||||
prompt: { type: Type.STRING }
|
||||
},
|
||||
required: ["role", "description", "temperature", "prompt"]
|
||||
}
|
||||
}
|
||||
},
|
||||
required: ["thought_process", "experts"]
|
||||
};
|
||||
const isGoogle = isGoogleProvider(ai);
|
||||
|
||||
try {
|
||||
const analysisResp = await withRetry(() => ai.models.generateContent({
|
||||
model: model,
|
||||
contents: `Context:\n${context}\n\nCurrent Query: "${query}"`,
|
||||
config: {
|
||||
systemInstruction: MANAGER_SYSTEM_PROMPT,
|
||||
responseMimeType: "application/json",
|
||||
responseSchema: managerSchema,
|
||||
thinkingConfig: {
|
||||
includeThoughts: true,
|
||||
thinkingBudget: budget
|
||||
if (isGoogle) {
|
||||
const managerSchema = {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
thought_process: { type: Type.STRING, description: "Brief explanation of why these supplementary experts were chosen." },
|
||||
experts: {
|
||||
type: Type.ARRAY,
|
||||
items: {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
role: { type: Type.STRING },
|
||||
description: { type: Type.STRING },
|
||||
temperature: { type: Type.NUMBER },
|
||||
prompt: { type: Type.STRING }
|
||||
},
|
||||
required: ["role", "description", "temperature", "prompt"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
const rawText = analysisResp.text || '{}';
|
||||
const cleanText = cleanJsonString(rawText);
|
||||
|
||||
const analysisJson = JSON.parse(cleanText) as AnalysisResult;
|
||||
if (!analysisJson.experts || !Array.isArray(analysisJson.experts)) {
|
||||
throw new Error("Invalid schema structure");
|
||||
}
|
||||
return analysisJson;
|
||||
} catch (e) {
|
||||
console.error("Manager Analysis Error:", e);
|
||||
// Return a fallback so the process doesn't completely die if planning fails
|
||||
return {
|
||||
thought_process: "Direct processing fallback due to analysis error.",
|
||||
experts: []
|
||||
},
|
||||
required: ["thought_process", "experts"]
|
||||
};
|
||||
|
||||
try {
|
||||
const analysisResp = await withRetry(() => ai.models.generateContent({
|
||||
model: model,
|
||||
contents: `Context:\n${context}\n\nCurrent Query: "${query}"`,
|
||||
config: {
|
||||
systemInstruction: MANAGER_SYSTEM_PROMPT,
|
||||
responseMimeType: "application/json",
|
||||
responseSchema: managerSchema,
|
||||
thinkingConfig: {
|
||||
includeThoughts: true,
|
||||
thinkingBudget: budget
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
const rawText = (analysisResp as any).text || '{}';
|
||||
const cleanText = cleanJsonString(rawText);
|
||||
|
||||
const analysisJson = JSON.parse(cleanText) as AnalysisResult;
|
||||
if (!analysisJson.experts || !Array.isArray(analysisJson.experts)) {
|
||||
throw new Error("Invalid schema structure");
|
||||
}
|
||||
return analysisJson;
|
||||
} catch (e) {
|
||||
console.error("Manager Analysis Error:", e);
|
||||
return {
|
||||
thought_process: "Direct processing fallback due to analysis error.",
|
||||
experts: []
|
||||
};
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
const response = await generateOpenAIContent(ai, {
|
||||
model,
|
||||
systemInstruction: MANAGER_SYSTEM_PROMPT,
|
||||
content: `Context:\n${context}\n\nCurrent Query: "${query}"\n\nReturn a JSON response with this structure:\n{\n "thought_process": "...",\n "experts": [\n { "role": "...", "description": "...", "temperature": number, "prompt": "..." }\n ]\n}`,
|
||||
temperature: 0.7,
|
||||
responseFormat: 'json_object',
|
||||
thinkingConfig: {
|
||||
includeThoughts: true,
|
||||
thinkingBudget: budget
|
||||
}
|
||||
});
|
||||
|
||||
const analysisJson = JSON.parse(response.text) as AnalysisResult;
|
||||
if (!analysisJson.experts || !Array.isArray(analysisJson.experts)) {
|
||||
throw new Error("Invalid schema structure");
|
||||
}
|
||||
return analysisJson;
|
||||
} catch (e) {
|
||||
console.error("Manager Analysis Error:", e);
|
||||
return {
|
||||
thought_process: "Direct processing fallback due to analysis error.",
|
||||
experts: []
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -72,57 +107,78 @@ export const executeManagerReview = async (
|
||||
currentExperts: ExpertResult[],
|
||||
budget: number
|
||||
): Promise<ReviewResult> => {
|
||||
const reviewSchema = {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
satisfied: { type: Type.BOOLEAN, description: "True if the experts have fully answered the query with high quality." },
|
||||
critique: { type: Type.STRING, description: "If not satisfied, explain why and what is missing." },
|
||||
next_round_strategy: { type: Type.STRING, description: "Plan for the next iteration." },
|
||||
refined_experts: {
|
||||
type: Type.ARRAY,
|
||||
description: "The list of experts for the next round. Can be the same roles or new ones.",
|
||||
items: {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
role: { type: Type.STRING },
|
||||
description: { type: Type.STRING },
|
||||
temperature: { type: Type.NUMBER },
|
||||
prompt: { type: Type.STRING }
|
||||
},
|
||||
required: ["role", "description", "temperature", "prompt"]
|
||||
}
|
||||
}
|
||||
},
|
||||
required: ["satisfied", "critique"]
|
||||
};
|
||||
|
||||
const expertOutputs = currentExperts.map(e =>
|
||||
const isGoogle = isGoogleProvider(ai);
|
||||
const expertOutputs = currentExperts.map(e =>
|
||||
`--- [Round ${e.round}] Expert: ${e.role} ---\nOutput: ${e.content?.slice(0, 2000)}...`
|
||||
).join('\n\n');
|
||||
|
||||
const content = `User Query: "${query}"\n\nCurrent Expert Outputs:\n${expertOutputs}`;
|
||||
|
||||
try {
|
||||
const resp = await withRetry(() => ai.models.generateContent({
|
||||
model: model,
|
||||
contents: content,
|
||||
config: {
|
||||
systemInstruction: MANAGER_REVIEW_SYSTEM_PROMPT,
|
||||
responseMimeType: "application/json",
|
||||
responseSchema: reviewSchema,
|
||||
thinkingConfig: {
|
||||
includeThoughts: true,
|
||||
thinkingBudget: budget
|
||||
if (isGoogle) {
|
||||
const reviewSchema = {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
satisfied: { type: Type.BOOLEAN, description: "True if the experts have fully answered the query with high quality." },
|
||||
critique: { type: Type.STRING, description: "If not satisfied, explain why and what is missing." },
|
||||
next_round_strategy: { type: Type.STRING, description: "Plan for the next iteration." },
|
||||
refined_experts: {
|
||||
type: Type.ARRAY,
|
||||
description: "The list of experts for the next round. Can be the same roles or new ones.",
|
||||
items: {
|
||||
type: Type.OBJECT,
|
||||
properties: {
|
||||
role: { type: Type.STRING },
|
||||
description: { type: Type.STRING },
|
||||
temperature: { type: Type.NUMBER },
|
||||
prompt: { type: Type.STRING }
|
||||
},
|
||||
required: ["role", "description", "temperature", "prompt"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}));
|
||||
},
|
||||
required: ["satisfied", "critique"]
|
||||
};
|
||||
|
||||
const rawText = resp.text || '{}';
|
||||
const cleanText = cleanJsonString(rawText);
|
||||
return JSON.parse(cleanText) as ReviewResult;
|
||||
} catch (e) {
|
||||
console.error("Review Error:", e);
|
||||
// Fallback: Assume satisfied if JSON or API fails to avoid infinite loops
|
||||
return { satisfied: true, critique: "Processing Error, proceeding to synthesis." };
|
||||
try {
|
||||
const resp = await withRetry(() => ai.models.generateContent({
|
||||
model: model,
|
||||
contents: content,
|
||||
config: {
|
||||
systemInstruction: MANAGER_REVIEW_SYSTEM_PROMPT,
|
||||
responseMimeType: "application/json",
|
||||
responseSchema: reviewSchema,
|
||||
thinkingConfig: {
|
||||
includeThoughts: true,
|
||||
thinkingBudget: budget
|
||||
}
|
||||
}
|
||||
}));
|
||||
|
||||
const rawText = (resp as any).text || '{}';
|
||||
const cleanText = cleanJsonString(rawText);
|
||||
return JSON.parse(cleanText) as ReviewResult;
|
||||
} catch (e) {
|
||||
console.error("Review Error:", e);
|
||||
return { satisfied: true, critique: "Processing Error, proceeding to synthesis." };
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
const response = await generateOpenAIContent(ai, {
|
||||
model,
|
||||
systemInstruction: MANAGER_REVIEW_SYSTEM_PROMPT,
|
||||
content: `${content}\n\nReturn a JSON response with this structure:\n{\n "satisfied": boolean,\n "critique": "...",\n "next_round_strategy": "...",\n "refined_experts": [...]\n}`,
|
||||
temperature: 0.7,
|
||||
responseFormat: 'json_object',
|
||||
thinkingConfig: {
|
||||
includeThoughts: true,
|
||||
thinkingBudget: budget
|
||||
}
|
||||
});
|
||||
|
||||
return JSON.parse(response.text) as ReviewResult;
|
||||
} catch (e) {
|
||||
console.error("Review Error:", e);
|
||||
return { satisfied: true, critique: "Processing Error, proceeding to synthesis." };
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user