ChatGPT API integration with Spring Boot

ChatGPT is one of the most important terms nowadays.
Ask ChatGPT anything and it will provide you an answer. ChatGPT understands any text, you no need to organize your prompt while asking to the ChatGPT.
We can integrate ChatGPT to any application, irrespective of any language. We will create a REST API using Spring Boot to interact with the ChatGPT API. We can ask the question and get the result from our application itself.
First of all you need to create an account with ChatGPT.
Create a new Spring Boot project with 2 dependencies:-
1) Lombok
2) Spring Web

ChatGPT provides public APIs that we can use and integrate with our applications.
Go to Open AI ChatGPT website and click on API reference. If you want to talk to ChatGPT from any application, ChatGPT will provide you an API key, we need to use that key for authentication. We will pass this as a token while invoking any API from ChatGPT.
You can open playground on ChatGPT website and issue prompts. You can then view code any get the curl command. You can import that curl command in postman and analyze the request and response.
In the request JSON, we see that “prompt” is the JSON key where we actually provide the question that we want to ask to ChatGPT as the JSON value.

Below is the POST API that we want to use:-
https://api.openai.com/v1/chat/completions.

And below is the model that we want to use:-
gpt-3.5-turbo

We need the API key, we will use this to pass in the request header as Authorization Bearer token.
So, we need to pass model and prompt in the request body and also the API key as Bearer token in the Authorization request header.
In the response we will receive choices as JSON array, this will contain answers from ChatGPT.

In our Spring Boot project we will use RestTemplate to consume the API.
Below is the GitHub project that I created for ChatGPT integration.
https://github.com/raianoop07/springboot-chaptgpt