优化Agent上下文大小

This commit is contained in:
jxxghp
2025-11-17 09:57:12 +08:00
parent 82113abe88
commit d572e523ba
7 changed files with 167 additions and 9 deletions

View File

@@ -60,7 +60,32 @@ class SearchMediaTool(MoviePilotTool):
filtered_results.append(result)
if filtered_results:
return json.dumps([r.to_dict() for r in filtered_results], ensure_ascii=False, indent=2)
# 限制最多20条结果
total_count = len(filtered_results)
limited_results = filtered_results[:20]
# 精简字段,只保留关键信息
simplified_results = []
for r in limited_results:
simplified = {
"title": r.title,
"en_title": r.en_title,
"year": r.year,
"type": r.type.value if r.type else None,
"season": r.season,
"tmdb_id": r.tmdb_id,
"imdb_id": r.imdb_id,
"douban_id": r.douban_id,
"overview": r.overview[:200] + "..." if r.overview and len(r.overview) > 200 else r.overview,
"vote_average": r.vote_average,
"poster_path": r.poster_path,
"detail_link": r.detail_link
}
simplified_results.append(simplified)
result_json = json.dumps(simplified_results, ensure_ascii=False, indent=2)
# 如果结果被裁剪,添加提示信息
if total_count > 20:
return f"注意:搜索结果共找到 {total_count} 条,为节省上下文空间,仅显示前 20 条结果。\n\n{result_json}"
return result_json
else:
return f"未找到符合条件的媒体资源: {title}"
else: