195 lines
6.0 KiB
Python
195 lines
6.0 KiB
Python
|
|
"""
|
|||
|
|
调试研究流程 V2 - 检查虚拟文件系统
|
|||
|
|
|
|||
|
|
使用方法:
|
|||
|
|
export PYTHONIOENCODING=utf-8 && python tests/debug_research_v2.py
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
import sys
|
|||
|
|
import os
|
|||
|
|
import json
|
|||
|
|
from datetime import datetime
|
|||
|
|
|
|||
|
|
# 添加项目根目录到Python路径
|
|||
|
|
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|||
|
|
|
|||
|
|
from src.agents.coordinator import create_research_coordinator
|
|||
|
|
from src.config import Config
|
|||
|
|
|
|||
|
|
|
|||
|
|
def debug_research_with_files(question: str, depth: str = "quick"):
|
|||
|
|
"""
|
|||
|
|
调试研究流程,重点检查虚拟文件系统
|
|||
|
|
|
|||
|
|
Args:
|
|||
|
|
question: 研究问题
|
|||
|
|
depth: 深度模式
|
|||
|
|
"""
|
|||
|
|
print("\n" + "🔬 " * 40)
|
|||
|
|
print("智能深度研究系统 - 调试模式 V2")
|
|||
|
|
print("🔬 " * 40)
|
|||
|
|
|
|||
|
|
print(f"\n研究问题: {question}")
|
|||
|
|
print(f"深度模式: {depth}")
|
|||
|
|
print(f"开始时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")
|
|||
|
|
|
|||
|
|
# 创建Agent
|
|||
|
|
print("\n" + "="*80)
|
|||
|
|
print("创建ResearchCoordinator Agent")
|
|||
|
|
print("="*80)
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
agent = create_research_coordinator(
|
|||
|
|
question=question,
|
|||
|
|
depth=depth,
|
|||
|
|
format="technical",
|
|||
|
|
min_tier=3
|
|||
|
|
)
|
|||
|
|
print("✅ Agent创建成功")
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"❌ Agent创建失败: {e}")
|
|||
|
|
import traceback
|
|||
|
|
traceback.print_exc()
|
|||
|
|
return
|
|||
|
|
|
|||
|
|
# 执行研究
|
|||
|
|
print("\n" + "="*80)
|
|||
|
|
print("执行研究流程")
|
|||
|
|
print("="*80)
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
start_time = datetime.now()
|
|||
|
|
|
|||
|
|
result = agent.invoke({
|
|||
|
|
"messages": [
|
|||
|
|
{
|
|||
|
|
"role": "user",
|
|||
|
|
"content": f"请开始研究这个问题:{question}"
|
|||
|
|
}
|
|||
|
|
]
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
end_time = datetime.now()
|
|||
|
|
duration = (end_time - start_time).total_seconds()
|
|||
|
|
|
|||
|
|
print(f"\n✅ 执行完成!耗时: {duration:.2f}秒")
|
|||
|
|
|
|||
|
|
# 分析结果
|
|||
|
|
print("\n" + "="*80)
|
|||
|
|
print("结果分析")
|
|||
|
|
print("="*80)
|
|||
|
|
|
|||
|
|
print(f"\n结果类型: {type(result)}")
|
|||
|
|
print(f"结果键: {list(result.keys())}")
|
|||
|
|
|
|||
|
|
# 检查消息
|
|||
|
|
if 'messages' in result:
|
|||
|
|
messages = result['messages']
|
|||
|
|
print(f"\n📨 消息数量: {len(messages)}")
|
|||
|
|
|
|||
|
|
print("\n所有消息内容:")
|
|||
|
|
for i, msg in enumerate(messages, 1):
|
|||
|
|
print(f"\n{'='*60}")
|
|||
|
|
print(f"消息 #{i}")
|
|||
|
|
print('='*60)
|
|||
|
|
|
|||
|
|
# 检查消息类型
|
|||
|
|
msg_type = type(msg).__name__
|
|||
|
|
print(f"类型: {msg_type}")
|
|||
|
|
|
|||
|
|
# 提取内容
|
|||
|
|
if hasattr(msg, 'content'):
|
|||
|
|
content = msg.content
|
|||
|
|
print(f"内容长度: {len(content)} 字符")
|
|||
|
|
|
|||
|
|
# 显示内容
|
|||
|
|
if len(content) > 500:
|
|||
|
|
print(f"\n内容预览:\n{content[:500]}...")
|
|||
|
|
else:
|
|||
|
|
print(f"\n完整内容:\n{content}")
|
|||
|
|
|
|||
|
|
# 检查其他属性
|
|||
|
|
if hasattr(msg, 'additional_kwargs'):
|
|||
|
|
kwargs = msg.additional_kwargs
|
|||
|
|
if kwargs:
|
|||
|
|
print(f"\n额外参数: {kwargs}")
|
|||
|
|
|
|||
|
|
if hasattr(msg, 'tool_calls'):
|
|||
|
|
tool_calls = msg.tool_calls
|
|||
|
|
if tool_calls:
|
|||
|
|
print(f"\n工具调用: {tool_calls}")
|
|||
|
|
|
|||
|
|
# 检查文件系统
|
|||
|
|
if 'files' in result:
|
|||
|
|
files = result['files']
|
|||
|
|
print("\n" + "="*80)
|
|||
|
|
print("虚拟文件系统")
|
|||
|
|
print("="*80)
|
|||
|
|
print(f"\n📁 文件数量: {len(files)}")
|
|||
|
|
|
|||
|
|
for file_path, file_info in files.items():
|
|||
|
|
print(f"\n{'='*60}")
|
|||
|
|
print(f"文件: {file_path}")
|
|||
|
|
print('='*60)
|
|||
|
|
|
|||
|
|
# 显示文件信息
|
|||
|
|
if isinstance(file_info, dict):
|
|||
|
|
for key, value in file_info.items():
|
|||
|
|
if key == 'content':
|
|||
|
|
if len(str(value)) > 300:
|
|||
|
|
print(f"{key}: {str(value)[:300]}...")
|
|||
|
|
else:
|
|||
|
|
print(f"{key}: {value}")
|
|||
|
|
else:
|
|||
|
|
print(f"{key}: {value}")
|
|||
|
|
else:
|
|||
|
|
if len(str(file_info)) > 300:
|
|||
|
|
print(f"内容: {str(file_info)[:300]}...")
|
|||
|
|
else:
|
|||
|
|
print(f"内容: {file_info}")
|
|||
|
|
|
|||
|
|
# 保存完整结果
|
|||
|
|
print("\n" + "="*80)
|
|||
|
|
print("保存调试结果")
|
|||
|
|
print("="*80)
|
|||
|
|
|
|||
|
|
output_dir = "outputs/debug"
|
|||
|
|
os.makedirs(output_dir, exist_ok=True)
|
|||
|
|
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
|||
|
|
|
|||
|
|
# 保存JSON结果
|
|||
|
|
output_file = os.path.join(output_dir, f"debug_v2_{timestamp}.json")
|
|||
|
|
with open(output_file, 'w', encoding='utf-8') as f:
|
|||
|
|
# 序列化结果
|
|||
|
|
serialized_result = {
|
|||
|
|
"question": question,
|
|||
|
|
"depth": depth,
|
|||
|
|
"duration_seconds": duration,
|
|||
|
|
"messages": [
|
|||
|
|
{
|
|||
|
|
"type": type(msg).__name__,
|
|||
|
|
"content": msg.content if hasattr(msg, 'content') else str(msg)
|
|||
|
|
}
|
|||
|
|
for msg in result.get('messages', [])
|
|||
|
|
],
|
|||
|
|
"files": {
|
|||
|
|
path: str(content)
|
|||
|
|
for path, content in result.get('files', {}).items()
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
json.dump(serialized_result, f, ensure_ascii=False, indent=2)
|
|||
|
|
|
|||
|
|
print(f"✅ 调试结果已保存到: {output_file}")
|
|||
|
|
|
|||
|
|
except KeyboardInterrupt:
|
|||
|
|
print("\n\n⚠️ 用户中断执行")
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"\n\n❌ 执行失败: {e}")
|
|||
|
|
import traceback
|
|||
|
|
traceback.print_exc()
|
|||
|
|
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
question = "Python asyncio最佳实践"
|
|||
|
|
debug_research_with_files(question, depth="quick")
|