Sfoglia il codice sorgente

fix: Qwen3 禁用思考模式 + 改进 JSON 解析

问题:Qwen3 默认输出大量思考内容,导致 JSON 解析失败

修复:
- 调用时添加 /no_think 前缀禁用思考模式
- 改进 JSON 解析:查找包含 entities 的 JSON 对象
- 多个 JSON 匹配时逐个尝试解析
何文松 1 mese fa
parent
commit
1b44494094
1 ha cambiato i file con 27 aggiunte e 7 eliminazioni
  1. 27 7
      python-services/ner-service/app/services/ollama_service.py

+ 27 - 7
python-services/ner-service/app/services/ollama_service.py

@@ -104,10 +104,14 @@ JSON结果:
             "stream": False,
             "options": {
                 "temperature": 0.1,  # 低温度,更确定性的输出
-                "num_predict": 4096,  # 最大输出 token
+                "num_predict": 2048,  # 最大输出 token
             }
         }
         
+        # Qwen3 特殊处理:使用 /no_think 标签禁用思考模式
+        if "qwen3" in self.model.lower():
+            payload["prompt"] = "/no_think\n" + prompt
+        
         try:
             async with httpx.AsyncClient(timeout=self.timeout) as client:
                 response = await client.post(url, json=payload)
@@ -137,16 +141,32 @@ JSON结果:
             
             # 移除 markdown code block 标记
             response = re.sub(r'```json\s*', '', response)
-            response = re.sub(r'```\s*$', '', response)
+            response = re.sub(r'```\s*', '', response)
             
-            # 尝试提取 JSON 部分
-            json_match = re.search(r'\{[\s\S]*\}', response)
-            if not json_match:
+            # 查找所有 JSON 对象,取最后一个(通常是实际结果)
+            json_matches = re.findall(r'\{[^{}]*"entities"[^{}]*\[[\s\S]*?\]\s*\}', response)
+            if not json_matches:
+                # 回退:尝试匹配任意 JSON
+                json_matches = re.findall(r'\{[\s\S]*?\}', response)
+            
+            if not json_matches:
                 logger.warning(f"LLM 响应中未找到 JSON, response={response[:300]}...")
                 return entities
             
-            json_str = json_match.group()
-            data = json.loads(json_str)
+            # 尝试解析每个匹配,使用第一个有效的
+            data = None
+            for json_str in json_matches:
+                try:
+                    parsed = json.loads(json_str)
+                    if "entities" in parsed:
+                        data = parsed
+                        break
+                except json.JSONDecodeError:
+                    continue
+            
+            if not data:
+                logger.warning(f"未找到有效的 entities JSON, response={response[:300]}...")
+                return entities
             
             entity_list = data.get("entities", [])