OpenAi4J: Unlocking the Power of OpenAI for Java Developers
OpenAi4J is a comprehensive Java library designed to enhance the interaction with OpenAI's state-of-the-art GPT models, notably including the latest advancements like gpt4-turbo vision and assistant-v2. This library, originally derived from the TheoKanning/openai-java project after it ceased being updated, continues to evolve, bringing the newest API capabilities to its users.
Key Features
OpenAi4J offers full support for a variety of OpenAI API models, covering all bases from Completions, Chat, Edits, and Embeddings to Audio, Files, and even the new Assistant-v2 alongside Images, Moderations, Batch, and Fine-tuning functionalities. It provides:
- Complete Coverage: Seamless integration options with all OpenAI API models.
- User-Friendly Setup: Powered by Retrofit, the library allows immediate communication with the API.
- Resource-Rich: Loaded with extensive examples and documentation for getting started quickly.
- Customizable and Secure: Configure your API keys and base URLs via environment variables.
- Versatile Calls: Supports both synchronous and asynchronous API request handling.
OpenAi4J’s main goal is to equip Java developers with an efficient and powerful tool, enabling them to embed OpenAI's AI capabilities directly into their applications.
Jumpstart Your Usage
Setting Up
- For Gradle Users: Add
implementation 'io.github.lambdua:<api|client|service>:0.22.4'
to your build.gradle file. - For Maven Users: Use the following XML snippet:
<dependency> <groupId>io.github.lambdua</groupId> <artifactId>service</artifactId> <version>0.22.4</version> </dependency>
Simple Integration Example
With OpenAi4J, managing an interaction with an OpenAI model is straightforward. Here is a basic example to get you started:
static void simpleChat() {
OpenAiService service = new OpenAiService(Duration.ofSeconds(30));
List<ChatMessage> messages = new ArrayList<>();
ChatMessage systemMessage = new SystemMessage("You are a cute cat and will speak as such.");
messages.add(systemMessage);
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
.model("gpt-4o-mini")
.messages(messages)
.n(1)
.maxTokens(50)
.build();
ChatCompletionResult chatCompletion = service.createChatCompletion(chatCompletionRequest);
System.out.println(chatCompletion.getChoices().get(0).getMessage().getContent());
}
Opportunities for Customization
The library provides numerous customization options, especially if you wish to create a bespoke client or extend its capabilities:
- POJO Integration: Build your client by importing POJOs from the API module.
- Flexible Service Setup: Tailor the OpenAiService to meet specific requirements, using the default settings or creating your configurations.
- Rich Example Repository: Explore various functional examples within the
example
package to implement features like image recognition, tool integration, and more through pre-coded templates.
Expanding Functionality
By incorporating tools and custom executors, you can simulate functions, such as querying weather data, and integrate them into your models. This extends OpenAi4J beyond mere communication with APIs to a robust tool capable of performing complex tasks aligned with various user inputs.
Conclusion
OpenAi4J stands out as a reliable, detailed, and highly supportive library for Java developers aiming to incorporate OpenAI's cutting-edge models into their software products. It simplifies the API interaction while offering a wide range of customizable options to fit different project needs, making it a valuable asset in the AI integration toolkit for developers. Whether you're building conversational agents or embedding sophisticated AI capabilities into your Java applications, OpenAi4J is poised to facilitate those advancements with ease.