| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- #!/usr/bin/env python3
- """分析缓存点位置的脚本"""
- import json
- from pathlib import Path
- trace_dir = Path("/Users/elksmmx/Desktop/agent 2.10/Agent/examples/find knowledge/.trace/7ff963bc-3106-47fc-9725-b712f3e0d7d5/messages")
- # 读取所有消息
- messages = []
- for f in sorted(trace_dir.glob("*.json")):
- with open(f) as fp:
- d = json.load(fp)
- messages.append({
- 'seq': d['sequence'],
- 'role': d['role'],
- 'file': f.name
- })
- print(f"总消息数: {len(messages)}\n")
- # 统计role分布
- from collections import Counter
- role_counts = Counter(m['role'] for m in messages)
- print("Role分布:")
- for role, count in role_counts.items():
- print(f" {role}: {count}")
- print("\n" + "="*60)
- print("修改前:包含tool消息的缓存点位置")
- print("="*60)
- # 修改前:user/assistant/tool都算可缓存
- cacheable_old = [m for m in messages if m['role'] in ('user', 'assistant', 'tool')]
- print(f"可缓存消息数: {len(cacheable_old)}")
- cache_positions_old = [19, 39, 59] # 第20, 40, 60条(索引19, 39, 59)
- for pos in cache_positions_old:
- if pos < len(cacheable_old):
- msg = cacheable_old[pos]
- print(f" 第{pos+1}条缓存点: seq={msg['seq']:3d}, role={msg['role']:10s}")
- print("\n" + "="*60)
- print("修改后:只有user/assistant的缓存点位置")
- print("="*60)
- # 修改后:只有user/assistant算可缓存
- cacheable_new = [m for m in messages if m['role'] in ('user', 'assistant')]
- print(f"可缓存消息数: {len(cacheable_new)}")
- cache_positions_new = [19, 39, 59]
- for pos in cache_positions_new:
- if pos < len(cacheable_new):
- msg = cacheable_new[pos]
- print(f" 第{pos+1}条缓存点: seq={msg['seq']:3d}, role={msg['role']:10s}")
- print("\n" + "="*60)
- print("结论")
- print("="*60)
- print("修改前缓存点落在tool消息上,Anthropic API不支持,所以缓存失效")
- print("修改后缓存点会落在assistant消息上,应该能正常创建缓存")
|