Build Your Own AI App Using OpenAI Java API

openai java api demo n.w
1 / 39
Embed
Share

Learn how to download the OpenAI API for Java, create an API key, build the API, and use it to create human-like text and content with GPT models. Follow step-by-step instructions to set up the OpenAI service in Java and generate responses with conversational AI capabilities.

  • Java API
  • OpenAI
  • GPT models
  • AI applications
  • Generative AI

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. OpenAI Java API Demo 1. In this demo, we will download openai api for Java 2. Build our own app by calling api

  2. Java api web site: https://github.com/TheoKanning/openai-java What is GPT? Generative Pre-trained Transformers, commonly known as GPT, are a family of neural network models that uses the transformer architecture and is a key advancement in artificial intelligence (AI) powering generative AI applications such as ChatGPT. GPT models give applications the ability to create human-like text and content (images, music, and more), and answer questions in a conversational manner. Organizations across industries are using GPT models and generative AI for Q&A bots, text summarization, content generation, and search.

  3. Step 1 Create folder and download the code: git clone https://github.com/TheoKanning/openai-java.git

  4. Step 2 Create api Key

  5. Step 3 Build api Check binary is generated.

  6. Step 4 Use openai API

  7. Create a OpenAI Service Create a Java class and copy/paste next code OpenAiService service = new OpenAiService("your_token"); CompletionRequest completionRequest = CompletionRequest.builder() .prompt("Somebody once told me the world is gonna roll me") .model("ada") .echo(true) .build(); service.createCompletion(completionRequest).getChoices().forEach(System.out::println);

  8. It has some error shows, because we need tell the Jdeveloper which library to use to refer those opanapi Java library.

  9. Right click the project and go to the project properties.

  10. Select the User group and click New button

  11. Give a library name, here is openai_api, and select classpath, and click AddEntry to add those jars we buit previously.

  12. Make sure select the right library, and click OK.

  13. The library is added to the project and click OK.

  14. The whole Java code is as follows, you can compile success after adding the openai api library. package com.david.openai; import com.theokanning.openai.completion.CompletionChoice; import com.theokanning.openai.completion.CompletionRequest; import com.theokanning.openai.completion.CompletionResult; import com.theokanning.openai.service.OpenAiService; import java.util.List; public class DemoService { public DemoService() { super(); } // Copy and Paste the code at here. private static final String myToken = "sk-****The Token Need be Yours *****x"; public void test() { OpenAiService service = new OpenAiService(myToken); CompletionRequest completionRequest = CompletionRequest.builder() .prompt("Somebody once told me the world is gonna roll me") .model("ada") .echo(true) .build(); CompletionResult result = service.createCompletion(completionRequest); List<CompletionChoice> choices = result.getChoices(); for (CompletionChoice choice : choices) { String choiceText = choice.getText(); System.out.println(choiceText); } } public static void main(String[] args) { DemoService demo = new DemoService(); demo.test(); } }

  15. In the next we will use maven to download the required Java run time library Create a maven file as follows:

  16. We need add dependencys of te retrofit: The latest version of these libraries can be found via https://mvnrepository.com/artifact/com.squareup.retrofit2 Let s adding the next dependencys to the pom file: <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>retrofit</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>converter-jackson</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.squareup.retrofit2</groupId> <artifactId>adapter-rxjava2</artifactId> <version>2.9.0</version> </dependency>

  17. Now lets run a maven clean or compile to download the library.

  18. Open the pom file and go to overview, add the src folder as Test Resource

  19. Lets run maven test

  20. When do the test, we need fix the code to limit a few results, otherwise we are over the limit. Also need to setup a payment method in the openAI.

  21. Lets do some runs, and the output is different.

More Related Content