Skip to content

Commit 13c0387

Browse files
committed
Add constructor for multiple strings for UserMessage and MessageContent
1 parent 65d01c6 commit 13c0387

File tree

5 files changed

+49
-42
lines changed

5 files changed

+49
-42
lines changed

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,64 +8,68 @@
88
import com.sap.ai.sdk.orchestration.model.SingleChatMessage;
99
import com.sap.ai.sdk.orchestration.model.TextContent;
1010
import javax.annotation.Nonnull;
11+
import javax.annotation.Nullable;
1112

1213
/** Interface representing convenience wrappers of chat message to the orchestration service. */
1314
public sealed interface Message permits UserMessage, AssistantMessage, SystemMessage {
1415

1516
/**
16-
* A convenience method to create a user message.
17+
* A convenience method to create a user message from one or more strings.
1718
*
18-
* @param msg the message content.
19+
* @param message the message content.
20+
* @param additionalMessages the additional messages.
1921
* @return the user message.
2022
*/
2123
@Nonnull
22-
static UserMessage user(@Nonnull final String msg) {
23-
return new UserMessage(msg);
24+
static UserMessage user(
25+
@Nonnull final String message, @Nullable final String... additionalMessages) {
26+
return new UserMessage(message, additionalMessages);
2427
}
2528

2629
/**
2730
* A convenience method to create a user message.
2831
*
29-
* @param msg the message content.
32+
* @param message the message content.
3033
* @return the user message.
3134
*/
3235
@Nonnull
33-
static UserMessage user(@Nonnull final MessageContent msg) {
34-
return new UserMessage(msg);
36+
static UserMessage user(@Nonnull final MessageContent message) {
37+
return new UserMessage(message);
3538
}
3639

3740
/**
3841
* A convenience method to create an assistant message.
3942
*
40-
* @param msg the message content.
43+
* @param message the message content.
4144
* @return the assistant message.
4245
*/
4346
@Nonnull
44-
static AssistantMessage assistant(@Nonnull final String msg) {
45-
return new AssistantMessage(msg);
47+
static AssistantMessage assistant(@Nonnull final String message) {
48+
return new AssistantMessage(message);
4649
}
4750

4851
/**
49-
* A convenience method to create a system message.
52+
* A convenience method to create a system message from one or more strings.
5053
*
51-
* @param msg the message content.
54+
* @param message the message content.
5255
* @return the system message.
5356
*/
5457
@Nonnull
55-
static SystemMessage system(@Nonnull final String msg) {
56-
return new SystemMessage(msg);
58+
static SystemMessage system(
59+
@Nonnull final String message, @Nullable final String... additionalMessages) {
60+
return new SystemMessage(message, additionalMessages);
5761
}
5862

5963
/**
6064
* A convenience method to create a system message. As of now, only text content is supported for
6165
* system messages by most AIs.
6266
*
63-
* @param msg the message content.
67+
* @param message the message content.
6468
* @return the system message.
6569
*/
6670
@Nonnull
67-
static SystemMessage system(@Nonnull final MessageContent msg) {
68-
return new SystemMessage(msg);
71+
static SystemMessage system(@Nonnull final MessageContent message) {
72+
return new SystemMessage(message);
6973
}
7074

7175
/**

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

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.sap.ai.sdk.orchestration.model.TextContent;
66
import java.util.Arrays;
77
import java.util.List;
8+
import java.util.stream.Stream;
89
import javax.annotation.Nonnull;
910
import javax.annotation.Nullable;
1011

@@ -45,26 +46,21 @@ public static MessageContent image(@Nonnull final String imageUrl) {
4546
}
4647

4748
/**
48-
* Creates a new message content containing one {@link TextItem} with the given text.
49+
* Creates a new message content containing one or more {@link TextItem}s with the given texts.
4950
*
50-
* @param text the text of the message
51+
* @param firstInput the first text of the message
52+
* @param moreInputs additional texts of the message
5153
* @return the new message content
5254
*/
5355
@Nonnull
54-
public static MessageContent text(@Nonnull final String text) {
55-
return new MessageContent(List.of(new TextItem(text)));
56-
}
57-
58-
/**
59-
* Creates a new message content containing multiple {@link TextItem}s with the given texts.
60-
*
61-
* @param texts the texts of the messages
62-
* @return the new message content
63-
*/
64-
@Nonnull
65-
public static MessageContent text(@Nonnull final String... texts) {
56+
public static MessageContent text(
57+
@Nonnull final String firstInput, @Nullable final String... moreInputs) {
6658
return new MessageContent(
67-
Arrays.stream(texts).map(text -> ((ContentItem) new TextItem(text))).toList());
59+
Stream.concat(
60+
Stream.of(firstInput),
61+
moreInputs == null ? Stream.empty() : Arrays.stream(moreInputs))
62+
.map(text -> (ContentItem) new TextItem(text))
63+
.toList());
6864
}
6965

7066
/**

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.List;
44
import java.util.stream.Stream;
55
import javax.annotation.Nonnull;
6+
import javax.annotation.Nullable;
67
import lombok.Value;
78
import lombok.experimental.Accessors;
89

@@ -24,12 +25,14 @@ public MessageContent content() {
2425
}
2526

2627
/**
27-
* Creates a new system message with the given single message.
28+
* Creates a new system message from one or more strings.
2829
*
29-
* @param singleMessage the single message.
30+
* @param message the first message.
31+
* @param additionalMessages the additional messages.
3032
*/
31-
public SystemMessage(@Nonnull final String singleMessage) {
32-
content = new MessageContent(singleMessage);
33+
public SystemMessage(
34+
@Nonnull final String message, @Nullable final String... additionalMessages) {
35+
content = MessageContent.text(message, additionalMessages);
3336
}
3437

3538
/**

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ public MessageContent content() {
2525
}
2626

2727
/**
28-
* Creates a new user message with the given single message.
28+
* Creates a new user message from one or more strings.
2929
*
30-
* @param singleMessage the single message.
30+
* @param message the first message.
31+
* @param additionalMessages the additional messages.
3132
*/
32-
public UserMessage(@Nonnull final String singleMessage) {
33-
content = new MessageContent(singleMessage);
33+
public UserMessage(@Nonnull final String message, @Nullable final String... additionalMessages) {
34+
content = MessageContent.text(message, additionalMessages);
3435
}
3536

3637
/**

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -681,8 +681,7 @@ void streamChatCompletionDeltas() throws IOException {
681681
void testMultiMessage() throws IOException {
682682
stubFor(
683683
post("/completion")
684-
.willReturn(
685-
aResponse().withStatus(SC_OK).withBodyFile("multiMessageResponse.json")));
684+
.willReturn(aResponse().withStatus(SC_OK).withBodyFile("multiMessageResponse.json")));
686685

687686
OrchestrationAiModel customGpt4o =
688687
GPT_4O_MINI
@@ -791,9 +790,11 @@ void testMultiMessage() throws IOException {
791790

792791
@Test
793792
void testMessageConstruction() {
793+
var userMessageTwoTexts = Message.user("Text 1", "Text 2");
794794
var userMessageTwoTextsAddText = Message.user("Text 1").addText("Text 2");
795795
var userMessageTwoTextsAdd = Message.user("Text 1").add(MessageContent.text("Text 2"));
796796
var userMessageTwoTextsContent = Message.user(MessageContent.text("Text 1", "Text 2"));
797+
assertThat(userMessageTwoTexts).isEqualTo(userMessageTwoTextsAddText);
797798
assertThat(userMessageTwoTextsAddText).isEqualTo(userMessageTwoTextsAdd);
798799
assertThat(userMessageTwoTextsAdd).isEqualTo(userMessageTwoTextsContent);
799800

@@ -807,9 +808,11 @@ void testMessageConstruction() {
807808
Message.user("Text 1").add(MessageContent.image("url", ImageItem.DetailLevel.low));
808809
assertThat(userMessageWithImageDetailAddImage).isEqualTo(userMessageWithImageDetailAdd);
809810

811+
var systemMessageTwoTexts = Message.system("Text 1", "Text 2");
810812
var systemMessageTwoTextsAddText = Message.system("Text 1").addText("Text 2");
811813
var systemMessageTwoTextsAdd = Message.system("Text 1").add(MessageContent.text("Text 2"));
812814
var systemMessageTwoTextsContent = Message.system(MessageContent.text("Text 1", "Text 2"));
815+
assertThat(systemMessageTwoTexts).isEqualTo(systemMessageTwoTextsAddText);
813816
assertThat(systemMessageTwoTextsAddText).isEqualTo(systemMessageTwoTextsAdd);
814817
assertThat(systemMessageTwoTextsAdd).isEqualTo(systemMessageTwoTextsContent);
815818
}

0 commit comments

Comments
 (0)