天天看点

python调用chatgpt的API

(357条消息) ChatGPT API调用python和脚本实现_AI架构师易筋的博客-CSDN博客

(357条消息) NLP | 打造一个‘OpenAI智能’机器人,只需要五分钟_夏天|여름이다的博客-CSDN博客

(357条消息) ChatGPT官方API可以抢先体验了_Coding的叶子的博客-CSDN博客

这个是我参考的博客

1.在你有openai账号的的前提下(没有也有很多办法注册和在某宝购买),去到这个网站去注册api-key:

,在https://beta.openai.com/account/api-keys申请你的api keys

代码:

import openai
openai.api_key = "sk-xxxxxxxxxxxxxxxxxxx"#这里是你的api-key
def askChatGPT(question):
    prompt = question
    model_engine = "text-davinci-003"

    completions = openai.Completion.create(
        engine=model_engine,
        prompt=prompt,
        max_tokens=1024,
        n=1,
        stop=None,
        temperature=0.5,
    )

    message = completions.choices[0].text
    print(message)
askChatGPT("what is love")
           
python调用chatgpt的API