ERP/backend/test_ai.py
2026-01-03 19:18:40 +08:00

57 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
测试AI服务配置
"""
import os
import asyncio
from ai_service import AIService, get_ai_config
async def test_ai_connection():
"""测试AI连接"""
print("测试AI服务配置...")
# 获取配置
config = get_ai_config()
print(f"AI提供商: {config.provider}")
print(f"模型: {config.model}")
print(f"Base URL: {config.base_url}")
print(f"API Key: {config.api_key[:10]}..." if config.api_key else "未配置")
# 测试连接
try:
async with AIService(config) as ai_service:
# 简单的测试提示
test_prompt = "请回答1+1等于几"
if config.provider == "qwen":
response = await ai_service._call_qwen(test_prompt)
elif config.provider == "openai":
response = await ai_service._call_openai(test_prompt)
else:
response = "暂不支持该提供商的测试"
print(f"\nAI响应: {response}")
print("\n✅ AI服务配置测试成功")
except Exception as e:
print(f"\n❌ 测试失败: {str(e)}")
print("\n请检查:")
print("1. API Key是否正确")
print("2. 网络连接是否正常")
print("3. Base URL是否正确")
if __name__ == "__main__":
# 设置环境变量(如果未设置)
if not os.getenv("AI_PROVIDER"):
os.environ["AI_PROVIDER"] = "qwen"
os.environ["QWEN_API_KEY"] = "sk-9bf015da696048eea231085615892b68"
os.environ["QWEN_BASE_URL"] = "https://dashscope.aliyuncs.com/compatible-mode/v1"
# 加载.env文件如果存在
from dotenv import load_dotenv
load_dotenv()
asyncio.run(test_ai_connection())