Build your ChatGPT Clone in Python with OpenAI API and Gradio - End-to-End Tutorial

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's up coders welcome to one little coder in this Python tutorial we're going to see how to build your own chat GPT clone using openai API and radio it may not be as same as chat GPT but the model that we are going to use is exactly what open AI has used as a base model for chargpt so even though it is not a perfect rgpt clone this is the closest that it can get to you today as of now while we are talking to make this video so the way this video is going to work is you need open AI API key which is if you sign up for free you will get some credits but after that you have to pay second you are going to use gradio which is to create a full stack machine learning application to create this interface first I'm going to show you how this looks then I'm going to take you to open AI to see how this entire setup works then we move into the code where we actually build this entire chatbot and then finally we will see couple of examples here without running out my credits let's get started first as you can see we have got a very simple chat interface and it is running on my local machine and it says build your own charging PT with open Ai and graduate a simple title and as you can see that I have sent the message to chatgpt or I would simply call it the GPT chat bot write a tweet on Elon Musk on Twitter and then it created this tweet and after it created the tweet I said make it a LinkedIn post and then it created a LinkedIn post this is to quickly demonstrate that it also handles history one of the reason why a lot of people like open zip chat GPD open GPD open a is chat GPT is because it can hold context so here we are going to hold that context and I'll also show you how we do it in the code the first thing that you need is you need an account on chat GPT or open Ai and once you have an account on open AI then you need to get your API key so go to once you log into open ai go here and then click view API keys and then create a new API key when you create a new APA key make sure that you copy it while the moment you create it because after you create it you can never see that key again you can delete it like for example the key that I'm using in this video I will delete it after I show it to you but I cannot see the key so once you click create new secret key and copy create it then immediately copy it that's the first requirement second I would like to show you how this entire thing works so open AI has an example for a chat button itself for example it says that you can use gpt3 to build a conversation so the way you build a conversation is first you give uh you create a prompt in such a way that you tell gpt3 what is it for example in this case we are going to say the following is a conversation with an AI assistant which means we are first giving an identity to the gpt3 model you're saying that you are an AI assistant the assistant has certain characteristics helpful creative clever very friendly and this is a place you can optimize how your chatbot you want it to be like for example you wanted to be witty you wanted to be clever you wanted to be um let's say angry so that these characteristics are defined here and then you give a sample example you say that in the human sensor message saying hello who are you and then the a response it by saying I am an a created by open a how can I help you so this is basically the start of the pro formed it's more like a prologue and then the human's message goes on then eia response human goes on and the intention here is that first we tell the APA the intent what this what is it supposed to do so this is supposed to be a chatbot which is helpful creative clever and very friendly and then we also give an identity that's what we have done so basically first this saying that it is a chat bot where we are going to have conversation second giving an identity of what kind of chatbot it is and this works extremely well so once you reach this place you can click open and playground and that will ideally take you to the example where open AI has put together this example so you can click this drop down and select chat and once you select chat you are going to be greeted with this interface and from this interface you can see that it is using the very latest text DaVinci 003 model which a lot of people have started calling it as GPT 3.5 open AI did not make any announcement that this is GPT 3.5 it's merely an improvement on the existing instruct GPT but a lot of people have started calling it open AI 3.5 so we'll also go ahead with that so what is text dominic03 the most capable model in the gpt3 series can perform any task that other gpt3 models can offer with higher quality longer output better instruction following which is an instruction because it's an instructor GPT model it can process up to 4000 tokens per request which is amazing so we're going to use this default setting so the way we are going to do this is first copy the code when you go click here view the code you can copy this entire code so I'm going to code it in Python because I'm going to use radio so you I'm going to copy python code but let us say that you want to do it in node.js you can copy the node.js code So based on the language in which you want to code your chat GPT clone you can copy the code but for our tutorial for our demo I am going to use radio which is a python Library so I'm going to copy the python code so I'm going to copy this entire code and I'm going to open my vs code Visual Studio code which is my local IDE integrated development environment to develop this application just to quickly summarize the first thing that we did is we get we got our open AI APA key if you have not got your open AAP key at this screen as well you can click here and then that will take you to the place where you can get the open AI API key make sure when you create a new secret key you copy it otherwise you cannot get it if you do not want or if you have shared this key with somebody and you want it to be erased you can simply click delete and revoke the key so after you have got the key we came to the playground or we went to the documentation understood how this works and then we came to this open AI playground where we copied the entire code snippet so I've copied this now I'm going to go to my visual studio code where I've got this application running so first thing that I'm going to do is I'm going to create a new file just to show you how it works um or or probably I can show you my current environment so in my current environment I've got two files one is my requirements.txt which requires two libraries open Ai and radio again you don't have to code all of these things from scratch I'm just explaining you this code repository will be given in the YouTube description um as a GitHub repo so when you get this code all you have to do is replace this key with your own open a APA key like if you are doing this you can replace this with your own API key or alternatively if you have already got your open API key open a APA key in your environment variable then you can comment this line and then you can uncomment this line and then start using it so you don't have to code from scratch I'm just going to explain you the code if you just want to use this project all you have to do is close this enable this if you have already got it as an environment variable environment key like if you have open a AI API key enable the below line if you have got open AI key as a string enable the below line so basically if it is as an environment as an environmental variable enable the below line and otherwise enable this below so basically you have to decide which one you want do you have an environment variable open a open AI APK enable this or if you want if you just copied it from there and then you are going to use it as string then enable this and paste your key here that is the only change that you have to do for you to run this code nothing else the other thing that you need to do is install these two libraries you need to install open AI Library second you need to install radio Library very simple like any python application pip install open AI then separately pip install gradu so it's it's as simple as that pip install open Ai and then pip install gradu that's it at this point after this you should be very well up and running let me take you through the code and finally I will show you how to run this we just copied the code and I've pasted this code here so I'm going to show you a new file where we paste this entire code so now the primary difference between our existing code and this code is here the gradu application code as well presents other than that everything else is same so the first thing is import the required libraries import OS import open AI import gradu as gr then the next is the place where we Define the open AI API key next we have a start sequence and restart sequence which says AI human then we have a prompt this prompt is something that we copied from there where we set what the open AI what this entire thing is about which is conversation second we are giving identity to the assistant units or the the chatbot the next thing is is the place where we create this response and that goes inside a function called open AI underscore create and the function takes one argument which is a prompt so replacing this prompt text I'm going to add this to prompt and as you can see I have already called it a prompt so now this entire thing has gone inside a function and then finally we are going to take the responses and then we are going to return the text this is one change where we were directly getting this open a API completion or creation whatever the text creation here that goes inside a function the next thing is we are going to create a new function just this is not required let me delete this then we are going to create a new function called chat GPT underscore clone this function has got two arguments one is called input second is called history this is required for gradio so for a gradu application to work anything that we need to do in gradu has to be wrapped inside a function and that function should have an input that function should have an output one or more outputs one or more inputs in this case we have an input which is a text the second is something called history which stores the state of the current gradu application so when we go to our gradu application as you can see this entire thing will be stored as a state and that is what we are going to store in history so this history is going to store the entire thing and that is how we are able to build the context we keep the knowledge of the context of the memory of what is happening in the past as well so input and history so if history does not exist then it is an empty list then we're going to take history converted to a list and then append our current input which is which is basically what we are going to send a current message along with the history like for example this would have been in the history so when I make this as my new message this gets appended to this entire thing and this entire thing goes to open AI as a prompt and that's what you see here so we have history if it is empty it is empty but if you have history we have history then we append the input to that and then that input goes inside like whatever we created this entire thing the history plus input goes inside open AI create the function that we created as the input so that goes as a prompt and based on that open air generates a response and that response now gets added as output Now history gets added one input one output and then we written history history and that gets displayed here how does it get displayed we're going to use blocks which is an advanced method to create Radio application so there are two ways one is interface other is block for the reason why I use blockers for us to get a very chat GPT style interface like the top to bottom um it's easy to do it in blocks and interface it will have a two column view that's why I used blocks if you're familiar with interface you can still go ahead with interface in Block first I've got the title so with block first first thing we need to Define is gr dot blocks as you can see I've imported gradio as gr the next thing is gr dot blocks and uh with block the title is there then first thing we need is we need a chatbot gr dot chatbot next thing we need is we need an input text box where we can type input that is the text box and we have placed we have kept the placeholder text like this one that we defined at the start here the prompt there just for somebody to understand then we need to store the state and then finally we need a button where we have to click Send and then we are going to say whenever this submit button is clicked I'm going to call this function with this input and this output and what is there in the input the input contains the actual input message which is the message from the text box and also the current state that's what we discussed before and the output contains the output back from the chatbot clone function and also the state that's why we are returning a double here so which one has the output answer phone as the state then that's it we're going to click block dot launch and then this is going to launch our application once it launches our application it is going to let me open the terminal I don't know if this application is still running okay it's still running so the way we can run it is we can say pipes on three space app dot Pi if you have got python python otherwise Python 3 add dot Pi make sure that you are inside the current working directory like in my case I am already inside the current directory that's why I'm able to run python3 space app.pi file otherwise you might get an error saying I couldn't find app.pi file just go inside this folder which is our project folder which I'm calling it chat GPT clone and then click python3 app dot Pi once you run this it is going to run it on a local URL again this is a good thing you are not going to deploy it anywhere so copy this come back here paste it and then run it and your you get this interface build your own charge APD with open AI API and radio whatever title you want to change your charge GP to go and change it and we have got the placeholder text so I'm going to write a I'm going to write a simple message okay right write a poem on Elon Musk firing Twitter employees send it once you send it you can see that this thing is happening now getting going to chat GPT sorry going to open AI um API and that it's making a call and then it is getting it back and then it sent a message so we got a poem and uh can you make a tweet announcement make an announcement tweet of the same so the reason why I said a tweet of the same is for you to understand that it preserves that context so if it is related to Elon Musk then you know that it preserves the context okay Twitter employees unfortunately we must inform you the today succession of changes has resulted in removal of some offer okay this is a very polite version I don't think Elon Musk ever tweeted like this but this is good and um I can I can ask programming questions as well like can you identify the bug in the following code I can say import pandas as PD PD dot read um read underscore msv input dot CSV and I can send it so let's see if it identifies okay it says it should be PD dot read underscore CSV fine it works fine I don't want to run out of my open AI credits but this is exactly how you can build your own chat CPT just to quickly summarize if you want to use this code you don't have to code everything from scratch this repository this code repository will be linked in the YouTube description as a GitHub repo clone the repo or download the repo into your machine copy the open AI API key Either add it to the APA like your environment and then use this or use it as a string here and then you have to all you have to do is python space app dot pi and you will have your own chat GPT built up and running very good the closest alternative that you can get in today's environment for chart GPT is this and um and yeah so we have successfully managed to build it in less than 20 minutes thanks to open AI API and grade you if you've got any questions let me know in the comment section otherwise I hope you enjoyed this new video see you in the next video Happy prompting
Info
Channel: 1littlecoder
Views: 50,113
Rating: undefined out of 5
Keywords:
Id: n5nn3mQxrE8
Channel Id: undefined
Length: 17min 46sec (1066 seconds)
Published: Wed Dec 14 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.