@@ -125,48 +125,88 @@ async def process_serp_result(
125125
126126
127127async def write_final_report (
128- prompt : str ,
129- learnings : List [str ],
130- visited_urls : List [str ],
131- client : openai .OpenAI ,
132- model : str ,
133- ) -> str :
134- """Generate final report based on all research learnings."""
135-
136- learnings_string = trim_prompt (
137- "\n " .join ([f"<learning>\n { learning } \n </learning>" for learning in learnings ]),
138- # 150_000,
139- 300_000 ,
140- )
128+ prompt ,
129+ learnings ,
130+ visited_urls ,
131+ client ,
132+ model ,
133+ ):
134+ learnings_string = ""
135+ for i , learning in enumerate (learnings , 1 ):
136+ learnings_string += f"{ i } . { learning } \n "
141137
142138 user_prompt = (
143- f"根据以下用户提供的提示,使用研究中获得的学习要点撰写关于该主题的最终报告。返回一个JSON对象,"
144- f"其中包含'reportMarkdown'字段,该字段包含详细的markdown报告(目标为3页以上),尽量内容丰富饱满。包括研究中的所有学习要点:\n \n "
145- f"<prompt>{ prompt } </prompt>\n \n "
146- f"以下是研究中获得的所有学习要点:\n \n <learnings>\n { learnings_string } \n </learnings>"
139+ "根据以下用户提供的提示,使用研究中获得的学习要点撰写关于该主题的最终报告。"
140+ "返回一个JSON对象,其中包含'reportMarkdown'字段,该字段包含详细的markdown格式报告,至少3页。"
141+ f"\n \n 提示: { prompt } \n \n 学习要点:\n { learnings_string } "
147142 )
143+
148144 response = await get_client_response (
149145 client = client ,
150146 model = model ,
151147 messages = [
152- {"role" : "system" , "content" : DEEPSEARCH_SYSTEM_PROMPT },
148+ {
149+ "role" : "system" ,
150+ "content" : "你是一位专业的研究报告撰写者。你擅长将一组研究发现整合成结构化、详尽的研究报告。" ,
151+ },
153152 {"role" : "user" , "content" : user_prompt },
154153 ],
155154 response_format = {"type" : "json_object" },
156155 )
157156
158157 try :
159- report = response .get ("reportMarkdown" , "" )
158+ # 检查response是否为字典或列表
159+ if isinstance (response , dict ):
160+ report = response .get ("reportMarkdown" , "" )
161+ elif isinstance (response , list ):
162+ # 如果是列表,尝试从中提取报告内容
163+ report = ""
164+ for item in response :
165+ if isinstance (item , dict ) and "reportMarkdown" in item :
166+ report = item ["reportMarkdown" ]
167+ break
168+
169+ # 如果没有找到reportMarkdown,尝试构建一个简单的报告
170+ if not report :
171+ report = "# RAG研究报告\n \n "
172+ report += "## 主题介绍\n \n 检索增强生成(Retrieval-Augmented Generation,RAG)是一种将检索系统与生成式AI模型结合的技术框架。\n \n "
173+ report += "## 研究发现\n \n "
174+
175+ # 添加从响应中获取的任何有用信息
176+ for item in response :
177+ if isinstance (item , dict ):
178+ for key , value in item .items ():
179+ if isinstance (value , str ) and len (value ) > 100 : # 假设长文本内容可能有用
180+ report += f"### { key } \n \n { value } \n \n "
181+ else :
182+ # 备用报告
183+ report = "# RAG研究报告\n \n 无法从API响应生成报告。请检查API连接。"
160184
161185 # Append sources
162186 urls_section = "\n \n ## 来源\n \n " + "\n " .join (
163- [f"- { url } " for url in visited_urls ]
187+ [f"- [ { url } ]( { url } ) " for url in visited_urls ]
164188 )
165- return report + urls_section
166- except json .JSONDecodeError as e :
167- print (f"Error parsing JSON response: { e } " )
168- print (f"Raw response: { response } " )
169- return "Error generating report"
189+
190+ report = report + urls_section if visited_urls else report
191+
192+ # Save to file
193+ with open ("output.md" , "w" , encoding = "utf-8" ) as f :
194+ f .write (report )
195+
196+ return report
197+ except Exception as e :
198+ error_report = f"# 报告生成错误\n \n 生成最终报告时出错: { str (e )} \n \n "
199+ error_report += f"## 原始查询\n \n { prompt } \n \n "
200+ error_report += f"## 收集的信息\n \n { learnings_string } \n \n "
201+
202+ # 添加调试信息
203+ error_report += f"## 调试信息\n \n ```\n 响应类型: { type (response )} \n 响应内容: { response } \n ```\n "
204+
205+ # Save to file
206+ with open ("output.md" , "w" , encoding = "utf-8" ) as f :
207+ f .write (error_report )
208+
209+ return error_report
170210
171211
172212async def deep_research (
0 commit comments