Skip to content

Lightweight Java library to interact with the OpenAI API (GPT, DALL-E, TTS, etc.)

License

Notifications You must be signed in to change notification settings

artemnefedov/JavaAI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

JavaAI logo

Maven Central GitHub Downloads (all assets, all releases) License: MIT Codacy Badge codecov

About

JavaAI is a lightweight Java library with minimal third-party dependencies designed to interact with the OpenAI API. It provides an intuitive interface for accessing advanced AI capabilities in Java applications. With JavaAI, you can easily integrate state-of-the-art features into your projects, including chat with GPT, image generation in DALL-E, and text-to-speech with Whisper.

Usage

Prerequisites

  • Java 17
  • Maven or Gradle
  • OpenAI API key

Integration

Maven

<dependency>
    <groupId>io.github.artemnefedov</groupId>
    <artifactId>javaai</artifactId>
    <version>0.4.1</version>
</dependency>

Gradle

implementation 'io.github.artemnefedov:javaai:0.4.1'

Initialize JavaAI

You can initialize JavaAI in two ways: by directly passing the API key to the constructor or by adding environment variables with the key to your system, naming it OPENAI_API_KEY as recommended by OpenAi

Passing the API key directly to the constructor

var javaAi = JavaAI.javaAiBuilder("YOUR_API_KEY");

Using an environment variable

var javaAI = JavaAI.javaAiBuilder();

Example

ChatGPT

You can use two ways to interact with ChatGPT:

  1. Pass the user's message, as a string, to the chat() method.
javaAi.chat("YOUR_QUESTION");
  1. Pass a saved conversation to the method as a List<ChatMessage>.
var messages = List.of(
       new ChatMessage("user", "what is the meaning of life?"),
       new ChatMessage("AI", "The meaning of life is to be happy."),
       new ChatMessage("user", "are you sure?")
);

javaAI.chat(messages);

Depending on the value of n you set, you can use either the chat() method, which returns a String response from the API, or the chatWithChoices() method, which returns multiple responses from the API as List<String>, depending on the value of n you set.


DALL-E

You can use the generateImage() method to generate an image from a text prompt. The model will return a URL to the result, as a List of String.

javaAI.generateImage("Computes science cat, photo on Fujifilm x100v, 2024");

Response CS cat


TTS

To translate text to speech, you must pass to the textToSpeech() method a string containing the text you want to voice and a string containing the location where the audio file will be saved.

javaAI.textToSpeech("Hi, my name is Artem, and I made this piece of... code.", "path/to/save/audio.mp3");

Response

piece_of_code.mp4

Configuration

You can specify different settings for each model, via the setChatConfig(), setDalleConfig(), and setTtsConfig() methods. You are accepting records ChatConfig, DalleConfig, and TtsConfig respectively.

Config records view


ChatConfig.java

public record ChatConfig(
        Model model,
        float temperature,
        int topP,
        int n,
        boolean stream,
        String stop,
        int maxTokens,
        float presencePenalty,
        float frequencyPenalty,
        Map<Integer, Integer> logitBias,
        String user) {
}

Parameters in OpenAI API docs


DalleConfig.java

public record DalleConfig(
        DalleModel model,
        int n,
        String quality,
        ResponseFormat responseFormat,
        Size size,
        Style style,
        String user) {
}

Parameters in OpenAI API docs


TtsConfig.java

public record TtsConfig(
        TtsModel model,
        Voice voice,
        VoiceResponseFormat responseFormat,
        float speed
) {
}

Parameters in OpenAI API docs


Example for Chat:

import io.github.artemnefedov.javaai.model.chat.ChatConfig;

var customChatConfig = new ChatConfig(
        ChatConfig.Model.GPT_3_5_TURBO,
        1F,
        1,
        1,
        false,
        "\n",
        2000,
        0F,
        0F,
        new HashMap<>(),
        UUID.randomUUID().toString()
);

javaAi.setChatConfig(customChatConfig);

Features


License

Distributed under the MIT License