Create Your Own Chatbot Like ChatGPT - OpenAI API and Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone in this video I will show you how can you create your own chatbot which works almost like jet GPT so when I'm saying almost it means you will not get exactly same response like chat GPT but it will it would be like very close to that and here we are using open AI API along with the python so let's get started so first of all I will quickly show you how to open that open AI portal so you can Google it and just click on this open AI so either you can click here or you can open directly openai.com so once you are here you need to click on API you need to log in here so I will quickly go ahead and login okay so once you are logged in uh what you need to do is you need to go to examples and search for chat and from here why we can go to this open and playground so it will open the same chat app in the playground so once you are here you can like read about all these parameters in the documentation but what I'm trying to show you here is to grab some initial code so rather than writing everything from scratch we will just take some Snippets which are already written over here and another thing which you will notice is like when you are working with like chatbot kind of thing you need to in case of this particular model you need to Define that what is your conversation about and what this assistant is all about so that's the reason it is saying the following conversation is with an AI assistant and it is helpful creative clever and very friendly so this is how human will speak I mean type and this is the response which you may get from this chatbot so let's click on this view code and this is the initial code which is written and which is working for this particular playground so I will quickly go ahead and grab this python code from here so here you can see there are three to four different languages so you can choose whatever you want but I will go with python here so once this is done the very first thing we will do as a developer is we need to go ahead and install the required modules so the first module is open AI so once it is done we need to install gradio so I will say pip install gradu so gradu is a free and open source python Library which we can use to demonstrate any like machine learning model with a very friendly web interface so rather than spending time on constructing the interface we can quickly use it and it will run on our local machine so okay so on my machine I have already installed these two packages early so that's why it is saying requirement already satisfied let me quickly go ahead and paste the code which we just copied okay so here we need open AI import OS and then I will go ahead and quickly say import gradu as let's say Alias as gr okay now coming on to this particular key what we can do is we can go back to our portal and click on this option here so it will take you to the API key so I will go ahead and quickly generate one new key and I will copy it let's quickly paste it here so I'm not setting it in environment variable rather just take it as a string right now okay so make sure to copy this because once you said okay you won't be able to see that key again entrance so that it would be easy for us to call so I will quickly go ahead and Define a function So Def and we can say create prompt we can say or we can say open AI create and here we will pass in the prompt so prompt is nothing but the user string which your user wants to like user wants to provide when interacting okay and this would be the entire thing let me shift these a bit okay and then we need to return so I would say return response dot choices and we'll go with the zeroth index we will quickly show you what is happening over here and apart from that we can quickly pull this prompt out so I will declare this variable over here and we can get rid of this because every time this is going to change whenever user is providing input so we have to make this variable okay so till here we are good next thing we need to do is we need to like maintain the history like what user is writing how to maintain the context so whatever the previous statement was accordingly we need to come up with the new statements so for that we need to write a function and I will call it as conversation history which will take two parameters and put and history so input is the one which user is going to provide and histories what has happened in past so here I have already written some code so I will quickly grab and paste it over here so what we are doing here is initially when string is empty then we are creating an empty list then we are appending the input to it so that our complete collection will become one single item and then we are returning uh here we are appending input to that output and we are returning this Tuple so let's go ahead and see what next so till here we have maintained the context we have taken the input from the user now we need to write something for the UI so for UI like I said we are taking this gradu here and gradu uh works with the blocks so we need to create block over here I will quickly say blocks equal to gr Dot blocks and we can use with block over here to refer this and then we will set all the initial properties which are required to associate this chatbot with the graders so let's say gr Dot uh okay so let me create an object first for check bot so gr Dot chatbot and then we need to say set the message here so we can say message equal to X Box because we need to create the blocks what all we want to see in our UI so that's what we are doing over here so placeholder equal to prompt and then we need to define the state so gr dot state at the end we will have one submit button on click of which we want to trigger this process so we will say trick gr dot button and let's name it click or send whatever okay so once this is defined next thing is we need to perform the submit button click submit click and inside this we need to pass are all the functions which we have created so our first function is this conversation history then we will have inputs which is like message which we are creating about and the state and the last parameter which we need here is outputs and here I will say chat bot and State okay so we are almost done next thing we need to is to launch so block dot launch I would say debug equal to true and let's try to run this first it will take few seconds so block is not defined probably we did some mistake uh it is blocks here also I need to say blocks launch ah it has no attribute let me quickly see what mistake I did it should be capital s now you can see that it runs successfully and it is running on this particular URL so let's go on to this URL and see what is happening here so you can see that these are the initial text which we have provided in the prompt let me quickly go ahead and ask it something so let's say what are the colors in rainbow and you can see that it is start working and it is seeing the colors in the rainbow are red orange yellow all these things it has given so it means it is giving us the response so let me ask some other question let's um get me uh tagline for let's say let's take the same example Barbershop which I used to take in my earlier videos so freshen up your look at your barber shop so you can see that how nicely it is giving and we can also ask it to like make it longer so let me quickly let me quickly give it something like uh get me long tagline so let's see so let us given for extra experience delicious Cuisine so it is kind of like not 100 accurate what we are expecting but uh yeah let's do some more experiments so uh write a poem on cute little kid so so basically what I'm trying to say is it is just an idea how I'm just trying to give you an idea how you can use that particular API key to make these things work so definitely this UI is not that zizzy because we are just taking the default web interface but when you are dealing with your own application you can definitely work with other kind of uis wherein you can construct your own UI rather than going with grader so and here I will say add more details to point so you can see that initially here it has written a poem then we said add more details so it added details to the previous context so it is still adding details to the cute little kid poem so this is how uh it is taking the context and I hope you enjoyed watching this video and do not forget to like And subscribe my channel thanks for watching
Info
Channel: Shweta Lodha
Views: 6,198
Rating: undefined out of 5
Keywords: How to use OpenAI API, Getting started with OpenAI, Shweta Lodha, OpenAI With Python, OpenAI beginners tutorial, OpenAI chatgpt, Python AI, ChatGPT, How to use OpenAI and implement it, OpenAI GPT-3, OpenAI, OpenAI playground, ChatGPT in python, Using ChatGPT inside Python, How to use ChatGPT to make money, ChatGPT API for Python, How To Use ChatGPT In Python Using Session Token, ChatGPT chatbot in python, Create ChatGPT type application using OpenAI and Python
Id: HmoGhSf1_0o
Channel Id: undefined
Length: 13min 20sec (800 seconds)
Published: Wed Feb 01 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.