Skip to content

Commit 7b56d90

Browse files
Best effort
1 parent 94c42d4 commit 7b56d90

File tree

3 files changed

+68
-24
lines changed

3 files changed

+68
-24
lines changed
Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,55 @@
11
package com.sap.ai.sdk.orchestration;
22

3+
import com.sap.ai.sdk.orchestration.model.ChatMessage;
4+
import com.sap.ai.sdk.orchestration.model.SingleChatMessage;
5+
import java.util.List;
6+
import java.util.Map;
37
import javax.annotation.Nonnull;
4-
import lombok.Value;
8+
import javax.annotation.Nullable;
9+
import lombok.Getter;
510
import lombok.experimental.Accessors;
11+
import org.springframework.ai.chat.messages.AssistantMessage.ToolCall;
612

713
/** Represents a chat message as 'assistant' to the orchestration service. */
8-
@Value
14+
@Getter
915
@Accessors(fluent = true)
10-
public class AssistantMessage implements Message {
16+
public final class AssistantMessage implements Message {
1117

1218
/** The role of the assistant. */
1319
@Nonnull String role = "assistant";
1420

1521
/** The content of the message. */
1622
@Nonnull String content;
23+
24+
@Nullable ToolCall toolCalls = null;
25+
26+
public AssistantMessage(@Nonnull final String content) {
27+
this.content = content;
28+
}
29+
30+
public AssistantMessage(@Nonnull final List<ToolCall> toolCalls) {
31+
content = "";
32+
this.toolCalls = toolCalls.get(0);
33+
}
34+
35+
@Nonnull
36+
@Override
37+
public ChatMessage createChatMessage() {
38+
if (toolCalls != null) {
39+
final SingleChatMessage message = SingleChatMessage.create().role(role).content(null);
40+
Map<String, String> map =
41+
Map.of(
42+
"id",
43+
toolCalls().id(),
44+
"name",
45+
toolCalls().name(),
46+
"type",
47+
toolCalls().type(),
48+
"arguments",
49+
toolCalls().arguments());
50+
message.setCustomField("tool_calls", map);
51+
return message;
52+
}
53+
return Message.super.createChatMessage();
54+
}
1755
}
Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
package com.sap.ai.sdk.orchestration;
22

3+
import com.sap.ai.sdk.orchestration.model.ChatMessage;
4+
import com.sap.ai.sdk.orchestration.model.SingleChatMessage;
5+
import javax.annotation.Nonnull;
36
import lombok.Value;
47
import lombok.experimental.Accessors;
5-
import org.springframework.ai.chat.messages.ToolResponseMessage;
6-
7-
import javax.annotation.Nonnull;
88

9-
/** Represents a chat message as 'system' to the orchestration service. */
9+
/** Represents a chat message as 'tool' to the orchestration service. */
1010
@Value
1111
@Accessors(fluent = true)
1212
public class ToolMessage implements Message {
1313

1414
/** The role of the assistant. */
15-
@Nonnull
16-
String role = "tool";
15+
@Nonnull String role = "tool";
1716

18-
/** The content of the message. */
19-
@Nonnull
20-
ToolResponseMessage message;
17+
@Nonnull String id;
18+
19+
@Nonnull String content;
2120

2221
@Nonnull
2322
@Override
24-
public String content()
25-
{
26-
return "";
23+
public ChatMessage createChatMessage() {
24+
SingleChatMessage message = SingleChatMessage.create().role(role()).content(content);
25+
message.setCustomField("tool_call_id", id);
26+
return message;
2727
}
2828
}
29-

orchestration/src/main/java/com/sap/ai/sdk/orchestration/spring/OrchestrationChatModel.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
import javax.annotation.Nonnull;
1818
import lombok.extern.slf4j.Slf4j;
1919
import lombok.val;
20+
import org.springframework.ai.chat.messages.AssistantMessage.ToolCall;
2021
import org.springframework.ai.chat.messages.Message;
2122
import org.springframework.ai.chat.messages.ToolResponseMessage;
23+
import org.springframework.ai.chat.messages.ToolResponseMessage.ToolResponse;
2224
import org.springframework.ai.chat.model.AbstractToolCallSupport;
2325
import org.springframework.ai.chat.model.ChatModel;
2426
import org.springframework.ai.chat.model.ChatResponse;
@@ -59,13 +61,11 @@ public OrchestrationChatModel(OrchestrationClient client) {
5961
@Nonnull
6062
@Override
6163
public ChatResponse call(@Nonnull final Prompt prompt) {
62-
return internalCall(prompt, null);
63-
}
64-
65-
public ChatResponse internalCall(Prompt prompt, ChatResponse previousChatResponse) {
66-
6764
if (prompt.getOptions() instanceof OrchestrationChatOptions options) {
68-
runtimeFunctionCallbackConfigurations(FunctionCallingOptions.builder().functionCallbacks(options.getFunctionCallbacks()).build());
65+
runtimeFunctionCallbackConfigurations(
66+
FunctionCallingOptions.builder()
67+
.functionCallbacks(options.getFunctionCallbacks())
68+
.build());
6969
val orchestrationPrompt = toOrchestrationPrompt(prompt);
7070
val response =
7171
new OrchestrationSpringChatResponse(
@@ -76,7 +76,7 @@ && isToolCall(response, Set.of("tool_calls", "stop"))) {
7676
var toolCallConversation = handleToolCalls(prompt, response);
7777
// Recursively call the call method with the tool call message
7878
// conversation that contains the call responses.
79-
return internalCall(new Prompt(toolCallConversation, prompt.getOptions()), response);
79+
return call(new Prompt(toolCallConversation, prompt.getOptions()));
8080
}
8181
return response;
8282
}
@@ -128,9 +128,16 @@ private static com.sap.ai.sdk.orchestration.Message[] toOrchestrationMessages(
128128
case USER:
129129
yield new UserMessage(msg.getText());
130130
case ASSISTANT:
131+
final List<ToolCall> toolCalls =
132+
((org.springframework.ai.chat.messages.AssistantMessage) msg).getToolCalls();
133+
if(toolCalls != null) {
134+
yield new AssistantMessage(toolCalls);
135+
}
131136
yield new AssistantMessage(msg.getText());
132137
case TOOL:
133-
yield new ToolMessage((ToolResponseMessage) msg);
138+
val responses = ((ToolResponseMessage) msg).getResponses();
139+
ToolResponse response = responses.get(0);
140+
yield new ToolMessage(response.id(), response.responseData());
134141
};
135142
return messages.stream().map(mapper).toArray(com.sap.ai.sdk.orchestration.Message[]::new);
136143
}

0 commit comments

Comments
 (0)