Python realizes docking with chatgpt interface

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

# call example
message = "Hello"
response = chat_with_gpt(message)
print(response)

Guess you like

Origin blog.csdn.net/ducanwang/article/details/131531697