添加自定义端点设置
This commit is contained in:
118
test_ai_config.py
Normal file
118
test_ai_config.py
Normal file
@@ -0,0 +1,118 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
AI服务配置测试脚本
|
||||
|
||||
测试不同的AI服务配置是否正确工作,包括自定义端点支持
|
||||
"""
|
||||
|
||||
import os
|
||||
import sys
|
||||
from dotenv import load_dotenv
|
||||
|
||||
# 添加backend目录到路径
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'backend'))
|
||||
|
||||
from ai_service import AIService
|
||||
|
||||
def test_configuration():
|
||||
"""测试AI服务配置"""
|
||||
print("=" * 60)
|
||||
print("AI服务配置测试")
|
||||
print("=" * 60)
|
||||
|
||||
# 加载环境变量
|
||||
load_dotenv()
|
||||
|
||||
# 创建AI服务实例
|
||||
ai = AIService()
|
||||
|
||||
# 显示配置信息
|
||||
print(f"\n当前配置:")
|
||||
print(f" 模型: {ai.model}")
|
||||
print(f" API密钥: {'已配置' if ai.api_key and ai.api_key != 'your_api_key_here' else '未配置'}")
|
||||
print(f" 自定义端点: {ai.api_base if ai.api_base else '未设置'}")
|
||||
print(f" 自定义Provider: {ai.custom_llm_provider if ai.custom_llm_provider else '未设置'}")
|
||||
print(f" 温度: {ai.temperature}")
|
||||
print(f" 最大Token数: {ai.max_tokens}")
|
||||
|
||||
# 获取模型信息
|
||||
model_info = ai.get_model_info()
|
||||
print(f"\n模型信息:")
|
||||
print(f" 服务商: {model_info['provider']}")
|
||||
print(f" 可用性: {'是' if model_info['available'] else '否'}")
|
||||
|
||||
# 显示使用场景说明
|
||||
print(f"\n配置说明:")
|
||||
|
||||
if ai.custom_llm_provider == 'openai' and ai.api_base:
|
||||
print(" ✓ 检测到自定义OpenAI格式端点配置")
|
||||
print(" → 将使用OpenAI兼容格式调用API")
|
||||
print(f" → 端点: {ai.api_base}")
|
||||
print(f" → 模型: {ai.model}")
|
||||
elif ai.model.startswith('gemini-') and not ai.custom_llm_provider:
|
||||
print(" ✓ 使用原生Gemini API")
|
||||
print(" → 需要设置 GEMINI_API_KEY 或 AI_API_KEY")
|
||||
elif ai.model.startswith('claude-'):
|
||||
print(" ✓ 使用原生Claude API")
|
||||
print(" → 需要设置 ANTHROPIC_API_KEY 或 AI_API_KEY")
|
||||
elif ai.model.startswith('gpt-'):
|
||||
print(" ✓ 使用原生OpenAI API")
|
||||
print(" → 需要设置 OPENAI_API_KEY 或 AI_API_KEY")
|
||||
|
||||
# 环境变量检查
|
||||
print(f"\n环境变量检查:")
|
||||
env_vars = {
|
||||
'AI_MODEL': os.getenv('AI_MODEL'),
|
||||
'AI_API_KEY': '已设置' if os.getenv('AI_API_KEY') else '未设置',
|
||||
'AI_API_BASE': os.getenv('AI_API_BASE', '未设置'),
|
||||
'AI_CUSTOM_PROVIDER': os.getenv('AI_CUSTOM_PROVIDER', '未设置'),
|
||||
'OPENAI_API_KEY': '已设置' if os.getenv('OPENAI_API_KEY') else '未设置',
|
||||
'ANTHROPIC_API_KEY': '已设置' if os.getenv('ANTHROPIC_API_KEY') else '未设置',
|
||||
'GEMINI_API_KEY': '已设置' if os.getenv('GEMINI_API_KEY') else '未设置',
|
||||
}
|
||||
|
||||
for key, value in env_vars.items():
|
||||
print(f" {key}: {value}")
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
|
||||
# 测试AI功能(如果配置了API密钥)
|
||||
if ai.is_available():
|
||||
print("\n是否要测试AI润色功能? (y/n): ", end='')
|
||||
try:
|
||||
choice = input().strip().lower()
|
||||
if choice == 'y':
|
||||
test_text = "完成项目文档"
|
||||
print(f"\n测试文本: {test_text}")
|
||||
print("正在调用AI服务...")
|
||||
|
||||
try:
|
||||
result = ai.polish_description(test_text)
|
||||
print(f"\n润色结果: {result}")
|
||||
print("\n✓ AI服务测试成功!")
|
||||
except Exception as e:
|
||||
print(f"\n✗ AI服务测试失败: {e}")
|
||||
except KeyboardInterrupt:
|
||||
print("\n跳过测试")
|
||||
else:
|
||||
print("\nAI服务未配置或配置不正确,跳过功能测试")
|
||||
|
||||
print("\n配置示例:")
|
||||
print("\n1. 使用原生Gemini API:")
|
||||
print(" AI_MODEL=gemini-pro")
|
||||
print(" AI_API_KEY=your_gemini_api_key")
|
||||
|
||||
print("\n2. 使用OpenAI格式的自定义端点调用Gemini:")
|
||||
print(" AI_MODEL=gemini-pro")
|
||||
print(" AI_API_KEY=your_api_key")
|
||||
print(" AI_API_BASE=https://your-endpoint.com/v1")
|
||||
print(" AI_CUSTOM_PROVIDER=openai")
|
||||
|
||||
print("\n3. 使用原生OpenAI API:")
|
||||
print(" AI_MODEL=gpt-3.5-turbo")
|
||||
print(" AI_API_KEY=your_openai_api_key")
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
|
||||
if __name__ == '__main__':
|
||||
test_configuration()
|
||||
Reference in New Issue
Block a user