Skip to content

Commit c71ac25

Browse files
new ToolCall class
1 parent bb616a9 commit c71ac25

File tree

4 files changed

+64
-35
lines changed

4 files changed

+64
-35
lines changed

orchestration/src/main/java/com/sap/ai/sdk/orchestration/AssistantMessage.java

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import com.sap.ai.sdk.orchestration.model.ChatMessage;
44
import com.sap.ai.sdk.orchestration.model.SingleChatMessage;
55
import java.util.List;
6-
import java.util.Map;
76
import javax.annotation.Nonnull;
87
import javax.annotation.Nullable;
98
import lombok.Getter;
@@ -24,16 +23,6 @@ public final class AssistantMessage implements Message {
2423
/** Tool call if there is any. */
2524
@Nullable List<ToolCall> toolCalls = null;
2625

27-
/**
28-
* Represents a tool call.
29-
*
30-
* @param id call id
31-
* @param type "function" or "tool"
32-
* @param name the name of the function to call
33-
* @param arguments the arguments to pass to the function
34-
*/
35-
public record ToolCall(String id, String type, String name, String arguments) {}
36-
3726
/**
3827
* Creates a new assistant message with the given text content.
3928
*
@@ -57,20 +46,12 @@ public AssistantMessage(@Nonnull final List<ToolCall> toolCalls) {
5746
@Override
5847
public ChatMessage createChatMessage() {
5948
if (toolCalls() != null) {
60-
final List<Map<String, Object>> toolCallList =
61-
toolCalls().stream()
62-
.map(
63-
toolCall -> {
64-
val function =
65-
Map.of("name", toolCall.name(), "arguments", toolCall.arguments());
66-
return Map.of(
67-
"id", toolCall.id(), "type", toolCall.type(), toolCall.type(), function);
68-
})
69-
.toList();
70-
7149
// content shouldn't be required for tool calls 🤷
7250
val message = SingleChatMessage.create().role(role).content("");
73-
message.setCustomField("tool_calls", toolCallList);
51+
52+
val maps = toolCalls().stream().map(ToolCall::map).toList();
53+
message.setCustomField("tool_calls", maps);
54+
7455
return message;
7556
}
7657
return Message.super.createChatMessage();
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.sap.ai.sdk.orchestration;
2+
3+
import com.google.common.annotations.Beta;
4+
import java.util.Map;
5+
import javax.annotation.Nonnull;
6+
import lombok.Getter;
7+
import lombok.NoArgsConstructor;
8+
import lombok.Setter;
9+
import lombok.experimental.Accessors;
10+
import lombok.val;
11+
12+
/**
13+
* Represents a tool call.
14+
*
15+
* @since 1.3.0
16+
*/
17+
@NoArgsConstructor(onConstructor_ = @Beta)
18+
@Accessors(fluent = true)
19+
@Getter
20+
@Setter
21+
public class ToolCall {
22+
/** call id */
23+
private String id;
24+
25+
/** "function" or "tool" */
26+
private String type;
27+
28+
/** the name of the function to call */
29+
private String name;
30+
31+
/** the arguments to pass to the function */
32+
private String arguments;
33+
34+
/**
35+
* Creates a Map representation of the tool call.
36+
*
37+
* @return a Map representation of the tool call.
38+
*/
39+
@Nonnull
40+
public Map<String, Object> map() {
41+
val function = Map.of("name", name, "arguments", arguments);
42+
return Map.of("id", id, "type", type, type, function);
43+
}
44+
}

orchestration/src/main/java/com/sap/ai/sdk/orchestration/ToolMessage.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
import lombok.Value;
77
import lombok.experimental.Accessors;
88

9-
/** Represents a chat message as 'tool' to the orchestration service. */
9+
/**
10+
* Represents a chat message as 'tool' to the orchestration service.
11+
*
12+
* @since 1.3.0
13+
*/
1014
@Value
1115
@Accessors(fluent = true)
1216
public class ToolMessage implements Message {

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.sap.ai.sdk.orchestration.OrchestrationClient;
99
import com.sap.ai.sdk.orchestration.OrchestrationPrompt;
1010
import com.sap.ai.sdk.orchestration.SystemMessage;
11+
import com.sap.ai.sdk.orchestration.ToolCall;
1112
import com.sap.ai.sdk.orchestration.ToolMessage;
1213
import com.sap.ai.sdk.orchestration.UserMessage;
1314
import java.util.List;
@@ -17,7 +18,6 @@
1718
import javax.annotation.Nonnull;
1819
import lombok.extern.slf4j.Slf4j;
1920
import lombok.val;
20-
import org.springframework.ai.chat.messages.AssistantMessage.ToolCall;
2121
import org.springframework.ai.chat.messages.Message;
2222
import org.springframework.ai.chat.messages.ToolResponseMessage;
2323
import org.springframework.ai.chat.model.AbstractToolCallSupport;
@@ -131,20 +131,20 @@ private static com.sap.ai.sdk.orchestration.Message[] toOrchestrationMessages(
131131
case USER:
132132
yield List.of(new UserMessage(msg.getText()));
133133
case ASSISTANT:
134-
final List<ToolCall> toolCalls =
134+
val springToolCalls =
135135
((org.springframework.ai.chat.messages.AssistantMessage) msg).getToolCalls();
136-
if (toolCalls != null) {
137-
final List<AssistantMessage.ToolCall> toolCallList =
138-
toolCalls.stream()
136+
if (springToolCalls != null) {
137+
final List<ToolCall> sdkToolCalls =
138+
springToolCalls.stream()
139139
.map(
140140
toolCall ->
141-
new AssistantMessage.ToolCall(
142-
toolCall.id(),
143-
toolCall.type(),
144-
toolCall.name(),
145-
toolCall.arguments()))
141+
new ToolCall()
142+
.id(toolCall.id())
143+
.type(toolCall.type())
144+
.name(toolCall.name())
145+
.arguments(toolCall.arguments()))
146146
.toList();
147-
yield List.of(new AssistantMessage(toolCallList));
147+
yield List.of(new AssistantMessage(sdkToolCalls));
148148
}
149149
yield List.of(new AssistantMessage(msg.getText()));
150150
case TOOL:

0 commit comments

Comments
 (0)