|
8 | 8 | import static com.sap.ai.sdk.orchestration.OrchestrationJacksonConfiguration.getOrchestrationObjectMapper; |
9 | 9 |
|
10 | 10 | import com.fasterxml.jackson.databind.ObjectMapper; |
11 | | -import com.google.common.annotations.Beta; |
12 | 11 | import com.sap.ai.sdk.orchestration.OrchestrationModuleConfig; |
13 | 12 | import com.sap.ai.sdk.orchestration.model.ChatCompletionTool; |
14 | 13 | import com.sap.ai.sdk.orchestration.model.ChatCompletionTool.TypeEnum; |
|
27 | 26 | import lombok.val; |
28 | 27 | import org.springframework.ai.chat.prompt.ChatOptions; |
29 | 28 | import org.springframework.ai.model.ModelOptionsUtils; |
30 | | -import org.springframework.ai.model.function.FunctionCallback; |
31 | 29 | import org.springframework.ai.model.tool.ToolCallingChatOptions; |
| 30 | +import org.springframework.ai.tool.ToolCallback; |
32 | 31 |
|
33 | 32 | /** |
34 | 33 | * Configuration to be used for orchestration requests. |
35 | 34 | * |
36 | 35 | * @since 1.2.0 |
37 | 36 | */ |
38 | | -@Beta |
39 | 37 | @Data |
40 | 38 | public class OrchestrationChatOptions implements ToolCallingChatOptions { |
41 | 39 |
|
42 | 40 | private static final ObjectMapper JACKSON = getOrchestrationObjectMapper(); |
43 | 41 |
|
44 | 42 | @Nonnull private OrchestrationModuleConfig config; |
45 | 43 |
|
46 | | - private List<FunctionCallback> functionCallbacks; |
| 44 | + @Nonnull private List<ToolCallback> toolCallbacks = List.of(); |
47 | 45 |
|
48 | 46 | @Getter(AccessLevel.NONE) |
| 47 | + @Nullable |
49 | 48 | private Boolean internalToolExecutionEnabled; |
50 | 49 |
|
51 | | - private Set<String> toolNames; |
| 50 | + @Nonnull private Set<String> toolNames = Set.of(); |
52 | 51 |
|
53 | | - private Map<String, Object> toolContext; |
| 52 | + @Nonnull private Map<String, Object> toolContext = Map.of(); |
54 | 53 |
|
55 | 54 | /** |
56 | 55 | * Returns the model to use for the chat. |
@@ -177,72 +176,37 @@ private <T> T getLlmConfigParam(@Nonnull final String param) { |
177 | 176 | return ((Map<String, T>) getLlmConfigNonNull().getModelParams()).get(param); |
178 | 177 | } |
179 | 178 |
|
180 | | - @Nonnull |
181 | | - private LLMModuleConfig getLlmConfigNonNull() { |
182 | | - return Objects.requireNonNull( |
183 | | - config.getLlmConfig(), |
184 | | - "LLM config is not set. Please set it: new OrchestrationChatOptions(new OrchestrationModuleConfig().withLlmConfig(...))"); |
185 | | - } |
186 | | - |
187 | | - @Nonnull |
188 | 179 | @Override |
189 | | - public List<FunctionCallback> getToolCallbacks() { |
190 | | - return functionCallbacks; |
191 | | - } |
192 | | - |
193 | | - @Override |
194 | | - @Deprecated |
195 | | - public void setFunctionCallbacks(@Nonnull final List<FunctionCallback> toolCallbacks) { |
196 | | - setToolCallbacks(toolCallbacks); |
197 | | - } |
198 | | - |
199 | | - @Override |
200 | | - public void setToolCallbacks(@Nonnull final List<FunctionCallback> toolCallbacks) { |
201 | | - this.functionCallbacks = toolCallbacks; |
| 180 | + public void setToolCallbacks(@Nonnull final List<ToolCallback> toolCallbacks) { |
| 181 | + this.toolCallbacks = toolCallbacks; |
202 | 182 | final Template template = |
203 | 183 | Objects.requireNonNullElse( |
204 | 184 | (Template) config.getTemplateConfig(), Template.create().template()); |
205 | 185 | val tools = toolCallbacks.stream().map(OrchestrationChatOptions::toOrchestrationTool).toList(); |
206 | 186 | config = config.withTemplateConfig(template.tools(tools)); |
207 | 187 | } |
208 | 188 |
|
209 | | - private static ChatCompletionTool toOrchestrationTool( |
210 | | - @Nonnull final FunctionCallback functionCallback) { |
211 | | - return ChatCompletionTool.create() |
212 | | - .type(TypeEnum.FUNCTION) |
213 | | - .function( |
214 | | - FunctionObject.create() |
215 | | - .name(functionCallback.getName()) |
216 | | - .description(functionCallback.getDescription()) |
217 | | - .parameters(ModelOptionsUtils.jsonToMap(functionCallback.getInputTypeSchema()))); |
218 | | - } |
219 | | - |
220 | | - @Override |
221 | 189 | @Nullable |
222 | | - public Boolean isInternalToolExecutionEnabled() { |
| 190 | + @Override |
| 191 | + public Boolean getInternalToolExecutionEnabled() { |
223 | 192 | return this.internalToolExecutionEnabled; |
224 | 193 | } |
225 | 194 |
|
226 | 195 | @Nonnull |
227 | | - @Override |
228 | | - public Set<String> getFunctions() { |
229 | | - return Set.of(); |
| 196 | + private LLMModuleConfig getLlmConfigNonNull() { |
| 197 | + return Objects.requireNonNull( |
| 198 | + config.getLlmConfig(), |
| 199 | + "LLM config is not set. Please set it: new OrchestrationChatOptions(new OrchestrationModuleConfig().withLlmConfig(...))"); |
230 | 200 | } |
231 | 201 |
|
232 | | - @Override |
233 | | - public void setFunctions(@Nonnull final Set<String> functions) { |
234 | | - // val template = |
235 | | - // Objects.requireNonNullElse( |
236 | | - // (Template) config.getTemplateConfig(), Template.create().template()); |
237 | | - // val tools = |
238 | | - // functionNames.stream() |
239 | | - // .map( |
240 | | - // functionName -> |
241 | | - // ChatCompletionTool.create() |
242 | | - // .type(TypeEnum.FUNCTION) |
243 | | - // .function(FunctionObject.create().name(functionName))) |
244 | | - // .toList(); |
245 | | - // config = config.withTemplateConfig(template.tools(tools)); |
246 | | - throw new UnsupportedOperationException("Not implemented yet"); |
| 202 | + private static ChatCompletionTool toOrchestrationTool(@Nonnull final ToolCallback toolCallback) { |
| 203 | + val toolDef = toolCallback.getToolDefinition(); |
| 204 | + return ChatCompletionTool.create() |
| 205 | + .type(TypeEnum.FUNCTION) |
| 206 | + .function( |
| 207 | + FunctionObject.create() |
| 208 | + .name(toolDef.name()) |
| 209 | + .description(toolDef.description()) |
| 210 | + .parameters(ModelOptionsUtils.jsonToMap(toolDef.inputSchema()))); |
247 | 211 | } |
248 | 212 | } |
0 commit comments