ChatGPT in a React Application – OpenAI API and React Tutorial

#ai #frontenddevelopment #chatgpt

Hey Guys, let’s build an application like ChatGPT, which can answer our questions, convert the text into different languages, or even convert JavaScript code to Python.

We cannot integrate ChatGPT directly into JavaScript because we have just a preview version. So, I thought it would be great to experiment a little bit and find out the alternatives.

And if you enjoyed this piece, please show some love with a like, comment, or click on the subscribe button. Every bit matters 🙂


Feel free to ask if you have any questions.

My Portfolio:

Foreign Article So in this video We will create one react AI app So I got the idea from this open AI API And from chat GPT So we cannot use child GPT currently in Some JavaScript or react app but we can Use all of these items which is inside The examples So you can use all of the items To create one fully functional aif in JavaScript or in react or angular as Well as you want So I will I have only used this for Example like q a grammar correction this This that that so let me give you how This works So first you have to choose this or any Option like if you choose q a This will give you answer based on your Knowledge on this knowledge from the AI So let’s type something like who is I mean who Is Narendra Modi question mark Do your stuff if you click this this Will give you the answer Like this mother is the current prime Minister of India and all of the other Things Now the next item should be this grammar Collection But in this example we have we have to

Specify what we need to do So let me just open grammar correction Tab here so it goes like this we need to Type this correct this to Standard English followed by the wrong option so Let’s say we type something wrong he Gone to the market To Buy stuff He Didn’t need Let’s try something random should not Need Now if you click this door stuff or do Your stuff Let’s see what we get you will get the Correct answer for this correction like He has gone to the market to buy things He does not need So we have always like movie to Emoji Explain code and JavaScript to Python And you can also add the other items as Well delicious topic so you can create Your own App in react to do all of these items Like a chatbot type thing Which will maybe replace charge Deputy One day Maybe it will not but fine so it’s a Great project for that portfolio So go ahead and build it and like the Video as well to make this reach out to Other people So in this setup I have a weight react

App with nothing so it’s all empty but We have a commented console log here so If you uncomment this we will see the Opening API API key here so let me show You Let me bring it down here okay now see This is the API that we have in the Console So let me just comment it out Fine so To get this key you have to create a Ana File here and you have to add this String wait open AI key and this API key From openai AI sorry open AI API to get The API key simply go to open Slash API If you don’t have any account Here sign Up or else just log in So let me log in and show you the API Key the source so you have to enter the Email and this check so we need all the Cars Verify Continue Then let me enter the password And continue So We will see this page Now to get the key just go to this Uh this this this personal and view API Keys Here you have these Keys you can create A key and get started to click the or to See the usage you can simply go to

Manage account here you will see the Usage so I have used 0.67 dollars of my Account from my account The total amount I have is set in Knowledge which is free fine and it Expires on March 1 Of the next year So that Out of the way now let me go to the home Page So we need to create a AI app in react Similar to what we have in charge of it So let me open chat GPT so we cannot use This in our react app for now or in Javascript app because We don’t have the capability to Integrate it it’s just Something Exemplating or we can say It is Uh what is it what is the word It is Per Preview usage yes So if we click the examples we will see All of these categories like q a grammar Collection so we can use all of these Into our react app so let me show how to Do that so first let me create a Component here or first a folder called Components In this folder let’s create two files First will be Uh

Option Selection Dot jsx And second component will be let’s say Let’s say what can we call it So let’s say Um what can we call the component Let’s say translation Because here we will translate the Component so jsx like this and we have Two components here Let me close this so Now what we have to do in this offset Selection we have to create all of these Options like this we see so make a Functional component here And simply call or Render the component inside App.jsx into our main component like This Fine now if you see the app we will see This Text here So let me remove this Uh just let me remove this rib and make A fragment from react Inside let’s make an S one tag for the Heading which says React AI app In bold letters if you want to get some Fonts just head over to and get some fonts from Here

Just paste this inside your CSS so let Me just Remove all Let me select pop-ins because it’s my Favorite font So we can select regular 400 italic Simple font medium 500 and that’s all so Just copy all this using clicking this Icon here this icon silicate families And just Just do what just copy This import and paste this inside The app.css At the top So we can remove all of these because We don’t need all of these Fine sorry move And To get the Text from H1 or in this font we can add A class name and say Heading Inside this we can say heading And font Ly Poppins Okay and this will be now in Poppins Font just like that Fine Now we need to create all these menus Menu items like small tabs so it’s Better to create one Array some mock data so we can say Let’s create a folder called

Uh Grid Or let’s say AI Options In this we can say index Dot j s x Sorry not jsx let’s say Json Or what should we call it let’s say jsx Fine Javascript file and then say const Array items which will contain all of These items in an array object so we can Say name This will be Q And a so I will not use all of these Items I will just use a few of them like Q a grammar collection text to command And all so we can say name then let’s Say id id will be Let’s say Q Question and a like this fine Let’s say some Description Description in here we will see answer Questions Based on existing Exist in Knowledge Fine so just like that we can create a Lot of these as much you can want or you Want so I’ve added a bunch of these into The react app from this list you can add Mode if you want but I have already or I

Have just added one two three four Five six seven seven pieces So let’s get the other items into our App.jsx but first we have to export this Array items Now we can simply do import Array items from AI options from That file Okay now we can simply console.log to Check what we are getting in the console So you will see we are getting seven Items in the array now we can Traverse Through each item So Let’s just Add the array items inside Or just pass it as proc into option Selection Because here we will select the option Fine now let me just map all these items So we can say we need to create a grid So we can say div first inside let’s map It so error items dot map Let’s item So we can say return An empty fragment In this we can simply have an H3 which Will be the title of that particular Item so we can see item Dot Let’s say what is the key name that I Have added The key name is called name So item dot name And second will be will be P tag

Item dot description We will get all these items According to the array now all we have To do is to Make them in a grid so we can say div Class name Let’s say grid Main So we can just copy this And add it here grid main display of Grid Then we can say grid template columns Auto and auto to make them two in a row But something is wrong here Okay let me open that index.css and I Have to remove something from here I Think So let me remove all of these except This one But why it is failing so I guess we have To add a div here Yes now it’s working If we removed if it will not be proper But if I add a div it will be properly Rendered So we are hitting this fine Now in this div give this a diva class Name of let’s say grid Child because it’s a child like every Item is the child here for the grid So we can say grid child let’s say let’s Say the Border Or what we have here is nothing So

What can we do let’s make this an H4 To make it a little bit smaller Fine so we can say great child in this Let’s say grit child H4 Let’s add Um Font family Poppins And font weight 600. Fine What if we had 700 We need to make them bold but it’s fine Now in great child P tag We need to add pop-ins but we need to Decrease the font size so we can say Font size let’s say 15 pixels Um Let’s remove this and make 500 It’s not getting refreshed but why let’s Add 12 Yes now it’s working So basically let’s make this an H3 and Revert this back to H3 to make the text Bigger Fine I think that’s fine looking fine now Let’s add a border So great child Border One pixels solid White smoke color Like this We can also add a gap between all the

Items so say Gap 10 pixels Or 15 will be fine as well Now we need to make these What is it we need to make the Border Curved around it so we can say Border radius 15 pixels as well Now they are curved curved what if you Make these two pixels yes now it looks Fine So also we can add some padding to Sync the items Like this now it looks much better We have react AI app title And this also let’s add margin top 20 pixels To shift them From the top At 20 but Margin top 20 Let’s add 30 40 now it’s fine yes It’s working Fine Now what we need to do is or let’s do Something like this so if you hover the Titles or this card we should see a Highlight option so we can say first Cursor pointer now if you hover them we Will see this pointer cuts are here now In the great child we can say dot Greek Child over Over In this we can say background color As white smoke and color as 2 1 2 1 2 1.

Now if you save and if you now hover the Items we’ll see this kind of thing Which is I think great Fine But something Should be checked here App.css we are having all of these items Max width margin text align Center app Index.css okay this is the problem here We have to remove font size from here Okay nothing happened okay leave it fine So we are saying this Ugly looking items Now what needs to happen is In this example so let’s say you open The first one called q a So we have a Response this we have this like we have This response which has model prompt Temperature and all So We have to make them default for each Item So to do that we need to create a Function Or we can do this as well so in this App in this array of objects we can add The model using an object so we can say Option Let’s add option and this will be in Object So for this q a we have to copy all of These and paste it here

And Save But the problem is it will not work like That so I guess we have to do it from Here You have to send it from here directly So we can say Let me remove this console log and then We can say con or const Select Option Fine we have our error function which Will take option value I mean option Param we can pass Let me okay I think that’s fine we have To pass this function as props to this Uh component option selection So let’s get it here Fine Now if you click this child we should Trigger this So we can on click The select option But I guess I have to add the items as Options here only then it will work so Let’s add this just remove the prompt Because this will be dynamic Except the rest So let me add this for now So what will happen is if we click this Select option function using one click We should send this item Dot This key option So let’s do that

Item dot option Now in here in the app.jsx we can simply Console DOT log this option Like this now let’s see what happens if You click Q and A at the top We are getting all of these but we still Need one text area or input field for These items So let me show you what I mean So click this click open in playground We need something like this a text area Okay like this we need Fine so what can we do so in this other Component Translation.jsx let’s create it so RFC Let’s save this And Let’s say import Translation from this component and just Enter it out here Translation And we will see it here if you Close the console But we need to see The translation only when we have Selected the option So let’s create one state here so let’s Say const let’s say option and set Option Equals to use state Use state And it will be an object So we can set it here Like this when we click or trigger the

Function so if we console Check in the console this option Initially we should get nothing I think So let’s see See we are getting this object and it’s Empty So to check if it is empty we have to Convert this into an array so we can say Wrap this inside one parenthesis object Dot values And it will convert it in an array of Values from this and we can say length If you refresh we will see length is 0 It means the error is empty or we have Nothing inside this object If you clear it and look again it will Be 7 because we will have seven items Inside the array to check we can simply Do this see we will have seven items So we need to show this translation Component only when We have something inside this object so We can say Wrap this inside a curly bracket to make It conditional So we can say object dot values dot Option I mean object.values option Length If it is 0 we need to show this Component Else we need to show translation Wait If it is 0 okay So refresh if you click q a

You will see this component now Translation so inside this let’s add a Text area Text area From HTML we have this so We can style this so the class name Text area We can also specify the row and columns So we can say columns let’s say four If too small let’s say 40 and now that’s Fine we can increase it make it 80 Fine this looks fine and let’s also add Some rows to increase The height so let’s say 40 as well my Effort is too much let’s add 30 Okay 20 20 is fine we’re gonna type it Inside it So in the text area We have to Change the font size so to make the font Bigger so we can say text area Font size let’s say 20 pixels Now we have this and it looks fine So if you are making this font size 20 So we have to decrease the column so Let’s say 60. Or 30 30 small let’s say 40. 40 what about 50 50 is fine but what About 60. It stays too much it is going out of the Screen 50 is fine Also make this as 10. now this looks Fine correct

Um Fine also we can add a little bit Padding here so we can say padding 10 pixels Now we have this cool looking text idea Let’s add the font size for different Memory of Poppins To get the font in Poppins You can also make this 55. Yes now it works fine fine So this is done Now we need a button to translate So let’s add it Like to trigger the function so we can Say button And say Do your Tough Now save Let’s style the button to the class name Let’s say Action button Action BTM In the CSS Style this we can say width 200 pixels Height 50 pixels And font size 20 pixels Fine let’s add margin top 20 pixels and that’s all also cursor as Pointer Now we have this and this Now The options are here

Now if you click or refresh the page You’ll see this page Click this we will see this if you Refresh or let me do one more thing as Well So in this translation let’s also add One more button to reset all of this so We can say button it is set It is set Reset reset Add the class name Here But I guess we don’t need that for now So let’s leave it remove the button We’ll just refresh the page Fine So if you do your stuff This should trigger a function correct So let’s say console Do Tough And in this function Let’s do something later so just first Pass it inside this translation Component as props receive it here And make on change sorry on click this Function Also Create one more state For the input so we can say const Input And set input So use state and string Pass this as well here

In Translation component Receive it Or desstructure it Then on this input of text area we can See on change We can say It will take a param called e or event And we can say or we can just store dot value inside this input Now if type something And check this state call input let’s See what we are getting Open the console And type something and we’re getting it Here so let’s say we type hello we will Get hello here Like this Now We are passing Let me open this we are passing all of The options here In this Function called option So this is the object so we can append This option like all of these options With this text area input To make the full set of this To make a full set of this using prompt So We can simply just structure this So we can take we can see options comma Let’s say prompt and prompt will be This input

File so if you’re not concerned along The option let’s see what we are getting So open the console Like this options are here But the prompt is empty see if you type Something like nishanth here You’ll see From the still empty okay wait So we need to set this in options Okay let’s do this let’s console.log This inside this two stuff Now if you click those stuff you will See this and prompt is still empty [Music] Um Yeah so I made a mistake so what we need To do is we need to set the options from This array object when we click the card Now if you console.log this let’s see What we are getting So we are consoling the options so click This and we’ll get all these but we Don’t have prompt here so when we need To we need to use the prompt or set it In this do stuff function If we can do it like this no if you type Something in the this text box You will see this prompt here now click The button do stuff and we will see the Prompt this So let me start again So initially this will be empty object See If you click

This card q a We will get all of the items but not the Prompt Now click Nishant or admission here You’ll see nothing But if you click the do stuff button we Will see the prompt here as Nisha So we are now having the full set Of the items now all we need to do is Translate this here So let me Copy all of these items So copy this configuration and open I Okay I need to install this as well Because I didn’t install it So we can say go to this Tutorial How to install this Introduction [Music] Quick start tutorial No Let’s say Examples playground API reference Overview Let me figure out how to install this Because I’m forgotten so to install this Simply open Terminal And add this so npm I Open AI Enter and it will install the open air Into our project that’s all check the Package I sent to see if it is have been

Installed or not see fine version 3.1.0 So close the terminal now we need to go Back to this quick start tutorial Okay not this try examples yes try q a And now we need all of these items from Open AI so We can import this So we can say let’s import them at the Top so we can say import something From open AI And Inside we can add configuration And open AI API Then we need to create the instance for This configuration just like it says Here So let’s create it here And this will take the API key so we can Simply cut the Apex from here and paste It The next step is to create instance of This open AI using the API key and this Configuration so add it here That’s done and now we need to do is This so in this function We need to do all of the items so we Don’t need to set options here we can Simply create one object so let’s select Object Equals to These items so cut this and paste remove The state and if you now console this Options Or option let’s see the result

No not option for the object Now let’s check this click this nothing Will happen add some text like hello Now enter or press this button we will See these items so prompt is hello and All the items that we are getting Perfect Fine so in this function We have to add The response and we have to make the Function as async Like this for asynchronous JavaScript Now We need to pass the object here not These items So we can remove this object parenthesis And simply say object Now if you console.log this is response Let’s see what we get So just say CLG Response and I think it will work now So let’s start from the beginning Let’s copy some question let’s say this Let’s paste it and see what we will get Like this Paste this question and say do your Stuff So it will take some time so now we are Getting it Yeah so now we’re getting it if you open Data we will see the choices in the Choice it says we will have the text So let me just to respond dot data Dot choices zeroth index dot text

Now we will get the entire text only not Some other thing So try again and you will get this now Let’s create one state called result So we can say const Result and set result Equals to U state Empty string Now we have to store this response Inside this state and pass this as Props now you have to get the props in The translation.j6 and add the text After this text area This so let’s do that So I have added this result dot length If it is less than 0 sorry more than 0 We will see result else will see nothing Or empty string I have also added the Model temperature and all all the Options in other Objects as well like grammar correction Like this now all we need to do is just Try this so refresh the page let’s try Grammar correction Which is after q a Which converts the wrong English to Standard English So let’s try this Do your stuff And we will get this correct answer So let’s try something else My Name a Nissan Note is we have typed a Now click do your stuff

We will see the both texts here she did Not go to the market my name is Nishan If we add one Space or enter let’s see what we get We will get same thing it will not be Separated by spaces So I think we have to do this One space I think that’s how we get Space here No it’s backslash not forward slash Do your stuff Okay it’s not working fine doesn’t Matter so that’s how it works If you add one only it will give you one Now let’s try other ones like Uh what is this JavaScript to python code translator So open this JavaScript to python Let’s copy this code And add it here now click do your stuff Let’s see the output here So we will get the python code but Without indentation so that’s fine Because I have not added Internet Indentation here So we have an array called Docs And we’re pushing all the docks Porridge into this car array dogs into Car ready So in the python we can simply do Append.orgn this is how it works Fine So I think this is done so we can say Translation of English to other

Languages So let’s try this one at last All right where is it Let’s see Let me search for this English to other languages Okay this is here So Let’s see this Translate this into French Spanish and Japanese So this text will be translated into Three items so do your stuff Hmm We will see This one is I think French this one is Spanish and third one is Japanese so it Is working So that’s how we create uh now you can Also create one react AI app this that Does all of these things dynamically It can explain code or convert the code To python from JavaScript or summarize For second grader and give it some Answers So you can go ahead and create this and I think that’s all like the video and Subscribe to my channel Thank you so much for watching the video

You May Also Like

Leave a Reply

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