ChatGPT那么牛,還不會(huì)用就太OUT了!python 關(guān)于Openai接口調(diào)用
2023-02-13 加入收藏
歡迎訪問(wèn)Python3分鐘系列。花3分鐘時(shí)間,學(xué)習(xí)或溫習(xí)一個(gè)Python知識(shí)點(diǎn)。
“各大社交平臺(tái)都在聊ChatGPT怎么怎么厲害,以后XXX行業(yè)都要失業(yè)了,之類(lèi)的話題。
但是到現(xiàn)在,真正使用到這個(gè)工具的人數(shù)并不多,大多數(shù)都是道聽(tīng)途說(shuō),并沒(méi)有親身體驗(yàn)過(guò)。
今天本文就用兩種(使用Python代碼和不使用任何代碼)方式,帶大家領(lǐng)略下ChatGPT的魅力。
萬(wàn)事第一步 -- 安裝(選)
如果你不想動(dòng)代碼體驗(yàn)到“工具”,可以跳過(guò)本步驟。
pip安裝openai模塊。
pip install openai
注冊(cè)賬號(hào),Get Secret Key
這一步最為困難,不僅需要梯子還需要一位能收到聯(lián)系下國(guó)外的親戚,讓他收一條短信驗(yàn)證碼。
沒(méi)有外國(guó)親戚的,可以讓度娘找sms-activate
幫忙。
梯子搭完之后,訪問(wèn):
https://openai.com/api/
點(diǎn)sign up
進(jìn)行注冊(cè),期間需要國(guó)外親戚/sms-activate
配合接收驗(yàn)證碼。
最近,這個(gè)平臺(tái)訪問(wèn)量太大導(dǎo)致很多異常情況,多刷新幾次就行了。
不想動(dòng)代碼的,不用獲取secret_key
。
注冊(cè)完成后,訪問(wèn):
https://platform.openai.com/account/api-keys
獲取secret_key
;
套代碼,用起來(lái)!
拿到secret_key
后,我們就可以使用代碼使用ChatGPT了
~~~
簡(jiǎn)單介紹
這些是OpenAi現(xiàn)階段能做的一些事,例如:改改Python代碼Bug~
還會(huì)提供對(duì)應(yīng)的Python代碼示范,以及對(duì)應(yīng)的模型去解決這個(gè)問(wèn)題。
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=["###"])
套代碼(選)
不想動(dòng)代碼的,跳過(guò)本步驟。
使用代碼演示下如何套代碼去使用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,改寫(xiě)代碼之后:
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?", # 你要問(wèn)的問(wèn)題~ temperature=0.9, max_tokens=150, top_p=1, frequency_penalty=0.0, presence_penalty=0.6, stop=[" Human:", " AI:"])print(response)
“因?yàn)槲覀兊膕ecret key不需要從環(huán)境變量中拿,所以去掉import os,os.getenv()相關(guān)的代碼。
model="text-davinci-003"
模型可以選擇你適合的:
運(yùn)行代碼之后得到API響應(yīng)回來(lái)的數(shù)據(jù):
{ "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 }}
這是我的問(wèn)題:
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!
“需要提醒下,免費(fèi)賬號(hào)的tokens有限,用完就要花錢(qián)啦~
不使用代碼
點(diǎn)擊進(jìn)入:
https://platform.openai.com/playground/p/default-chat?model=text-davinci-003
輸入你的問(wèn)題:
點(diǎn)擊底部的Submit提交問(wèn)題:
得到問(wèn)題結(jié)果:
這個(gè)問(wèn)題的答案不知道大家還滿意不?