Skip to content

Commit 5ecf5bb

Browse files
committed
Message classes extending generated model
1 parent 5675b65 commit 5ecf5bb

File tree

8 files changed

+25
-50
lines changed

8 files changed

+25
-50
lines changed

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
import com.sap.ai.sdk.orchestration.client.model.ChatMessage;
44
import javax.annotation.Nonnull;
55

6-
public record AssistantMessage(@Nonnull String content) implements Message {
6+
public class AssistantMessage extends ChatMessage {
77

8-
public static final String ROLE = "assistant";
9-
10-
@Nonnull
11-
public ChatMessage toChatMessage() {
12-
return new ChatMessage().role(ROLE).content(content);
8+
public AssistantMessage(@Nonnull String content) {
9+
this.role("assistant").content(content);
1310
}
1411
}

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

Lines changed: 0 additions & 7 deletions
This file was deleted.

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public class OrchestrationPrompt {
3232
* @param message A user message.
3333
*/
3434
public OrchestrationPrompt(@Nonnull final String message) {
35-
messages.add(new UserMessage(message).toChatMessage());
35+
messages.add(new UserMessage(message));
3636
}
3737

3838
/**
@@ -41,9 +41,10 @@ public OrchestrationPrompt(@Nonnull final String message) {
4141
* @param message The first message.
4242
* @param messages Optionally, more messages.
4343
*/
44-
public OrchestrationPrompt(@Nonnull final Message message, @Nonnull final Message... messages) {
45-
this.messages.add(message.toChatMessage());
46-
this.messages.addAll(Arrays.stream(messages).map(Message::toChatMessage).toList());
44+
public OrchestrationPrompt(
45+
@Nonnull final ChatMessage message, @Nonnull final ChatMessage... messages) {
46+
this.messages.add(message);
47+
this.messages.addAll(Arrays.asList(messages));
4748
}
4849

4950
/**
@@ -52,9 +53,9 @@ public OrchestrationPrompt(@Nonnull final Message message, @Nonnull final Messag
5253
* @param inputParams The input parameters as entries of template variables and their contents.
5354
*/
5455
public OrchestrationPrompt(
55-
@Nonnull final Map<String, String> inputParams, @Nonnull final Message... messages) {
56+
@Nonnull final Map<String, String> inputParams, @Nonnull final ChatMessage... messages) {
5657
this.templateParameters.putAll(inputParams);
57-
this.messages.addAll(Arrays.stream(messages).map(Message::toChatMessage).toList());
58+
this.messages.addAll(Arrays.asList(messages));
5859
}
5960

6061
/**
@@ -63,9 +64,9 @@ public OrchestrationPrompt(
6364
* @param messagesHistory The chat history to add.
6465
*/
6566
@Nonnull
66-
public OrchestrationPrompt messageHistory(@Nonnull final List<Message> messagesHistory) {
67+
public OrchestrationPrompt messageHistory(@Nonnull final List<ChatMessage> messagesHistory) {
6768
this.messagesHistory.clear();
68-
this.messagesHistory.addAll(messagesHistory.stream().map(Message::toChatMessage).toList());
69+
this.messagesHistory.addAll(messagesHistory);
6970
return this;
7071
}
7172
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
import com.sap.ai.sdk.orchestration.client.model.ChatMessage;
44
import javax.annotation.Nonnull;
55

6-
public record SystemMessage(@Nonnull String content) implements Message {
6+
public class SystemMessage extends ChatMessage {
77

8-
public static final String ROLE = "system";
9-
10-
@Nonnull
11-
public ChatMessage toChatMessage() {
12-
return new ChatMessage().role(ROLE).content(content);
8+
public SystemMessage(@Nonnull String content) {
9+
this.role("system").content(content);
1310
}
1411
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@
33
import com.sap.ai.sdk.orchestration.client.model.ChatMessage;
44
import javax.annotation.Nonnull;
55

6-
public record UserMessage(@Nonnull String content) implements Message {
6+
public class UserMessage extends ChatMessage {
77

8-
public static final String ROLE = "user";
9-
10-
@Nonnull
11-
public ChatMessage toChatMessage() {
12-
return new ChatMessage().role(ROLE).content(content);
8+
public UserMessage(@Nonnull String content) {
9+
this.role("user").content(content);
1310
}
1411
}

orchestration/src/test/java/com/sap/ai/sdk/orchestration/ConfigToRequestTransformerTest.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,7 @@ void testEmptyTemplateConfig() {
3333
var systemMessage = new SystemMessage("foo");
3434
var userMessage = new UserMessage("Hello");
3535

36-
var expected =
37-
new Template()
38-
.template(List.of(systemMessage.toChatMessage(), userMessage.toChatMessage()));
36+
var expected = new Template().template(List.of(systemMessage, userMessage));
3937

4038
var prompt = new OrchestrationPrompt(systemMessage, userMessage);
4139
var actual =
@@ -56,18 +54,10 @@ void testMergingTemplateConfig() {
5654
var userMessage = new UserMessage("Hello ");
5755
var userMessage2 = new UserMessage("World");
5856

59-
var expected =
60-
new Template()
61-
.template(
62-
List.of(
63-
systemMessage.toChatMessage(),
64-
userMessage.toChatMessage(),
65-
userMessage2.toChatMessage()));
57+
var expected = new Template().template(List.of(systemMessage, userMessage, userMessage2));
6658

6759
var prompt = new OrchestrationPrompt(userMessage2);
68-
var templateConfig =
69-
new Template()
70-
.template(List.of(systemMessage.toChatMessage(), userMessage.toChatMessage()));
60+
var templateConfig = new Template().template(List.of(systemMessage, userMessage));
7161
var actual = ConfigToRequestTransformer.toTemplateModuleConfig(prompt, templateConfig);
7262

7363
assertThat(actual).isEqualTo(expected);
@@ -82,6 +72,6 @@ void testMessagesHistory() {
8272
ConfigToRequestTransformer.toCompletionPostRequest(
8373
prompt, new OrchestrationModuleConfig().withLlmConfig(CUSTOM_GPT_35));
8474

85-
assertThat(actual.getMessagesHistory()).containsExactly(systemMessage.toChatMessage());
75+
assertThat(actual.getMessagesHistory()).containsExactly(systemMessage);
8676
}
8777
}

orchestration/src/test/java/com/sap/ai/sdk/orchestration/OrchestrationUnitTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
3030
import com.github.tomakehurst.wiremock.stubbing.Scenario;
3131
import com.sap.ai.sdk.core.AiCoreService;
32+
import com.sap.ai.sdk.orchestration.client.model.ChatMessage;
3233
import com.sap.ai.sdk.orchestration.client.model.CompletionPostRequest;
3334
import com.sap.ai.sdk.orchestration.client.model.DPIEntities;
3435
import com.sap.ai.sdk.orchestration.client.model.GenericModuleResult;
@@ -256,7 +257,7 @@ void messagesHistory() throws IOException {
256257
.withBodyFile("templatingResponse.json")
257258
.withHeader("Content-Type", "application/json")));
258259

259-
final List<Message> messagesHistory =
260+
final List<ChatMessage> messagesHistory =
260261
List.of(
261262
new UserMessage("What is the capital of France?"),
262263
new AssistantMessage("The capital of France is Paris."));

sample-code/spring-app/src/main/java/com/sap/ai/sdk/app/controllers/OrchestrationController.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import com.sap.ai.sdk.orchestration.AzureContentFilter;
77
import com.sap.ai.sdk.orchestration.AzureFilterThreshold;
88
import com.sap.ai.sdk.orchestration.DpiMasking;
9-
import com.sap.ai.sdk.orchestration.Message;
109
import com.sap.ai.sdk.orchestration.OrchestrationChatResponse;
1110
import com.sap.ai.sdk.orchestration.OrchestrationClient;
1211
import com.sap.ai.sdk.orchestration.OrchestrationModuleConfig;
@@ -73,7 +72,7 @@ public OrchestrationChatResponse template() {
7372
@GetMapping("/messagesHistory")
7473
@Nonnull
7574
public OrchestrationChatResponse messagesHistory() {
76-
final List<Message> messagesHistory =
75+
final List<ChatMessage> messagesHistory =
7776
List.of(
7877
new UserMessage("What is the capital of France?"),
7978
new AssistantMessage("The capital of France is Paris."));

0 commit comments

Comments
 (0)