Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ChatGPT API written for Java - For Pulling Requests

License

Notifications You must be signed in to change notification settings

ndepomereu/ChatGPT-Java-API

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

ChatGPT-Java-API

This is an easy-to-use "drag and drop" API that allows you to use OpenAI's new ChatGPT API. This API works by wrapping HTTPS requests with java variables, making the generated results much easier to control.

Feel free to use, modify, and distribute this code as needed.

Working Example

import java.io.IOException;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) throws IOException {
        Scanner scan = new Scanner(System.in);
        String key = "sk-YOUR KEY HERE";  // TODO Add your open ai key here
 
        // Create the initial prompt, we will reuse it later.
        String initialPrompt = "You are a customer support chat-bot. Write brief summaries of the user's questions so that agents can easily find the answer in a database.";
        List<ChatBot.ChatMessage> messages = List.of(new ChatBot.ChatMessage("system", initialPrompt));
        ChatBot.ChatCompletionRequest request = new ChatBot.ChatCompletionRequest("gpt-3.5-turbo", messages);
        ChatBot bot = new ChatBot(key);

        // ChatCompletionRequest copies the list, so let's modify the request's
        // copy of the list.
        messages = request.getMessages();

        while (true) {
            System.out.println("Enter text below:\n\n");
            String input = scan.nextLine();

            // Generate a response, and print it to the user.
            messages.add(new ChatBot.ChatMessage("user", input));
            ChatBot.ChatCompletionResponse response = bot.generateResponse(request);
            System.out.println("\n" + response.getChoices().get(0).getMessage().getContent());

            // Save the generated message to the bot's conversational memory
            messages.add(response.getChoices().get(0).getMessage());
        }
    }
}

Installation

  1. Add okhttp and gson as dependencies (see below)
  2. Drag and drop the ChatBot.java file into your project

Maven:

  <dependencies>
    <dependency>
      <groupId>com.squareup.okhttp3</groupId>
      <artifactId>okhttp</artifactId>
      <version>4.9.2</version>
    </dependency>
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.9</version>
    </dependency>
  </dependencies>

Gradle KTS:

repositories {
    mavenCentral()
}

dependencies {
    implementation("com.squareup.okhttp3:okhttp:4.9.2")
    implementation("com.google.code.gson:gson:2.8.9")
}

More

  1. I also wrote a Kotlin Version of the API.
  2. Need inspiration for prompts? Check out awesome prompts.
  3. Looking for the official API? OpenAI only officially supports python.

Support

If I have saved you time, please consider sponsoring me. If you cannot financially support me, consider leaving a star on the repository and sharing it. Thanks!

License

ChatGPT-Java-API is licensed under the MIT License.

About

ChatGPT API written for Java - For Pulling Requests

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%