Spring Boot + OpenAI ChatGPT API Integration | JavaTechie

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone welcome to Java techy charge GPT is one of the most popular terms nowadays and I believe you all aware about what chart GPT does for us ask anything charge GPT is here to answer you for example if I'll ask something like spring boot JWT implementation example then let's see how the charge Deputy respond to us great I got the complete code within a minute Even If You observe any text it understands you no need to organize your prompt while asking to the Chart GPT that's awesome but can I integrate this to my application or can I create my custom bird using charge GPT yes we can integrate charge GPT to any application irrespective of language like Java python.net Etc so in this tutorial I will be creating a new rest API using spring boot to interact with charge GPT API simply you can ask your question and get the result from application itself okay all right so without any further delay let's get started [Music] thank you [Music] before we move into implementation I'm assuming you have active charge GPT account if not then quickly create it don't worry you don't ask for money if you are using the trial version okay here also I am using the trial version of chat GPT account okay so let's go to the IntelliJ IDEA and let's create a project from scratch let me create a new project click on file then Define all the field then just add the required dependency so for now I will just add lombok dependency and web dependency spring web that's it okay because from our application we are going to call the APA from chart GPT I will show you how you can call the API how the structure should be looks like okay now create the project click on next so project inputted successfully if we'll go and check in the palm.xml we have added only two dependency web dependency and Lumbar dependency okay that's fine so from this project or from this application we'll talk to the Chart GPT to make our job easy chart GPT provides a public API using that you can call its API okay you can call all the charge GPT API so let's take a look into API endpoint request and response structure then we'll implement it in our project okay so let's go to the home page of open API then you can find a section called AP reference just click on that and there is Introduction steps you can follow all the steps and there is something called authentication okay the open AI API use API keys for authentication visit your API key page to retrieve the API key you will be using in your request okay so from any application if you want to talk to the Chart GPT charge GPT will give you an API key that key you can use as a authentication or you can pass it as a token while invoking any API from chart GPT okay but before that let me show you the request and response so just there is another section called playground just click on it ask something here okay let me ask something current temperature in Bangalore okay now to visualize how this request will go to the Chart GPT what should be the request format what should be the end point you can click on something called view code here okay this icon this tab just click on this and you will find the curl command you can also find the code for node.js python directly from here itself okay so just copy this call then go to your Postman let me import it okay so if You observe here we are giving the messages what we want to search this is called prompt okay current temperature in Bangalore this is where the input I want to give to the Chart GPT and I am expecting the response and the model here is text this one okay there are couple of model you can use as for your need and then if you go and check in the authorization section not in header section you will find that as part of the authorization header we are giving the barrier token and that token is nothing here open API key for example I don't have this open APK with me I want to generate it first okay so this is the page where you can generate the API key simply click on create new secret key give anything my key and you got the secret key this particular token will be act as a authorization header or barrier token from any application while invoking the charge GPT okay so let me copy these for reference now to access this now if I'll access this I will get there can you see here invalid request error because I have not passed this particular token from where I can pass go to the authorization choose the barrier token and give the token or APA key now send the request we got the result current temperature in Bangalore is 32 degree celsius okay fine so we understand we need to call this specific endpoint from the chart GPT to create our custom bot and this is how we need to structure your request you know to pass the model prompt prompt is nothing the question what you want to ask to the Chart GPT okay and then you have this response structure this response contains list of choices and here you will get the index and messages okay and also the main key factor here you need to pass the API key as a barrier token while consuming the API from chart GPT okay so these things we need to collect while implementing it in our code this is the straightforward thing this is the end point this is the request and response but how can I consume the chart GPT API usually to consume any API in the web application we are using the rest template right you can use the retrofit you can use the web client it is up to you but the rest template since this is the traditional project we can use the rest template to consume any third party API okay so first let me create a bin or object of rest template then we will try to consume this particular endpoint by giving this input so let's go to the IntelliJ IDEA and I will create a package called config then quickly I will create a class now here I'll create a bin of rest template so just annotate this class with other configuration then create a bin of rest template annotate here at the red bin then let's quickly create the DTU class where you can configure the request and response format so I'll create another package then I will create a class called and let's create another class called then create another class called this message should contains to a field Roland content so this content is nothing your prompt what you are searching for and this rule is nothing who is accessing it okay either you can specify user or admin so these are the two field now let me annotate your other data I mean all the lumber specific annotation then next go to the request class and here also I will annotate at the Red Data fine I have the model which model you want to use of charge GPT and what is the prompt you want to pass as part of this message okay so I will just Define a Constructor fine so in this Constructor I will simply change it because I want to pass the prompt and that prompt I want to bind to the list of messages so simply change the argument to string prompt and here I will create the object of new arraylist now in this messages I am going to add the object simply like you can add this Dot messages dot add now add the prompt what you will pass so how can I add simply we have created the objective message just pass the rule as a nothing user and what is the value we are looking for nothing prompt that's it okay simple guys as for the payload structure here which you inputted in our Postman we need to build the request okay we need to pass the prompt and model in the form of messages okay so you can try with the different API from the this particular playground you can understand the complete request and response structure fine all looks good now let's complete the response class animated your other data let's define the field so in response it will return list of choice right so if You observe it carefully we are getting list of choice so just go to the response class Define the field private list of then create this Choice class or I can directly create as a inner class here fine now just Define All the lumber specific annotation I will copy The annotation from here I mean we can type it let's save our time and for this Choice class as well fine so we have created the dto which contains the request and we need to pass the model and messages as part of the messages you need to pass the prompt what you are looking for and this is how the response will build with the list of choice and this is what the message contains role and content so this content is nothing your prompt that's it now we are good let's create the controller class we have the rest template simply we can consume the API click on package I'll create a Java class let's say I named it bot controller or you can name it then simply annotate here at the rate rest controller at the rate request mapping then just Define the method response this will the return type okay so you can Define the method name that say chart and then you can pass your request okay but what we want to do we'll just pass the input from the request param or as part of the URL so I'll just Define string prompt you can pass the request body there is nothing wrong but let's make it easy I am just giving the request param fine you have the prompt with you now you can build the request object request so to build a chart GPT request you need to pass the which model you want to use and what is the prompt prompt you will get it from here I mean as part of the input itself and model you can specify like this okay but let's not hard code in the code what we can do we'll go to the properties file and will Define the key something like let's say open AI dot let me Zoom this similarly I have the APA key right I can set in the properties file rather than hard code in the application API dot key and you can specify the key we copied here and also you can specify what endpoint you want to access this is what the end point we are going to access right so you can specify here fine these are the all required information we need to specify now we can face this particular field from our code using advert value annotation private model another value Define the key now I no need to hard code here I can pass the model fine I have the request with me now simply I can consume using the rest template so I have created the bin let me inject it fine so I can call Simply rest template Dot post for object what it is expecting from us give the URL give what is the request you want to pass and specify what is the response you want to get so you can specify the URL here you can hard code here but that is not required since we configured in our properties file let me load it just Define the key as part of this other value annotation so let me copy the key what we have defined here now simply pass this API URL here and we have the request with us and what is the response type we are expecting charge GPT response just capture this and we can simply return it back you can return the entire object let's say HR GPT response but I just want to see the content okay so what you can do charge GPT response dot get the list of choices and I want to get the first index of it fine then get the messages and get the content and change it to the string I just want to print what response I was getting as part of string so it's up to you if you want you can return the entire object if you want you can just extract the only response choice fine I need to annotate your other rate gate mapping Define here chat okay all good now if I'll hit this specific endpoint whether it will work or not let's see I am sure it will give the error because you have not configured the API key or we have not configured API key as a barrier token in the rest template while consuming it let's see what is the error we are getting based on that we'll work on the fix go to the main class run this so you can see here it started on port 8080 go to the postman and let's try to hit this specific endpoint slash bot slash chat and give the prompt as a request param this is the URL let me Zoom this for you let me send the request we're getting the error internal server error go to the application and see in the console what is the error we are getting 401 unauthorized it means we have not passed the API key as a token as authorization header if you won't pass that you cannot consume an API from the chart GPT so what you can do just go to the config class and here rather than directly return the race template create the object of phrase template and set the APA key as a authorization barrier token okay so I will just create the object of rest template now in the rest template you just need to define the see this is a simple step get the header and add the key as your authorization and value as a barrier and your API key so you can get our API key from application.properties file just Define string we can get it from properties use at the red value then get the key here and pass it here now you have the API key with us that's great so we have created the rest template and we have also specified the authorization header as part of the rest template and the same template we are using in our controller class while consuming it right so I need to return it once I have set the barrier token I need to return it that's fine now let me restart our application so it started on port 8080 just go to the postman and hit the end point we are getting error let me see bad request unrecognized request argument supplied message so are we trying to access the correct endpoint is the DPS API open api.com B1 okay so I have did a mistake this need to be chart completion endpoint okay so just Define this endpoint B1 slash chart slash completions just rerun it so it started again go to the postman hit the endpoint we got the response here right spring batch is an open source framework I mean the entire string we got from the charge GPT now you can ask n number of question here let's say I just want to ask what is free marker send the request you can see here the messages we are getting from the chart GPT so let's ask the same question what we have done in this tutorial okay so I just want to ask to the charge GPT HR GPT I want to integrate spring boot with chart GPT I mean the things what you have asked in the page in the chart GPT page same thing now you can able to access from your application okay it will be act like a custom bot now let's see what is the response we have got here it will take few second because from the application it will redirect request to the Chart GPT yeah can you see here and as an a language model OKAY spring boot is a framework for building production grid application search GPT on the other hand what it is saying to integrate charge GP to Spring boot you can use a restful API for communication between the chatbot and the application that is what we have done here right you can also ask for example so that you can find the code okay send the request we got the response here first create a new project then add this dependency I don't know what is the purpose of this dependency but you can give a try since charge GPD is suggesting you can give a try create a chart GPT client see here it's asking you to give your API key and base URL which is our chart hyphen completion rate and rest AP client okay all the states whatever you have done here you can get it from the chart GPT so this is how you can integrate charge GPT to your application to create a custom bot okay so do let me know in a comment section if you guys have any doubts that's all about this particular video guys thanks for watching this video meet you soon with A New Concept
Info
Channel: Java Techie
Views: 22,177
Rating: undefined out of 5
Keywords: Spring boot chatGPT, chatGPT, spring boot, javatechie
Id: HlDkuFy8xRM
Channel Id: undefined
Length: 22min 21sec (1341 seconds)
Published: Sat May 27 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.