import requests
import os
# 设置代理服务器地址和端口号
proxies = {
"https": "http://127.0.0.1:9910",
"http": "http://127.0.0.1:9910"
}
# 设置API密钥和API请求
API_KEY = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
#https://api.openai.com/v1/models
#https://api.openai.com/v1/models/{model}
# https://api.openai.com/v1/images/generations
#https://api.openai.com/v1/edits text-davinci-edit-001 code-davinci-edit-001
# https://api.openai.com/v1/chat/completions "gpt-3.5-turbo" 可以使用
url = "https://api.openai.com/v1/chat/completions"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {API_KEY}"
}
data = {
"messages": [{"role": "user", "content": "写一个排序算法"}],
"max_tokens": 1000,
"model": "gpt-3.5-turbo"
}
# 发送API请求,通过代理服务器发送
response = requests.post(url, headers=headers, json=data, proxies=proxies)
# 解析API响应
if response.status_code == 200:
result = response.json()
text = result['choices']
print(text)
else:
print("Error:", response.status_code, response.text)