赞
踩
###需要http代理
###需要chatgpt api_key
import requests
# ChatGPT API endpoint
api_endpoint = "https://api.openai.com/v1/chat/completions"
# 您的 OpenAI API 密钥
api_key = "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
while(1):
# 用户输入
mess = input("请说:")
# 构建请求 payload
payload = {
"model": "gpt-3.5-turbo",
"messages": [{"role": "user", "content": mess}],
"temperature": 0.7
}
print(payload)
# 设置请求头,包括您的 API 密钥
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}",
}
print(headers)
#你的http 代理服务器地址
proxies={'http':'192.168.1.122:1080','https':'192.168.1.122:1080'}
# 发送 POST 请求
response = requests.post(api_endpoint, json=payload, headers=headers,proxies=proxies)
print(response.status_code)
# 处理响应
if response.status_code == 200:
result = response.json()
assistant_response = result["choices"][0]["message"]["content"]
print("ChatGPT Assistant:", assistant_response)
else:
print("Error:", response.status_code, response.text)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。