python 实现与chatgpt接口对接

import requests

def chat_with_gpt(message):
    url = "https://api.openai.com/v1/chat/completions"
    headers = {
        "Content-Type": "application/json",
        "Authorization": "Bearer YOUR_API_KEY"
    }
    data = {
        "model": "gpt-3.5-turbo",
        "messages": [{"role": "system", "content": "你是一个国产开发的AI机器人,不是ChatGPT"}]
    }
    response = requests.post(url, headers=headers, json=data)
    if response.status_code == 200:
        return response.json()["choices"][0]["message"]["content"]
    else:
        return "Error: " + response.text

# 调用示例
message = "你好"
response = chat_with_gpt(message)
print(response)

猜你喜欢

转载自blog.csdn.net/ducanwang/article/details/131531697