check_cache_positions.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #!/usr/bin/env python3
  2. """分析缓存点位置的脚本"""
  3. import json
  4. from pathlib import Path
  5. trace_dir = Path("/Users/elksmmx/Desktop/agent 2.10/Agent/examples/find knowledge/.trace/7ff963bc-3106-47fc-9725-b712f3e0d7d5/messages")
  6. # 读取所有消息
  7. messages = []
  8. for f in sorted(trace_dir.glob("*.json")):
  9. with open(f) as fp:
  10. d = json.load(fp)
  11. messages.append({
  12. 'seq': d['sequence'],
  13. 'role': d['role'],
  14. 'file': f.name
  15. })
  16. print(f"总消息数: {len(messages)}\n")
  17. # 统计role分布
  18. from collections import Counter
  19. role_counts = Counter(m['role'] for m in messages)
  20. print("Role分布:")
  21. for role, count in role_counts.items():
  22. print(f" {role}: {count}")
  23. print("\n" + "="*60)
  24. print("修改前:包含tool消息的缓存点位置")
  25. print("="*60)
  26. # 修改前:user/assistant/tool都算可缓存
  27. cacheable_old = [m for m in messages if m['role'] in ('user', 'assistant', 'tool')]
  28. print(f"可缓存消息数: {len(cacheable_old)}")
  29. cache_positions_old = [19, 39, 59] # 第20, 40, 60条(索引19, 39, 59)
  30. for pos in cache_positions_old:
  31. if pos < len(cacheable_old):
  32. msg = cacheable_old[pos]
  33. print(f" 第{pos+1}条缓存点: seq={msg['seq']:3d}, role={msg['role']:10s}")
  34. print("\n" + "="*60)
  35. print("修改后:只有user/assistant的缓存点位置")
  36. print("="*60)
  37. # 修改后:只有user/assistant算可缓存
  38. cacheable_new = [m for m in messages if m['role'] in ('user', 'assistant')]
  39. print(f"可缓存消息数: {len(cacheable_new)}")
  40. cache_positions_new = [19, 39, 59]
  41. for pos in cache_positions_new:
  42. if pos < len(cacheable_new):
  43. msg = cacheable_new[pos]
  44. print(f" 第{pos+1}条缓存点: seq={msg['seq']:3d}, role={msg['role']:10s}")
  45. print("\n" + "="*60)
  46. print("结论")
  47. print("="*60)
  48. print("修改前缓存点落在tool消息上,Anthropic API不支持,所以缓存失效")
  49. print("修改后缓存点会落在assistant消息上,应该能正常创建缓存")