CLEAR_CACHE_GUIDE.md 2.6 KB

快速清除缓存指南

问题说明

如果您在代码更新前运行过程序,旧的缓存文件不包含detail信息(prompt和response)。

更新代码后,系统会从旧缓存读取,导致最终的execution_record.json中某些步骤缺少detail字段。

解决方案

方案1: 使用清除工具(推荐)

运行清除缓存工具:

python clear_cache.py

选项:

  1. 列出所有缓存 - 查看缓存了哪些问题
  2. 清除所有缓存 - 删除所有缓存,重新运行
  3. 清除特定问题 - 只删除某个问题的缓存

方案2: 手动删除缓存目录

直接删除缓存目录:

# PowerShell
Remove-Item -Recurse -Force .cache

或者在文件管理器中删除 .cache 文件夹。

方案3: 代码中清除

from knowledge_v2.cache_manager import CacheManager

cache = CacheManager()

# 清除所有缓存
cache.clear()

# 或清除特定问题的缓存
cache.clear("小老虎 穿搭")

重新运行

清除缓存后,重新运行您的代码:

from knowledge_v2.function_knowledge import get_knowledge

result = get_knowledge(
    question="小老虎 穿搭",
    post_info="无",
    persona_info="游戏博主"
)

此时所有步骤都会重新执行,execution_record.json中的每个步骤都会包含完整的detail信息。

验证结果

检查生成的 execution_record.json

import json

# 读取执行记录
with open('.cache/{hash}/execution_record.json', 'r', encoding='utf-8') as f:
    record = json.load(f)

# 检查每个步骤是否有detail
for step in record['execution']['steps']:
    print(f"步骤 {step['step']}: {step['name']}")
    if 'detail' in step:
        detail = step['detail']
        print(f"  - cached: {detail.get('cached', False)}")
        print(f"  - 有prompt: {'prompt' in detail and detail['prompt'] is not None}")
        print(f"  - 有response: {'response' in detail and detail['response'] is not None}")
    else:
        print("  - ⚠️ 缺少detail字段")

预期输出

清除缓存后重新运行,应该看到:

步骤 1: generate_query
  - cached: False
  - 有prompt: True
  - 有response: True
步骤 2: select_tool
  - cached: False
  - 有prompt: True
  - 有response: True
步骤 3: extract_tool_params
  - cached: False
  - 有prompt: True
  - 有response: True
步骤 4: call_tool
  (此步骤不涉及LLM,没有detail字段)

未来避免此问题

以后如果修改了缓存结构,建议:

  1. 清除旧缓存
  2. 或者升级缓存版本号
  3. 让系统自动识别并忽略旧格式的缓存