Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Orchestration] Convenience Message on Response #205

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 6 additions & 13 deletions docs/guides/ORCHESTRATION_CHAT_COMPLETION.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,7 @@ The LLM response is available as the first choice under the `result.getOrchestra
Use a prepared template and execute requests with by passing only the input parameters:

```java
var template =
ChatMessage.create()
.role("user")
.content("Reply with 'Orchestration Service is working!' in {{?language}}");
var template = Message.user("Reply with 'Orchestration Service is working!' in {{?language}}");
var templatingConfig = TemplatingModuleConfig.create().template(template);
var configWithTemplate = config.withTemplateConfig(templatingConfig);

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

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

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

var systemMessage = ChatMessage.create()
.role("system")
.content("Please evaluate the following user feedback and judge if the sentiment is positive or negative.");
var userMessage = ChatMessage.create()
.role("user")
.content("""
var systemMessage = Message.system("Please evaluate the following user feedback and judge if the sentiment is positive or negative.");
var userMessage = Message.user("""
I think the SDK is good, but could use some further enhancements.
My architect Alice and manager Bob pointed out that we need the grounding capabilities, which aren't supported yet.
""");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sap.ai.sdk.orchestration;

import com.google.common.annotations.Beta;
import com.sap.ai.sdk.orchestration.model.ChatMessage;
import javax.annotation.Nonnull;

Expand Down Expand Up @@ -63,5 +64,6 @@ default ChatMessage createChatMessage() {
* @return the content.
*/
@Nonnull
@Beta
String content();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.sap.ai.sdk.orchestration.model.TokenUsage;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nonnull;
import lombok.RequiredArgsConstructor;
import lombok.Value;
Expand Down Expand Up @@ -54,10 +53,21 @@ public TokenUsage getTokenUsage() {
* @return A list of all messages.
*/
@Nonnull
public List<ChatMessage> getAllMessages() {
final var items = Objects.requireNonNull(originalResponse.getModuleResults().getTemplating());
final var messages = new ArrayList<>(items);
messages.add(getCurrentChoice().getMessage());
public List<Message> getAllMessages() {
final var messages = new ArrayList<Message>();

for (final ChatMessage chatMessage : originalResponse.getModuleResults().getTemplating()) {
final var message =
switch (chatMessage.getRole()) {
case "user" -> new UserMessage(chatMessage.getContent());
case "assistant" -> new AssistantMessage(chatMessage.getContent());
case "system" -> new SystemMessage(chatMessage.getContent());
default -> throw new IllegalStateException("Unexpected role: " + chatMessage.getRole());
};
messages.add(message);
}

messages.add(new AssistantMessage(getCurrentChoice().getMessage().getContent()));
return messages;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,9 @@ void testTemplating() throws IOException {

final var response = result.getOriginalResponse();
assertThat(response.getRequestId()).isEqualTo("26ea36b5-c196-4806-a9a6-a686f0c6ad91");
assertThat(result.getAllMessages().get(0).getContent())
assertThat(result.getAllMessages().get(0).content())
.isEqualTo("Reply with 'Orchestration Service is working!' in German");
assertThat(result.getAllMessages().get(0).getRole()).isEqualTo("user");
assertThat(result.getAllMessages().get(0).role()).isEqualTo("user");
var llm = (LLMModuleResultSynchronous) response.getModuleResults().getLlm();
assertThat(llm).isNotNull();
assertThat(llm.getId()).isEqualTo("chatcmpl-9lzPV4kLrXjFckOp2yY454wksWBoj");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static com.sap.ai.sdk.orchestration.OrchestrationAiModel.GPT_35_TURBO;
import static com.sap.ai.sdk.orchestration.OrchestrationAiModel.Parameter.TEMPERATURE;

import com.sap.ai.sdk.orchestration.AssistantMessage;
import com.sap.ai.sdk.orchestration.AzureContentFilter;
import com.sap.ai.sdk.orchestration.AzureFilterThreshold;
import com.sap.ai.sdk.orchestration.DpiMasking;
Expand All @@ -12,8 +11,6 @@
import com.sap.ai.sdk.orchestration.OrchestrationClient;
import com.sap.ai.sdk.orchestration.OrchestrationModuleConfig;
import com.sap.ai.sdk.orchestration.OrchestrationPrompt;
import com.sap.ai.sdk.orchestration.SystemMessage;
import com.sap.ai.sdk.orchestration.UserMessage;
import com.sap.ai.sdk.orchestration.model.DPIEntities;
import com.sap.ai.sdk.orchestration.model.Template;
import java.util.List;
Expand Down Expand Up @@ -54,7 +51,7 @@ public OrchestrationChatResponse completion() {
@Nonnull
public OrchestrationChatResponse template() {
final var template =
new UserMessage("Reply with 'Orchestration Service is working!' in {{?language}}");
Message.user("Reply with 'Orchestration Service is working!' in {{?language}}");
final var templatingConfig = Template.create().template(List.of(template.createChatMessage()));
final var configWithTemplate = config.withTemplateConfig(templatingConfig);

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

final var prompt = new OrchestrationPrompt(message).messageHistory(messagesHistory);
final var result = client.chatCompletion(prompt, config);

return client.chatCompletion(prompt, config);
// Let's presume a user asks the following follow-up question
final var nextPrompt =
new OrchestrationPrompt(Message.user("What is the typical food there?"))
.messageHistory(result.getAllMessages());

return client.chatCompletion(nextPrompt, config);
}

/**
Expand Down Expand Up @@ -120,10 +118,10 @@ public OrchestrationChatResponse filter(
@Nonnull
public OrchestrationChatResponse maskingAnonymization() {
final var systemMessage =
new SystemMessage(
Message.system(
"Please evaluate the following user feedback and judge if the sentiment is positive or negative.");
final var userMessage =
new UserMessage(
Message.user(
"""
I think the SDK is good, but could use some further enhancements.
My architect Alice and manager Bob pointed out that we need the grounding capabilities, which aren't supported yet.
Expand All @@ -146,13 +144,13 @@ public OrchestrationChatResponse maskingAnonymization() {
@Nonnull
public OrchestrationChatResponse maskingPseudonymization() {
final var systemMessage =
new SystemMessage(
Message.system(
"""
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.
Please make sure to address the user in person and end with "Best regards, the AI SDK team".
""");
final var userMessage =
new UserMessage(
Message.user(
"""
Username: Mallory
userEmail: mallory@sap.com
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ void testTemplate() {
final var response = result.getOriginalResponse();

assertThat(response.getRequestId()).isNotEmpty();
assertThat(result.getAllMessages().get(0).getContent())
assertThat(result.getAllMessages().get(0).content())
.isEqualTo("Reply with 'Orchestration Service is working!' in German");
assertThat(result.getAllMessages().get(0).getRole()).isEqualTo("user");
assertThat(result.getAllMessages().get(0).role()).isEqualTo("user");
var llm = (LLMModuleResultSynchronous) response.getModuleResults().getLlm();
assertThat(llm.getId()).isNotEmpty();
assertThat(llm.getObject()).isEqualTo("chat.completion");
Expand Down