Spaces:
Running
Running
""" | |
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) |