100 lines
3.4 KiB
Python
100 lines
3.4 KiB
Python
|
|
"""通义千问API连接测试脚本"""
|
|||
|
|
import sys
|
|||
|
|
from pathlib import Path
|
|||
|
|
|
|||
|
|
# 添加项目路径到sys.path
|
|||
|
|
project_root = Path(__file__).parent
|
|||
|
|
sys.path.insert(0, str(project_root))
|
|||
|
|
|
|||
|
|
from modules.api_client import APIClient
|
|||
|
|
|
|||
|
|
def test_qianwen_api():
|
|||
|
|
"""测试通义千问API连接"""
|
|||
|
|
print("=" * 50)
|
|||
|
|
print("通义千问API连接测试")
|
|||
|
|
print("=" * 50)
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
# 创建API客户端
|
|||
|
|
print("\n1️⃣ 正在初始化API客户端...")
|
|||
|
|
client = APIClient()
|
|||
|
|
|
|||
|
|
# 设置为qianwen提供商
|
|||
|
|
print("2️⃣ 切换到通义千问提供商...")
|
|||
|
|
client.set_provider('qianwen')
|
|||
|
|
|
|||
|
|
# 获取配置信息
|
|||
|
|
config = client.get_provider_config()
|
|||
|
|
print(f"\n📋 当前配置:")
|
|||
|
|
print(f" - 提供商: qianwen")
|
|||
|
|
print(f" - 模型: {config.get('model', 'N/A')}")
|
|||
|
|
print(f" - API地址: {config.get('base_url', 'N/A')}")
|
|||
|
|
print(f" - API密钥: {'已配置' if config.get('api_key') else '未配置'}")
|
|||
|
|
print(f" - 最大tokens: {config.get('max_tokens', 'N/A')}")
|
|||
|
|
print(f" - 温度参数: {config.get('temperature', 'N/A')}")
|
|||
|
|
|
|||
|
|
# 检查API密钥
|
|||
|
|
if not config.get('api_key'):
|
|||
|
|
print("\n❌ 错误: API密钥未配置")
|
|||
|
|
print(" 请在 config/api_config.yaml 中配置 qianwen.api_key")
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
# 发送测试请求
|
|||
|
|
print("\n3️⃣ 发送测试请求...")
|
|||
|
|
test_prompt = "请用一句话介绍你自己。"
|
|||
|
|
print(f" 测试Prompt: {test_prompt}")
|
|||
|
|
|
|||
|
|
print("\n⏳ 等待API响应...")
|
|||
|
|
response = client.call_api(test_prompt)
|
|||
|
|
|
|||
|
|
# 显示响应
|
|||
|
|
print("\n✅ API连接成功!")
|
|||
|
|
print(f"\n📤 API响应:")
|
|||
|
|
print("-" * 50)
|
|||
|
|
print(response)
|
|||
|
|
print("-" * 50)
|
|||
|
|
|
|||
|
|
# 响应统计
|
|||
|
|
print(f"\n📊 响应统计:")
|
|||
|
|
print(f" - 响应长度: {len(response)} 字符")
|
|||
|
|
print(f" - 响应行数: {response.count(chr(10)) + 1} 行")
|
|||
|
|
|
|||
|
|
print("\n" + "=" * 50)
|
|||
|
|
print("✅ 通义千问API连接测试成功!")
|
|||
|
|
print("=" * 50)
|
|||
|
|
return True
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print("\n" + "=" * 50)
|
|||
|
|
print("❌ API连接测试失败!")
|
|||
|
|
print("=" * 50)
|
|||
|
|
print(f"\n错误类型: {type(e).__name__}")
|
|||
|
|
print(f"错误信息: {str(e)}")
|
|||
|
|
|
|||
|
|
# 常见错误提示
|
|||
|
|
print("\n💡 可能的解决方案:")
|
|||
|
|
if "api_key" in str(e).lower() or "unauthorized" in str(e).lower():
|
|||
|
|
print(" - 检查API密钥是否正确")
|
|||
|
|
print(" - 确认API密钥是否有效且未过期")
|
|||
|
|
elif "timeout" in str(e).lower() or "connect" in str(e).lower():
|
|||
|
|
print(" - 检查网络连接")
|
|||
|
|
print(" - 确认API地址是否正确")
|
|||
|
|
print(" - 尝试使用VPN或代理")
|
|||
|
|
elif "max_tokens" in str(e).lower():
|
|||
|
|
print(" - 调整max_tokens参数")
|
|||
|
|
else:
|
|||
|
|
print(" - 查看详细错误信息")
|
|||
|
|
print(" - 检查API配置文件格式")
|
|||
|
|
|
|||
|
|
import traceback
|
|||
|
|
print(f"\n📝 详细错误堆栈:")
|
|||
|
|
print("-" * 50)
|
|||
|
|
traceback.print_exc()
|
|||
|
|
print("-" * 50)
|
|||
|
|
|
|||
|
|
return False
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
success = test_qianwen_api()
|
|||
|
|
sys.exit(0 if success else 1)
|