Skip to content

Commit 03c1628

Browse files
committed
fn-calling: raw example for Responses API.
1 parent 3b65bd6 commit 03c1628

File tree

2 files changed

+137
-2
lines changed

2 files changed

+137
-2
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,7 +615,7 @@ import com.openai.client.okhttp.OpenAIOkHttpClient;
615615
import com.openai.models.ChatModel;
616616
import com.openai.models.chat.completions.*;
617617
import java.util.Collection;
618-
618+
619619
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
620620

621621
ChatCompletionCreateParams.Builder createParamsBuilder = ChatCompletionCreateParams.builder()
@@ -678,7 +678,12 @@ class using
678678
[`ResponseFunctionToolCall.arguments(Class<T>)`](openai-java-core/src/main/kotlin/com/openai/models/responses/ResponseFunctionToolCall.kt).
679679
Finally, pass the result of each call back to the model.
680680

681-
For a full example of the usage of _Function Calling_ with the Responses API, see
681+
For a full example of the usage of _Function Calling_ with the Responses API using the low-level
682+
API to define and parse function parameters, see
683+
[`ResponsesFunctionCallingRawExample`](openai-java-example/src/main/java/com/openai/example/ResponsesFunctionCallingRawExample.java).
684+
685+
For a full example of the usage of _Function Calling_ with the Responses API using Java classes to
686+
define and parse function parameters, see
682687
[`ResponsesFunctionCallingExample`](openai-java-example/src/main/java/com/openai/example/ResponsesFunctionCallingExample.java).
683688

684689
### Local function JSON schema validation
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package com.openai.example;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.openai.client.OpenAIClient;
6+
import com.openai.client.okhttp.OpenAIOkHttpClient;
7+
import com.openai.core.JsonObject;
8+
import com.openai.core.JsonValue;
9+
import com.openai.models.ChatModel;
10+
import com.openai.models.responses.FunctionTool;
11+
import com.openai.models.responses.ResponseCreateParams;
12+
import com.openai.models.responses.ResponseFunctionToolCall;
13+
import com.openai.models.responses.ResponseInputItem;
14+
import java.util.ArrayList;
15+
import java.util.List;
16+
import java.util.Map;
17+
18+
public final class ResponsesFunctionCallingRawExample {
19+
private ResponsesFunctionCallingRawExample() {}
20+
21+
static class SdkQuality {
22+
public String quality;
23+
24+
public SdkQuality(String name, String evaluation) {
25+
quality = name + ": " + evaluation;
26+
}
27+
}
28+
29+
public static void main(String[] args) {
30+
// Configures using one of:
31+
// - The `OPENAI_API_KEY` environment variable
32+
// - The `OPENAI_BASE_URL` and `AZURE_OPENAI_KEY` environment variables
33+
OpenAIClient client = OpenAIOkHttpClient.fromEnv();
34+
List<ResponseInputItem> inputs = new ArrayList<>();
35+
36+
inputs.add(ResponseInputItem.ofMessage(ResponseInputItem.Message.builder()
37+
.addInputTextContent("What is the quality of the following SDKs and what do reviewers say: "
38+
+ "OpenAI Java SDK, Unknown Company SDK.")
39+
.role(ResponseInputItem.Message.Role.USER)
40+
.build()));
41+
42+
// Use a `Builder` so that more messages can be appended below. When `build()` is called, it
43+
// creates an immutable object that is unaffected by future mutations of the builder.
44+
ResponseCreateParams.Builder createParamsBuilder = ResponseCreateParams.builder()
45+
.model(ChatModel.GPT_3_5_TURBO)
46+
.addTool(FunctionTool.builder()
47+
.name("get-sdk-quality")
48+
.description("Gets the quality of the given SDK.")
49+
.parameters(FunctionTool.Parameters.builder()
50+
.putAdditionalProperty("type", JsonValue.from("object"))
51+
.putAdditionalProperty(
52+
"properties",
53+
JsonValue.from(Map.of(
54+
"name",
55+
Map.of("type", "string", "description", "The name of the SDK."))))
56+
.putAdditionalProperty("required", JsonValue.from(List.of("name")))
57+
.putAdditionalProperty("additionalProperties", JsonValue.from(false))
58+
.build())
59+
.strict(true)
60+
.build())
61+
.addTool(FunctionTool.builder()
62+
.name("get-sdk-score")
63+
.description("Gets the review score (out of 10) for the given SDK.")
64+
.parameters(FunctionTool.Parameters.builder()
65+
.putAdditionalProperty("type", JsonValue.from("object"))
66+
.putAdditionalProperty(
67+
"properties", JsonValue.from(Map.of("name", Map.of("type", "string"))))
68+
.putAdditionalProperty("required", JsonValue.from(List.of("name")))
69+
.putAdditionalProperty("additionalProperties", JsonValue.from(false))
70+
.build())
71+
.strict(true)
72+
.build())
73+
.maxOutputTokens(2048)
74+
.input(ResponseCreateParams.Input.ofResponse(inputs));
75+
76+
client.responses().create(createParamsBuilder.build()).output().forEach(item -> {
77+
if (item.isFunctionCall()) {
78+
ResponseFunctionToolCall functionCall = item.asFunctionCall();
79+
80+
inputs.add(ResponseInputItem.ofFunctionCall(functionCall));
81+
inputs.add(ResponseInputItem.ofFunctionCallOutput(ResponseInputItem.FunctionCallOutput.builder()
82+
.callId(functionCall.callId())
83+
.output(callFunction(functionCall))
84+
.build()));
85+
}
86+
});
87+
88+
// Pass the function call results back to the model to complete the process.
89+
createParamsBuilder.input(ResponseCreateParams.Input.ofResponse(inputs));
90+
91+
client.responses().create(createParamsBuilder.build()).output().stream()
92+
.flatMap(item -> item.message().stream())
93+
.flatMap(message -> message.content().stream())
94+
.flatMap(content -> content.outputText().stream())
95+
.forEach(outputText -> System.out.println(outputText.text()));
96+
}
97+
98+
private static String callFunction(ResponseFunctionToolCall function) {
99+
ObjectMapper mapper = new ObjectMapper();
100+
JsonValue arguments;
101+
102+
try {
103+
arguments = JsonValue.from(mapper.readTree(function.arguments()));
104+
} catch (JsonProcessingException e) {
105+
throw new IllegalArgumentException("Bad function arguments", e);
106+
}
107+
108+
String sdkName = ((JsonObject) arguments).values().get("name").asStringOrThrow();
109+
Object result;
110+
111+
switch (function.name()) {
112+
case "get-sdk-quality":
113+
result = new SdkQuality(sdkName, sdkName.contains("OpenAI") ? "It's robust and polished!" : "*shrug*");
114+
break;
115+
116+
case "get-sdk-score":
117+
result = sdkName.contains("OpenAI") ? 10 : 3;
118+
break;
119+
120+
default:
121+
throw new IllegalArgumentException("Unknown function: " + function.name());
122+
}
123+
124+
try {
125+
return mapper.writeValueAsString(result);
126+
} catch (JsonProcessingException e) {
127+
throw new IllegalArgumentException("Bad function result", e);
128+
}
129+
}
130+
}

0 commit comments

Comments
 (0)