Skip to content

Commit 7a9c30f

Browse files
Green
1 parent 7d1ba19 commit 7a9c30f

File tree

4 files changed

+60
-16
lines changed

4 files changed

+60
-16
lines changed

orchestration/src/test/java/com/sap/ai/sdk/orchestration/spring/OrchestrationChatModelTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ void testStreamCompletion() throws IOException {
144144

145145
@Test
146146
void testToolCalls() throws IOException {
147+
// https://platform.openai.com/docs/guides/function-calling
147148
stubFor(
148149
post(urlPathEqualTo("/completion"))
149150
.inScenario("Tool Calls")

sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/SpringAiOrchestrationController.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ class SpringAiOrchestrationController {
2121
@GetMapping("/completion")
2222
Object completion(
2323
@Nullable @RequestParam(value = "format", required = false) final String format) {
24-
val response = (OrchestrationSpringChatResponse) service.completion();
24+
val response = service.completion();
2525

2626
if ("json".equals(format)) {
27-
return response.getOrchestrationResponse().getOriginalResponse();
27+
return ((OrchestrationSpringChatResponse) response)
28+
.getOrchestrationResponse()
29+
.getOriginalResponse();
2830
}
2931
return response.getResult().getOutput().getContent();
3032
}
@@ -39,20 +41,37 @@ Flux<String> streamChatCompletion() {
3941

4042
@GetMapping("/template")
4143
Object template(@Nullable @RequestParam(value = "format", required = false) final String format) {
42-
val response = (OrchestrationSpringChatResponse) service.template();
44+
val response = service.template();
4345

4446
if ("json".equals(format)) {
45-
return response.getOrchestrationResponse().getOriginalResponse();
47+
return ((OrchestrationSpringChatResponse) response)
48+
.getOrchestrationResponse()
49+
.getOriginalResponse();
4650
}
4751
return response.getResult().getOutput().getContent();
4852
}
4953

5054
@GetMapping("/masking")
5155
Object masking(@Nullable @RequestParam(value = "format", required = false) final String format) {
52-
val response = (OrchestrationSpringChatResponse) service.masking();
56+
val response = service.masking();
5357

5458
if ("json".equals(format)) {
55-
return response.getOrchestrationResponse().getOriginalResponse();
59+
return ((OrchestrationSpringChatResponse) response)
60+
.getOrchestrationResponse()
61+
.getOriginalResponse();
62+
}
63+
return response.getResult().getOutput().getContent();
64+
}
65+
66+
@GetMapping("/functionCalling")
67+
Object functionCalling(
68+
@Nullable @RequestParam(value = "format", required = false) final String format) {
69+
val response = service.functionCalling();
70+
71+
if ("json".equals(format)) {
72+
return ((OrchestrationSpringChatResponse) response)
73+
.getOrchestrationResponse()
74+
.getOriginalResponse();
5675
}
5776
return response.getResult().getOutput().getContent();
5877
}

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

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,7 @@ public class SpringAiOrchestrationService {
3434
*/
3535
@Nonnull
3636
public ChatResponse completion() {
37-
final OrchestrationChatOptions options = new OrchestrationChatOptions(config);
38-
options.setFunctionCallbacks(
39-
List.of(
40-
FunctionCallback.builder()
41-
.function(
42-
"CurrentWeather", new MockWeatherService()) // (1) function name and instance
43-
.description("Get the weather in location") // (2) function description
44-
.inputType(MockWeatherService.Request.class) // (3) function input type
45-
.build()));
46-
val prompt = new Prompt("What is the weather in Potsdam and in Toulouse?", options);
37+
val prompt = new Prompt("What is the capital of France?", defaultOptions);
4738

4839
return client.call(prompt);
4940
}
@@ -98,4 +89,27 @@ public ChatResponse masking() {
9889

9990
return client.call(prompt);
10091
}
92+
93+
/**
94+
* Register a function that will be called when the user asks for the weather. <a
95+
* href="https://docs.spring.io/spring-ai/reference/api/chat/functions/openai-chat-functions.html#_registercall_functions_with_prompt_options">Spring
96+
* AI Function Calling</a>
97+
*
98+
* @return the assistant response object
99+
*/
100+
@Nonnull
101+
public ChatResponse functionCalling() {
102+
final OrchestrationChatOptions options = new OrchestrationChatOptions(config);
103+
options.setFunctionCallbacks(
104+
List.of(
105+
FunctionCallback.builder()
106+
.function(
107+
"CurrentWeather", new MockWeatherService()) // (1) function name and instance
108+
.description("Get the weather in location") // (2) function description
109+
.inputType(MockWeatherService.Request.class) // (3) function input type
110+
.build()));
111+
val prompt = new Prompt("What is the weather in Potsdam and in Toulouse?", options);
112+
113+
return client.call(prompt);
114+
}
101115
}

sample-code/spring-app/src/main/resources/static/index.html

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,16 @@ <h5 class="mb-1">Orchestration Integration</h5>
466466
</div>
467467
</div>
468468
</li>
469+
<li class="list-group-item">
470+
<div class="info-tooltip">
471+
<button type="submit" formaction="/spring-ai-orchestration/functionCalling" class="link-offset-2-hover link-underline link-underline-opacity-0 link-underline-opacity-75-hover endpoint">
472+
<code>/spring-ai-orchestration/functionCalling</code>
473+
</button>
474+
<div class="tooltip-content">
475+
Register a function that will be called when the user asks for the weather.
476+
</div>
477+
</div>
478+
</li>
469479
</ul>
470480
</div>
471481
</div>

0 commit comments

Comments
 (0)