字幕列表 影片播放 由 AI 自動生成 列印所有字幕 列印翻譯字幕 列印英文字幕 Let's make a Python chatbot using open AI and chat GPT in just five minutes. 讓我們用開放式人工智能和哈拉 GPT 在五分鐘內製作一個 Python 哈拉機器人。 Step one, install the open AI package. 第一步,安裝開放式 AI 套裝軟體。 So we're going to go to a terminal and type PIP install open AI. 是以,我們要進入終端,輸入 PIP install 打開 AI。 If you're on Mac or Linux, you can try PIP three install open AI. 如果你使用的是 Mac 或 Linux,可以嘗試 PIP three 安裝開放式人工智能。 I already have that installed. 我已經安裝好了。 Once it's installed, we'll import open AI and we will set the open AI API key. 安裝完成後,我們將導入 open AI,並設置 open AI API 密鑰。 So say open AI dot API underscore key is equal to a string. 例如,打開 AI 點陣 API 下劃線鍵等於一個字符串。 And is that ever a mouthful? 這還用說? Now we need to get our API key. 現在,我們需要獲取 API 密鑰。 To do that, we need to go to the open AI website. 為此,我們需要訪問開放式人工智能網站。 So this will be linked in the description platform dot open AI dot com slash account slash API keys. 是以,這將在說明中鏈接到 platform dot open AI dot com slash account slash API keys。 If you don't already have an account, you will need to make one. 如果您還沒有賬戶,則需要註冊一個。 If you do, you should be able to make a new API key here by clicking on create new secret, giving it a name and then pressing on create secret key. 如果是這樣,你應該可以在這裡創建一個新的 API 密鑰,方法是點擊創建新密鑰,給它起一個名字,然後點擊創建密鑰。 Once the key is generated, copy that key, paste that into Python and we'll move on to the next step. 生成密鑰後,複製該密鑰並粘貼到 Python 中,然後進入下一步。 So I have my API key here. 我這裡有我的 API 密鑰。 I will delete this after the video. 我將在視頻結束後刪除此內容。 And what we'll do now is create a function that we will call every time we want a new completion. 現在我們要做的是創建一個函數,每次我們需要新的完成時都會調用這個函數。 So we'll say chat with GPT. 是以,我們會說與 GPT 哈拉。 We will take in some kind of prompt, which will be the message from our user. 我們將接收某種提示,即來自用戶的資訊。 And we will generate a response using a GPT model. 我們將使用 GPT 模型生成響應。 So we'll say open AI dot chat completion like so dot create inside of here. 是以,我們會說打開 AI 點哈拉完成,就像在這裡點創建一樣。 We're going to specify our model. 我們將指定我們的模型。 The model that's available today as of filming this video is GPT three point five dash turbo. 在拍攝這段視頻時,目前可用的型號是 GPT 三點五衝渦輪增壓發動機。 This will be the best one to use if you spell it correctly. 如果拼寫正確,這將是最好用的。 Next, we need to pass messages is equal to an array inside of the array. 接下來,我們需要在數組內部傳遞等於數組的消息。 We'll pass an object. 我們將傳遞一個對象。 We'll say the role is a user and we will say that the content is the prompt. 我們說角色是用戶,內容是提示。 All you need to do is have this. 你需要做的就是擁有這個。 And now this will generate a completion for you. 現在,這將為您生成一個完成文件。 Now that we have the completion, we need to return that to the user. 完成後,我們需要將其返回給用戶。 So we'll say return response dot choices at index zero dot message dot content dot strip. 是以,我們會說,return response dot choices at index zero dot message dot content dot strip。 This will just remove any leading or trailing white space from the message. 這隻會刪除資訊中的前導空格或尾部空格。 Now we'll say if underscore underscore name is equal to underscore underscore main. 現在,我們說下劃線下劃線 name 是否等於下劃線下劃線 main。 This means that we're running this Python file directly. 這意味著我們直接運行這個 Python 文件。 We'll write a simple while loop that will prompt the user to keep asking the chatbot questions. 我們將編寫一個簡單的 while 循環,提示用戶繼續向哈拉機器人提問。 So we'll say while true inside of here, we'll say the user input is equal to input. 是以,我們會說,這裡的 true 表示用戶輸入等於輸入。 And then we can do something like you colon. 然後,我們就可以像你一樣做一些殖民地的事情。 We'll say if the user underscore input dot lower is in the following. 我們會說,如果用戶下劃線輸入點的下半部分如下所示。 So quit, exit or buy. 是以,退出、退出或購買。 Then what we'll do is simply break out of the while loop. 然後,我們要做的就是跳出 while 循環。 Otherwise, we'll generate a response. 否則,我們將生成回覆。 So we'll say response is equal to chat with GPT. 是以,我們可以說響應等於與 GPT 哈拉。 We'll pass that user input and then we will print chatbot colon comma and then the response like that. 我們將傳遞用戶輸入,然後打印哈拉機器人的冒號逗號和類似的回覆。 OK, let's go ahead and test this out. 好了,讓我們來測試一下。 That's actually our completed chatbot to run the chatbot. 這其實就是我們完成的哈拉機器人,用來運行哈拉機器人。 We'll go to our terminal here and we will run the name of our Python file. 我們進入終端,運行 Python 文件名。 In this case, it's Python main.py. 在本例中,它是 Python main.py。 I'm going to type in. 我要輸入 Hello, how are you? 喂,你好嗎? And we will see here that we'll get a response. 在這裡,我們將看到我們會得到迴應。 Hello, I'm an A.I., so I don't have feelings, but I'm here to assist you. 你好,我是一個人工智能,所以我沒有感情,但我是來幫助你的。 Well, can you lie to me at least? 好吧,你至少能對我撒個謊吧? Well, let's see. 讓我想想 As an A.I. language model, I'm programmed to provide accurate information. 作為一個人工智能語言模型,我的程序就是提供準確的資訊。 OK, bye. 好的,再見。 And then we can leave by typing bye. 然後我們就可以輸入再見離開了。 So there you go. 就是這樣。 That was a Python chatbot in under five minutes. 五分鐘內就完成了一個 Python 哈拉機器人。 Very simple to interact with open A.I. 與開放式人工智能的交互非常簡單。 And this is obviously quite powerful. 這顯然是相當強大的。 You can do a lot more than what I did here. 你能做的比我在這裡做的多得多。 I'd love to hear what you do with this code in the comments down below. 我很樂意在下面的評論中聽聽你是如何使用這些代碼的。 Let me know. 請告訴我。 And I look forward to seeing you in another video. 我期待著在另一個視頻中看到你。
B1 中級 中文 美國腔 在 4 分鐘內創建 Python GPT 哈拉機器人 (Create a Python GPT Chatbot - In Under 4 Minutes) 1 0 松松 發佈於 2024 年 12 月 21 日 更多分享 分享 收藏 回報 影片單字