ChatGPT那么牛,還不會用就太OUT了!python 關(guān)于Openai接口調(diào)用
2023-02-13 加入收藏
歡迎訪問Python3分鐘系列。花3分鐘時間,學習或溫習一個Python知識點。
“各大社交平臺都在聊ChatGPT怎么怎么厲害,以后XXX行業(yè)都要失業(yè)了,之類的話題。
但是到現(xiàn)在,真正使用到這個工具的人數(shù)并不多,大多數(shù)都是道聽途說,并沒有親身體驗過。
今天本文就用兩種(使用Python代碼和不使用任何代碼)方式,帶大家領(lǐng)略下ChatGPT的魅力。
萬事第一步 -- 安裝(選)
如果你不想動代碼體驗到“工具”,可以跳過本步驟。
pip安裝openai模塊。
pip install openai
注冊賬號,Get Secret Key
這一步最為困難,不僅需要梯子還需要一位能收到聯(lián)系下國外的親戚,讓他收一條短信驗證碼。
沒有外國親戚的,可以讓度娘找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現(xiàn)階段能做的一些事,例如:改改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不需要從環(huán)境變量中拿,所以去掉import os,os.getenv()相關(guān)的代碼。
model="text-davinci-003"
模型可以選擇你適合的:
運行代碼之后得到API響應回來的數(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 }}
這是我的問題:
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提交問題:
得到問題結(jié)果:
這個問題的答案不知道大家還滿意不?