Create runway_openai.py
Browse files- runway_openai.py +34 -0
runway_openai.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
|
3 |
+
# 设置 API 密钥和请求的 URL
|
4 |
+
url = 'https://sanbo1200-degpt.hf.space/hf/v1/chat/completions'
|
5 |
+
|
6 |
+
# 构建请求头
|
7 |
+
headers = {
|
8 |
+
# 'Authorization': f'Bearer {api_key}',
|
9 |
+
'Content-Type': 'application/json',
|
10 |
+
}
|
11 |
+
|
12 |
+
# 构建请求体
|
13 |
+
data = {
|
14 |
+
'model': 'gpt-4o', # 或者使用其他可用的模型
|
15 |
+
'messages': [
|
16 |
+
{'role': 'user', 'content': '你好,你是谁?'},
|
17 |
+
],
|
18 |
+
'max_tokens': 100, # 设置生成的最大 token 数量
|
19 |
+
}
|
20 |
+
|
21 |
+
# 发送 POST 请求
|
22 |
+
response = requests.post(url, headers=headers, json=data)
|
23 |
+
|
24 |
+
# 检查响应状态
|
25 |
+
if response.status_code == 200:
|
26 |
+
# print('请求成功',response.text)
|
27 |
+
# 解析 JSON 响应
|
28 |
+
response_data = response.json()
|
29 |
+
# 获取生成的文本
|
30 |
+
generated_text = response_data['choices'][0]['message']['content']
|
31 |
+
print('ChatGPT 的回复:', generated_text)
|
32 |
+
else:
|
33 |
+
print('请求失败,状态码:', response.status_code)
|
34 |
+
print('错误信息:', response.text)
|