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

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:
- Pass the user's message, as a string, to the
chat()method.javaAi.chat("YOUR_QUESTION");
- 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
nyou set, you can use either thechat()method, which returns aStringresponse from the API, or thechatWithChoices()method, which returns multiple responses from the API asList<String>, depending on the value ofnyou 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
TTS
To translate text to speech, you must pass to the
textToSpeech()method astringcontaining the text you want to voice and astringcontaining 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
https://github.com/artemnefedov/JavaAI/assets/74130706/82d315be-def0-4946-b560-ab0772f64051
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);
