Skip to content

Commit a7ff995

Browse files
release: 0.22.1 (#214)
* 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>
1 parent 6390dfd commit a7ff995

File tree

6 files changed

+160
-6
lines changed

6 files changed

+160
-6
lines changed

.release-please-manifest.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
".": "0.22.0"
2+
".": "0.22.1"
33
}

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22

3+
## 0.22.1 (2025-02-12)
4+
5+
Full Changelog: [v0.22.0...v0.22.1](https://github.com/openai/openai-java/compare/v0.22.0...v0.22.1)
6+
7+
### Documentation
8+
9+
* function calling example ([#213](https://github.com/openai/openai-java/issues/213)) ([89700ec](https://github.com/openai/openai-java/commit/89700ecff1ec8e0d952bc7c77fcc3e2dd20b8a63))
10+
311
## 0.22.0 (2025-02-06)
412

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

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
1010
<!-- x-release-please-start-version -->
1111

12-
[![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)
13-
[![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)
12+
[![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)
13+
[![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)
1414

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

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

2727
```kotlin
28-
implementation("com.openai:openai-java:0.22.0")
28+
implementation("com.openai:openai-java:0.22.1")
2929
```
3030

3131
### Maven
@@ -34,7 +34,7 @@ implementation("com.openai:openai-java:0.22.0")
3434
<dependency>
3535
<groupId>com.openai</groupId>
3636
<artifactId>openai-java</artifactId>
37-
<version>0.22.0</version>
37+
<version>0.22.1</version>
3838
</dependency>
3939
```
4040

build.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ repositories {
88

99
allprojects {
1010
group = "com.openai"
11-
version = "0.22.0" // x-release-please-version
11+
version = "0.22.1" // x-release-please-version
1212
}
1313

1414
subprojects {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.openai.example;
2+
3+
import static com.openai.core.ObjectMappers.jsonMapper;
4+
5+
import com.fasterxml.jackson.core.JsonProcessingException;
6+
import com.openai.client.OpenAIClientAsync;
7+
import com.openai.client.okhttp.OpenAIOkHttpClientAsync;
8+
import com.openai.core.JsonObject;
9+
import com.openai.core.JsonValue;
10+
import com.openai.models.*;
11+
import java.util.Collection;
12+
import java.util.List;
13+
import java.util.Map;
14+
15+
public final class FunctionCallingAsyncExample {
16+
private FunctionCallingAsyncExample() {}
17+
18+
public static void main(String[] args) {
19+
// Configures using one of:
20+
// - The `OPENAI_API_KEY` environment variable
21+
// - The `AZURE_OPENAI_ENDPOINT` and `AZURE_OPENAI_KEY` environment variables
22+
OpenAIClientAsync client = OpenAIOkHttpClientAsync.fromEnv();
23+
24+
ChatCompletionCreateParams createParams = ChatCompletionCreateParams.builder()
25+
.model(ChatModel.GPT_3_5_TURBO)
26+
.maxCompletionTokens(2048)
27+
.addTool(ChatCompletionTool.builder()
28+
.function(FunctionDefinition.builder()
29+
.name("get-sdk-quality")
30+
.description("Gets the quality of the given SDK.")
31+
.parameters(FunctionParameters.builder()
32+
.putAdditionalProperty("type", JsonValue.from("object"))
33+
.putAdditionalProperty(
34+
"properties", JsonValue.from(Map.of("name", Map.of("type", "string"))))
35+
.putAdditionalProperty("required", JsonValue.from(List.of("name")))
36+
.putAdditionalProperty("additionalProperties", JsonValue.from(false))
37+
.build())
38+
.build())
39+
.build())
40+
.addUserMessage("How good are the following SDKs: OpenAI Java SDK, Unknown Company SDK")
41+
.build();
42+
43+
client.chat()
44+
.completions()
45+
.create(createParams)
46+
.thenAccept(completion -> completion.choices().stream()
47+
.map(ChatCompletion.Choice::message)
48+
.flatMap(message -> {
49+
message.content().ifPresent(System.out::println);
50+
return message.toolCalls().stream().flatMap(Collection::stream);
51+
})
52+
.forEach(toolCall -> System.out.println(callFunction(toolCall.function()))))
53+
.join();
54+
}
55+
56+
private static String callFunction(ChatCompletionMessageToolCall.Function function) {
57+
if (!function.name().equals("get-sdk-quality")) {
58+
throw new IllegalArgumentException("Unknown function: " + function.name());
59+
}
60+
61+
JsonValue arguments;
62+
try {
63+
arguments = JsonValue.from(jsonMapper().readTree(function.arguments()));
64+
} catch (JsonProcessingException e) {
65+
throw new IllegalArgumentException("Bad function arguments", e);
66+
}
67+
68+
String sdkName = ((JsonObject) arguments).values().get("name").asStringOrThrow();
69+
if (sdkName.contains("OpenAI")) {
70+
return sdkName + ": It's robust and polished!";
71+
}
72+
73+
return sdkName + ": *shrug*";
74+
}
75+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package com.openai.example;
2+
3+
import static com.openai.core.ObjectMappers.jsonMapper;
4+
5+
import com.fasterxml.jackson.core.JsonProcessingException;
6+
import com.openai.client.OpenAIClient;
7+
import com.openai.client.okhttp.OpenAIOkHttpClient;
8+
import com.openai.core.JsonObject;
9+
import com.openai.core.JsonValue;
10+
import com.openai.models.*;
11+
import java.util.Collection;
12+
import java.util.List;
13+
import java.util.Map;
14+
15+
public final class FunctionCallingExample {
16+
private FunctionCallingExample() {}
17+
18+
public static void main(String[] args) {
19+
// Configures using one of:
20+
// - The `OPENAI_API_KEY` environment variable
21+
// - The `AZURE_OPENAI_ENDPOINT` and `AZURE_OPENAI_KEY` environment variables
22+
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
23+
24+
ChatCompletionCreateParams createParams = ChatCompletionCreateParams.builder()
25+
.model(ChatModel.GPT_3_5_TURBO)
26+
.maxCompletionTokens(2048)
27+
.addTool(ChatCompletionTool.builder()
28+
.function(FunctionDefinition.builder()
29+
.name("get-sdk-quality")
30+
.description("Gets the quality of the given SDK.")
31+
.parameters(FunctionParameters.builder()
32+
.putAdditionalProperty("type", JsonValue.from("object"))
33+
.putAdditionalProperty(
34+
"properties", JsonValue.from(Map.of("name", Map.of("type", "string"))))
35+
.putAdditionalProperty("required", JsonValue.from(List.of("name")))
36+
.putAdditionalProperty("additionalProperties", JsonValue.from(false))
37+
.build())
38+
.build())
39+
.build())
40+
.addUserMessage("How good are the following SDKs: OpenAI Java SDK, Unknown Company SDK")
41+
.build();
42+
43+
client.chat().completions().create(createParams).choices().stream()
44+
.map(ChatCompletion.Choice::message)
45+
.flatMap(message -> {
46+
message.content().ifPresent(System.out::println);
47+
return message.toolCalls().stream().flatMap(Collection::stream);
48+
})
49+
.forEach(toolCall -> System.out.println(callFunction(toolCall.function())));
50+
}
51+
52+
private static String callFunction(ChatCompletionMessageToolCall.Function function) {
53+
if (!function.name().equals("get-sdk-quality")) {
54+
throw new IllegalArgumentException("Unknown function: " + function.name());
55+
}
56+
57+
JsonValue arguments;
58+
try {
59+
arguments = JsonValue.from(jsonMapper().readTree(function.arguments()));
60+
} catch (JsonProcessingException e) {
61+
throw new IllegalArgumentException("Bad function arguments", e);
62+
}
63+
64+
String sdkName = ((JsonObject) arguments).values().get("name").asStringOrThrow();
65+
if (sdkName.contains("OpenAI")) {
66+
return sdkName + ": It's robust and polished!";
67+
}
68+
69+
return sdkName + ": *shrug*";
70+
}
71+
}

0 commit comments

Comments
 (0)