Home Artificial Intelligence Implementing your individual ChatGPT in Java :OpenAI Models GPT-4,GPT-3.5,GPT-3,Whisper,Codex,Moderation Prerequisites : Does OpenAI API is free ? Conclusion : References:

Implementing your individual ChatGPT in Java :OpenAI Models GPT-4,GPT-3.5,GPT-3,Whisper,Codex,Moderation Prerequisites : Does OpenAI API is free ? Conclusion : References:

0
Implementing your individual ChatGPT in Java :OpenAI Models GPT-4,GPT-3.5,GPT-3,Whisper,Codex,Moderation
Prerequisites :
Does OpenAI API is free ?
Conclusion :
References:

Photo by Owen Beard on Unsplash

OpenAI is a widely known company specializing in the event of advanced artificial intelligence (AI) technologies. Among the many impressive range of Generative Pre-trained Transformer (GPT) products, API stands out as a flagship innovation. This language model can generate human responses to queries in natural language, making it a really perfect tool for constructing chatbots.

This blog post walks you thru making a chatbot using the OpenAI GPT API in Java. Here we are going to try to construct a Investment Advisor Bot that responds to your investment query using ChatGPT OpenAI Api.

Get your OpenAI Credentials by creating an account on OpenAI website. After creating the account generate an API key/token to make use of in your Java application.

Install Java 11

Add below dependency in you pom.xml.Please use this maven link:

https://mvnrepository.com/artifact/com.theokanning.openai-gpt3-java

 
com.theokanning.openai-gpt3-java
service
version

Now implement your individual Investment Advisor Bot class in lower than 100 LOC.

public class InvestmentAdviserBot {

public static void essential(String[] args) {
// must required open ai toc
String token = System.getenv("OPENAI_TOKEN");

// Create an instance of OpenAiService that can interact with OpenAI Chat GPT API.
OpenAiService investmentBotService = recent OpenAiService(token);

// Get the required model of your selection
Engine davinciEngine = investmentBotService.getEngine("davinci");

// The generated text results are returned on this array
ArrayList < CompletionChoice > investmentCompletionChoiceList = recent ArrayList < CompletionChoice > ();

System.out.println("nLets Begin...");

String investMentQuery = "Are you able to please help me where should I invest 1000 Rs in mutul fund";

CompletionRequest completionRequest = CompletionRequest.builder()
.prompt(investMentQuery)
.temperature(0.7)
.maxTokens(96)
.topP(1.0)
.frequencyPenalty(0.0)
.presencePenalty(0.3)
.echo(true)
.construct();

investmentBotService.createCompletion("davinci", completionRequest).getChoices().forEach(line - > {
storyArray.add(line);
});
System.out.println(investmentCompletionChoiceList);
}

}

Let try to grasp above code.

  1. We’d like must required OpenAI token to authenticate apis.
  2. Initialize the article of
  3. Create a Completion Request with below parameters :
  4. The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays.
  5. :What sampling temperature to make use of, between 0 and a couple of. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
  6. Maximum variety of tokens generated upon completion. The token count variety of prompt and max_tokens cannot exceed the model context length. Most models have a context length of 2048 tokens (except newer models that support 4096).
  7. An alternative choice to temperature sampling, called core sampling, during which the model considers the final result of tokens using the stochastic mass top_p. So 0.1 implies that only tokens that make up 10% of essentially the most probable mass will likely be considered generally recommend changing either this item or the temperature, but not each.
  8. To optimize the performance of a language model, it’s important to set a worth between -2.0 and a couple of.0. When this value is positive, it penalizes the introduction of recent tokens based on their frequency within the text. Because of this the model is less more likely to repeat the identical line verbatim, leading to more natural and fascinating language output.
  9. :Please enter a number between -2.0 and a couple of.0. If you happen to input a positive value, the model will penalize recent tokens based on their appearance within the text thus far. It will increase the likelihood of the model discussing recent topics
  10. To make sure that your response is obvious and complete, please repeat the prompt before providing your answer. It will help to avoid any confusion and make sure that your response is fully understood.
  11. he OpenAI API is fueled by a big selection of models, each with unique capabilities and pricing structures. This diversity allows for greater flexibility in choosing essentially the most suitable model for a given task, while also accommodating a variety of budgets. Whether you’re in search of a high-end model with advanced features or a cheaper option, the OpenAI API has you covered. With its comprehensive suite of models, you may rest assured that you simply’ll find the right fit on your needs.
OpenAI Models

The OpenAI API pricing model is predicated on the variety of tokens generated by the API. Tokens are individual units of text, resembling words or characters, that the API generates in response to a prompt. The associated fee of generating tokens varies depending on the model you’re using, and the pricing is tiered based on the amount of usage.

As of March 2023, the pricing for the OpenAI API is as follows:

GPT-3

The primary 100,000 tokens generated per 30 days are free. After that, the associated fee is $0.006 per token for the primary 1 billion tokens per 30 days. When you exceed 1 billion tokens, the associated fee drops to $0.003 per token.

GPT-2

The primary 5 million tokens generated per 30 days are free. After that, the associated fee is $0.00006 per token for the subsequent 500 million tokens per 30 days. When you exceed 500 million tokens, the associated fee drops to $0.00003 per token.

Other Models

The pricing for other models, resembling DALL-E, CLIP, and Codex, varies depending on the precise model and the amount of usage. You’ll find more information on the OpenAI website.

It’s vital to notice that the above pricing is subject to alter, so you should definitely check the OpenAI website for the newest information. Moreover, certain use cases, resembling academic research or non-profit organizations, could also be eligible for discounts. Make sure to check in case you qualify for any discounts.

On this blog post, we’ve walked through the way to use the OpenAI GPT API in Java to construct a chatbot.

LEAVE A REPLY

Please enter your comment!
Please enter your name here