DEV Community

lou
lou

Posted on

Build Your Own ChatGPT AI Spring Application

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"
}
Enter fullscreen mode Exit fullscreen mode

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

{
  "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7",
  "object": "text_completion",
  "created": 1589478378,
  "model": "text-davinci-003",
  "choices": [
    {
      "text": "\n\nThis is indeed a test",
      "index": 0,
      "logprobs": null,
      "finish_reason": "length"
    }
  ],
  "usage": {
    "prompt_tokens": 5,
    "completion_tokens": 7,
    "total_tokens": 12
  }
}

Enter fullscreen mode Exit fullscreen mode

The most interesting property here is "text" which is a String, it will contain the answer to the question sent earlier to the API.

Now we can start to imagine how our java classes will look like.

The Input java class



Since Choices in a list of Objects we will create a Choice class



And a Usage record since we don't need a getter



The Output java class

You can hardcode the parameters inside the constructor or use the application.properties with the following variables

Generate your OpenAI API Key here

OpenAI Platform

Explore developer resources, tutorials, API docs, and dynamic examples to get the most out of OpenAI's platform.

favicon platform.openai.com

Click on Create new secret key

API 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
}'

Enter fullscreen mode Exit fullscreen mode

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

Link to the Source code

GitHub logo leriaetnasta / OpenAI-Question-Answering-API

This web app asks questions and saves answers retrieved from chatgpt. With link to tutorial

GitHub Clones

OpenAI Question Answering API

This API utilizes the OpenAI API to answer user queries.

The OpenAI expected 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 I only used 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.

The expected Output

{
  "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7"
  "object": "text_completion",
  "created": 1589478378,
  "model": "text-davinci-003",
  "choices": [

Top comments (0)