Skip to content

ebanma-tech/openai4j

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

168 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Java client library for OpenAI API

This is an unofficial Java client library that helps to connect your Java applications with OpenAI API. It was developed by AI for Java, but is no longer maintained. This is a fork of the original library, with some new features by Banma Network developers.

What's New in 0.30.0

  • Support Alibaba Qwen-omni models with audio and video as input
  • Support enabled / disable Alibaba Qwen3 thinking mode using extra_body (like what's in python client)
  • Support run Prompty
  • Support Deepseek-R1 model reasoning content
  • Added request_id in ChatCompletionRequest for tracing support
  • Added BUILD_FUNCTION for kimi model support

Current capabilities:

Coming soon:

Start using

Maven:

<dependency>
    <groupId>com.ebanma</groupId>
    <artifactId>openai4j</artifactId>
    <version>0.30.0</version>
</dependency>

Gradle:

implementation 'com.ebanma:openai4j:0.30.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();

Qwen Omin Model

Audio Support

    OpenAiClient client = OpenAiClient.builder()
        .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
        .openAiApiKey("<your api key>")
        .logRequests()
        .logResponses()
        .build();
        
    List<Message> messages = new ArrayList<>();

    CustomMessage sys = CustomMessage.builder().role(Role.SYSTEM).addContent(Content.builder().type(ContentType.TEXT).text("You are a helpful assistant.").build()).build();
        messages.add(sys);

    InputAudio audio = InputAudio.builder()
        .data("https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20250211/tixcef/cherry.wav")
        .format("wav")
        .build();
    CustomMessage user = CustomMessage.builder().role(Role.USER)
        .addContent(Content.builder().type(ContentType.INPUT_AUDIO).inputAudio(audio).build())
        .addContent(Content.builder().type(ContentType.TEXT).text("这段音频在说什么?").build())
        .build();
        messages.add(user);

    ChatCompletionRequest request = ChatCompletionRequest.builder()
        .model("qwen-omni-turbo")
        .stream(true)
        .modalities(asList("text", "audio"))
        .audio(audio())
        .messages(messages)
        .requestId(UUID.randomUUID().toString())
        .build();
        
    client.chatCompletion(request)        

See more examples with image, audio and video in src/test/java/dev/ai4j/openai4j/chat/QwenOmniTest.java

Qwen3 Thinking Mode

Disable Thinking

    OpenAiClient client = OpenAiClient.builder()
        .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
        .openAiApiKey("<your api key>")
        .logRequests()
        .logResponses()
        .build();
        
        Map<String, Object> extraBody = new HashMap<>();
        extraBody.put("enable_thinking", Boolean.FALSE);

        ChatCompletionRequest request = ChatCompletionRequest
            .builder()
            .model("qwen3-8b")
            .requestId(UUID.randomUUID().toString().replace("-", ""))
            .stream(true)
            .extraBody(extraBody)
            .messages(UserMessage.from("你是谁?"))
            .build();

        StringBuilder content = new StringBuilder();
        StringBuilder reasoning_content = new StringBuilder();
        client.chatCompletion(request)

See more examples in src/test/java/dev/ai4j/openai4j/chat/QwenThinkingTest.java

Useful materials

About

Java client library for OpenAI API

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%