|
@@ -104,10 +104,14 @@ JSON结果:
|
|
|
"stream": False,
|
|
"stream": False,
|
|
|
"options": {
|
|
"options": {
|
|
|
"temperature": 0.1, # 低温度,更确定性的输出
|
|
"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:
|
|
try:
|
|
|
async with httpx.AsyncClient(timeout=self.timeout) as client:
|
|
async with httpx.AsyncClient(timeout=self.timeout) as client:
|
|
|
response = await client.post(url, json=payload)
|
|
response = await client.post(url, json=payload)
|
|
@@ -137,16 +141,32 @@ JSON结果:
|
|
|
|
|
|
|
|
# 移除 markdown code block 标记
|
|
# 移除 markdown code block 标记
|
|
|
response = re.sub(r'```json\s*', '', response)
|
|
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]}...")
|
|
logger.warning(f"LLM 响应中未找到 JSON, response={response[:300]}...")
|
|
|
return entities
|
|
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", [])
|
|
entity_list = data.get("entities", [])
|
|
|
|
|
|