Simple-OpenAI is a Java library designed for intuitive and straightforward integration with OpenAI APIs. It streamlines the process of making API calls, providing a user-friendly approach to access various OpenAI services. The project aims to simplify complexities and enhance productivity for developers working with AI functionalities.
Simple-OpenAI is a lightweight Java library designed for seamless integration with the OpenAI API. This library simplifies the process of sending requests and receiving responses from OpenAI, providing a user-friendly interface comparable to those available in Python and Node.js. It serves as an unofficial client utilizing libraries such as CleverClient for HTTP communication, Jackson for JSON parsing, and Lombok to reduce boilerplate code.
Features
- Comprehensive Support: Stay updated with the latest OpenAI features, including:
- Audio: Speech synthesis, transcription, and translation
- Chat Completion: Text generation, streaming, function calling, vision, and structured outputs
- Image Processing: Image generation, editing, and variations
- Voice Models: Speech-to-speech conversations
- Real-Time Capabilities: Handle real-time applications effectively
- Model Fine Tuning: Custom model training
Usage Example
Creating a SimpleOpenAI Object
To initiate usage of the OpenAI services, create a SimpleOpenAI
object by providing your OpenAI API Key:
var openAI = SimpleOpenAI.builder()
.apiKey(System.getenv("OPENAI_API_KEY"))
.build();
Audio Service Example
Transform text to audio using the Audio service:
var speechRequest = SpeechRequest.builder()
.model("tts-1")
.input("Hello world, welcome to the AI universe!")
.voice(Voice.ALLOY)
.responseFormat(SpeechResponseFormat.MP3)
.build();
var futureSpeech = openAI.audios().speak(speechRequest);
var speechResponse = futureSpeech.join();
// Handle audio response.
Image Generation Example
Create two images based on a prompt:
var imageRequest = ImageRequest.builder()
.prompt("A cartoon of a hummingbird that is flying around a flower.")
.n(2)
.size(Size.X256)
.responseFormat(ImageResponseFormat.URL)
.model("dall-e-2")
.build();
var futureImage = openAI.images().create(imageRequest);
var imageResponse = futureImage.join();
// Process image URLs.
Exception Handling
Utilize the OpenAIExceptionConverter
class to manage OpenAI API errors effectively, mapping HTTP errors to specific exceptions such as BadRequestException
, AuthenticationException
, and others. This allows for granular handling of errors in applications.
Simple-OpenAI is continuously updated to support the evolving capabilities of OpenAI, making it an essential tool for developers looking to harness AI technologies with minimal complexity.
No comments yet.
Sign in to be the first to comment.