|
5 | 5 | - [Introduction](#introduction) |
6 | 6 | - [Orchestration Chat Completion](#orchestration-chat-completion) |
7 | 7 | - [Orchestration Masking](#orchestration-masking) |
| 8 | +- [Stream chat completion](#stream-chat-completion) |
| 9 | +- [Function Calling](#function-calling) |
8 | 10 |
|
9 | 11 | ## Introduction |
10 | 12 |
|
@@ -99,3 +101,39 @@ Flux<String> responseFlux = |
99 | 101 | _Note: A Spring endpoint can return `Flux` instead of `ResponseEntity`._ |
100 | 102 |
|
101 | 103 | Please find [an example in our Spring Boot application](../../sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/SpringAiOrchestrationService.java). |
| 104 | + |
| 105 | +## Function Calling |
| 106 | + |
| 107 | +First define a function that will be called by the LLM: |
| 108 | + |
| 109 | +```java |
| 110 | +public class MockWeatherService implements Function<Request, Response> { |
| 111 | + public enum Unit {C, F} |
| 112 | + public record Request(String location, Unit unit) {} |
| 113 | + public record Response(double temp, Unit unit) {} |
| 114 | + |
| 115 | + public Response apply(Request request) { |
| 116 | + return new Response(30.0, Unit.C); |
| 117 | + } |
| 118 | +} |
| 119 | +``` |
| 120 | + |
| 121 | +Then add your function to the options: |
| 122 | + |
| 123 | +```java |
| 124 | +OrchestrationChatOptions options = new OrchestrationChatOptions(config); |
| 125 | +options.setFunctionCallbacks( |
| 126 | + List.of( |
| 127 | + FunctionCallback.builder() |
| 128 | + .function( |
| 129 | + "CurrentWeather", new MockWeatherService()) // (1) function name and instance |
| 130 | + .description("Get the weather in location") // (2) function description |
| 131 | + .inputType(MockWeatherService.Request.class) // (3) function input type |
| 132 | + .build())); |
| 133 | +Prompt prompt = new Prompt("What is the weather in Potsdam and in Toulouse?", options); |
| 134 | + |
| 135 | +ChatResponse response = client.call(prompt); |
| 136 | +``` |
| 137 | + |
| 138 | +Please find [an example in our Spring Boot application](../../sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/services/SpringAiOrchestrationService.java). |
| 139 | + |
0 commit comments