Skip to content

Commit 76a7356

Browse files
authored
last step of orchestration tutorial
1 parent 011fc58 commit 76a7356

1 file changed

Lines changed: 156 additions & 10 deletions

File tree

tutorials/ai-core-orchestration-consumption/ai-core-orchestration-consumption.md

Lines changed: 156 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -372,24 +372,24 @@ In this step, we will create an orchestration configuration using the core modul
372372

373373
```java
374374
// Define the resource group, change this to your resource group name
375-
private static final String RESOURCE_GROUP = "default";
375+
var RESOURCE_GROUP = "yourResourceGroup";
376376

377377
// Define parameter and input artifact bindings you may need for orchestration
378-
final var modelFilterList =
379-
AiParameterArgumentBinding.create().key("modelFilterList").value("null");
380-
final var modelFilterListType =
381-
AiParameterArgumentBinding.create().key("modelFilterListType").value("allow");
378+
var modelFilterList = AiParameterArgumentBinding.create()
379+
.key("modelFilterList").value("null");
380+
var modelFilterListType = AiParameterArgumentBinding.create()
381+
.key("modelFilterListType").value("allow");
382382

383383
// Create a configuration data object for your configuration
384-
final var configurationData = AiConfigurationBaseData.create()
384+
var configurationData = AiConfigurationBaseData.create()
385385
.name("orchestration-config") // Choose a meaningful name
386386
.executableId("orchestration") // Orchestration executable ID
387387
.scenarioId("orchestration") // Orchestration scenario ID
388388
.addParameterBindingsItem(modelFilterList)
389389
.addParameterBindingsItem(modelFilterListType);
390390

391391
// Create the configuration with your individual resource group
392-
final var configuration = new ConfigurationApi().create(RESOURCE_GROUP, configurationData);
392+
var configuration = new ConfigurationApi().create(RESOURCE_GROUP, configurationData);
393393

394394
// Print the configuration response message
395395
System.out.println(configuration.getMessage());
@@ -578,12 +578,11 @@ In this step, we will create a deployment from the configuration created in the
578578

579579
```java
580580
// Create a deployment creation request with the ID of the created configuration
581-
final var deploymentCreationRequest =
581+
var deploymentCreationRequest =
582582
AiDeploymentCreationRequest.create().configurationId(configuration.getId());
583583

584584
// Create the deployment with the deployment creation request
585-
final var deployment =
586-
new DeploymentApi().create(RESOURCE_GROUP, deploymentCreationRequest);
585+
var deployment = new DeploymentApi().create(RESOURCE_GROUP, deploymentCreationRequest);
587586

588587
// Print the deployment response message
589588
System.out.println(deployment.getMessage());
@@ -1072,6 +1071,153 @@ Data masking and content filtering are available to enhance data privacy and saf
10721071

10731072
[OPTION END]
10741073

1074+
[OPTION BEGIN [SAP Cloud SDK for Java]]
1075+
1076+
In this step, we will consume an LLM through the orchestration service with the created deployment, using the core and orchestration module of the SAP Cloud SDK for Java.
1077+
1078+
To begin the consumption process for the orchestration you’ve deployed, follow the steps below:
1079+
1080+
**Prepare the CV File**
1081+
1082+
• Download the [cv.txt](img/cv.txt) file, which contains the CV used this tutorial. Add it to your project.
1083+
1084+
• Read the CV file from the correct path using the following code:
1085+
1086+
```java
1087+
// Adapt filepath to the location you stored the file
1088+
var filePath = "path/to/cv.txt";
1089+
1090+
// Read file into string
1091+
String cvContent;
1092+
try {
1093+
cvContent = new String(Files.readAllBytes(Paths.get(filePath)));
1094+
} catch (IOException e) {
1095+
throw new RuntimeException(e);
1096+
}
1097+
1098+
// Print file content
1099+
System.out.println(cvContent);
1100+
1101+
```
1102+
The next step involves creating the prompt for the LLM including both `SystemMessage` and `UserMessage` components.
1103+
1104+
`SystemMessage`: Defines the AI assistant's role and instructions.
1105+
1106+
`UserMessage`: Represents the user's input (i.e., the CV content) to be processed by the LLM.
1107+
1108+
```java
1109+
// Define system and user messages for prompt
1110+
var systemMessage = new SystemMessage(
1111+
"""
1112+
You are an AI assistant designed to screen resumes for HR purposes.
1113+
Please assess the candidate qualifications based on the provided resume.
1114+
"""
1115+
);
1116+
var userMessage = new UserMessage("Candidate Resume: \n" + cvContent);
1117+
1118+
// Define the prompt for resume screening
1119+
var prompt = new OrchestrationPrompt(systemMessage, userMessage);
1120+
1121+
```
1122+
1123+
We can define multiple models for the use case. Only use those models that are already deployed in your instances. For this example, we have selected the following three models:
1124+
1125+
```java
1126+
// List of models to iterate through
1127+
var models = List.of("gpt-4o", "mistralai--mistral-large-instruct", "anthropic--claude-3.5-sonnet");
1128+
```
1129+
1130+
With the following function we create an `OrchestrationModuleConfig` containing information about the `LLMModule`. This can be extended to contain information regarding templating, masking, filtering and grounding, if desired to use these functionality of orchestration.
1131+
1132+
```java
1133+
// Function to create orchestration module configuration
1134+
OrchestrationModuleConfig createModuleConfig(String modelName) {
1135+
var config = LLMModuleConfig.create()
1136+
.modelName(modelName)
1137+
.modelParams(Map.of( // add model parameters as needed
1138+
"max_tokens", 1000,
1139+
"temperature", 0.6
1140+
));
1141+
1142+
return new OrchestrationModuleConfig().withLlmConfig(config);
1143+
}
1144+
```
1145+
1146+
The following function writes the responses from different models, stored in a list, to a file:
1147+
1148+
```java
1149+
// Function writitng responses to a file
1150+
void createFileFromResponses (ArrayList<Map> responses) {
1151+
// Format model responses
1152+
var formattedResponses = responses.stream().
1153+
map(response -> "Response from model " + response.get("model") +
1154+
": \n\n" + response.get("response") + "\n" + "-".repeat(120));
1155+
1156+
// Write model responses to provided file path
1157+
try {
1158+
Files.writeString(Path.of("src/main/resources/static/responses.txt"),
1159+
String.join("\n", formattedResponses.toList()));
1160+
} catch (IOException e) {
1161+
throw new RuntimeException(e);
1162+
}
1163+
}
1164+
```
1165+
1166+
**Generate Responses for Multiple Models**
1167+
1168+
This step outlines the process of generating responses for a set of queries using different models. We iterate through the list of models created earlier and query the model with the created prompt using an `OrchestrationClient`.
1169+
1170+
```java
1171+
// Create the client used for interaction with orchestration service
1172+
var client = new OrchestrationClient(new AiCoreService()
1173+
.getInferenceDestination(RESOURCE_GROUP).forScenario("orchestration"));
1174+
1175+
// A list to store all responses from the different models
1176+
var responses = new ArrayList<Map>();
1177+
1178+
// Iterate through the list of models
1179+
for (var model: models) {
1180+
System.out.println("\n=== Responses for model: %s ===\n".formatted(model));
1181+
1182+
// Create orchestration module configuration for current model
1183+
var moduleConfig = createModuleConfig(model);
1184+
1185+
// Prompt model with orchestration module configuration
1186+
var response = client.chatCompletion(prompt, moduleConfig);
1187+
1188+
// Add response to list of all model responses
1189+
responses.add(Map.of("model", model, "response", response.getContent()));
1190+
1191+
System.out.println(response.getContent());
1192+
}
1193+
1194+
// Write all responses to a file
1195+
createFileFromResponses(responses);
1196+
```
1197+
• If not done automaticaly by your IDE, add the following imports:
1198+
1199+
```java
1200+
import com.sap.ai.sdk.core.AiCoreService;
1201+
1202+
import com.sap.ai.sdk.orchestration.OrchestrationClient;
1203+
import com.sap.ai.sdk.orchestration.OrchestrationModuleConfig;
1204+
import com.sap.ai.sdk.orchestration.OrchestrationPrompt;
1205+
import com.sap.ai.sdk.orchestration.SystemMessage;
1206+
import com.sap.ai.sdk.orchestration.UserMessage;
1207+
import com.sap.ai.sdk.orchestration.model.LLMModuleConfig;
1208+
```
1209+
1210+
1211+
**Important Note**
1212+
1213+
Ensure at least one orchestration deployment is ready to be consumed during this process.
1214+
1215+
**Optional Advanced Modules**
1216+
1217+
Together with document grounding and templating, data masking and content filtering are available to enhance data privacy and safety. Data masking hides sensitive information like phone numbers or organization names, while content filtering can screen for categories such as hate self-harm, sexual content, and violence. In this tutorial, the response generated by the LLM models may carry sensitive information, such as names and phone numbers. For further enhancement, refer to the next tutorial on implementing these modules.
1218+
1219+
[OPTION END]
1220+
10751221
[OPTION BEGIN [Bruno]]
10761222
- Go to the 08_consume_model section in the collection.
10771223

0 commit comments

Comments
 (0)