This commit is contained in:
@@ -158,12 +158,13 @@ class WebSearchTool:
|
||||
|
||||
return results
|
||||
|
||||
def format_search_results(self, results: List[SearchResult]) -> str:
|
||||
def format_search_results(self, results: List[SearchResult], query: str = "") -> str:
|
||||
"""
|
||||
格式化搜索结果(带引用溯源)
|
||||
格式化搜索结果(使用模板渲染)
|
||||
|
||||
Args:
|
||||
results: 搜索结果列表
|
||||
query: 搜索关键词
|
||||
|
||||
Returns:
|
||||
格式化后的 Markdown 文本
|
||||
@@ -171,22 +172,27 @@ class WebSearchTool:
|
||||
if not results:
|
||||
return "未找到相关搜索结果"
|
||||
|
||||
lines = ["## 🔍 联网搜索结果\n"]
|
||||
from backend.app.core import get_formatter
|
||||
formatter = get_formatter()
|
||||
|
||||
for idx, result in enumerate(results, 1):
|
||||
lines.append(f"### [{idx}] {result.title}")
|
||||
lines.append(f"- 🔗 来源:[{result.url}]({result.url})")
|
||||
lines.append(f"- 📝 摘要:{result.snippet}")
|
||||
lines.append(f"- 📅 时间:{result.timestamp.strftime('%Y-%m-%d %H:%M:%S')}")
|
||||
lines.append("")
|
||||
# 转换为字典列表供模板使用
|
||||
result_dicts = []
|
||||
for r in results:
|
||||
result_dicts.append({
|
||||
"title": r.title,
|
||||
"url": r.url,
|
||||
"snippet": r.snippet,
|
||||
"source": r.source,
|
||||
"timestamp": r.timestamp.strftime('%Y-%m-%d %H:%M:%S') if r.timestamp else "",
|
||||
})
|
||||
|
||||
lines.append("---")
|
||||
lines.append("💡 **引用溯源说明**:")
|
||||
lines.append("- 以上搜索结果均标注了来源链接")
|
||||
lines.append("- 使用方括号数字标识引用(如 [1]、[2])")
|
||||
lines.append("- 可通过链接追溯原始信息")
|
||||
|
||||
return "\n".join(lines)
|
||||
return formatter.render(
|
||||
"web_search_result",
|
||||
query=query,
|
||||
result_count=len(results),
|
||||
results=result_dicts,
|
||||
citation_note="💡 **引用溯源说明**:以上搜索结果均标注了来源链接,可通过链接追溯原始信息。"
|
||||
)
|
||||
|
||||
|
||||
# 单例实例
|
||||
@@ -214,4 +220,4 @@ def web_search(query: str, max_results: int = 5) -> str:
|
||||
"""
|
||||
tool = get_web_search_tool()
|
||||
results = tool.search(query, max_results)
|
||||
return tool.format_search_results(results)
|
||||
return tool.format_search_results(results, query=query)
|
||||
|
||||
Reference in New Issue
Block a user