Spaces:
Running
Running
File size: 2,570 Bytes
f12ea8b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
#!/usr/bin/env python3
"""
简单的 Amazon Q API 测试脚本
"""
import requests
import json
def test_api():
"""测试 API 基本功能"""
url = "http://localhost:8000/v1/chat/completions"
payload = {
"model": "claude-sonnet-4",
"messages": [
{"role": "user", "content": "你好,请简单介绍一下你自己"}
],
"stream": False
}
try:
response = requests.post(url, json=payload, timeout=30)
if response.status_code == 200:
result = response.json()
content = result.get("choices", [{}])[0].get("message", {}).get("content", "")
print("✅ API 测试成功!")
print(f"响应内容: {content}")
return True
else:
print(f"❌ API 测试失败: {response.status_code}")
print(f"错误信息: {response.text}")
return False
except Exception as e:
print(f"❌ 请求异常: {e}")
return False
def test_streaming():
"""测试流式响应"""
url = "http://localhost:8000/v1/chat/completions"
payload = {
"model": "claude-sonnet-4",
"messages": [
{"role": "user", "content": "请用一句话介绍人工智能"}
],
"stream": True
}
try:
response = requests.post(url, json=payload, stream=True, timeout=30)
if response.status_code == 200:
print("✅ 流式测试成功!")
print("流式响应:")
for line in response.iter_lines():
if line:
line_str = line.decode('utf-8')
if line_str.startswith('data: ') and line_str != 'data: [DONE]':
data = json.loads(line_str[6:])
content = data.get("choices", [{}])[0].get("delta", {}).get("content", "")
if content:
print(content, end='', flush=True)
print() # 换行
return True
else:
print(f"❌ 流式测试失败: {response.status_code}")
return False
except Exception as e:
print(f"❌ 流式请求异常: {e}")
return False
if __name__ == "__main__":
print("🧪 Amazon Q API 快速测试")
print("=" * 40)
# 测试基本功能
print("1. 测试基本对话功能...")
test_api()
print("\n2. 测试流式响应...")
test_streaming()
print("\n✨ 测试完成!")
|