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