Fixed the Issue Where Search History was not Displayed in Media Engine
Showing
3 changed files
with
59 additions
and
14 deletions
| @@ -263,7 +263,12 @@ class DeepSearchAgent: | @@ -263,7 +263,12 @@ class DeepSearchAgent: | ||
| 263 | logger.info(" - 未找到搜索结果") | 263 | logger.info(" - 未找到搜索结果") |
| 264 | 264 | ||
| 265 | # 更新状态中的搜索历史 | 265 | # 更新状态中的搜索历史 |
| 266 | - paragraph.research.add_search_results(search_query, search_results) | 266 | + paragraph.research.add_search_results( |
| 267 | + search_query, | ||
| 268 | + search_results, | ||
| 269 | + search_tool=search_tool, | ||
| 270 | + paragraph_title=paragraph.title, | ||
| 271 | + ) | ||
| 267 | 272 | ||
| 268 | # 生成初始总结 | 273 | # 生成初始总结 |
| 269 | logger.info(" - 生成初始总结...") | 274 | logger.info(" - 生成初始总结...") |
| @@ -341,7 +346,12 @@ class DeepSearchAgent: | @@ -341,7 +346,12 @@ class DeepSearchAgent: | ||
| 341 | logger.info(" 未找到反思搜索结果") | 346 | logger.info(" 未找到反思搜索结果") |
| 342 | 347 | ||
| 343 | # 更新搜索历史 | 348 | # 更新搜索历史 |
| 344 | - paragraph.research.add_search_results(search_query, search_results) | 349 | + paragraph.research.add_search_results( |
| 350 | + search_query, | ||
| 351 | + search_results, | ||
| 352 | + search_tool=search_tool, | ||
| 353 | + paragraph_title=paragraph.title, | ||
| 354 | + ) | ||
| 345 | 355 | ||
| 346 | # 生成反思总结 | 356 | # 生成反思总结 |
| 347 | reflection_summary_input = { | 357 | reflection_summary_input = { |
| @@ -17,6 +17,9 @@ class Search: | @@ -17,6 +17,9 @@ class Search: | ||
| 17 | title: str = "" # 搜索结果标题 | 17 | title: str = "" # 搜索结果标题 |
| 18 | content: str = "" # 搜索返回的内容 | 18 | content: str = "" # 搜索返回的内容 |
| 19 | score: Optional[float] = None # 相关度评分 | 19 | score: Optional[float] = None # 相关度评分 |
| 20 | + paragraph_title: str = "" # 段落标题,便于展示归属 | ||
| 21 | + search_tool: str = "" # 使用的搜索工具 | ||
| 22 | + has_result: bool = True # 是否有返回结果 | ||
| 20 | timestamp: str = field(default_factory=lambda: datetime.now().isoformat()) | 23 | timestamp: str = field(default_factory=lambda: datetime.now().isoformat()) |
| 21 | 24 | ||
| 22 | def to_dict(self) -> Dict[str, Any]: | 25 | def to_dict(self) -> Dict[str, Any]: |
| @@ -27,6 +30,9 @@ class Search: | @@ -27,6 +30,9 @@ class Search: | ||
| 27 | "title": self.title, | 30 | "title": self.title, |
| 28 | "content": self.content, | 31 | "content": self.content, |
| 29 | "score": self.score, | 32 | "score": self.score, |
| 33 | + "paragraph_title": self.paragraph_title, | ||
| 34 | + "search_tool": self.search_tool, | ||
| 35 | + "has_result": self.has_result, | ||
| 30 | "timestamp": self.timestamp | 36 | "timestamp": self.timestamp |
| 31 | } | 37 | } |
| 32 | 38 | ||
| @@ -39,6 +45,9 @@ class Search: | @@ -39,6 +45,9 @@ class Search: | ||
| 39 | title=data.get("title", ""), | 45 | title=data.get("title", ""), |
| 40 | content=data.get("content", ""), | 46 | content=data.get("content", ""), |
| 41 | score=data.get("score"), | 47 | score=data.get("score"), |
| 48 | + paragraph_title=data.get("paragraph_title", ""), | ||
| 49 | + search_tool=data.get("search_tool", ""), | ||
| 50 | + has_result=data.get("has_result", True), | ||
| 42 | timestamp=data.get("timestamp", datetime.now().isoformat()) | 51 | timestamp=data.get("timestamp", datetime.now().isoformat()) |
| 43 | ) | 52 | ) |
| 44 | 53 | ||
| @@ -55,23 +64,42 @@ class Research: | @@ -55,23 +64,42 @@ class Research: | ||
| 55 | """添加搜索记录""" | 64 | """添加搜索记录""" |
| 56 | self.search_history.append(search) | 65 | self.search_history.append(search) |
| 57 | 66 | ||
| 58 | - def add_search_results(self, query: str, results: List[Dict[str, Any]]): | 67 | + def add_search_results(self, query: str, results: List[Dict[str, Any]], search_tool: str = "", paragraph_title: str = ""): |
| 59 | """批量添加搜索结果""" | 68 | """批量添加搜索结果""" |
| 69 | + if not results: | ||
| 70 | + # 记录一次“无结果”搜索,方便前端显示搜索轨迹 | ||
| 71 | + self.add_search( | ||
| 72 | + Search( | ||
| 73 | + query=query or "", | ||
| 74 | + title="未找到结果", | ||
| 75 | + content="本次搜索未返回结果或调用失败", | ||
| 76 | + url="", | ||
| 77 | + score=None, | ||
| 78 | + paragraph_title=paragraph_title, | ||
| 79 | + search_tool=search_tool, | ||
| 80 | + has_result=False, | ||
| 81 | + ) | ||
| 82 | + ) | ||
| 83 | + return | ||
| 84 | + | ||
| 60 | for result in results: | 85 | for result in results: |
| 61 | - # 防御空值,避免下游展示时报错 | ||
| 62 | url = result.get("url") or "" | 86 | url = result.get("url") or "" |
| 63 | title = result.get("title") or "" | 87 | title = result.get("title") or "" |
| 64 | - content = result.get("content") or "" | 88 | + content = result.get("content") or result.get("raw_content") or "" |
| 65 | if not isinstance(content, str): | 89 | if not isinstance(content, str): |
| 66 | content = str(content) | 90 | content = str(content) |
| 67 | - search = Search( | ||
| 68 | - query=query or "", | ||
| 69 | - url=url, | ||
| 70 | - title=title, | ||
| 71 | - content=content, | ||
| 72 | - score=result.get("score") | 91 | + self.add_search( |
| 92 | + Search( | ||
| 93 | + query=query or "", | ||
| 94 | + url=url, | ||
| 95 | + title=title, | ||
| 96 | + content=content, | ||
| 97 | + score=result.get("score"), | ||
| 98 | + paragraph_title=paragraph_title or result.get("paragraph_title", ""), | ||
| 99 | + search_tool=search_tool or result.get("search_tool", ""), | ||
| 100 | + has_result=True, | ||
| 101 | + ) | ||
| 73 | ) | 102 | ) |
| 74 | - self.add_search(search) | ||
| 75 | 103 | ||
| 76 | def get_search_count(self) -> int: | 104 | def get_search_count(self) -> int: |
| 77 | """获取搜索次数""" | 105 | """获取搜索次数""" |
| @@ -222,14 +222,21 @@ def display_results(agent: DeepSearchAgent, final_report: str): | @@ -222,14 +222,21 @@ def display_results(agent: DeepSearchAgent, final_report: str): | ||
| 222 | for i, search in enumerate(all_searches): | 222 | for i, search in enumerate(all_searches): |
| 223 | query_label = search.query if search.query else "未记录查询" | 223 | query_label = search.query if search.query else "未记录查询" |
| 224 | with st.expander(f"搜索 {i + 1}: {query_label}"): | 224 | with st.expander(f"搜索 {i + 1}: {query_label}"): |
| 225 | + paragraph_title = getattr(search, "paragraph_title", "") or "未标注段落" | ||
| 226 | + search_tool = getattr(search, "search_tool", "") or "未标注工具" | ||
| 227 | + has_result = getattr(search, "has_result", True) | ||
| 228 | + st.write("**段落:**", paragraph_title) | ||
| 229 | + st.write("**使用的工具:**", search_tool) | ||
| 225 | preview = search.content or "" | 230 | preview = search.content or "" |
| 226 | if not isinstance(preview, str): | 231 | if not isinstance(preview, str): |
| 227 | preview = str(preview) | 232 | preview = str(preview) |
| 228 | if len(preview) > 200: | 233 | if len(preview) > 200: |
| 229 | preview = preview[:200] + "..." | 234 | preview = preview[:200] + "..." |
| 230 | - st.write("**URL:**", search.url) | ||
| 231 | - st.write("**标题:**", search.title) | 235 | + st.write("**URL:**", search.url or "无") |
| 236 | + st.write("**标题:**", search.title or "无") | ||
| 232 | st.write("**内容预览:**", preview if preview else "无可用内容") | 237 | st.write("**内容预览:**", preview if preview else "无可用内容") |
| 238 | + if not has_result: | ||
| 239 | + st.info("本次搜索未返回结果") | ||
| 233 | if search.score: | 240 | if search.score: |
| 234 | st.write("**相关度评分:**", search.score) | 241 | st.write("**相关度评分:**", search.score) |
| 235 | 242 |
-
Please register or login to post a comment