python请求chatgpt

bash 复制代码
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 的版本

bash 复制代码
pip install urllib3==1.25.11
相关推荐
用户4356782936376 小时前
五线并行认知架构:从算力内卷到思考秩序的底层革命
人工智能
一 铭7 小时前
WrenAI 详解:给 AI Agent 的开源 GenBI 引擎
人工智能·开源
say_fall7 小时前
【AI大模型】MCP 模型上下文协议:客户端-服务器架构、JSON-RPC 消息与能力协商、工具发现与调用链路、与函数调用的分工
服务器·人工智能·架构
一条大祥脚7 小时前
CuTe 与 CuTe DSL:从 C++ 模板元编程到 Python DSL 的 GPU 编程演进
开发语言·c++·python·cutlass·cute·cute dsl
早起CaiCai7 小时前
【2023-01 OM】利用深度学习算法融合异质观测实现海洋场降尺度
人工智能·深度学习·算法
周杰伦的稻香7 小时前
AI 助手 2.0:多轮对话、卡片化与文章页感知
人工智能
Martina_03217 小时前
AI生成3D场景从白模换成精模后镜头全偏了?用6步检查单位、枢轴与层级替换
图像处理·人工智能·3d·ai·自然语言处理·材质·1024程序员节
10年前端老司机7 小时前
Milvus 8种检索能力实践
数据库·python·agent
狒狒AI8 小时前
手绘白板动画的生产线拆解:AI 出图、笔迹对齐、镜头渲染七道工序(附 21 个开源脚本)
人工智能·aigc
用户2763965765108 小时前
上手 GitHub 官方 Spec Kit:AI 编码别再裸奔,先写 spec 再出码,SDD规约开发
人工智能