autool/doc/test/test_llm.py
2026-06-17 19:44:18 +08:00

54 lines
1.9 KiB
Python

import httpx
import asyncio
import json
async def test_chat_completion():
url = "http://192.168.0.10:8888/v1/chat/completions"
# 使用配置文件中存在的模型名称,例如 "GPT-5"
# 如果 keys.yaml 中配置了其他模型,请相应修改
model_name = "moonshotai/kimi-k2.5"
payload = {
"model": model_name,
"messages": [
{"role": "user", "content": "Hello! This is a test request from the test script."}
]
}
print(f"正在发送请求到 {url} ...")
print(f"请求参数: {json.dumps(payload, indent=2, ensure_ascii=False)}")
try:
async with httpx.AsyncClient(timeout=60.0) as client:
response = await client.post(url, json=payload)
print(f"\n响应状态码: {response.status_code}")
try:
response_data = response.json()
print("响应内容:")
print(json.dumps(response_data, indent=2, ensure_ascii=False))
except json.JSONDecodeError:
print("响应内容 (非 JSON):")
print(response.text)
if response.status_code == 200:
print("\n✅ 测试通过: 成功收到响应。")
else:
print(f"\n❌ 测试失败: 服务器返回了错误代码 {response.status_code}")
except httpx.ConnectError:
print(f"\n❌ 连接错误: 无法连接到 {url}。请确认 KeyPool 服务已启动。")
except httpx.ReadTimeout:
print("\n❌ 请求超时: 服务器没有在指定时间内响应。")
except Exception as e:
print(f"\n❌ 发生错误: {type(e).__name__}: {e}")
if __name__ == "__main__":
# 检查 httpx 是否已安装
try:
asyncio.run(test_chat_completion())
except ImportError:
print("错误: 需要安装 httpx 库。请运行 `pip install httpx`。")