|
|
@@ -12,6 +12,32 @@ import uuid
|
|
|
from langchain_openai import ChatOpenAI
|
|
|
from langchain_core.prompts import ChatPromptTemplate
|
|
|
from langchain_core.output_parsers import JsonOutputParser
|
|
|
+from langchain_core.outputs import Generation
|
|
|
+import re
|
|
|
+
|
|
|
+class SafeJsonOutputParser(JsonOutputParser):
|
|
|
+
|
|
|
+ def parse_result(self, result, *, partial: bool = False):
|
|
|
+ if isinstance(result, list) and len(result) > 0:
|
|
|
+ generation = result[0]
|
|
|
+ elif isinstance(result, Generation):
|
|
|
+ generation = result
|
|
|
+ else:
|
|
|
+ raise ValueError(f"Unexpected result type: {type(result)}")
|
|
|
+ text = generation.text
|
|
|
+ # 1️⃣ 去 <think>...</think>
|
|
|
+ text = re.sub(r"<think>.*?</think>", "", text, flags=re.S).strip()
|
|
|
+
|
|
|
+ # 2️⃣ 去 ```json ``` 包裹
|
|
|
+ text = re.sub(r"^```(?:json)?|```$", "", text, flags=re.I | re.M).strip()
|
|
|
+
|
|
|
+ # 3️⃣ ⭐ 只截取 JSON 本体
|
|
|
+ match = re.search(r"(\[\s*{.*}\s*\]|\{\s*\".*\"\s*\})", text, flags=re.S)
|
|
|
+ if not match:
|
|
|
+ raise ValueError(f"Invalid json output after clean: {text[:200]}")
|
|
|
+
|
|
|
+ json_text = match.group(1)
|
|
|
+ return json.loads(json_text)
|
|
|
|
|
|
|
|
|
# --- 核心 Parser ---
|
|
|
@@ -34,7 +60,7 @@ class TransactionParserAgent:
|
|
|
self.multimodal_api_url = multimodal_api_url
|
|
|
|
|
|
# 定义 JSON 解析器
|
|
|
- self.parser = JsonOutputParser()
|
|
|
+ self.parser = SafeJsonOutputParser()
|
|
|
|
|
|
# 初始化API调用跟踪
|
|
|
self.api_calls = []
|