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.
- Java 17
- Maven or Gradle
- OpenAI API key
<dependency>
<groupId>io.github.artemnefedov</groupId>
<artifactId>javaai</artifactId>
<version>0.4.1</version>
</dependency>
implementation 'io.github.artemnefedov:javaai:0.4.1'
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
var javaAi = JavaAI.javaAiBuilder("YOUR_API_KEY");
var javaAI = JavaAI.javaAiBuilder();
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
n
you set, you can use either thechat()
method, which returns aString
response from the API, or thechatWithChoices()
method, which returns multiple responses from the API asList<String>
, depending on the value ofn
you set.
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");
To translate text to speech, you must pass to the
textToSpeech()
method astring
containing the text you want to voice and astring
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
You can specify different settings for each model, via the setChatConfig()
, setDalleConfig()
, and setTtsConfig()
methods. You are accepting records ChatConfig
, DalleConfig
, and TtsConfig
respectively.
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
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);