-
Notifications
You must be signed in to change notification settings - Fork 22
feat: migrate spring-ai to v2 #1006
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
20a485c
0c9fbe8
e168629
410a2b1
196295d
1c3f02a
0ae5a5f
373bff0
befaa00
940d610
8ba7ab5
3448b10
f775900
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ | |
|
|
||
| ### 🔧 Compatibility Notes | ||
|
|
||
| - | ||
| -[Orchestration] Spring AI support was upgraded to version `2.0.1` | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could make a mini migration guide, |
||
|
|
||
| ### ✨ New Functionality | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,5 @@ | ||
| package com.sap.ai.sdk.foundationmodels.openai.spring; | ||
|
|
||
| import static org.springframework.ai.model.tool.ToolCallingChatOptions.isInternalToolExecutionEnabled; | ||
|
|
||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.core.type.TypeReference; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
|
@@ -35,7 +33,7 @@ | |
| import org.springframework.ai.chat.model.Generation; | ||
| import org.springframework.ai.chat.prompt.ChatOptions; | ||
| import org.springframework.ai.chat.prompt.Prompt; | ||
| import org.springframework.ai.model.tool.DefaultToolCallingManager; | ||
| import org.springframework.ai.model.tool.DefaultToolCallingChatOptions; | ||
| import org.springframework.ai.model.tool.ToolCallingChatOptions; | ||
| import reactor.core.publisher.Flux; | ||
|
|
||
|
|
@@ -49,8 +47,10 @@ public class OpenAiChatModel implements ChatModel { | |
| private final OpenAiClient client; | ||
|
|
||
| @Nonnull | ||
| private final DefaultToolCallingManager toolCallingManager = | ||
| DefaultToolCallingManager.builder().build(); | ||
| @Override | ||
| public ChatOptions getOptions() { | ||
| return DefaultToolCallingChatOptions.builder().toolCallbacks(List.of()).build(); | ||
| } | ||
|
|
||
| @Override | ||
| @Nonnull | ||
|
|
@@ -66,18 +66,7 @@ public ChatResponse call(@Nonnull final Prompt prompt) { | |
| } | ||
|
|
||
| val result = client.chatCompletion(request); | ||
| val response = new ChatResponse(toGenerations(result)); | ||
|
|
||
| if (options != null && isInternalToolExecutionEnabled(options) && response.hasToolCalls()) { | ||
| val toolCalls = | ||
| response.getResult().getOutput().getToolCalls().stream().map(ToolCall::name).toList(); | ||
| log.info("Executing {} tool call(s) - {}.", toolCalls.size(), toolCalls); | ||
| val toolExecutionResult = toolCallingManager.executeToolCalls(prompt, response); | ||
| // Send the tool execution result back to the model. | ||
| log.debug("Re-invoking model with tool execution results."); | ||
| return call(new Prompt(toolExecutionResult.conversationHistory(), options)); | ||
| } | ||
| return response; | ||
| return new ChatResponse(toGenerations(result)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Question, Breaking change?) Is it correct to skip this entire logic here? It looks to me that now there is no way anymore to directly execute the tool calls during the call. What is the migration guide for |
||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -129,14 +118,15 @@ private static List<OpenAiMessage> extractMessages(final Prompt prompt) { | |
|
|
||
| private static void addAssistantMessage( | ||
| final List<OpenAiMessage> result, final AssistantMessage message) { | ||
| if (message.getText() != null) { | ||
| result.add(OpenAiMessage.assistant(message.getText())); | ||
| final var toolCalls = message.getToolCalls(); | ||
| if (toolCalls != null && !toolCalls.isEmpty()) { | ||
| final Function<ToolCall, OpenAiToolCall> callTranslate = | ||
| toolCall -> OpenAiToolCall.function(toolCall.id(), toolCall.name(), toolCall.arguments()); | ||
| val calls = toolCalls.stream().map(callTranslate).toList(); | ||
| result.add(OpenAiMessage.assistant(calls)); | ||
| return; | ||
| } | ||
| final Function<ToolCall, OpenAiToolCall> callTranslate = | ||
| toolCall -> OpenAiToolCall.function(toolCall.id(), toolCall.name(), toolCall.arguments()); | ||
| val calls = message.getToolCalls().stream().map(callTranslate).toList(); | ||
| result.add(OpenAiMessage.assistant(calls)); | ||
| Option.of(message.getText()).peek(t -> result.add(OpenAiMessage.assistant(t))); | ||
| } | ||
|
|
||
| private static void addToolMessages( | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -118,6 +118,10 @@ | |
| <groupId>com.github.victools</groupId> | ||
| <artifactId>jsonschema-module-jackson</artifactId> | ||
| </dependency> | ||
| <dependency> | ||
| <groupId>tools.jackson.core</groupId> | ||
| <artifactId>jackson-databind</artifactId> | ||
| </dependency> | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| <dependency> | ||
| <groupId>com.fasterxml.jackson.dataformat</groupId> | ||
| <artifactId>jackson-dataformat-yaml</artifactId> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why about the other dependencies that are ignored here?