/openai4j

Java client library for OpenAI API

Primary LanguageJavaApache License 2.0Apache-2.0

Java client library for OpenAI API

This is an unofficial Java client library that helps to connect your Java applications with OpenAI API.

Current capabilities:

Coming soon:

Start using

Maven:

<dependency>
    <groupId>dev.ai4j</groupId>
    <artifactId>openai4j</artifactId>
    <version>0.17.0</version>
</dependency>

Gradle:

implementation 'dev.ai4j:openai4j:0.17.0'

Code examples

Create an OpenAI Client

Simple way:

String apiKey = System.getenv("OPENAI_API_KEY");

OpenAiClient client = OpenAiClient.builder()
    .openAiApiKey(apiKey)
    .build();

Customizable way:

String apiKey = System.getenv("OPENAI_API_KEY");

OpenAiClient client = OpenAiClient.builder()
	.baseUrl(baseUrl)
	.openAiApiKey(apiKey)
	.organizationId(orgId)
	.callTimeout(ofSeconds(60))
	.connectTimeout(ofSeconds(60))
	.readTimeout(ofSeconds(60))
	.writeTimeout(ofSeconds(60))
	.proxy(HTTP, "XXX.XXX.XXX.XXX", 8080)
	.logRequests()
	.logResponses()
	// other customizations coming soon!
	.build();

Completions

Synchronously

Simple way:

String completion = client.completion("Write a poem about ChatGPT").execute();

Customizable way:

CompletionRequest request = CompletionRequest.builder()
	.model(GPT_3_5_TURBO_INSTRUCT)
	.prompt("Write a poem about ChatGPT")
	.temperature(0.9)
	...
	.build();

CompletionResponse response = client.completion(request).execute();

Asynchronously

Simple way:

client.completion("Write a poem about ChatGPT")
	.onResponse(response -> ...)
	.onError(error -> ...)
	.execute();

Customizable way:

CompletionRequest request = CompletionRequest.builder()
	.model(GPT_3_5_TURBO_INSTRUCT)
	.prompt("Write a poem about ChatGPT")
	.temperature(0.9)
	...
	.build();

client.completion(request)
	.onResponse(response -> ...)
	.onError(error -> ...)
	.execute();

Streaming

Simple way:

client.completion("Write a poem about ChatGPT")
	.onPartialResponse(partialResponse -> ...)
	.onComplete(() -> ...)
	.onError(error -> ...)
	.execute();

Customizable way:

CompletionRequest request = CompletionRequest.builder()
	.model(GPT_3_5_TURBO_INSTRUCT)
	.prompt("Write a poem about ChatGPT")
	.temperature(0.9)
	...
	.build();

client.completion(request)
	.onPartialResponse(partialResponse -> ...)
	.onComplete(() -> ...)
	.onError(error -> ...)
	.execute();

Chat Completions

Synchronously

Simple way:

String completion = client.chatCompletion("Write a poem about ChatGPT").execute();

Customizable way:

ChatCompletionRequest request = ChatCompletionRequest.builder()
	.model(GPT_3_5_TURBO)
	.addSystemMessage("You are a helpful assistant")
	.addUserMessage("Write a poem about ChatGPT")
	.temperature(0.9)
	...
	.build();

ChatCompletionResponse response = client.chatCompletions(request).execute();

Asynchronously

Simple way:

client.chatCompletion("Write a poem about ChatGPT")
	.onResponse(response -> ...)
	.onError(error -> ...)
	.execute();

Customizable way:

ChatCompletionRequest request = ChatCompletionRequest.builder()
	.model(GPT_3_5_TURBO)
	.addSystemMessage("You are a helpful assistant")
	.addUserMessage("Write a poem about ChatGPT")
	.temperature(0.9)
	...
	.build();

client.chatCompletion(request)
	.onResponse(response -> ...)
	.onError(error -> ...)
	.execute();

Streaming

Simple way:

client.chatCompletion("Write a poem about ChatGPT")
	.onPartialResponse(partialResponse -> ...)
	.onComplete(() -> ...)
	.onError(error -> ...)
	.execute();

Customizable way:

ChatCompletionRequest request = ChatCompletionRequest.builder()
	.model(GPT_3_5_TURBO)
	.addSystemMessage("You are a helpful assistant")
	.addUserMessage("Write a poem about ChatGPT")
	.temperature(0.9)
	...
	.build();

client.chatCompletion(request)
	.onPartialResponse(partialResponse -> ...)
	.onComplete(() -> ...)
	.onError(error -> ...)
	.execute();

Embeddings

Synchronously

Simple way:

List<Float> embedding = client.embedding("Write a poem about ChatGPT").execute();

Customizable way:

EmbeddingRequest request = EmbeddingRequest.builder()
	.model(TEXT_EMBEDDING_ADA_002)
	.input("Write a poem about ChatGPT", "Write a haiku about ChatGPT")
	...
	.build();

EmbeddingResponse embedding = client.embedding(request).execute();

Asynchronously

Simple way:

client.embedding("Write a poem about ChatGPT")
	.onResponse(response -> ...)
	.onError(error -> ...)
	.execute();

Customizable way:

EmbeddingRequest request = EmbeddingRequest.builder()
	.model(TEXT_EMBEDDING_ADA_002)
	.input("Write a poem about ChatGPT", "Write a haiku about ChatGPT")
	...
	.build();

client.embedding(request)
	.onResponse(response -> ...)
	.onError(error -> ...)
	.execute();

Moderations

Synchronously

Simple way:

ModerationResult moderationResult = client.moderation("Write a poem about ChatGPT").execute();

Customizable way:

ModerationRequest request = ModerationRequest.builder()
	.input(INPUT)
	.model(TEXT_MODERATION_STABLE)
	.build();

ModerationResponse response = client.moderation(request).execute();

Asynchronously

Simple way:

client.moderation("Write a poem about ChatGPT")
	.onResponse(response -> ...)
	.onError(error -> ...)
	.execute();

Customizable way:

ModerationRequest request = ModerationRequest.builder()
	.input(INPUT)
	.model(TEXT_MODERATION_STABLE)
	.build();

client.moderation(request)
	.onResponse(response -> ...)
	.onError(error -> ...)
	.execute();

Images Generations with DALL·E

Simple way:

    OpenAiClient client = OpenAiClient
      .builder()
      .openAiApiKey(System.getenv("OPENAI_API_KEY"))
      .build();

    ImageRequest request = ImageRequest
      .builder()
      .prompt("Beautiful house on country side")
      .build();

    ImageResponse response = client.imagesGenerations(request).execute();

    // remote image generated with model DALL·E 3 and resolution 1024x1024
    String remoteImage = response.data().get(0).url();

Customizable way:

    OpenAiClient client = OpenAiClient
      .builder()
      .openAiApiKey(System.getenv("OPENAI_API_KEY"))
      .logRequests()
      .logResponses()
      .withPersisting()
      .build();

    ImageRequest request = ImageRequest
      .builder()
      .model(DALL_E_3)
      .size(DALL_E_SIZE_1792_x_1024)
      .quality(DALL_E_QUALITY_HD)
      .responseFormat(DALL_E_RESPONSE_FORMAT_B64_JSON) // if you like to get the image within the response body
      .prompt("Cute red parrot flying in the sky")
      .build();

    ImageResponse response = client.imagesGenerations(request).execute();

    // your generated image is here locally: 
    String localImage = response.data().get(0).url();

Useful materials