最近有需求需要使用chatGPT接入API,找了很多接入代码都有问题,然后只好自己看了官方文档,
Talk is easy, show me the code,话不多说直接看代码:
哦对,肯定要已经申请了API哈,没有可以看别的教程
API申请链接:https://platform.openai.com/account/api-keys
进入后
然后点击
申请一个API
import os
import openai
import requests
import json
openai.organization = "输入上图中红色框的内容"
url = "https://api.openai.com/v1/chat/completions"
headers = {"Content-Type": "application/json","Authorization": "Bearer " + "你的API的key"
}
data = {"model": "gpt-3.5-turbo","messages": [{"role": "user", "content": "你好,给我介绍一下CSDN"}],"temperature": 0.7
}
proxies = {"http": "http://127.0.0.1:7890","https": "http://127.0.0.1:7890"
}
response = requests.post(url, headers=headers, data=json.dumps(data), proxies=proxies)
print(response.json())
补充一下:proxies就是看你的代理的端口设置