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

OpenAI API allows us to build a Chatbot using the latest text-davinci-003 (which many consider as GPT3.5 model). We can use this model to build our ChatGPT clone. Even though it won’t be as same as ChatGPT, this is the closest to ChatGPT that we have today. ChatGPT used InstructGPT as base model to develop which is the same as text-davinci-003.

The API is extremely adept at carrying on conversations with humans and even with itself. With just a few lines of instruction, we’ve seen the API perform as a customer service chatbot that intelligently answers questions without ever getting flustered or a wise-cracking conversation partner that makes jokes and puns. The key is to tell the API how it should behave and then provide a few examples.

Code –

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

You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *