chat-GPT接入Python示例代码——最新

编程知识 更新时间:2023-05-02 18:07:46

最近有需求需要使用chatGPT接入API,找了很多接入代码都有问题,然后只好自己看了官方文档,
Talk is easy, show me the code,话不多说直接看代码:
哦对,肯定要已经申请了API哈,没有可以看别的教程
API申请链接:https://platform.openai/account/api-keys
进入后

然后点击

申请一个API

import os
import openai
import requests
import json
openai.organization = "输入上图中红色框的内容"
url = "https://api.openai/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就是看你的代理的端口设置

更多推荐

chat-GPT接入Python示例代码——最新

本文发布于:2023-04-27 20:45:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/6f866baf111ac80b9cbcac089fbe3b0b.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:示例   代码   最新   chat   GPT

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!

  • 107458文章数
  • 27130阅读数
  • 0评论数