Placeholder Image

Subtitles section Play video

  • Let's make a Python chatbot using open AI and chat GPT in just five minutes.

  • Step one, install the open AI package.

  • So we're going to go to a terminal and type PIP install open AI.

  • If you're on Mac or Linux, you can try PIP three install open AI.

  • I already have that installed.

  • Once it's installed, we'll import open AI and we will set the open AI API key.

  • So say open AI dot API underscore key is equal to a string.

  • And is that ever a mouthful?

  • Now we need to get our API key.

  • 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.

  • 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.

  • Once the key is generated, copy that key, paste that into Python and we'll move on to the next step.

  • So I have my API key here.

  • 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.

  • 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.

  • So we'll say open AI dot chat completion like so dot create inside of here.

  • 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.

  • 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.

  • 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.

  • This means that we're running this Python file directly.

  • We'll write a simple while loop that will prompt the user to keep asking the chatbot questions.

  • So we'll say while true inside of here, we'll say the user input is equal to input.

  • 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.

  • Otherwise, we'll generate a response.

  • So we'll say response is equal to chat with 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.

  • In this case, it's 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.

  • 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.

Let's make a Python chatbot using open AI and chat GPT in just five minutes.

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it