@@ -43,6 +43,32 @@ def load_existing_ids(output_file):
4343 print (f"读取现有文件时出错: { e } " , file = sys .stderr )
4444 return existing_ids
4545
46+ def parse_llm_response (response_text ):
47+ """解析LLM返回的文本为结构化数据"""
48+ try :
49+ # 尝试直接解析整个响应为JSON
50+ return json .loads (response_text )
51+ except json .JSONDecodeError :
52+ # 如果失败,尝试提取JSON部分
53+ try :
54+ # 查找可能的JSON开始和结束位置
55+ start_idx = response_text .find ('{' )
56+ end_idx = response_text .rfind ('}' ) + 1
57+ if start_idx >= 0 and end_idx > start_idx :
58+ json_str = response_text [start_idx :end_idx ]
59+ return json .loads (json_str )
60+ except (json .JSONDecodeError , ValueError ):
61+ pass
62+
63+ # 如果仍然失败,返回错误结构
64+ return {
65+ "tldr" : "解析错误" ,
66+ "motivation" : "解析错误" ,
67+ "method" : "解析错误" ,
68+ "result" : "解析错误" ,
69+ "conclusion" : "解析错误"
70+ }
71+
4672def main ():
4773 args = parse_args ()
4874 model_name = os .environ .get ("MODEL_NAME" , 'deepseek-chat' )
@@ -80,8 +106,8 @@ def main():
80106
81107 print ('Open:' , args .data , file = sys .stderr )
82108
83- # 修改这里,使用structured_output而不是function_calling
84- llm = ChatOpenAI (model = model_name ). with_structured_output ( Structure )
109+ # 不使用function_calling,直接使用LLM
110+ llm = ChatOpenAI (model = model_name )
85111 print ('Connect to:' , model_name , file = sys .stderr )
86112 prompt_template = ChatPromptTemplate .from_messages ([
87113 SystemMessagePromptTemplate .from_template (system ),
@@ -92,12 +118,25 @@ def main():
92118
93119 for idx , d in enumerate (data ):
94120 try :
95- response : Structure = chain .invoke ({
121+ # 获取文本响应
122+ response = chain .invoke ({
96123 "language" : language ,
97124 "content" : d ['summary' ]
98125 })
99- d ['AI' ] = response .model_dump ()
100- except langchain_core .exceptions .OutputParserException as e :
126+
127+ # 解析响应为结构化数据
128+ response_text = response .content
129+ structured_data = parse_llm_response (response_text )
130+
131+ # 确保所有必要字段都存在
132+ required_fields = ["tldr" , "motivation" , "method" , "result" , "conclusion" ]
133+ for field in required_fields :
134+ if field not in structured_data :
135+ structured_data [field ] = "未提供"
136+
137+ d ['AI' ] = structured_data
138+
139+ except Exception as e :
101140 print (f"{ d ['id' ]} has an error: { e } " , file = sys .stderr )
102141 d ['AI' ] = {
103142 "tldr" : "Error" ,
0 commit comments