Skip to content

Commit

Permalink
release: 0.22.1 (#214)
Browse files Browse the repository at this point in the history
* docs: function calling example (#213)

* release: 0.22.1

---------

Co-authored-by: Tomer Aberbach <[email protected]>
Co-authored-by: stainless-app[bot] <142633134+stainless-app[bot]@users.noreply.github.com>
  • Loading branch information
stainless-app[bot] and TomerAberbach authored Feb 13, 2025
1 parent 6390dfd commit a7ff995
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .release-please-manifest.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
".": "0.22.0"
".": "0.22.1"
}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 0.22.1 (2025-02-12)

Full Changelog: [v0.22.0...v0.22.1](https://github.com/openai/openai-java/compare/v0.22.0...v0.22.1)

### Documentation

* function calling example ([#213](https://github.com/openai/openai-java/issues/213)) ([89700ec](https://github.com/openai/openai-java/commit/89700ecff1ec8e0d952bc7c77fcc3e2dd20b8a63))

## 0.22.0 (2025-02-06)

Full Changelog: [v0.21.1...v0.22.0](https://github.com/openai/openai-java/compare/v0.21.1...v0.22.0)
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<!-- x-release-please-start-version -->

[![Maven Central](https://img.shields.io/maven-central/v/com.openai/openai-java)](https://central.sonatype.com/artifact/com.openai/openai-java/0.22.0)
[![javadoc](https://javadoc.io/badge2/com.openai/openai-java/0.22.0/javadoc.svg)](https://javadoc.io/doc/com.openai/openai-java/0.22.0)
[![Maven Central](https://img.shields.io/maven-central/v/com.openai/openai-java)](https://central.sonatype.com/artifact/com.openai/openai-java/0.22.1)
[![javadoc](https://javadoc.io/badge2/com.openai/openai-java/0.22.1/javadoc.svg)](https://javadoc.io/doc/com.openai/openai-java/0.22.1)

<!-- x-release-please-end -->

Expand All @@ -25,7 +25,7 @@ The REST API documentation can be found on [platform.openai.com](https://platfor
### Gradle

```kotlin
implementation("com.openai:openai-java:0.22.0")
implementation("com.openai:openai-java:0.22.1")
```

### Maven
Expand All @@ -34,7 +34,7 @@ implementation("com.openai:openai-java:0.22.0")
<dependency>
<groupId>com.openai</groupId>
<artifactId>openai-java</artifactId>
<version>0.22.0</version>
<version>0.22.1</version>
</dependency>
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ repositories {

allprojects {
group = "com.openai"
version = "0.22.0" // x-release-please-version
version = "0.22.1" // x-release-please-version
}

subprojects {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.openai.example;

import static com.openai.core.ObjectMappers.jsonMapper;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.openai.client.OpenAIClientAsync;
import com.openai.client.okhttp.OpenAIOkHttpClientAsync;
import com.openai.core.JsonObject;
import com.openai.core.JsonValue;
import com.openai.models.*;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public final class FunctionCallingAsyncExample {
private FunctionCallingAsyncExample() {}

public static void main(String[] args) {
// Configures using one of:
// - The `OPENAI_API_KEY` environment variable
// - The `AZURE_OPENAI_ENDPOINT` and `AZURE_OPENAI_KEY` environment variables
OpenAIClientAsync client = OpenAIOkHttpClientAsync.fromEnv();

ChatCompletionCreateParams createParams = ChatCompletionCreateParams.builder()
.model(ChatModel.GPT_3_5_TURBO)
.maxCompletionTokens(2048)
.addTool(ChatCompletionTool.builder()
.function(FunctionDefinition.builder()
.name("get-sdk-quality")
.description("Gets the quality of the given SDK.")
.parameters(FunctionParameters.builder()
.putAdditionalProperty("type", JsonValue.from("object"))
.putAdditionalProperty(
"properties", JsonValue.from(Map.of("name", Map.of("type", "string"))))
.putAdditionalProperty("required", JsonValue.from(List.of("name")))
.putAdditionalProperty("additionalProperties", JsonValue.from(false))
.build())
.build())
.build())
.addUserMessage("How good are the following SDKs: OpenAI Java SDK, Unknown Company SDK")
.build();

client.chat()
.completions()
.create(createParams)
.thenAccept(completion -> completion.choices().stream()
.map(ChatCompletion.Choice::message)
.flatMap(message -> {
message.content().ifPresent(System.out::println);
return message.toolCalls().stream().flatMap(Collection::stream);
})
.forEach(toolCall -> System.out.println(callFunction(toolCall.function()))))
.join();
}

private static String callFunction(ChatCompletionMessageToolCall.Function function) {
if (!function.name().equals("get-sdk-quality")) {
throw new IllegalArgumentException("Unknown function: " + function.name());
}

JsonValue arguments;
try {
arguments = JsonValue.from(jsonMapper().readTree(function.arguments()));
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Bad function arguments", e);
}

String sdkName = ((JsonObject) arguments).values().get("name").asStringOrThrow();
if (sdkName.contains("OpenAI")) {
return sdkName + ": It's robust and polished!";
}

return sdkName + ": *shrug*";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package com.openai.example;

import static com.openai.core.ObjectMappers.jsonMapper;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.core.JsonObject;
import com.openai.core.JsonValue;
import com.openai.models.*;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public final class FunctionCallingExample {
private FunctionCallingExample() {}

public static void main(String[] args) {
// Configures using one of:
// - The `OPENAI_API_KEY` environment variable
// - The `AZURE_OPENAI_ENDPOINT` and `AZURE_OPENAI_KEY` environment variables
OpenAIClient client = OpenAIOkHttpClient.fromEnv();

ChatCompletionCreateParams createParams = ChatCompletionCreateParams.builder()
.model(ChatModel.GPT_3_5_TURBO)
.maxCompletionTokens(2048)
.addTool(ChatCompletionTool.builder()
.function(FunctionDefinition.builder()
.name("get-sdk-quality")
.description("Gets the quality of the given SDK.")
.parameters(FunctionParameters.builder()
.putAdditionalProperty("type", JsonValue.from("object"))
.putAdditionalProperty(
"properties", JsonValue.from(Map.of("name", Map.of("type", "string"))))
.putAdditionalProperty("required", JsonValue.from(List.of("name")))
.putAdditionalProperty("additionalProperties", JsonValue.from(false))
.build())
.build())
.build())
.addUserMessage("How good are the following SDKs: OpenAI Java SDK, Unknown Company SDK")
.build();

client.chat().completions().create(createParams).choices().stream()
.map(ChatCompletion.Choice::message)
.flatMap(message -> {
message.content().ifPresent(System.out::println);
return message.toolCalls().stream().flatMap(Collection::stream);
})
.forEach(toolCall -> System.out.println(callFunction(toolCall.function())));
}

private static String callFunction(ChatCompletionMessageToolCall.Function function) {
if (!function.name().equals("get-sdk-quality")) {
throw new IllegalArgumentException("Unknown function: " + function.name());
}

JsonValue arguments;
try {
arguments = JsonValue.from(jsonMapper().readTree(function.arguments()));
} catch (JsonProcessingException e) {
throw new IllegalArgumentException("Bad function arguments", e);
}

String sdkName = ((JsonObject) arguments).values().get("name").asStringOrThrow();
if (sdkName.contains("OpenAI")) {
return sdkName + ": It's robust and polished!";
}

return sdkName + ": *shrug*";
}
}

0 comments on commit a7ff995

Please sign in to comment.