python调用chatGPT的API

前言:

最近一直在研究GPT,LLM,把其他东西都荒废了,随便更新一个,凑个字数。

1.python标准接口使用

python接入chatGPT,用flask封装成API接口,这样你就可以自己把他接入到微信,小程序,公众号或者各种地方了。

先上代码:

import openai

#openai.api_key = "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"
openai.api_key = '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'

def generate_response(user_input):
    prompt = "The following is a conversation with an AI assistant. The assistant helps with various tasks. User: " + user_input + " AI:"
    response = openai.Completion.create(
        engine="text-davinci-003",
        prompt=prompt,
        max_tokens=1024,
        n=1,
        stop=None,
        temperature=0.7,
    )
    message = response.choices[0].text.strip()
    return message



if __name__ == "__main__":
    user_input = "我很失望怎么办"
    #prompt = "The following is a conversation with an AI assistant. The assistant helps with various tasks. User: " + user_input + " AI:"
    response = generate_response(user_input)
    print( response)

里面有几个点需要注意。

1.是APIkey的获取。

首先登陆OpenAI,登陆自己的账号,在这里。

 2.其次点击

 就可以了。

3.代码我已经封装好了,直接使用。

2.python标准接口使用结果

提问:我很失望怎么办

对话结果如下 

有粉丝反映这个APIkey不好搞,我每天会在星球里分享最近可以用的,各位大佬转需。

但是仅供学习和测试用,单位小时内请求频率过高会被封。

请不要商用或者恶意使用。谢谢。

AI博学者联盟:AI博学者联盟

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
青葱年少的头像青葱年少普通用户
上一篇 2023年6月15日
下一篇 2023年6月15日

相关推荐