About: Hi! I made this blog because I find that explaining things is the key to retaining information ✨
Joined:
May 6, 2022
Build Your Own ChatGPT AI Spring Application
Publish Date: Feb 3 '23
2 0
By the end of this tutorial we will be able to communicate with ChatGPT API using our own spring application.
Before we start we need to understand the ChatGPT request, expected Input and Output.
For that we will refer to the examples provided by the OpenAI documentation
Take a look at the Input.
{
"model": "text-davinci-003",
"prompt": "Say this is a test",
"max_tokens": 7,
"temperature": 0,
"top_p": 1,
"n": 1,
"stream": false,
"logprobs": null,
"stop": "\n"
}
You can see that the post body defines some parameters some of which are optional except for the model parameter.
For this project we will do with just 4 parameters:
model expects a String, we will keep the same model as the example which is text-davinci-003 but you can check their overview for more models. prompt expects a String or an Array, it will contain the question we want to ask ChatGPT. temperature expects a Number. max_tokens expects an Integer.
You can play around with temperature and max_tokens values but will use 4096 and 1 respectively.
Now that we're finished with the Input let's take a look at the Output
Explore developer resources, tutorials, API docs, and dynamic examples to get the most out of OpenAI's platform.
platform.openai.com
Click on Create new secret key
In your Service Java class add the methods that will communicate with the API
To understand the next steps take a look at an Example of a request
curl https://api.openai.com/v1/completions \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer YOUR_API_KEY' \
-d '{
"model": "text-davinci-003",
"prompt": "Say this is a test",
"max_tokens": 7,
"temperature": 0
}'
This request expects a URL that points to the API
A header that defines the content type as JSON object
An authorization header as a Bearer Token
And the body
So we will build an http client request that will take the parameters defined above and send it to the URI. The answer will then be returned as a Json String.
Create another method that takes as parameters the prompt which is a String that contains the question.
Inside this method we will create an Input object using the class constructor and pass it the predefined parameters along with the prompt.
Next we will pass the object to the sendChatgptRequest() method as a JSON string and map the response into an Output Object using the ObjectMapper class.
If you want to save the data to a database add the JPA repository Interfaces and save the call and answer objects at this point.
Inside your Rest Controller class add your Post request