Skip to content

Commit 4aa87e6

Browse files
committed
Improve sample code
1 parent 00f93cc commit 4aa87e6

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/OpenAiService.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatCompletionParameters;
1919
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatCompletionTool;
2020
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatMessage;
21+
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiChatToolCall;
2122
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiEmbeddingOutput;
2223
import com.sap.ai.sdk.foundationmodels.openai.model.OpenAiEmbeddingParameters;
2324
import java.util.ArrayList;
@@ -103,7 +104,8 @@ public OpenAiChatCompletionOutput chatCompletionImage(@Nonnull final String link
103104
public OpenAiChatCompletionOutput chatCompletionToolExecution(
104105
@Nonnull final String location, @Nonnull final String unit) {
105106

106-
final var schemaMap = generateSchema(WeatherMethod.Request.class);
107+
// 1. Define the function
108+
final Map<String, Object> schemaMap = generateSchema(WeatherMethod.Request.class);
107109
final var function =
108110
new OpenAiChatCompletionFunction()
109111
.setName("weather")
@@ -116,19 +118,24 @@ public OpenAiChatCompletionOutput chatCompletionToolExecution(
116118
new OpenAiChatMessage.OpenAiChatUserMessage()
117119
.addText("What's the weather in %s in %s?".formatted(location, unit)));
118120

121+
// Assistant will call the function
119122
final var request =
120123
new OpenAiChatCompletionParameters()
121124
.addMessages(messages.toArray(OpenAiChatMessage[]::new))
122125
.setTools(List.of(tool));
123126

124-
final var response = OpenAiClient.forModel(GPT_4O_MINI).chatCompletion(request);
127+
final OpenAiChatCompletionOutput response =
128+
OpenAiClient.forModel(GPT_4O_MINI).chatCompletion(request);
125129

126-
final var toolCall = response.getChoices().get(0).getMessage().getToolCalls().get(0);
127-
final var arguments =
130+
// 2. Optionally, execute the function.
131+
final OpenAiChatToolCall toolCall =
132+
response.getChoices().get(0).getMessage().getToolCalls().get(0);
133+
final WeatherMethod.Request arguments =
128134
parseJson(toolCall.getFunction().getArguments(), WeatherMethod.Request.class);
129-
final var currentWeather = new WeatherMethod().getCurrentWeather(arguments);
135+
final WeatherMethod.Response currentWeather = WeatherMethod.getCurrentWeather(arguments);
130136

131-
final var assistantMessage = response.getChoices().get(0).getMessage();
137+
final OpenAiChatMessage.OpenAiChatAssistantMessage assistantMessage =
138+
response.getChoices().get(0).getMessage();
132139
messages.add(assistantMessage);
133140

134141
final var toolMessage =

sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/WeatherMethod.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ record Response(double temp, Unit unit) {}
3535
@Nonnull
3636
@SuppressWarnings("unused")
3737
@Tool(description = "Get the weather in location")
38-
Response getCurrentWeather(@ToolParam @Nonnull final Request request) {
38+
static Response getCurrentWeather(@ToolParam @Nonnull final Request request) {
3939
final int temperature = request.location.hashCode() % 30;
4040
return new Response(temperature, request.unit);
4141
}

sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/services/OpenAiServiceV2.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.fasterxml.jackson.databind.ObjectMapper;
1212
import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator;
1313
import com.sap.ai.sdk.core.AiCoreService;
14+
import com.sap.ai.sdk.foundationmodels.openai.OpenAiAssistantMessage;
1415
import com.sap.ai.sdk.foundationmodels.openai.OpenAiChatCompletionDelta;
1516
import com.sap.ai.sdk.foundationmodels.openai.OpenAiChatCompletionRequest;
1617
import com.sap.ai.sdk.foundationmodels.openai.OpenAiChatCompletionResponse;
@@ -20,6 +21,7 @@
2021
import com.sap.ai.sdk.foundationmodels.openai.OpenAiFunctionCall;
2122
import com.sap.ai.sdk.foundationmodels.openai.OpenAiImageItem;
2223
import com.sap.ai.sdk.foundationmodels.openai.OpenAiMessage;
24+
import com.sap.ai.sdk.foundationmodels.openai.OpenAiToolCall;
2325
import com.sap.ai.sdk.foundationmodels.openai.generated.model.ChatCompletionTool;
2426
import com.sap.ai.sdk.foundationmodels.openai.generated.model.FunctionObject;
2527
import java.util.ArrayList;
@@ -100,7 +102,8 @@ public OpenAiChatCompletionResponse chatCompletionImage(@Nonnull final String li
100102
public OpenAiChatCompletionResponse chatCompletionToolExecution(
101103
@Nonnull final String location, @Nonnull final String unit) {
102104

103-
var schemaMap = generateSchema(WeatherMethod.Request.class);
105+
// 1. Define the function
106+
Map<String, Object> schemaMap = generateSchema(WeatherMethod.Request.class);
104107
final var function =
105108
new FunctionObject()
106109
.name("weather")
@@ -111,23 +114,28 @@ public OpenAiChatCompletionResponse chatCompletionToolExecution(
111114
final var messages = new ArrayList<OpenAiMessage>();
112115
messages.add(OpenAiMessage.user("What's the weather in %s in %s?".formatted(location, unit)));
113116

117+
// Assistant will call the function
114118
final var request = new OpenAiChatCompletionRequest(messages).withTools(List.of(tool));
115-
final var response = OpenAiClient.forModel(GPT_4O_MINI).chatCompletion(request);
119+
final OpenAiChatCompletionResponse response =
120+
OpenAiClient.forModel(GPT_4O_MINI).chatCompletion(request);
116121

117-
final var assistantMessage = response.getMessage();
122+
// 2. Optionally, execute the function.
123+
final OpenAiAssistantMessage assistantMessage = response.getMessage();
118124
messages.add(assistantMessage);
119125

120-
var toolCall = assistantMessage.toolCalls().get(0);
126+
final OpenAiToolCall toolCall = assistantMessage.toolCalls().get(0);
121127
if (!(toolCall instanceof OpenAiFunctionCall functionCall)) {
122128
throw new IllegalArgumentException(
123129
"Expected a function call, but got: %s".formatted(assistantMessage));
124130
}
125131

126-
var arguments = parseJson(functionCall.getArguments(), WeatherMethod.Request.class);
127-
var weatherMethod = new WeatherMethod().getCurrentWeather(arguments);
132+
WeatherMethod.Request arguments =
133+
parseJson(functionCall.getArguments(), WeatherMethod.Request.class);
134+
WeatherMethod.Response weatherMethod = WeatherMethod.getCurrentWeather(arguments);
128135

129136
messages.add(OpenAiMessage.tool(weatherMethod.toString(), functionCall.getId()));
130137

138+
// Send back the results, and the model will incorporate them into its final response.
131139
return OpenAiClient.forModel(GPT_4O_MINI).chatCompletion(request.withMessages(messages));
132140
}
133141

0 commit comments

Comments
 (0)