Python调用ChatGPT

import requests
import json


def send_prompt(prompt=''):
API_KEY = '密钥'

if not prompt:
return

headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + API_KEY
}

data = {
'model': 'text-davinci-003',
'prompt': prompt,
"max_tokens": 150,
"temperature": 1,
"top_p": 1,
"frequency_penalty": 0.0,
"presence_penalty": 0.6,
"stop": [" Human:", " AI:"]
}

response = requests.post('https://api.openai.com/v1/completions', headers=headers, data=json.dumps(data))

response_data = response.json()
# print(response_data)
print(response_data['choices'][0]['text'].strip())


prompt = '如何学习Python?'
send_prompt(prompt)

你可能感兴趣的:(python,chatgpt)