first commit
This commit is contained in:
237
tests/test_phase1_setup.py
Normal file
237
tests/test_phase1_setup.py
Normal file
@ -0,0 +1,237 @@
|
||||
"""
|
||||
Phase 1 基础设施测试
|
||||
|
||||
测试项:
|
||||
1. 依赖包导入
|
||||
2. API密钥配置
|
||||
3. LLM连接
|
||||
4. 批量搜索工具
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
|
||||
# 添加src目录到Python路径
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
|
||||
|
||||
|
||||
def test_imports():
|
||||
"""测试所有必要的包是否能正确导入"""
|
||||
print("=" * 60)
|
||||
print("测试 1: 检查依赖包导入")
|
||||
print("=" * 60)
|
||||
|
||||
try:
|
||||
import deepagents
|
||||
print("✓ deepagents 导入成功")
|
||||
except ImportError as e:
|
||||
print(f"✗ deepagents 导入失败: {e}")
|
||||
return False
|
||||
|
||||
try:
|
||||
import langchain
|
||||
print("✓ langchain 导入成功")
|
||||
except ImportError as e:
|
||||
print(f"✗ langchain 导入失败: {e}")
|
||||
return False
|
||||
|
||||
try:
|
||||
import tavily
|
||||
print("✓ tavily 导入成功")
|
||||
except ImportError as e:
|
||||
print(f"✗ tavily 导入失败: {e}")
|
||||
return False
|
||||
|
||||
try:
|
||||
from dotenv import load_dotenv
|
||||
print("✓ python-dotenv 导入成功")
|
||||
except ImportError as e:
|
||||
print(f"✗ python-dotenv 导入失败: {e}")
|
||||
return False
|
||||
|
||||
try:
|
||||
import click
|
||||
print("✓ click 导入成功")
|
||||
except ImportError as e:
|
||||
print(f"✗ click 导入失败: {e}")
|
||||
return False
|
||||
|
||||
try:
|
||||
from rich import print as rprint
|
||||
print("✓ rich 导入成功")
|
||||
except ImportError as e:
|
||||
print(f"✗ rich 导入失败: {e}")
|
||||
return False
|
||||
|
||||
print("\n所有依赖包导入成功!\n")
|
||||
return True
|
||||
|
||||
|
||||
def test_config():
|
||||
"""测试配置是否正确"""
|
||||
print("=" * 60)
|
||||
print("测试 2: 检查配置")
|
||||
print("=" * 60)
|
||||
|
||||
try:
|
||||
from src.config import Config
|
||||
|
||||
print(f"LLM模型: {Config.LLM_MODEL}")
|
||||
print(f"LLM温度: {Config.LLM_TEMPERATURE}")
|
||||
print(f"最大Tokens: {Config.LLM_MAX_TOKENS}")
|
||||
print(f"默认深度模式: {Config.DEFAULT_DEPTH}")
|
||||
print(f"最大并行搜索数: {Config.MAX_PARALLEL_SEARCHES}")
|
||||
print(f"搜索超时: {Config.SEARCH_TIMEOUT}秒")
|
||||
|
||||
# 检查API密钥
|
||||
if Config.DASHSCOPE_API_KEY and Config.DASHSCOPE_API_KEY != "your_dashscope_api_key_here":
|
||||
print("✓ DASHSCOPE_API_KEY 已配置")
|
||||
else:
|
||||
print("✗ DASHSCOPE_API_KEY 未配置或使用默认值")
|
||||
print(" 请在.env文件中设置真实的API密钥")
|
||||
return False
|
||||
|
||||
if Config.TAVILY_API_KEY and Config.TAVILY_API_KEY != "your_tavily_api_key_here":
|
||||
print("✓ TAVILY_API_KEY 已配置")
|
||||
else:
|
||||
print("✗ TAVILY_API_KEY 未配置或使用默认值")
|
||||
print(" 请在.env文件中设置真实的API密钥")
|
||||
return False
|
||||
|
||||
print("\n配置检查通过!\n")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ 配置检查失败: {e}\n")
|
||||
return False
|
||||
|
||||
|
||||
def test_llm_connection():
|
||||
"""测试LLM连接"""
|
||||
print("=" * 60)
|
||||
print("测试 3: 检查LLM连接")
|
||||
print("=" * 60)
|
||||
|
||||
try:
|
||||
from src.config import Config
|
||||
|
||||
llm = Config.get_llm()
|
||||
print(f"LLM实例创建成功: {llm.model_name}")
|
||||
|
||||
# 发送一个简单的测试消息
|
||||
print("发送测试消息...")
|
||||
response = llm.invoke("你好,请用一句话介绍你自己。")
|
||||
print(f"LLM响应: {response.content[:100]}...")
|
||||
|
||||
print("\n✓ LLM连接测试成功!\n")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ LLM连接测试失败: {e}\n")
|
||||
return False
|
||||
|
||||
|
||||
def test_search_tools():
|
||||
"""测试批量搜索工具"""
|
||||
print("=" * 60)
|
||||
print("测试 4: 检查批量搜索工具")
|
||||
print("=" * 60)
|
||||
|
||||
try:
|
||||
from src.tools.search_tools import batch_internet_search
|
||||
|
||||
# 测试并行搜索
|
||||
test_queries = [
|
||||
"Python programming",
|
||||
"Machine learning basics",
|
||||
"Web development tutorial"
|
||||
]
|
||||
|
||||
print(f"执行 {len(test_queries)} 个并行搜索...")
|
||||
print(f"查询: {test_queries}")
|
||||
|
||||
result = batch_internet_search.invoke({
|
||||
"queries": test_queries,
|
||||
"max_results_per_query": 3
|
||||
})
|
||||
|
||||
print(f"\n搜索结果统计:")
|
||||
print(f" 总查询数: {result['total_queries']}")
|
||||
print(f" 成功查询: {result['successful_queries']}")
|
||||
print(f" 失败查询: {result['failed_queries']}")
|
||||
print(f" 总结果数: {result['total_results']}")
|
||||
print(f" 去重后结果数: {result['unique_results']}")
|
||||
|
||||
if result['errors']:
|
||||
print(f"\n错误信息:")
|
||||
for error in result['errors']:
|
||||
print(f" - {error}")
|
||||
|
||||
if result['success'] and result['unique_results'] > 0:
|
||||
print(f"\n前3个搜索结果:")
|
||||
for i, res in enumerate(result['results'][:3], 1):
|
||||
print(f" {i}. {res.get('title', 'N/A')}")
|
||||
print(f" URL: {res.get('url', 'N/A')}")
|
||||
print(f" 得分: {res.get('score', 'N/A')}")
|
||||
|
||||
print("\n✓ 批量搜索工具测试成功!\n")
|
||||
return True
|
||||
else:
|
||||
print("\n✗ 批量搜索工具测试失败:未返回有效结果\n")
|
||||
return False
|
||||
|
||||
except Exception as e:
|
||||
print(f"✗ 批量搜索工具测试失败: {e}\n")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
"""运行所有测试"""
|
||||
print("\n")
|
||||
print("=" * 60)
|
||||
print("Phase 1 基础设施测试")
|
||||
print("=" * 60)
|
||||
print("\n")
|
||||
|
||||
results = []
|
||||
|
||||
# 测试1: 导入检查
|
||||
results.append(("依赖包导入", test_imports()))
|
||||
|
||||
# 测试2: 配置检查
|
||||
results.append(("配置检查", test_config()))
|
||||
|
||||
# 测试3: LLM连接(如果配置通过)
|
||||
if results[-1][1]:
|
||||
results.append(("LLM连接", test_llm_connection()))
|
||||
|
||||
# 测试4: 搜索工具(如果配置通过)
|
||||
if results[1][1]:
|
||||
results.append(("批量搜索工具", test_search_tools()))
|
||||
|
||||
# 总结
|
||||
print("=" * 60)
|
||||
print("测试总结")
|
||||
print("=" * 60)
|
||||
|
||||
for test_name, passed in results:
|
||||
status = "✓ 通过" if passed else "✗ 失败"
|
||||
print(f"{test_name}: {status}")
|
||||
|
||||
all_passed = all(result[1] for result in results)
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
if all_passed:
|
||||
print("✓ 所有测试通过!Phase 1 基础设施搭建完成。")
|
||||
else:
|
||||
print("✗ 部分测试失败,请检查配置和依赖。")
|
||||
print("=" * 60 + "\n")
|
||||
|
||||
return all_passed
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
success = main()
|
||||
sys.exit(0 if success else 1)
|
||||
Reference in New Issue
Block a user