refactor(search): 将 Exa 引擎迁移到 search_and_contents API
此提交将 Exa 搜索引擎更新为使用 `search_and_contents` 方法,而不是之前的 `search` 方法。 `search_and_contents` 端点更高效,并且可以直接提供高亮片段。此更改用更具上下文感知的高亮替代了对摘要的依赖,从而显著提高了搜索结果的质量和相关性。
This commit is contained in:
@@ -39,7 +39,7 @@ class ExaSearchEngine(BaseSearchEngine):
|
|||||||
return self.api_manager.is_available()
|
return self.api_manager.is_available()
|
||||||
|
|
||||||
async def search(self, args: dict[str, Any]) -> list[dict[str, Any]]:
|
async def search(self, args: dict[str, Any]) -> list[dict[str, Any]]:
|
||||||
"""执行优化的Exa搜索(使用新的search API)"""
|
"""执行优化的Exa搜索(使用 search_and_contents API)"""
|
||||||
if not self.is_available():
|
if not self.is_available():
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@@ -47,13 +47,12 @@ class ExaSearchEngine(BaseSearchEngine):
|
|||||||
num_results = min(args.get("num_results", 5), 5) # 默认5个结果,但限制最多5个
|
num_results = min(args.get("num_results", 5), 5) # 默认5个结果,但限制最多5个
|
||||||
time_range = args.get("time_range", "any")
|
time_range = args.get("time_range", "any")
|
||||||
|
|
||||||
# 使用新的搜索参数格式
|
# 使用 search_and_contents 的参数格式
|
||||||
exa_args = {
|
exa_args = {
|
||||||
|
"query": query,
|
||||||
"num_results": num_results,
|
"num_results": num_results,
|
||||||
"contents": {
|
"type": "auto",
|
||||||
"text": True,
|
"highlights": True, # 获取高亮片段
|
||||||
"summary": True, # 启用自动摘要
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# 时间范围过滤
|
# 时间范围过滤
|
||||||
@@ -70,20 +69,20 @@ class ExaSearchEngine(BaseSearchEngine):
|
|||||||
return []
|
return []
|
||||||
|
|
||||||
loop = asyncio.get_running_loop()
|
loop = asyncio.get_running_loop()
|
||||||
# 使用新的search方法
|
# 使用 search_and_contents 方法
|
||||||
func = functools.partial(exa_client.search, query, **exa_args)
|
func = functools.partial(exa_client.search_and_contents, **exa_args)
|
||||||
search_response = await loop.run_in_executor(None, func)
|
search_response = await loop.run_in_executor(None, func)
|
||||||
|
|
||||||
# 优化结果处理 - 更注重答案质量
|
# 优化结果处理 - 更注重答案质量
|
||||||
results = []
|
results = []
|
||||||
for res in search_response.results:
|
for res in search_response.results:
|
||||||
# 获取最佳内容片段
|
# 获取高亮内容或文本
|
||||||
summary = getattr(res, "summary", "")
|
highlights = getattr(res, "highlights", [])
|
||||||
text = getattr(res, "text", "")
|
text = getattr(res, "text", "")
|
||||||
|
|
||||||
# 智能内容选择:摘要 > 文本开头
|
# 智能内容选择:高亮 > 文本开头
|
||||||
if summary and len(summary) > 50:
|
if highlights and len(highlights) > 0:
|
||||||
snippet = summary.strip()
|
snippet = " ".join(highlights[:3]).strip()
|
||||||
elif text:
|
elif text:
|
||||||
snippet = text[:300] + "..." if len(text) > 300 else text
|
snippet = text[:300] + "..." if len(text) > 300 else text
|
||||||
else:
|
else:
|
||||||
@@ -114,13 +113,12 @@ class ExaSearchEngine(BaseSearchEngine):
|
|||||||
query = args["query"]
|
query = args["query"]
|
||||||
num_results = min(args.get("num_results", 3), 3) # answer模式默认3个结果,专注质量
|
num_results = min(args.get("num_results", 3), 3) # answer模式默认3个结果,专注质量
|
||||||
|
|
||||||
# 精简的搜索参数 - 专注快速答案
|
# 精简的搜索参数 - 使用 search_and_contents
|
||||||
exa_args = {
|
exa_args = {
|
||||||
|
"query": query,
|
||||||
"num_results": num_results,
|
"num_results": num_results,
|
||||||
"contents": {
|
"type": "auto",
|
||||||
"text": False, # 不需要全文
|
"highlights": True,
|
||||||
"summary": True, # 优先摘要
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -129,16 +127,16 @@ class ExaSearchEngine(BaseSearchEngine):
|
|||||||
return []
|
return []
|
||||||
|
|
||||||
loop = asyncio.get_running_loop()
|
loop = asyncio.get_running_loop()
|
||||||
func = functools.partial(exa_client.search, query, **exa_args)
|
func = functools.partial(exa_client.search_and_contents, **exa_args)
|
||||||
search_response = await loop.run_in_executor(None, func)
|
search_response = await loop.run_in_executor(None, func)
|
||||||
|
|
||||||
# 极简结果处理 - 只保留最核心信息
|
# 极简结果处理 - 只保留最核心信息
|
||||||
results = []
|
results = []
|
||||||
for res in search_response.results:
|
for res in search_response.results:
|
||||||
summary = getattr(res, "summary", "")
|
highlights = getattr(res, "highlights", [])
|
||||||
|
|
||||||
# 使用摘要作为答案
|
# 使用高亮作为答案
|
||||||
answer_text = summary.strip() if summary else ""
|
answer_text = " ".join(highlights[:2]).strip() if highlights else ""
|
||||||
|
|
||||||
if answer_text and len(answer_text) > 20:
|
if answer_text and len(answer_text) > 20:
|
||||||
results.append({
|
results.append({
|
||||||
|
|||||||
Reference in New Issue
Block a user