-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-spark-python.py
More file actions
48 lines (40 loc) · 1.42 KB
/
test-spark-python.py
File metadata and controls
48 lines (40 loc) · 1.42 KB
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
import requests
import os
def test_spark_api():
url = "https://spark-api-open.xf-yun.com/v1/chat/completions"
# 从环境变量获取API密钥,如果没有则使用默认值
api_key = os.getenv('SPARK_API_KEY', 'hkkdhRUdnrXkkDtAMrFz:CKFQKyeLzhvXsyhggfOY')
data = {
"max_tokens": 4096,
"top_k": 4,
"temperature": 0.5,
"messages": [
{
"role": "user",
"content": "运动建议"
}
],
"model": "lite",
"stream": False
}
headers = {
"Authorization": f"Bearer {api_key}"
}
try:
response = requests.post(url, headers=headers, json=data)
response.encoding = "utf-8"
if response.status_code == 200:
print("✅ 讯飞星火API调用成功!")
result = response.json()
print("🤖 回复内容:")
print(result['choices'][0]['message']['content'])
else:
print(f"❌ 讯飞星火API调用失败,状态码: {response.status_code}")
print("响应内容:", response.text)
except Exception as e:
print(f"❌ 讯飞星火API调用异常: {str(e)}")
if __name__ == '__main__':
print("开始测试讯飞星火API...")
print("📍 API地址:", "https://spark-api-open.xf-yun.com/v1/chat/completions")
print("🤖 模型: lite")
test_spark_api()