当前位置:   article > 正文

调用chatgpt 3.5接口提问_调用chatgpt免费接口

调用chatgpt免费接口

###需要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)
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/爱喝兽奶帝天荒/article/detail/1007703
推荐阅读
相关标签
  

闽ICP备14008679号