OpenAI now provides an official Java SDK: https://github.com/openai/openai-java
Unofficial, community-maintained Java implementation of the OpenAI API:
https://platform.openai.com/docs/api-reference
openai4j is a Java library that implements most documented endpoints as of 16. April 2024,
including the endpoints that are only available in beta.
Legacy, deprecated and streaming endpoints are not implemented.
<dependency>
<groupId>ch.rasc</groupId>
<artifactId>openai4j</artifactId>
<version>1.3.6</version>
</dependency>
String apiKey = ... // read OpenAI api key from environment variable
var client = OpenAIClient.create(c -> c.apiKey(apiKey));
var response = client.chatCompletions.create(r -> r
.addMessages(SystemMessage.of("You are a helpful assistant"),
UserMessage.of("What is the capital of Spain?"))
.model("gpt-4o"));
String response = response.choices().get(0).message().content();
public class ChatCompletionsFunctionExample {
private final static ObjectMapper om = new ObjectMapper();
static class Location {
@JsonProperty(required = true)
@JsonPropertyDescription("Latitude of the location. Geographical WGS84 coordinates")
public float latitude;
@JsonProperty(required = true)
@JsonPropertyDescription("Longitude of the location. Geographical WGS84 coordinates")
public float longitude;
}
static class TemperatureFetcher {
public Float fetchTemperature(Location location) {
System.out.println("calling fetchTemperature");
try (var client = HttpClient.newHttpClient()) {
var request = HttpRequest.newBuilder()
.uri(URI.create("https://api.open-meteo.com/v1/metno?latitude="
+ location.latitude + "&longitude=" + location.longitude
+ "¤t=temperature_2m"))
.build();
var response = client.send(request,
java.net.http.HttpResponse.BodyHandlers.ofString());
var body = response.body();
System.out.println(body);
var jsonNode = om.readTree(body);
var current = jsonNode.get("current");
var temperature = current.get("temperature_2m");
return temperature.floatValue();
}
catch (IOException | InterruptedException e) {
throw new RuntimeException(e);
}
}
}
public static void main(String[] args) throws JsonProcessingException {
String apiKey = ... // read OpenAI api key from environment variable
var client = OpenAIClient.create(c -> c.apiKey(apiKey));
TemperatureFetcher fetcher = new TemperatureFetcher();
String functionName = "get_temperature";
JavaFunction<Location, Float> getWeather = JavaFunction.of(functionName,
"Get the current temperature of a location", Location.class,
fetcher::fetchTemperature);
var service = new ChatCompletionsService(client.chatCompletions, om);
var response = service.createJavaFunctions(r -> r.addMessages(UserMessage.of(
"What are the current temperatures in Oslo, Norway and Helsinki, Finland?"))
.model("gpt-4o").javaFunctions(List.of(getWeather)));
var choice = response.choices().get(0);
System.out.println(choice.message().content());
}
}
String apiKey = ... // read OpenAI api key from environment variable
var client = OpenAIClient.create(c -> c.apiKey(apiKey));
Assistant assistant = client.assistants.create(c -> c.name("Math Tutor").instructions(
"You are a personal math tutor. Write and run code to answer math questions.")
.addTools(CodeTool.of()).model("gpt-4o"));
}
var thread = client.threads.create();
var message = client.threadsMessages.create(thread.id(), c -> c.content(
"I need to solve the equation `3x + 11 = 14`. Can you help me?"));
var run = client.threadsRuns.create(thread.id(),
c -> c.assistantId(assistant.id()).instructions(
"Please address the user as Jane Doe. The user has a premium account."));
client.threadsRuns.waitForProcessing(run, 30, TimeUnit.SECONDS, 2, TimeUnit.MINUTES);
var messages = client.threadsMessages.list(thread.id(), p -> p.before(message.id()));
for (var msg : messages.data()) {
var content = msg.content().get(0);
if (content instanceof MessageContentText text) {
System.out.println(text.text().value());
}
}
String apiKey = ... // read OpenAI api key from environment variable
String azureEndpoint = ... // "https://myresource.openai.azure.com/"
var azureClient = OpenAIClient
.create(Configuration.builder()
.apiVersion("2024-02-01")
.apiKey(apiKey)
.azureDeployment("gpt-35-turbo")
.azureEndpoint(azureEndpoint)
.build());
Check out the openai4j-examples repository for more examples.
- Add support for Predicted Outputs
- Support for gpt-4o-audio-preview model for chat completions, which supports both audio inputs and outputs.
- Add audioTokens and cachedTokens to the usage object of ChatCompletionResponse
- Add store and metadata field to ChatCompletionRequest
- Add completion_tokens_details and reasoning_tokens to usage
- Including file search results used by the file search tool, and customizing ranking behavior.
- Added support for Structured Outputs
- Added support for Uploads API
- Parallel function calling can be disabled in Chat Completions and the Assistants API by passing parallel_tool_calls=false.
- Added support for file search customizations.
- Support for Assistants API v2
- Added support for the Batch API
- Added support for seed in the fine-tuning API
- Added support for checkpoints in the fine-tuning API
- Added support for adding Messages when creating a Run in the Assistants API
- Added support for temperature and assistant message creation in the Assistants API
- Added timestamp_granularities parameter to the Audio API
- Added dimensions parameter to the Embeddings API
- Added additional_instructions parameter to run creation in the Assistants API
- Added logprobs and top_logprobs parameters to the Chat Completions API
- Changed function parameters argument on a tool call to be optional.
- Refactor all the builders
- Add ChatCompletionsService for calling Java functions and returning Java models
- More JavaDoc
- Add
name
property to Chat Completions SystemMessage and UserMessage - More JavaDoc
- Replace arrays with List
- Bugfix: SubmitToolOutputs not properly JSON decoded
- Bugfix: waitForProcessing in ThreadsRuns must return the last ThreadRun object
- Bugfix: ChatCompletions UserMessage for GPT Vision
- Initial release
Code released under the Apache license.