mirror of
https://github.com/margrop/wrong-notebook.git
synced 2026-12-06 02:39:36 +08:00
处理公益站截断问题
This commit is contained in:
@@ -58,40 +58,75 @@ export class OpenAIProvider implements AIService {
|
||||
private parseResponse(text: string): ParsedQuestion {
|
||||
console.log("[OpenAI] Parsing AI response, length:", text.length);
|
||||
|
||||
try {
|
||||
// With JSON mode enabled, response should be valid JSON
|
||||
const parsed = JSON.parse(text);
|
||||
|
||||
// Validate with Zod schema
|
||||
const tryParseAndValidate = (jsonStr: string): ParsedQuestion => {
|
||||
const parsed = JSON.parse(jsonStr);
|
||||
const result = safeParseParsedQuestion(parsed);
|
||||
if (result.success) {
|
||||
return result.data;
|
||||
}
|
||||
throw result.error; // Throw ZodError to be caught
|
||||
};
|
||||
|
||||
const recoverPartialData = (parsed: any): ParsedQuestion | null => {
|
||||
if (typeof parsed === 'object' && parsed !== null && typeof parsed.questionText === 'string') {
|
||||
console.warn("[OpenAI] ⚠ Recovering partial data from truncated response");
|
||||
return {
|
||||
questionText: parsed.questionText,
|
||||
answerText: parsed.answerText || "(AI 响应被截断,请手动补充)",
|
||||
analysis: parsed.analysis || "(AI 响应被截断,请手动补充)",
|
||||
subject: typeof parsed.subject === 'string' && ["数学", "物理", "化学", "生物", "英语", "语文", "历史", "地理", "政治", "其他"].includes(parsed.subject)
|
||||
? parsed.subject as any
|
||||
: "其他",
|
||||
knowledgePoints: Array.isArray(parsed.knowledgePoints) ? parsed.knowledgePoints : []
|
||||
};
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
try {
|
||||
// 1. Direct Parse
|
||||
const parsed = JSON.parse(text);
|
||||
const result = safeParseParsedQuestion(parsed);
|
||||
if (result.success) {
|
||||
console.log("[OpenAI] ✓ Direct parse and validation succeeded");
|
||||
return result.data;
|
||||
} else {
|
||||
// If structure is valid JSON but invalid schema, try partial recovery
|
||||
const partial = recoverPartialData(parsed);
|
||||
if (partial) return partial;
|
||||
|
||||
// Otherwise throw original error
|
||||
console.warn("[OpenAI] ⚠ Validation failed:", result.error.format());
|
||||
// Try to extract JSON from potential markdown wrapping
|
||||
const extracted = this.extractJson(text);
|
||||
const parsedExtracted = JSON.parse(extracted);
|
||||
return validateParsedQuestion(parsedExtracted);
|
||||
throw result.error;
|
||||
}
|
||||
} catch (error) {
|
||||
console.warn("[OpenAI] ⚠ Direct parse failed, attempting extraction");
|
||||
|
||||
try {
|
||||
// Fallback: extract JSON from markdown or text
|
||||
// 2. Extract JSON
|
||||
const jsonString = this.extractJson(text);
|
||||
const parsed = JSON.parse(jsonString);
|
||||
return validateParsedQuestion(parsed);
|
||||
return tryParseAndValidate(jsonString);
|
||||
} catch (extractError) {
|
||||
console.warn("[OpenAI] ⚠ Extraction failed, trying jsonrepair");
|
||||
|
||||
try {
|
||||
// Last resort: use jsonrepair
|
||||
// 3. JsonRepair
|
||||
const jsonString = this.extractJson(text);
|
||||
const repairedJson = jsonrepair(jsonString);
|
||||
const parsed = JSON.parse(repairedJson);
|
||||
return validateParsedQuestion(parsed);
|
||||
|
||||
// Validate regular first
|
||||
const result = safeParseParsedQuestion(parsed);
|
||||
if (result.success) return result.data;
|
||||
|
||||
// If failed, try partial recovery
|
||||
const partial = recoverPartialData(parsed);
|
||||
if (partial) {
|
||||
console.log("[OpenAI] ✓ Recovered partial data using jsonrepair");
|
||||
return partial;
|
||||
}
|
||||
|
||||
return validateParsedQuestion(parsed); // Will throw if invalid
|
||||
} catch (finalError) {
|
||||
console.error("[OpenAI] ✗ All parsing attempts failed");
|
||||
console.error("[OpenAI] Original text (first 500 chars):", text.substring(0, 500));
|
||||
@@ -109,6 +144,7 @@ export class OpenAIProvider implements AIService {
|
||||
console.log("=".repeat(80));
|
||||
console.log("[OpenAI] Image size:", imageBase64.length, "bytes");
|
||||
console.log("[OpenAI] MimeType:", mimeType);
|
||||
console.log("[OpenAI] Model:", this.model);
|
||||
console.log("[OpenAI] Language:", language);
|
||||
console.log("[OpenAI] Grade:", grade || "all");
|
||||
console.log("-".repeat(80));
|
||||
@@ -136,7 +172,7 @@ export class OpenAIProvider implements AIService {
|
||||
],
|
||||
},
|
||||
],
|
||||
response_format: { type: "json_object" },
|
||||
// response_format: { type: "json_object" }, // Removing to improve compatibility with 3rd party providers
|
||||
max_tokens: 4096,
|
||||
});
|
||||
|
||||
@@ -196,7 +232,8 @@ export class OpenAIProvider implements AIService {
|
||||
{ role: "system", content: systemPrompt },
|
||||
{ role: "user", content: userPrompt },
|
||||
],
|
||||
response_format: { type: "json_object" },
|
||||
// response_format: { type: "json_object" }, // Removing to improve compatibility with 3rd party providers
|
||||
max_tokens: 4096,
|
||||
});
|
||||
|
||||
const text = response.choices[0]?.message?.content || "";
|
||||
|
||||
@@ -127,7 +127,7 @@ ${tagsSection}
|
||||
1. 语言一致性(CRITICAL):"questionText" 和 "answerText" 必须使用与原始题目图片完全相同的语言。
|
||||
2. JSON 纯净性(CRITICAL):只返回一个有效的 JSON 对象,前后禁止添加任何文本或说明(如 "The final answer is...")。
|
||||
3. 格式禁止(CRITICAL):禁止将 JSON 对象包裹在任何 Markdown 代码块中。
|
||||
4. 文本提取:必须提取图片中实际的文本内容,禁止包含 HTML 标签或图片引用(如 <img>)。
|
||||
4. 纯文本内容提取 (CRITICAL): 必须只提取图片中**实际的文本和可转换为 LaTeX 的数学内容**。**严格禁止**在任何字段中输出**任何形式的图片引用、链接或视觉描述**,包括但不限于 HTML 的 <img>、Markdown 的  格式,或对图表、图形的文字描述(除非该描述本身就是题目文本的一部分)。**若遇无法用 LaTeX 表达的图表,必须用纯文本精确描述其关键特征,绝不能使用图片链接。**
|
||||
5. 多题处理:如果图片包含多个子问题(如 (1), (2), (3)),请将所有子问题纳入 "questionText" 字段。如果图片包含完全不相关的独立题目,则只专注于提取其中一题。
|
||||
|
||||
【预期 JSON 格式 (EXPECTED JSON FORMAT)】
|
||||
|
||||
Reference in New Issue
Block a user