Home/OpenAI using Spring Boot

OpenAI using Spring Boot

Published On: 1 January 2023.By .
  • General

OpenAI is an artificial intelligence research laboratory focused on creating a digital intelligence that surpasses human intelligence. OpenAI‘s mission is to ensure that artificial general intelligence (AGI) benefits all of humanity. OpenAI works on advancing AI capabilities, safety, and policy. OpenAI focuses on developing algorithms, creating tools and frameworks, and conducting research to make AGI a reality. OpenAI‘s research includes robotics, reinforcement learning, natural language processing, computer vision, and more. OpenAI also works on developing safety measures to ensure AGI is developed responsibly. OpenAI is working to create a future where AGI is a positive force for humanity.

(Note : The paragraph above is created using OpenAI playground . )

Features offered by Open-AI  : https://beta.openai.com/examples


OpenAI provides a variety of models that can be used for numerous solutions , here we will discuss about 2 of them  :


  1. Language Converter
  2. Training a model which will be further used as Chat-Bot


Language Converter :

Language converter , as the name states is used to convert a given text from one language to another. It’s working with the settings mentioned below :

1) Engine / Model : text-davinci-003
2) Max tokens : 300 (can be increased or decreased  depending upon requirement)
3) Temperature : 0.3
4) Top p : 1.0
5) Frequency penalty : 0.0
6) Presence penalty : 0.0

(We have discussed about these terms / settings in last section of this blog . )

  • Added action named LANGUAGE_TRANSLATION :

  • Setting for action as discussed above :


Chat Bot :

So we all must have used Chat-Bot services in one way or other where we interact with a BOT which tries to understand our query and respond in a particular manner to that query . Nowadays most of the helplines services redirect us to BOT which asks for all the necessary information from us and tries to help us on same by itself , or forward that information to  a agent who can then consume all the information thus saving time of agent in asking for that information .

Here we will be training our model from pre-trained models present on OpenAI by default if no specific model name is given then model curie is selected .
Pre-trained  models provided by OPEN-AI to further train our personalised models are : ada, babbage, curie, or davinci  . Moreover we can also use our already trained model as base model if we want new model to be also trained with data set for which we already have a model . This gives us more flexibility in training new models and saves time in training models with same data-set again and again .

Now let’s train a model which will be used for our personalised chat-bot .
Here we have also added a feature which will replace our already used model with new one so that as soon as new model becomes available our software will automatically shift to new model and will provide response using new model thus reducing down time of changing model after training new one .

We have trained our model with some data related to our organisation , so now bot can handle majority of questions related to our organisation .

It’s working with the settings mentioned below :

1) Max tokens : 150 (can be increased or decreased  depending upon requirement)
2) Temperature : 0.9
3) Top p : 1.0
4) Frequency penalty : 0.0
5) Presence penalty : 0.6

(We have discussed about these terms / settings in last section of this blog . )

Steps to be followed : 

  1. Prepare a dataset in jsonl format and upload that to OpenAI .
  2. Using that uploaded file id train a model (here we will be training using base model as davinci) .
  3. In starting our chat bot will be using model named : text-davinci-003 for giving response and we have added a scheduler which will keeps on checking whether our new model is trained or not as soon as it is trained text-davinci-003 will be replaced automatically with new model
    (Right now we will be running our scheduler every minute but that duration can be altered according to person’s need )


Uploading our training file to OPEN-AI :


Running Chat-Bot on basic model provided by Open-AI (text-davinci-003):


Training our model with the file we have uploaded and running it Chat-Bot again :



Brief explanation of settings / terms used :

  1. Model / Engine : Denotes the ID of the particular engine which we are using for that particular task .
  2. Prompt : The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays.
  3. Suffix : The suffix that comes after a completion of inserted text.
  4. Max Token : Denotes the maximum number of tokens that can be consumed in response from Open AI
  5. Temperature : What sampling temperature to use. Higher values means the model will take more risks. Try 0.9 for more creative applications, and 0 (argmax sampling) for ones with a well-defined answer.Generally it’s recommend to alter this or top_p but not both.
  6. Top_P : An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.
  7. N : How many completions to generate for each prompt.Note: Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for max_tokens and stop.
  8. Stream : Whether to stream back partial progress. If set, tokens will be sent as data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message.
  9. Logprobs : Include the log probabilities on the logprobs most likely tokens, as well the chosen tokens. For example, if logprobs is 5, the API will return a list of the 5 most likely tokens. The API will always return the logprob of the sampled token, so there may be up to logprobs+1 elements in the response.
  10. Echo : Echo back the prompt in addition to the completion .
  11. Stop : Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence.
  12. Presence Penalty : Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model’s likelihood to talk about new topics.
  13. Frequency Penalty : Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model’s likelihood to repeat the same line verbatim.
  14. Best Of : Generates best_of completions server-side and returns the “best” (the one with the highest log probability per token). Results cannot be streamed.When used with n, best_of controls the number of candidate completions and n specifies how many to return – best_of must be greater than n.
    Note: Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for max_tokens and stop.
  15. Logit Bias : Modify the likelihood of specified tokens appearing in the completion.Accepts a json object that maps tokens (specified by their token ID in the GPT tokenizer) to an associated bias value from -100 to 100. You can use this tokenizer tool (which works for both GPT-2 and GPT-3) to convert text to token IDs. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token.
    As an example, you can pass {“50256”: -100} to prevent the <|endoftext|> token from being generated.
  16. User : A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse


Source Code : https://github.com/akshat-jainn/OpenAI-SpringBoot

Related content

We Love Conversations

Say Hello
Go to Top