Skip to content

Commit d921c49

Browse files
authored
[Orchestration] Convenience Message on Response (#205)
* Response returns convenience Message - Mark content getter beta - Improve e2e test for continuous message interaction. * Update docs --------- Co-authored-by: Roshin Rajan Panackal <[email protected]>
1 parent 61d3fc5 commit d921c49

File tree

6 files changed

+40
-37
lines changed

6 files changed

+40
-37
lines changed

docs/guides/ORCHESTRATION_CHAT_COMPLETION.md

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,7 @@ The LLM response is available as the first choice under the `result.getOrchestra
102102
Use a prepared template and execute requests with by passing only the input parameters:
103103

104104
```java
105-
var template =
106-
ChatMessage.create()
107-
.role("user")
108-
.content("Reply with 'Orchestration Service is working!' in {{?language}}");
105+
var template = Message.user("Reply with 'Orchestration Service is working!' in {{?language}}");
109106
var templatingConfig = TemplatingModuleConfig.create().template(template);
110107
var configWithTemplate = config.withTemplateConfig(templatingConfig);
111108

@@ -124,10 +121,10 @@ Include a message history to maintain context in the conversation:
124121
```java
125122
var messagesHistory =
126123
List.of(
127-
ChatMessage.create().role("user").content("What is the capital of France?"),
128-
ChatMessage.create().role("assistant").content("The capital of France is Paris."));
124+
Message.user("What is the capital of France?"),
125+
Message.assistant("The capital of France is Paris."));
129126
var message =
130-
ChatMessage.create().role("user").content("What is the typical food there?");
127+
Message.user("What is the typical food there?");
131128

132129
var prompt = new OrchestrationPrompt(message).messageHistory(messagesHistory);
133130

@@ -175,12 +172,8 @@ var maskingConfig =
175172
DpiMasking.anonymization().withEntities(DPIEntities.PHONE, DPIEntities.PERSON);
176173
var configWithMasking = config.withMaskingConfig(maskingConfig);
177174

178-
var systemMessage = ChatMessage.create()
179-
.role("system")
180-
.content("Please evaluate the following user feedback and judge if the sentiment is positive or negative.");
181-
var userMessage = ChatMessage.create()
182-
.role("user")
183-
.content("""
175+
var systemMessage = Message.system("Please evaluate the following user feedback and judge if the sentiment is positive or negative.");
176+
var userMessage = Message.user("""
184177
I think the SDK is good, but could use some further enhancements.
185178
My architect Alice and manager Bob pointed out that we need the grounding capabilities, which aren't supported yet.
186179
""");

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.sap.ai.sdk.orchestration;
22

3+
import com.google.common.annotations.Beta;
34
import com.sap.ai.sdk.orchestration.model.ChatMessage;
45
import javax.annotation.Nonnull;
56

@@ -63,5 +64,6 @@ default ChatMessage createChatMessage() {
6364
* @return the content.
6465
*/
6566
@Nonnull
67+
@Beta
6668
String content();
6769
}

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import com.sap.ai.sdk.orchestration.model.TokenUsage;
1010
import java.util.ArrayList;
1111
import java.util.List;
12-
import java.util.Objects;
1312
import javax.annotation.Nonnull;
1413
import lombok.RequiredArgsConstructor;
1514
import lombok.Value;
@@ -54,10 +53,21 @@ public TokenUsage getTokenUsage() {
5453
* @return A list of all messages.
5554
*/
5655
@Nonnull
57-
public List<ChatMessage> getAllMessages() {
58-
final var items = Objects.requireNonNull(originalResponse.getModuleResults().getTemplating());
59-
final var messages = new ArrayList<>(items);
60-
messages.add(getCurrentChoice().getMessage());
56+
public List<Message> getAllMessages() {
57+
final var messages = new ArrayList<Message>();
58+
59+
for (final ChatMessage chatMessage : originalResponse.getModuleResults().getTemplating()) {
60+
final var message =
61+
switch (chatMessage.getRole()) {
62+
case "user" -> new UserMessage(chatMessage.getContent());
63+
case "assistant" -> new AssistantMessage(chatMessage.getContent());
64+
case "system" -> new SystemMessage(chatMessage.getContent());
65+
default -> throw new IllegalStateException("Unexpected role: " + chatMessage.getRole());
66+
};
67+
messages.add(message);
68+
}
69+
70+
messages.add(new AssistantMessage(getCurrentChoice().getMessage().getContent()));
6171
return messages;
6272
}
6373

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ void testTemplating() throws IOException {
129129

130130
final var response = result.getOriginalResponse();
131131
assertThat(response.getRequestId()).isEqualTo("26ea36b5-c196-4806-a9a6-a686f0c6ad91");
132-
assertThat(result.getAllMessages().get(0).getContent())
132+
assertThat(result.getAllMessages().get(0).content())
133133
.isEqualTo("Reply with 'Orchestration Service is working!' in German");
134-
assertThat(result.getAllMessages().get(0).getRole()).isEqualTo("user");
134+
assertThat(result.getAllMessages().get(0).role()).isEqualTo("user");
135135
var llm = (LLMModuleResultSynchronous) response.getModuleResults().getLlm();
136136
assertThat(llm).isNotNull();
137137
assertThat(llm.getId()).isEqualTo("chatcmpl-9lzPV4kLrXjFckOp2yY454wksWBoj");

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import static com.sap.ai.sdk.orchestration.OrchestrationAiModel.GPT_35_TURBO;
44
import static com.sap.ai.sdk.orchestration.OrchestrationAiModel.Parameter.TEMPERATURE;
55

6-
import com.sap.ai.sdk.orchestration.AssistantMessage;
76
import com.sap.ai.sdk.orchestration.AzureContentFilter;
87
import com.sap.ai.sdk.orchestration.AzureFilterThreshold;
98
import com.sap.ai.sdk.orchestration.DpiMasking;
@@ -12,8 +11,6 @@
1211
import com.sap.ai.sdk.orchestration.OrchestrationClient;
1312
import com.sap.ai.sdk.orchestration.OrchestrationModuleConfig;
1413
import com.sap.ai.sdk.orchestration.OrchestrationPrompt;
15-
import com.sap.ai.sdk.orchestration.SystemMessage;
16-
import com.sap.ai.sdk.orchestration.UserMessage;
1714
import com.sap.ai.sdk.orchestration.model.DPIEntities;
1815
import com.sap.ai.sdk.orchestration.model.Template;
1916
import java.util.List;
@@ -54,7 +51,7 @@ public OrchestrationChatResponse completion() {
5451
@Nonnull
5552
public OrchestrationChatResponse template() {
5653
final var template =
57-
new UserMessage("Reply with 'Orchestration Service is working!' in {{?language}}");
54+
Message.user("Reply with 'Orchestration Service is working!' in {{?language}}");
5855
final var templatingConfig = Template.create().template(List.of(template.createChatMessage()));
5956
final var configWithTemplate = config.withTemplateConfig(templatingConfig);
6057

@@ -72,15 +69,16 @@ public OrchestrationChatResponse template() {
7269
@GetMapping("/messagesHistory")
7370
@Nonnull
7471
public OrchestrationChatResponse messagesHistory() {
75-
final List<Message> messagesHistory =
76-
List.of(
77-
new UserMessage("What is the capital of France?"),
78-
new AssistantMessage("The capital of France is Paris."));
79-
final var message = new UserMessage("What is the typical food there?");
72+
final var prompt = new OrchestrationPrompt(Message.user("What is the capital of France?"));
8073

81-
final var prompt = new OrchestrationPrompt(message).messageHistory(messagesHistory);
74+
final var result = client.chatCompletion(prompt, config);
8275

83-
return client.chatCompletion(prompt, config);
76+
// Let's presume a user asks the following follow-up question
77+
final var nextPrompt =
78+
new OrchestrationPrompt(Message.user("What is the typical food there?"))
79+
.messageHistory(result.getAllMessages());
80+
81+
return client.chatCompletion(nextPrompt, config);
8482
}
8583

8684
/**
@@ -120,10 +118,10 @@ public OrchestrationChatResponse filter(
120118
@Nonnull
121119
public OrchestrationChatResponse maskingAnonymization() {
122120
final var systemMessage =
123-
new SystemMessage(
121+
Message.system(
124122
"Please evaluate the following user feedback and judge if the sentiment is positive or negative.");
125123
final var userMessage =
126-
new UserMessage(
124+
Message.user(
127125
"""
128126
I think the SDK is good, but could use some further enhancements.
129127
My architect Alice and manager Bob pointed out that we need the grounding capabilities, which aren't supported yet.
@@ -146,13 +144,13 @@ public OrchestrationChatResponse maskingAnonymization() {
146144
@Nonnull
147145
public OrchestrationChatResponse maskingPseudonymization() {
148146
final var systemMessage =
149-
new SystemMessage(
147+
Message.system(
150148
"""
151149
Please write an initial response to the below user feedback, stating that we are working on the feedback and will get back to them soon.
152150
Please make sure to address the user in person and end with "Best regards, the AI SDK team".
153151
""");
154152
final var userMessage =
155-
new UserMessage(
153+
Message.user(
156154
"""
157155
Username: Mallory
158156
userEmail: [email protected]

sample-code/spring-app/src/test/java/com/sap/ai/sdk/app/controllers/OrchestrationTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ void testTemplate() {
4040
final var response = result.getOriginalResponse();
4141

4242
assertThat(response.getRequestId()).isNotEmpty();
43-
assertThat(result.getAllMessages().get(0).getContent())
43+
assertThat(result.getAllMessages().get(0).content())
4444
.isEqualTo("Reply with 'Orchestration Service is working!' in German");
45-
assertThat(result.getAllMessages().get(0).getRole()).isEqualTo("user");
45+
assertThat(result.getAllMessages().get(0).role()).isEqualTo("user");
4646
var llm = (LLMModuleResultSynchronous) response.getModuleResults().getLlm();
4747
assertThat(llm.getId()).isNotEmpty();
4848
assertThat(llm.getObject()).isEqualTo("chat.completion");

0 commit comments

Comments
 (0)