Java client library for OpenAI API.Full support for all OpenAI API models including Completions, Chat, Edits, Embeddings, Audio, Files, Assistants-v2, Images, M
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 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.
This library aims to provide Java developers with a robust tool to integrate OpenAI's powerful capabilities into their applications effortlessly.
implementation 'io.github.lambdua::0.22.92'
io.github.lambdua
service
0.22.92
…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:
io.github.lambdua
api
0.22.92
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:
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:
//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.
…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.
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:
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.
main.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!
Released under the MIT License
No open issues yet, or sync has not completed.