使用Python调用ChatGPT
注册openai账号
申请ApiKey
登陆openai后,去Api Keys管理申请key
Pip安装Openai
pip install openai
编写Python程序
import os
import openai
openai.api_key = "sk-xxxxxx" ## apikeys
question="How to earn billions?"
print(f'Question : {question}')
response = openai.Completion.create(
model="text-davinci-003",
prompt=question,
temperature=1,
max_tokens=1024,
top_p=1,
n=1,
frequency_penalty=0.0,
presence_penalty=0.0
)
if 'choices' in response:
if len(response['choices']) > 0:
answer = response['choices'][0]['text']
else:
answer = 'Sorry!'
else:
answer = 'Sorry!'
print(f'Answer : {answer}')