112 lines
3.3 KiB
Python
112 lines
3.3 KiB
Python
|
|
#!/usr/bin/env python
|
|||
|
|
# -*- coding: utf-8 -*-
|
|||
|
|
|
|||
|
|
"""
|
|||
|
|
模型管理器测试脚本
|
|||
|
|
用于验证新模型管理功能的正确性
|
|||
|
|
"""
|
|||
|
|
|
|||
|
|
import os
|
|||
|
|
import sys
|
|||
|
|
import json
|
|||
|
|
|
|||
|
|
# 添加项目根目录到Python路径
|
|||
|
|
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
|||
|
|
|
|||
|
|
from model_manager import model_router, OpenAIProvider, OpenRouterProvider, SiliconFlowProvider
|
|||
|
|
from config import settings
|
|||
|
|
|
|||
|
|
def test_model_router():
|
|||
|
|
"""测试模型路由器功能"""
|
|||
|
|
print("=== 测试模型路由器功能 ===")
|
|||
|
|
|
|||
|
|
# 显示所有可用模型
|
|||
|
|
print("可用模型:")
|
|||
|
|
for model in model_router.list_models():
|
|||
|
|
print(f" - {model}")
|
|||
|
|
|
|||
|
|
# 显示所有提供商
|
|||
|
|
print("\n可用提供商:")
|
|||
|
|
for provider in model_router.list_providers():
|
|||
|
|
print(f" - {provider}")
|
|||
|
|
|
|||
|
|
# 测试模型路由
|
|||
|
|
print("\n测试模型路由:")
|
|||
|
|
test_models = ["gpt-3.5-turbo", "text-embedding-ada-002"]
|
|||
|
|
for model in test_models:
|
|||
|
|
provider = model_router.get_provider_for_model(model)
|
|||
|
|
if provider:
|
|||
|
|
print(f" {model} -> {provider.name}")
|
|||
|
|
else:
|
|||
|
|
print(f" {model} -> 未找到提供商")
|
|||
|
|
|
|||
|
|
print("\n=== 测试完成 ===")
|
|||
|
|
|
|||
|
|
def test_openai_provider():
|
|||
|
|
"""测试OpenAI提供商"""
|
|||
|
|
print("\n=== 测试OpenAI提供商 ===")
|
|||
|
|
|
|||
|
|
if settings.OPENAI_API_KEY:
|
|||
|
|
provider = OpenAIProvider(
|
|||
|
|
name="openai_test",
|
|||
|
|
api_key=settings.OPENAI_API_KEY,
|
|||
|
|
base_url=settings.OPENAI_API_BASE or "https://api.openai.com/v1",
|
|||
|
|
models=["gpt-3.5-turbo"]
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
try:
|
|||
|
|
# 测试文本生成
|
|||
|
|
messages = [
|
|||
|
|
{"role": "user", "content": "你好,这是一个测试消息"}
|
|||
|
|
]
|
|||
|
|
response = provider.generate_text("gpt-3.5-turbo", messages, max_tokens=50)
|
|||
|
|
print(f"文本生成测试通过: {response[:50]}...")
|
|||
|
|
|
|||
|
|
# 测试嵌入获取
|
|||
|
|
texts = ["这是一个测试文本"]
|
|||
|
|
embeddings = provider.get_embeddings("text-embedding-ada-002", texts)
|
|||
|
|
print(f"嵌入获取测试通过: 维度 {len(embeddings[0])}")
|
|||
|
|
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"OpenAI提供商测试失败: {e}")
|
|||
|
|
else:
|
|||
|
|
print("未配置OpenAI API密钥,跳过测试")
|
|||
|
|
|
|||
|
|
print("=== OpenAI提供商测试完成 ===")
|
|||
|
|
|
|||
|
|
def test_config_loading():
|
|||
|
|
"""测试配置加载功能"""
|
|||
|
|
print("\n=== 测试配置加载功能 ===")
|
|||
|
|
|
|||
|
|
# 检查模型配置文件是否存在
|
|||
|
|
config_file = "model_config.json"
|
|||
|
|
if os.path.exists(config_file):
|
|||
|
|
try:
|
|||
|
|
with open(config_file, 'r', encoding='utf-8') as f:
|
|||
|
|
config = json.load(f)
|
|||
|
|
print("模型配置文件加载成功")
|
|||
|
|
print(f"配置文件包含 {len(config.get('providers', []))} 个提供商")
|
|||
|
|
except Exception as e:
|
|||
|
|
print(f"模型配置文件加载失败: {e}")
|
|||
|
|
else:
|
|||
|
|
print("模型配置文件不存在")
|
|||
|
|
|
|||
|
|
print("=== 配置加载测试完成 ===")
|
|||
|
|
|
|||
|
|
def main():
|
|||
|
|
"""主测试函数"""
|
|||
|
|
print("开始测试模型管理功能...")
|
|||
|
|
|
|||
|
|
# 测试配置加载
|
|||
|
|
test_config_loading()
|
|||
|
|
|
|||
|
|
# 测试模型路由器
|
|||
|
|
test_model_router()
|
|||
|
|
|
|||
|
|
# 测试OpenAI提供商
|
|||
|
|
test_openai_provider()
|
|||
|
|
|
|||
|
|
print("\n所有测试完成!")
|
|||
|
|
|
|||
|
|
if __name__ == "__main__":
|
|||
|
|
main()
|