ChatGPT那么牛,还不会用就太OUT了!python 关于Openai接口调用
2023-02-13 加入收藏
欢迎访问Python3分钟系列。花3分钟时间,学习或温习一个Python知识点。
“各大社交平台都在聊ChatGPT怎么怎么厉害,以后XXX行业都要失业了,之类的话题。
但是到现在,真正使用到这个工具的人数并不多,大多数都是道听途说,并没有亲身体验过。
今天本文就用两种(使用Python代码和不使用任何代码)方式,带大家领略下ChatGPT的魅力。
万事第一步 -- 安装(选)
如果你不想动代码体验到“工具”,可以跳过本步骤。
pip安装openai模块。
pip install openai
注册账号,Get Secret Key
这一步最为困难,不仅需要梯子还需要一位能收到联系下国外的亲戚,让他收一条短信验证码。
没有外国亲戚的,可以让度娘找sms-activate
帮忙。
梯子搭完之后,访问:
https://openai.com/api/
点sign up
进行注册,期间需要国外亲戚/sms-activate
配合接收验证码。
最近,这个平台访问量太大导致很多异常情况,多刷新几次就行了。
不想动代码的,不用获取secret_key
。
注册完成后,访问:
https://platform.openai.com/account/api-keys
获取secret_key
;
套代码,用起来!
拿到secret_key
后,我们就可以使用代码使用ChatGPT了
~~~
简单介绍
这些是OpenAi现阶段能做的一些事,例如:改改Python代码Bug~
还会提供对应的Python代码示范,以及对应的模型去解决这个问题。
import osimport openaiopenai.api_key = os.getenv("OPENAI_API_KEY")response = openai.Completion.create( model="code-davinci-002", prompt="##### Fix bugs in the below function\n \n### Buggy Python\nimport Random\na = random.randint(1,12)\nb = random.randint(1,12)\nfor i in range(10):\n question = \"What is \"+a+\" x \"+b+\"? \"\n answer = input(question)\n if answer = a*b\n print (Well done!)\n else:\n print(\"No.\")\n \n### Fixed Python", temperature=0, max_tokens=182, top_p=1.0, frequency_penalty=0.0, presence_penalty=0.0, stop=["###"])
套代码(选)
不想动代码的,跳过本步骤。
使用代码演示下如何套代码去使用Chat Q&A
功能:
示例代码:
import osimport openaiopenai.api_key = os.getenv("OPENAI_API_KEY")response = openai.Completion.create( model="text-davinci-003", prompt="I am a highly intelligent question answering bot. If you ask me a question that is rooted in truth, I will give you the answer. If you ask me a question that is nonsense, trickery, or has no clear answer, I will respond with \"Unknown\".\n\nQ: What is human life expectancy in the United States?\nA: Human life expectancy in the United States is 78 years.\n\nQ: Who was president of the United States in 1955?\nA: Dwight D. Eisenhower was president of the United States in 1955.\n\nQ: Which party did he belong to?\nA: He belonged to the Republican Party.\n\nQ: What is the square root of banana?\nA: Unknown\n\nQ: How does a telescope work?\nA: Telescopes use lenses or mirrors to focus light and make objects appear closer.\n\nQ: Where were the 1992 Olympics held?\nA: The 1992 Olympics were held in Barcelona, Spain.\n\nQ: How many squigs are in a bonk?\nA: Unknown\n\nQ: Where is the Valley of Kings?\nA:", temperature=0, max_tokens=100, top_p=1, frequency_penalty=0.0, presence_penalty=0.0, stop=["\n"])
带着你的secret key,改写代码之后:
import openaiOPEN_AI_API_KEY = "YOUR KEY" # 改成你的secret keyopenai.api_key = OPEN_AI_API_KEYresponse = openai.Completion.create( model="text-davinci-003", prompt="Human: could you tell me how to learn Python?", # 你要问的问题~ temperature=0.9, max_tokens=150, top_p=1, frequency_penalty=0.0, presence_penalty=0.6, stop=[" Human:", " AI:"])print(response)
“因为我们的secret key不需要从环境变量中拿,所以去掉import os,os.getenv()相关的代码。
model="text-davinci-003"
模型可以选择你适合的:
运行代码之后得到API响应回来的数据:
{ "choices": [ { "finish_reason": "stop", "index": 0, "logprobs": null, "text": "\n\nBot: Sure! Learning Python is easy. There are plenty of great resources available online that can help you get started. You could start with a free tutorial, like those offered by sites like Codecademy and Coursera. You can also take an online course or find free e-books and books to learn Python. Additionally, there are plenty of online blogs and YouTube videos about learning Python. Good luck!" } ], "created": 1675941536, "id": "cmpl-6hzF2oWKruVOZEP4YOLEFJ9rtAdTo", "model": "text-davinci-003", "object": "text_completion", "usage": { "completion_tokens": 85, "prompt_tokens": 11, "total_tokens": 96 }}
这是我的问题:
could you tell me how to learn Python?
这是她给的回答:
Sure! Learning Python is easy. There are plenty of great resources available online that can help you get started. You could start with a free tutorial, like those offered by sites like Codecademy and Coursera. You can also take an online course or find free e-books and books to learn Python. Additionally, there are plenty of online blogs and YouTube videos about learning Python. Good luck!
“需要提醒下,免费账号的tokens有限,用完就要花钱啦~
不使用代码
点击进入:
https://platform.openai.com/playground/p/default-chat?model=text-davinci-003
输入你的问题:
点击底部的Submit提交问题:
得到问题结果:
这个问题的答案不知道大家还满意不?