Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
O

openai4j

> AI 编程
Open source

Java client library for OpenAI API.Full support for all OpenAI API models including Completions, Chat, Edits, Embeddings, Audio, Files, Assistants-v2, Images, M

375 stars0 likes0 views
WebsiteGitHub

About

Java client library for OpenAI API.Full support for all OpenAI API models including Completions, Chat, Edits, Embeddings, Audio, Files, Assistants-v2, Images, M

OpenAi4J

OpenAi4J is an unofficial Java library tailored to facilitate the interaction with OpenAI's GPT models, including the newest additions such as gpt4-turbo vision,assistant-v2. Originally forked from TheoKanning/openai-java, this library continues development to incorporate latest API features after the original project's maintenance was discontinued.

中文介绍☕

Features

  • Full support for all OpenAI API models including Completions, Chat, Edits, Embeddings, Audio, Files, Assistants-v2, Images, Moderations, Batch, and Fine-tuning.
  • Easy-to-use client setup with Retrofit for immediate API interaction.
  • Extensive examples and documentation to help you start quickly.
  • Customizable setup with environment variable integration for API keys and base URLs.
  • Supports synchronous and asynchronous API calls.

This library aims to provide Java developers with a robust tool to integrate OpenAI's powerful capabilities into their applications effortlessly.

Quick Start

Import

Gradle

implementation 'io.github.lambdua::0.22.92'

Maven

xml

  io.github.lambdua
  service
    0.22.92

chat with OpenAi model

…

Just Using POJO

If you wish to develop your own client, simply import POJOs from the api module. Ensure your client adopts snake case naming for compatibility with the OpenAI API. To utilize pojos, import the api module:

xml

  io.github.lambdua
  api
    0.22.92

other examples:

The sample code is all in the example package, which includes most of the functional usage. You can refer to the code in the example package. Below are some commonly used feature usage examples

gpt-vision image recognition

…

Customizing OpenAiService OpenAiService is versatile in its setup options, as demonstrated in the example.ServiceCreateExample within the example package.

…

stream chat

…

Tools This library supports both the outdated method of function calls and the current tool-based approach.

First, we define a function object. The definition of a function object is flexible; you can use POJO to define it ( automatically serialized by JSON schema) or use methods like map and FunctionDefinition to define it. You can refer to the code in the example package. Here, we define a weather query function object:

java
public class Weather {
    @JsonPropertyDescription("City and state, for example: León, Guanajuato")
    public String location;
    @JsonPropertyDescription("The temperature unit, can be 'celsius' or 'fahrenheit'")
    @JsonProperty(required = true)
    public WeatherUnit unit;
}
public enum WeatherUnit {
    CELSIUS, FAHRENHEIT;
}
public static class WeatherResponse {
    public String location;
    public WeatherUnit unit;
    public int temperature;
    public String description;
    
    // constructor
}

Next, we declare the function and associate it with an executor, here simulating an API response:

java
//First, a function to fetch the weather
public static FunctionDefinition weatherFunction() {
    return FunctionDefinition.builder()
            .name("get_weather")
            .description("Get the current weather in a given location")
            .parametersDefinitionByClass(Weather.class)
            //The executor here is a lambda expression that accepts a Weather object and returns a Weather Response object
            .executor(w -> new WeatherResponse(w.location, w.unit, 25, "sunny"))
            .build();
}

Then, the service is used for a chatCompletion request, incorporating the tool:

…

stream chat with tool call (support Concurrent tool call)

…

Assistant Tool Call

…

Assistant Stream

…

Assistant Stream Manager

By using the AssistantEventHandler class and the AssistantStreamManager class, it is easier to manage the streaming calls of Assistant AssistantEventHandler contains all Assistant stream event callback hooks, and you can implement different events as needed:

…

AssistantStreamManager arranges and manages various events in the stream, supporting synchronous/asynchronous retrieval of content from the stream, which can be obtained through the manager. Below is a usage example, for more examples, please refer to AssistantStreamManagerTest.java.

…
  • Assistant iamge chat

FAQs

Is it possible to customize the OpenAI URL or use a proxy URL?

Yes, you can specify a URL when constructing OpenAiService, which will serve as the base URL.But we recommend using the environment variable OPENAI_API_BASE_URL and OPENAI_API_KEY to load the OpenAI API key.

Why am I experiencing connection timeouts?

Ensure your network is stable and your OpenAI server is accessible. If you face network instability, consider increasing the timeout duration.

Contributing to OpenAi4J

We welcome contributions from the community and are always looking for ways to make our project better. If you're interested in helping improve OpenAi4J, here are some ways you can contribute:

Reporting Issues

Please use the GitHub Issues page to report issues. Be as specific as possible about how to reproduce the problem you are having, and include details like your operating system, Java version, and any relevant stack traces.

Submitting Pull Requests

  1. Fork the repository and create your branch from main.
  2. If you've added code that should be tested, add tests.
  3. Ensure your code lints and adheres to the existing style guidelines.
  4. Write a clear log message for your commits. One-line messages are fine for small changes, but bigger changes should have detailed descriptions.
  5. Complete the pull request form, linking to any issues your PR addresses.

Support Us

We hope you find this library useful! If you do, consider giving it a star on library❤️❤️❤️. Your support helps us keep the project alive and continuously improve it. Stay tuned for updates and feel free to contribute to the project or suggest new features. Thank you for supporting OpenAi4J!

Contributors

License

Released under the MIT License

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Javaassistants-apichatgptchatgpt4gpt-4o

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
CategoryAI 编程
PricingOpen source

> Related tools

G
GitHub Copilot
GitHub 官方 AI 编程助手,覆盖补全、Chat 与 Agent 模式。
C
Cursor
AI 原生代码编辑器,对话改代码、多文件 Agent 与规则体系是其核心。
S
skills
Skills for Real Engineers. Straight from my .agents directory.