Local-Voice/test_llm.py
2025-09-20 17:18:41 +08:00

96 lines
2.9 KiB
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
测试大语言模型API功能
"""
import os
import requests
import json
def test_llm_api():
"""测试大语言模型API"""
# 检查API密钥
api_key = os.environ.get("ARK_API_KEY")
if not api_key:
print("❌ 未设置 ARK_API_KEY 环境变量")
return False
print(f"✅ API密钥已设置: {api_key[:20]}...")
# API配置
api_url = "https://ark.cn-beijing.volces.com/api/v3/chat/completions"
model = "doubao-1-5-pro-32k-250115"
# 测试消息
test_message = "你好,请简单介绍一下自己"
try:
print("🤖 测试大语言模型API...")
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
data = {
"model": model,
"messages": [
{
"role": "system",
"content": "你是一个智能助手,请根据用户的语音输入提供有帮助的回答。保持回答简洁明了。"
},
{
"role": "user",
"content": test_message
}
]
}
response = requests.post(api_url, headers=headers, json=data, timeout=30)
print(f"📡 HTTP状态码: {response.status_code}")
if response.status_code == 200:
result = response.json()
print("✅ API调用成功")
if "choices" in result and len(result["choices"]) > 0:
llm_response = result["choices"][0]["message"]["content"]
print(f"💬 AI回复: {llm_response}")
# 显示完整响应结构
print("\n📋 完整响应结构:")
print(json.dumps(result, indent=2, ensure_ascii=False))
return True
else:
print("❌ 响应格式错误")
print(f"响应内容: {response.text}")
return False
else:
print(f"❌ API调用失败: {response.status_code}")
print(f"响应内容: {response.text}")
return False
except requests.exceptions.RequestException as e:
print(f"❌ 网络请求失败: {e}")
return False
except Exception as e:
print(f"❌ 测试失败: {e}")
return False
if __name__ == "__main__":
print("🧪 测试大语言模型API功能")
print("=" * 50)
success = test_llm_api()
if success:
print("\n✅ 大语言模型功能测试通过!")
print("🚀 现在可以运行完整的语音助手系统了")
else:
print("\n❌ 大语言模型功能测试失败")
print("🔧 请检查API密钥和网络连接")