Files
ailine/test_multi_model.py

135 lines
4.0 KiB
Python
Raw Normal View History

2026-04-13 19:49:18 +08:00
"""
多模型切换功能测试脚本
用于验证后端是否正确支持多模型动态切换
"""
import requests
import json
API_URL = "http://localhost:8001/chat"
def test_model_switching():
"""测试模型切换功能"""
print("=" * 60)
print("测试多模型切换功能")
print("=" * 60)
# 测试消息
test_message = "你好,请简单介绍一下自己"
# 测试不同的模型
models_to_test = ["zhipu", "local"]
for model in models_to_test:
print(f"\n📤 测试模型: {model}")
print("-" * 60)
try:
response = requests.post(
API_URL,
json={
"message": test_message,
"model": model
},
timeout=30
)
if response.status_code == 200:
data = response.json()
print(f"✅ 成功!")
print(f" 使用的模型: {data['model_used']}")
print(f" 会话 ID: {data['thread_id'][:8]}...")
print(f" 回复预览: {data['reply'][:100]}...")
else:
print(f"❌ 失败! 状态码: {response.status_code}")
print(f" 错误信息: {response.text}")
except requests.exceptions.Timeout:
print(f"⏰ 超时! 模型 '{model}' 响应时间过长")
except requests.exceptions.ConnectionError:
print(f"🔌 连接失败! 请确认后端服务正在运行 (python backend.py)")
except Exception as e:
print(f"💥 异常: {str(e)}")
print("\n" + "=" * 60)
print("测试完成!")
print("=" * 60)
def test_conversation_memory():
"""测试跨模型的会话记忆"""
print("\n" + "=" * 60)
print("测试跨模型会话记忆")
print("=" * 60)
import uuid
thread_id = str(uuid.uuid4())
print(f"\n📝 使用固定会话 ID: {thread_id[:8]}...")
# 第一轮对话 - 使用 zhipu 模型
print("\n📤 第1轮 - 使用 zhipu 模型")
try:
response1 = requests.post(
API_URL,
json={
"message": "我叫小明,记住我的名字",
"thread_id": thread_id,
"model": "zhipu"
},
timeout=30
)
if response1.status_code == 200:
data1 = response1.json()
print(f" ✅ 回复: {data1['reply'][:100]}...")
print(f" 🤖 使用模型: {data1['model_used']}")
except Exception as e:
print(f" ❌ 失败: {e}")
return
# 第二轮对话 - 切换到 local 模型,测试是否记得名字
print("\n📤 第2轮 - 切换到 local 模型")
try:
response2 = requests.post(
API_URL,
json={
"message": "我叫什么名字?",
"thread_id": thread_id,
"model": "local"
},
timeout=30
)
if response2.status_code == 200:
data2 = response2.json()
print(f" ✅ 回复: {data2['reply'][:100]}...")
print(f" 🤖 使用模型: {data2['model_used']}")
# 检查是否记得名字
if "小明" in data2['reply']:
print(" 🎉 成功!跨模型记忆功能正常")
else:
print(" ⚠️ 注意:模型可能没有正确回忆上下文")
except Exception as e:
print(f" ❌ 失败: {e}")
print("\n" + "=" * 60)
print("会话记忆测试完成!")
print("=" * 60)
if __name__ == "__main__":
print("\n⚠️ 请确保后端服务正在运行 (python backend.py)\n")
# 运行基本测试
test_model_switching()
# 询问是否运行记忆测试
choice = input("\n是否运行会话记忆测试?(y/n): ").strip().lower()
if choice == 'y':
test_conversation_memory()
print("\n✨ 所有测试完成!")