refactor(cache): 重构缓存系统为分层语义缓存

将原有的基于文件的 `ToolCache` 替换为全新的 `CacheManager`,引入了更复杂和高效的分层语义缓存机制。

新系统特性:
- **分层缓存**:
  - L1 缓存: 内存字典 (KV) + FAISS (向量),用于极速访问。
  - L2 缓存: SQLite (KV) + ChromaDB (向量),用于持久化存储。
- **语义缓存**: 利用嵌入模型 (Embedding) 对查询进行向量化,实现基于语义相似度的缓存命中,显著提高了缓存命中率。
- **自动失效**: 缓存键包含工具源代码的哈希值,当工具代码更新时,相关缓存会自动失效,避免了脏数据问题。
- **异步支持**: 缓存的 `get` 和 `set` 方法现在是异步的,以适应项目中异步化的工具调用流程。

`web_search_tool` 已更新以使用新的 `CacheManager`,在调用缓存时传递 `tool_class` 和 `semantic_query` 以充分利用新功能。

Co-Authored-By: tt-P607 <68868379+tt-P607@users.noreply.github.com>
This commit is contained in:
minecraft1024a
2025-08-17 22:18:26 +08:00
committed by Windpicker-owo
parent 73f15670e4
commit da73c5593e
4 changed files with 546 additions and 318 deletions

View File

@@ -88,7 +88,8 @@ class WebSurfingTool(BaseTool):
return {"error": "搜索查询不能为空。"}
# 检查缓存
cached_result = tool_cache.get(self.name, function_args)
query = function_args.get("query")
cached_result = await tool_cache.get(self.name, function_args, tool_class=self.__class__, semantic_query=query)
if cached_result:
logger.info(f"缓存命中: {self.name} -> {function_args}")
return cached_result
@@ -109,7 +110,8 @@ class WebSurfingTool(BaseTool):
# 保存到缓存
if "error" not in result:
tool_cache.set(self.name, function_args, result)
query = function_args.get("query")
await tool_cache.set(self.name, function_args, self.__class__, result, semantic_query=query)
return result
@@ -463,7 +465,7 @@ class URLParserTool(BaseTool):
执行URL内容提取和总结。优先使用Exa失败后尝试本地解析。
"""
# 检查缓存
cached_result = tool_cache.get(self.name, function_args)
cached_result = await tool_cache.get(self.name, function_args, tool_class=self.__class__)
if cached_result:
logger.info(f"缓存命中: {self.name} -> {function_args}")
return cached_result
@@ -577,7 +579,7 @@ class URLParserTool(BaseTool):
# 保存到缓存
if "error" not in result:
tool_cache.set(self.name, function_args, result)
await tool_cache.set(self.name, function_args, self.__class__, result)
return result