python请求chatgpt

import requests

def chat_with_gpt(prompt, model="gpt-3.5-turbo", api_key=""):
    headers = {
   
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }

    data = {
   
        "model": model,
        "prompt": prompt,
        "temperature": 0.7,
        "max_tokens": 100
    }

    response = requests.post("https://api.openai.com/v1/engines/gpt-3.5-turbo/completions",
                             headers=headers, json=data, verify=False)

    if response.status_code == 200:
        return response.json()['choices'][0]['text'].strip()
    else:
        return f"Error: {response.status_code}"


# 使用示例
prompt = "你好,我是ChatGPT,请问有什么可以帮助你的?"
response = chat_with_gpt(prompt, api_key="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")
print(response)

报错如下:
在这里插入图片描述
因为 开了代理(访问chatgpt需要vpn代理):
解决办法: 指定 urllib3 的版本

pip install urllib3==1.25.11

相关推荐

  1. Python requests库请求封装

    2024-01-26 23:32:01       55 阅读
  2. ChatGPT:SpringBoot 响应请求是串行还是并行?

    2024-01-26 23:32:01       18 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-26 23:32:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-26 23:32:01       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-26 23:32:01       82 阅读
  4. Python语言-面向对象

    2024-01-26 23:32:01       91 阅读

热门阅读

  1. Python中写入csv格式文件出现乱码的解决方法

    2024-01-26 23:32:01       54 阅读
  2. Git 对文件名大小写不敏感的问题解决方案

    2024-01-26 23:32:01       59 阅读
  3. 常见的循环结构

    2024-01-26 23:32:01       52 阅读
  4. 温湿度传感器的工作原理

    2024-01-26 23:32:01       53 阅读
  5. ChatGPT AI革命-阅读心得

    2024-01-26 23:32:01       86 阅读
  6. 数字图像处理:图像内插

    2024-01-26 23:32:01       48 阅读
  7. python+matlab text(按图的相对位置显示)

    2024-01-26 23:32:01       59 阅读
  8. Element-plus修改样式

    2024-01-26 23:32:01       52 阅读
  9. leedcode串联所有单词的子串

    2024-01-26 23:32:01       56 阅读
  10. WebSocket详解及使用教程:打造高效的实时通信

    2024-01-26 23:32:01       43 阅读