File size: 1,339 Bytes
442e443
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66c533d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
curl -X GET http://localhost:7680/api/models

curl -X POST https://sanbo1200-degpt.hf.space/hf/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
  "model": "Qwen2.5-72B",
    "messages": [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "你是什么模型,你有什么特点?"}
    ]
}'


"""
import requests

# 设置 API 密钥和请求的 URL
url = 'https://sanbo1200-degpt.hf.space/hf/v1/chat/completions'

# 构建请求头
headers = {
    # 'Authorization': f'Bearer {api_key}',
    'Content-Type': 'application/json',
}

# 构建请求体
data = {
    'model': 'gpt-4o',  # 或者使用其他可用的模型
    'messages': [
        {'role': 'user', 'content': '你好,你是谁?'},
    ],
    'max_tokens': 100,  # 设置生成的最大 token 数量
}

# 发送 POST 请求
response = requests.post(url, headers=headers, json=data)

# 检查响应状态
if response.status_code == 200:
    # print('请求成功',response.text)
    # 解析 JSON 响应
    response_data = response.json()
    # 获取生成的文本
    generated_text = response_data['choices'][0]['message']['content']
    print('ChatGPT 的回复:', generated_text)
else:
    print('请求失败,状态码:', response.status_code)
    print('错误信息:', response.text)