Parcourir la source

fix: 修复大纲解析器 - 增加思考标签清理逻辑

MyFramework User il y a 4 mois
Parent
commit
4947f8bd08
1 fichiers modifiés avec 11 ajouts et 2 suppressions
  1. 11 2
      server/src/modules/book-generator/parsers/outline.parser.ts

+ 11 - 2
server/src/modules/book-generator/parsers/outline.parser.ts

@@ -25,11 +25,20 @@ export function parseOutline(jsonStr: string): OutlineData | null {
     let data: any;
 
     // 策略1:直接解析(最理想情况,LLM 直接输出纯 JSON)
+    let cleanedStr = jsonStr.trim();
+    
+    // 清理思考标签
+    cleanedStr = cleanedStr.replace(/^[\s\S]*?<\/?blockquote>[\s\S]*/gi, '');
+    const thinkEndIdx = cleanedStr.indexOf('</think>');
+    if (thinkEndIdx !== -1) {
+      cleanedStr = cleanedStr.substring(thinkEndIdx + 8).trim();
+    }
+    
     try {
-      data = JSON.parse(jsonStr.trim());
+      data = JSON.parse(cleanedStr);
     } catch {
       // 策略2:提取 JSON 对象(处理 LLM 加了 markdown 标记或多余文字)
-      const match = jsonStr.match(/\{[\s\S]*\}/);
+      const match = cleanedStr.match(/\{[\s\S]*\}/);
       if (!match) {
         console.error('[OutlineParser] 未找到 JSON 对象');
         return null;