Skip to content

A minimalistic OpenAI API client for the JVM, written in Java

License

Notifications You must be signed in to change notification settings

shivaam/jvm-openai

 
 

Repository files navigation

jvm-openai

build Maven Central javadoc Quality Gate Status

A minimalistic unofficial OpenAI API client for the JVM, written in Java

Add dependency

Java 17+ is a prerequisite

Gradle

implementation("io.github.stefanbratanov:jvm-openai:${version}")

Maven

<dependency>
    <groupId>io.github.stefanbratanov</groupId>
    <artifactId>jvm-openai</artifactId>
    <version>${version}</version>
</dependency>

Minimal example

OpenAI openAI = OpenAI.newBuilder(System.getenv("OPENAI_API_KEY")).build();

ChatClient chatClient = openAI.chatClient();
CreateChatCompletionRequest createChatCompletionRequest = CreateChatCompletionRequest.newBuilder()
        .model("gpt-3.5-turbo")
        .message(ChatMessage.userMessage("Who won the world series in 2020?"))
        .build();
ChatCompletion chatCompletion = chatClient.createChatCompletion(createChatCompletionRequest);
// ChatCompletion[id=chatcmpl-123, created=1703506594, model=gpt-3.5-turbo-0613, systemFingerprint=fp_44709d6fcb, choices=[Choice[index=0, message=Message[content=The Los Angeles Dodgers won the World Series in 2020., toolCalls=null, role=assistant], logProbs=null, finishReason=stop]], usage=Usage[promptTokens=17, completionTokens=13, totalTokens=30]]

ImagesClient imagesClient = openAI.imagesClient();
CreateImageRequest createImageRequest = CreateImageRequest.newBuilder()
        .model("dall-e-3")
        .prompt("A cute baby sea otter")
        .build();
Images images = imagesClient.createImage(createImageRequest);
// Images[created=1704009569, data=[Image[b64Json=null, url=https://foo.bar/cute-baby-sea-otter.png, revisedPrompt=Generate an image of a baby sea otter, exuding cuteness. The small, furry creature should be floating blissfully on its back in clear, calm waters, its round button eyes are brimming with innocence and curiosity.]]]

Supported APIs

API Status
Audio ✔️
Chat ️ ✔️
Embeddings ✔️
Fine-tuning ✔️
Files ✔️
Images ✔️
Models ✔️
Moderations ✔️

Beta APIs

API Status
Assistants ✔️
Threads ️ ✔️
Messages ✔️
Runs ✔️

About

A minimalistic OpenAI API client for the JVM, written in Java

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%