Skip to content

Commit 65d01c6

Browse files
committed
Simplify multiMessage unit test
1 parent 57330d4 commit 65d01c6

File tree

4 files changed

+40
-77
lines changed

4 files changed

+40
-77
lines changed

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

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -678,11 +678,11 @@ void streamChatCompletionDeltas() throws IOException {
678678
}
679679

680680
@Test
681-
void testRequestWithMultiChatMessage() throws IOException {
681+
void testMultiMessage() throws IOException {
682682
stubFor(
683683
post("/completion")
684684
.willReturn(
685-
aResponse().withStatus(SC_OK).withBodyFile("multiChatMessageResponse.json")));
685+
aResponse().withStatus(SC_OK).withBodyFile("multiMessageResponse.json")));
686686

687687
OrchestrationAiModel customGpt4o =
688688
GPT_4O_MINI
@@ -694,55 +694,41 @@ void testRequestWithMultiChatMessage() throws IOException {
694694
.withParam(N, 1);
695695
var llmWithImageSupportConfig = new OrchestrationModuleConfig().withLlmConfig(customGpt4o);
696696

697-
var messageWithThreeTexts =
697+
var messageWithTwoTexts =
698698
Message.system("Please answer in exactly two sentences.")
699-
.add(
700-
MessageContent.text(
701-
"Start the first sentence with the word 'Well'.",
702-
"And the second with 'By the way'."));
699+
.addText("Start the first sentence with the word 'Well'.");
703700

704701
var messageWithImage =
705702
Message.user("What is in this image?")
706-
.addText("How many letters can you see?")
707-
.add(MessageContent.text("What is the main color?"))
708703
.addImage(
709704
"https://upload.wikimedia.org/wikipedia/commons/thumb/5/59/SAP_2011_logo.svg/440px-SAP_2011_logo.svg.png");
710705
var prompt =
711-
new OrchestrationPrompt(messageWithImage).messageHistory(List.of(messageWithThreeTexts));
706+
new OrchestrationPrompt(messageWithImage).messageHistory(List.of(messageWithTwoTexts));
712707

713708
var result = client.chatCompletion(prompt, llmWithImageSupportConfig);
714709
var response = result.getOriginalResponse();
715710

716711
assertThat(result.getContent())
717712
.isEqualTo(
718-
"Well, the image features the logo of SAP, which consists of three letters: S, A, and P. By the way, the main color of the logo is a gradient of blue.");
713+
"Well, this image features the logo of SAP, a multinational software corporation known for its enterprise resource planning (ERP) software. The logo is prominently displayed in white against a gradient blue background.");
719714
assertThat(result.getAllMessages()).hasSize(3);
720715
var systemMessage = result.getAllMessages().get(0);
721716
assertThat(systemMessage.role()).isEqualTo("system");
722-
assertThat(systemMessage.content().contentItemList()).hasSize(3);
717+
assertThat(systemMessage.content().contentItemList()).hasSize(2);
723718
assertThat(systemMessage.content().contentItemList().get(0)).isInstanceOf(TextItem.class);
724719
assertThat(((TextItem) systemMessage.content().contentItemList().get(0)).text())
725720
.isEqualTo("Please answer in exactly two sentences.");
726721
assertThat(systemMessage.content().contentItemList().get(1)).isInstanceOf(TextItem.class);
727722
assertThat(((TextItem) systemMessage.content().contentItemList().get(1)).text())
728723
.isEqualTo("Start the first sentence with the word 'Well'.");
729-
assertThat(systemMessage.content().contentItemList().get(2)).isInstanceOf(TextItem.class);
730-
assertThat(((TextItem) systemMessage.content().contentItemList().get(2)).text())
731-
.isEqualTo("And the second with 'By the way'.");
732724
var userMessage = result.getAllMessages().get(1);
733725
assertThat(userMessage.role()).isEqualTo("user");
734-
assertThat(userMessage.content().contentItemList()).hasSize(4);
726+
assertThat(userMessage.content().contentItemList()).hasSize(2);
735727
assertThat(userMessage.content().contentItemList().get(0)).isInstanceOf(TextItem.class);
736728
assertThat(((TextItem) userMessage.content().contentItemList().get(0)).text())
737729
.isEqualTo("What is in this image?");
738-
assertThat(userMessage.content().contentItemList().get(1)).isInstanceOf(TextItem.class);
739-
assertThat(((TextItem) userMessage.content().contentItemList().get(1)).text())
740-
.isEqualTo("How many letters can you see?");
741-
assertThat(userMessage.content().contentItemList().get(2)).isInstanceOf(TextItem.class);
742-
assertThat(((TextItem) userMessage.content().contentItemList().get(2)).text())
743-
.isEqualTo("What is the main color?");
744-
assertThat(userMessage.content().contentItemList().get(3)).isInstanceOf(ImageItem.class);
745-
assertThat(((ImageItem) userMessage.content().contentItemList().get(3)).imageUrl())
730+
assertThat(userMessage.content().contentItemList().get(1)).isInstanceOf(ImageItem.class);
731+
assertThat(((ImageItem) userMessage.content().contentItemList().get(1)).imageUrl())
746732
.isEqualTo(
747733
"https://upload.wikimedia.org/wikipedia/commons/thumb/5/59/SAP_2011_logo.svg/440px-SAP_2011_logo.svg.png");
748734
var assistantMessage = result.getAllMessages().get(2);
@@ -751,51 +737,51 @@ void testRequestWithMultiChatMessage() throws IOException {
751737
assertThat(assistantMessage.content().contentItemList().get(0)).isInstanceOf(TextItem.class);
752738
assertThat(((TextItem) assistantMessage.content().contentItemList().get(0)).text())
753739
.isEqualTo(
754-
"Well, the image features the logo of SAP, which consists of three letters: S, A, and P. By the way, the main color of the logo is a gradient of blue.");
740+
"Well, this image features the logo of SAP, a multinational software corporation known for its enterprise resource planning (ERP) software. The logo is prominently displayed in white against a gradient blue background.");
755741

756742
assertThat(response).isNotNull();
757-
assertThat(response.getRequestId()).isEqualTo("3330c0df-2811-4849-9855-c40b170424bd");
743+
assertThat(response.getRequestId()).isEqualTo("4709911e-bc40-48d9-8464-17d99b8329c6");
758744
assertThat(response.getModuleResults()).isNotNull();
759745
assertThat(response.getModuleResults().getTemplating()).hasSize(2);
760746

761747
var llmResults = (LLMModuleResultSynchronous) response.getModuleResults().getLlm();
762748
assertThat(llmResults).isNotNull();
763-
assertThat(llmResults.getId()).isEqualTo("chatcmpl-AwtHVWwArgAtB81ankVnNASpTMYKU");
749+
assertThat(llmResults.getId()).isEqualTo("chatcmpl-Ax9PbdnAUqOb4geJBr9k1mxKmCHes");
764750
assertThat(llmResults.getObject()).isEqualTo("chat.completion");
765-
assertThat(llmResults.getCreated()).isEqualTo(1738598889);
751+
assertThat(llmResults.getCreated()).isEqualTo(1738660895);
766752
assertThat(llmResults.getModel()).isEqualTo("gpt-4o-mini-2024-07-18");
767753
assertThat(llmResults.getSystemFingerprint()).isEqualTo("fp_f3927aa00d");
768754
assertThat(llmResults.getChoices()).hasSize(1);
769755
assertThat(llmResults.getChoices().get(0).getMessage().getContent())
770756
.isEqualTo(
771-
"Well, the image features the logo of SAP, which consists of three letters: S, A, and P. By the way, the main color of the logo is a gradient of blue.");
757+
"Well, this image features the logo of SAP, a multinational software corporation known for its enterprise resource planning (ERP) software. The logo is prominently displayed in white against a gradient blue background.");
772758
assertThat(llmResults.getChoices().get(0).getFinishReason()).isEqualTo("stop");
773759
assertThat(llmResults.getChoices().get(0).getMessage().getRole()).isEqualTo("assistant");
774760
assertThat(llmResults.getChoices().get(0).getIndex()).isZero();
775-
assertThat(llmResults.getUsage().getCompletionTokens()).isEqualTo(39);
776-
assertThat(llmResults.getUsage().getPromptTokens()).isEqualTo(265);
777-
assertThat(llmResults.getUsage().getTotalTokens()).isEqualTo(304);
761+
assertThat(llmResults.getUsage().getCompletionTokens()).isEqualTo(38);
762+
assertThat(llmResults.getUsage().getPromptTokens()).isEqualTo(243);
763+
assertThat(llmResults.getUsage().getTotalTokens()).isEqualTo(281);
778764

779765
var orchestrationResult = (LLMModuleResultSynchronous) response.getOrchestrationResult();
780766
assertThat(orchestrationResult).isNotNull();
781-
assertThat(orchestrationResult.getId()).isEqualTo("chatcmpl-AwtHVWwArgAtB81ankVnNASpTMYKU");
767+
assertThat(orchestrationResult.getId()).isEqualTo("chatcmpl-Ax9PbdnAUqOb4geJBr9k1mxKmCHes");
782768
assertThat(orchestrationResult.getObject()).isEqualTo("chat.completion");
783-
assertThat(orchestrationResult.getCreated()).isEqualTo(1738598889);
769+
assertThat(orchestrationResult.getCreated()).isEqualTo(1738660895);
784770
assertThat(orchestrationResult.getModel()).isEqualTo("gpt-4o-mini-2024-07-18");
785771
assertThat(orchestrationResult.getSystemFingerprint()).isEqualTo("fp_f3927aa00d");
786772
assertThat(orchestrationResult.getChoices()).hasSize(1);
787773
assertThat(orchestrationResult.getChoices().get(0).getMessage().getContent())
788774
.isEqualTo(
789-
"Well, the image features the logo of SAP, which consists of three letters: S, A, and P. By the way, the main color of the logo is a gradient of blue.");
775+
"Well, this image features the logo of SAP, a multinational software corporation known for its enterprise resource planning (ERP) software. The logo is prominently displayed in white against a gradient blue background.");
790776
assertThat(orchestrationResult.getChoices().get(0).getFinishReason()).isEqualTo("stop");
791777
assertThat(orchestrationResult.getChoices().get(0).getMessage().getRole())
792778
.isEqualTo("assistant");
793779
assertThat(orchestrationResult.getChoices().get(0).getIndex()).isZero();
794-
assertThat(orchestrationResult.getUsage().getCompletionTokens()).isEqualTo(39);
795-
assertThat(orchestrationResult.getUsage().getPromptTokens()).isEqualTo(265);
796-
assertThat(orchestrationResult.getUsage().getTotalTokens()).isEqualTo(304);
780+
assertThat(orchestrationResult.getUsage().getCompletionTokens()).isEqualTo(38);
781+
assertThat(orchestrationResult.getUsage().getPromptTokens()).isEqualTo(243);
782+
assertThat(orchestrationResult.getUsage().getTotalTokens()).isEqualTo(281);
797783

798-
try (var requestInputStream = fileLoader.apply("multiChatMessageRequest.json")) {
784+
try (var requestInputStream = fileLoader.apply("multiMessageRequest.json")) {
799785
final String requestBody = new String(requestInputStream.readAllBytes());
800786
verify(
801787
postRequestedFor(urlPathEqualTo("/completion"))

orchestration/src/test/resources/__files/multiChatMessageResponse.json renamed to orchestration/src/test/resources/__files/multiMessageResponse.json

Lines changed: 13 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"request_id": "3330c0df-2811-4849-9855-c40b170424bd",
2+
"request_id": "4709911e-bc40-48d9-8464-17d99b8329c6",
33
"module_results": {
44
"templating": [
55
{
@@ -12,10 +12,6 @@
1212
{
1313
"type": "text",
1414
"text": "Start the first sentence with the word 'Well'."
15-
},
16-
{
17-
"type": "text",
18-
"text": "And the second with 'By the way'."
1915
}
2016
]
2117
},
@@ -26,14 +22,6 @@
2622
"type": "text",
2723
"text": "What is in this image?"
2824
},
29-
{
30-
"type": "text",
31-
"text": "How many letters can you see?"
32-
},
33-
{
34-
"type": "text",
35-
"text": "What is the main color?"
36-
},
3725
{
3826
"type": "image_url",
3927
"image_url": {
@@ -45,48 +33,48 @@
4533
}
4634
],
4735
"llm": {
48-
"id": "chatcmpl-AwtHVWwArgAtB81ankVnNASpTMYKU",
36+
"id": "chatcmpl-Ax9PbdnAUqOb4geJBr9k1mxKmCHes",
4937
"object": "chat.completion",
50-
"created": 1738598889,
38+
"created": 1738660895,
5139
"model": "gpt-4o-mini-2024-07-18",
5240
"system_fingerprint": "fp_f3927aa00d",
5341
"choices": [
5442
{
5543
"index": 0,
5644
"message": {
5745
"role": "assistant",
58-
"content": "Well, the image features the logo of SAP, which consists of three letters: S, A, and P. By the way, the main color of the logo is a gradient of blue."
46+
"content": "Well, this image features the logo of SAP, a multinational software corporation known for its enterprise resource planning (ERP) software. The logo is prominently displayed in white against a gradient blue background."
5947
},
6048
"finish_reason": "stop"
6149
}
6250
],
6351
"usage": {
64-
"completion_tokens": 39,
65-
"prompt_tokens": 265,
66-
"total_tokens": 304
52+
"completion_tokens": 38,
53+
"prompt_tokens": 243,
54+
"total_tokens": 281
6755
}
6856
}
6957
},
7058
"orchestration_result": {
71-
"id": "chatcmpl-AwtHVWwArgAtB81ankVnNASpTMYKU",
59+
"id": "chatcmpl-Ax9PbdnAUqOb4geJBr9k1mxKmCHes",
7260
"object": "chat.completion",
73-
"created": 1738598889,
61+
"created": 1738660895,
7462
"model": "gpt-4o-mini-2024-07-18",
7563
"system_fingerprint": "fp_f3927aa00d",
7664
"choices": [
7765
{
7866
"index": 0,
7967
"message": {
8068
"role": "assistant",
81-
"content": "Well, the image features the logo of SAP, which consists of three letters: S, A, and P. By the way, the main color of the logo is a gradient of blue."
69+
"content": "Well, this image features the logo of SAP, a multinational software corporation known for its enterprise resource planning (ERP) software. The logo is prominently displayed in white against a gradient blue background."
8270
},
8371
"finish_reason": "stop"
8472
}
8573
],
8674
"usage": {
87-
"completion_tokens": 39,
88-
"prompt_tokens": 265,
89-
"total_tokens": 304
75+
"completion_tokens": 38,
76+
"prompt_tokens": 243,
77+
"total_tokens": 281
9078
}
9179
}
9280
}

orchestration/src/test/resources/multiChatMessageRequest.json renamed to orchestration/src/test/resources/multiMessageRequest.json

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@
1919
"content" : [ {
2020
"type" : "text",
2121
"text" : "What is in this image?"
22-
}, {
23-
"type" : "text",
24-
"text" : "How many letters can you see?"
25-
}, {
26-
"type" : "text",
27-
"text" : "What is the main color?"
2822
}, {
2923
"type" : "image_url",
3024
"image_url" : {
@@ -48,9 +42,6 @@
4842
}, {
4943
"type" : "text",
5044
"text" : "Start the first sentence with the word 'Well'."
51-
}, {
52-
"type" : "text",
53-
"text" : "And the second with 'By the way'."
5445
} ]
5546
} ]
5647
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,7 @@ Object grounding(
193193

194194
@GetMapping("/image")
195195
@Nonnull
196-
Object imageInput(
197-
@RequestParam(value = "format", required = false) final String format) {
196+
Object imageInput(@RequestParam(value = "format", required = false) final String format) {
198197
final var response =
199198
service.imageInput(
200199
"https://upload.wikimedia.org/wikipedia/commons/thumb/5/59/SAP_2011_logo.svg/440px-SAP_2011_logo.svg.png");
@@ -206,8 +205,7 @@ Object imageInput(
206205

207206
@GetMapping("/multiString")
208207
@Nonnull
209-
Object multiStringInput(
210-
@RequestParam(value = "format", required = false) final String format) {
208+
Object multiStringInput(@RequestParam(value = "format", required = false) final String format) {
211209
final var response =
212210
service.multiStringInput(
213211
List.of("What is the capital of France?", "What is Chess about?", "What is 2+2?"));

0 commit comments

Comments
 (0)