Introduction to simple-openai
These articles are AI-generated summaries. Please check the original sources for full details.
Introduction to simple-openai
The simple-openai library is a unified, community-driven Java HTTP client for the OpenAI API and compatible providers, streamlining interactions with large language models (LLMs). It wraps low-level REST calls in a type-safe API covering chat completions, tools, vision, and embeddings, supporting providers like OpenAI, Google Gemini, Mistral, and Azure OpenAI.
This library allows developers to avoid vendor lock-in by keeping application code independent of specific model vendors, while still accessing the standard OpenAI API.
Why This Matters
Developing applications with LLMs often requires managing complex API interactions and provider-specific SDKs, increasing development time and maintenance overhead. simple-openai addresses this by providing a consistent interface across multiple providers, reducing the risk of code changes when switching models or vendors. The failure to abstract away provider details can lead to significant refactoring costs and delays in feature delivery.
Key Insights
- Multi-Provider Support: Supports OpenAI, Gemini Google, Mistral, DeepSeek, Azure OpenAI, and Anyscale.
- Tool Calling: Enables AI models to interact with external systems and execute structured operations, expanding the capabilities beyond simple text generation.
- Streaming Responses: Provides incremental responses for longer answers, improving user experience by reducing perceived latency.
Working Example
import io.github.sashirestela.simpleopenai.ChatRequest;
import io.github.sashirestela.simpleopenai.SimpleOpenAIGeminiGoogle;
import io.github.sashirestela.simpleopenai.chat.UserMessage;
import java.util.logging.Level;
import java.util.logging.Logger;
public class SimpleChat {
public static void main(String[] args) {
Logger logger = System.getLogger("simpleopenai");
String apiKey = System.getenv("GEMINI_API_KEY");
if (apiKey == null || apiKey.isBlank()) {
logger.log(Level.SEVERE, "GEMINI_API_KEY is not set");
return;
}
SimpleOpenAIGeminiGoogle client = SimpleOpenAIGeminiGoogle.builder()
.apiKey(apiKey)
.build();
ChatRequest chatRequest = ChatRequest.builder()
.model("gemini-2.5-flash")
.message(UserMessage.of("Suggest a weekend trip in Japan, no more than 60 words."))
.build();
client.chatCompletions().create(chatRequest).thenAccept(chat -> {
logger.log(Level.INFO, "Model reply: {0}", chat.firstContent());
});
}
}
Practical Applications
- Travel Booking Assistant: A system that uses tool calling to search for hotels and create bookings based on user requests.
- Pitfall: Failing to sanitize tool call payloads can lead to unexpected errors and vulnerabilities, especially when integrating with different providers that have varying API requirements.
References:
Continue reading
Next article
AWS re:Invent 2025: AI Agent Hype Meets Enterprise Reality
Related Content
Building Your First AI Client in Java (Cerebras AI)
Learn to build a simple AI chat client in Java using Cerebras AI's free tier, demonstrating a practical application of large language models.
Voices: Open-Source Text-to-Speech Library for Java Applications
Voices is an open-source Java library for generating text-to-speech audio without external APIs. It leverages ONNX Runtime and supports multiple languages via prepackaged models or OpenVoice.
A Guide to @ClassTemplate in JUnit 5
Learn about ClassTemplate in JUnit 5, enabling test execution with different configurations for increased code coverage.