From 7b82bebf185cf0f623bceccdc8390cc9a7819e84 Mon Sep 17 00:00:00 2001
From: Till Kurek <132505079+TillK17@users.noreply.github.com>
Date: Thu, 27 Feb 2025 17:24:02 +0100
Subject: [PATCH 01/10] first 2 steps of advanced orchestration tutorial
---
.../ai-core-orchestration-consumption-opt.md | 85 +++++++++++++++++--
1 file changed, 80 insertions(+), 5 deletions(-)
diff --git a/tutorials/ai-core-orchestration-consumption-opt/ai-core-orchestration-consumption-opt.md b/tutorials/ai-core-orchestration-consumption-opt/ai-core-orchestration-consumption-opt.md
index 2f607ecb53..b2fa42273c 100644
--- a/tutorials/ai-core-orchestration-consumption-opt/ai-core-orchestration-consumption-opt.md
+++ b/tutorials/ai-core-orchestration-consumption-opt/ai-core-orchestration-consumption-opt.md
@@ -43,7 +43,7 @@ Familiarity with the orchestration workflow is recommended
[OPTION END]
-[OPTION BEGIN [Gen AI Hub SDK]]
+[OPTION BEGIN [Python SDK]]
- **In this tutorial**, we will build upon the orchestration framework introduced in [Tutorial](https://developers.sap.com/tutorials/ai-core-orchestration-consumption.html). The focus will shift from basic orchestration to leveraging optional advanced modules to enhance data privacy and refine response quality. These enhancements include:
@@ -73,7 +73,7 @@ print(cv_content)
[OPTION END]
-[OPTION BEGIN [SAP Cloud SDK]]
+[OPTION BEGIN [JavaScript SDK]]
- **In this tutorial**, we will build upon the orchestration framework introduced in [Tutorial](https://developers.sap.com/tutorials/ai-core-orchestration-consumption.html). The focus will shift from basic orchestration to leveraging optional advanced modules to enhance data privacy and refine response quality. These enhancements include:
@@ -114,6 +114,43 @@ console.log(txtContent);
[OPTION END]
+[OPTION BEGIN [Java SDK]]
+
+- **In this tutorial**, we will build upon the orchestration framework introduced in [Tutorial](https://developers.sap.com/tutorials/ai-core-orchestration-consumption.html). The focus will shift from basic orchestration to leveraging optional advanced modules to enhance data privacy and refine response quality. These enhancements include:
+
+ - **Data Masking**: Hiding sensitive information like phone numbers, organizational details, or personal identifiers.
+
+ - **Content Filtering**: Screening for categories such as hate speech, self-harm, explicit content, and violence to ensure safe and relevant responses.
+
+- Here, we extend the use case introduced in Previous Tutorial, where orchestration was executed without incorporating data masking or content filtering. Here, we will include these advanced modules to improve data privacy, security, and response quality.
+
+**NOTE** : If you are continuing with the same project from the previous tutorial, skip steps 1 and 2. Otherwise, create a new Java Maven project using the already deployed orchestration URL to access the Harmonized API. Please find detailed information on orchestration configuration and deployment in the previous tutorial or our [GitHub repository](https://github.com/SAP/ai-sdk-java).
+
+
+
+- The [cv.txt](img/cv.txt) file, containing the resume content, must be added to the working directory. Use the following code to load the file content:
+
+
+```java
+// Adapt filepath to the location you stored the file
+var filePath = "path/to/cv.txt";
+
+// Read file into string
+String cvContent;
+try {
+ cvContent = new String(Files.readAllBytes(Paths.get(filePath)));
+} catch (IOException e) {
+ throw new RuntimeException(e);
+}
+
+// Print file content
+System.out.println(cvContent);
+
+```
+
+[OPTION END]
+
+
[OPTION BEGIN [Bruno]]
- **In this tutorial**, we will build upon the orchestration framework introduced in [Tutorial](https://developers.sap.com/tutorials/ai-core-orchestration-consumption.html). The focus will shift from basic orchestration to leveraging optional advanced modules to enhance data privacy and refine response quality. These enhancements include:
@@ -226,7 +263,7 @@ You are a helpful AI assistant for HR. Summarize the following CV in 10 sentence
[OPTION END]
-[OPTION BEGIN [Gen AI SDK]]
+[OPTION BEGIN [Python SDK]]
- To define how the AI should process the resume, we need a template comprising **SystemMessage** and **UserMessage** components:
@@ -271,7 +308,7 @@ models = [
```
[OPTION END]
-[OPTION BEGIN [SAP Cloud SDK ]]
+[OPTION BEGIN [JavaScript SDK ]]
- To define how the AI should process the resume, we need a template comprising **SystemMessage** and **UserMessage** components:
@@ -316,6 +353,44 @@ const models = [
[OPTION END]
+[OPTION BEGIN [Java SDK]]
+
+The next step involves creating the prompt for the LLM including both `SystemMessage` and `UserMessage` components.
+
+• `SystemMessage`: Defines the AI assistant's role and instructions.
+
+• `UserMessage`: Represents the user's input (i.e., the CV content) to be processed by the LLM.
+
+```java
+// Define system and user messages for prompt
+var systemMessage = Message.system(
+ """
+ You are an AI assistant designed to screen resumes for HR purposes.
+ Please assess the candidate qualifications based on the provided resume.
+ """
+);
+var userMessage = Message.user("Candidate Resume: \n" + cvContent);
+
+// Define the prompt for resume screening
+var prompt = new OrchestrationPrompt(systemMessage, userMessage);
+
+```
+
+
+We can define model parameters and a list of models to use. Only use those models that are already deployed in your instances. For this example, we have selected the following parameters and models:
+
+```java
+// List of models with parameters to iterate through, can be adapted if desired
+var models = Stream.of(
+ OrchestrationAiModel.GPT_4O,
+ OrchestrationAiModel.MISTRAL_LARGE_INSTRUCT,
+ OrchestrationAiModel.CLAUDE_3_5_SONNET
+ ).map(model -> model.withParam(MAX_TOKENS, 1000).withParam(TEMPERATURE, 0.6)).toList();
+
+```
+
+[OPTION END]
+
### Setting Up Data Masking Parameters
[OPTION BEGIN [AI Launchpad]]
@@ -337,7 +412,7 @@ const models = [
[OPTION END]
-[OPTION BEGIN [Gen AI SDK]]
+[OPTION BEGIN [Python SDK]]
- The **Data Masking** Module ensures data privacy by anonymizing or pseudonymizing sensitive information before it is processed.
From 9aa1e107f1d0cb2d423d3135b44826fb8dcdc3e9 Mon Sep 17 00:00:00 2001
From: Till Kurek <132505079+TillK17@users.noreply.github.com>
Date: Wed, 5 Mar 2025 10:28:23 +0100
Subject: [PATCH 02/10] Reintegration of java content in first orchestration
tutorial
---
.../ai-core-orchestration-consumption.md | 247 +++++++++++++++++-
1 file changed, 234 insertions(+), 13 deletions(-)
diff --git a/tutorials/ai-core-orchestration-consumption/ai-core-orchestration-consumption.md b/tutorials/ai-core-orchestration-consumption/ai-core-orchestration-consumption.md
index 7b0ec49079..ba6e8ae0e2 100644
--- a/tutorials/ai-core-orchestration-consumption/ai-core-orchestration-consumption.md
+++ b/tutorials/ai-core-orchestration-consumption/ai-core-orchestration-consumption.md
@@ -35,7 +35,7 @@ author_profile: https://github.com/I321506
[OPTION END]
-[OPTION BEGIN [Gen AI Hub SDK]]
+[OPTION BEGIN [Python SDK]]
• Configure proxy modules by setting up environment variables for AI Core credentials.
@@ -48,7 +48,7 @@ author_profile: https://github.com/I321506
[OPTION END]
-[OPTION BEGIN [SAP Cloud SDK]]
+[OPTION BEGIN [JavaScript SDK]]
• Download the service key for the AI Core service instance.
@@ -66,9 +66,9 @@ author_profile: https://github.com/I321506
• SAP Cloud SDK for AI: Uses the `dotenv` library to load environment variables. If you encounter issues with the dotenv library, ensure it is installed correctly by running:
- ```javascript
- npm install dotenv
- ```
+ ```javascript
+ npm install dotenv
+ ```
```javascript
import dotenv from 'dotenv';
@@ -83,6 +83,40 @@ console.log(process.env.AICORE_SERVICE_KEY);
[OPTION END]
+[OPTION BEGIN [Java SDK]]
+
+• [Create a service key](https://help.sap.com/docs/sap-ai-core/sap-ai-core-service-guide/create-service-key) for your AI Core instance and copy the JSON object.
+
+• Create a `.env` file in the **working directory from which you run the code**. Add the following line using the copied JSON:
+
+```dotenv
+AICORE_SERVICE_KEY={"clientid": "...", "clientsecret": "...", "url": "...", "serviceurls": { "AI_API_URL": "..." } }
+```
+
+• **IMPORTANT:** The value of `AICORE_SERVICE_KEY` must be a single line, so remove any line breaks from the service key JSON.
+
+• This tutorial is designed for a Java [maven project](https://maven.apache.org/guides/getting-started/maven-in-five-minutes.html). Add the following dependencies to your project `pom.xml` file:
+
+```xml
+
+ com.sap.ai.sdk
+ core
+
+ ${ai-sdk.version}
+
+
+
+ com.sap.ai.sdk
+ orchestration
+
+ ${ai-sdk.version}
+
+```
+
+• For other options of access configuration and detailed information on installation and usage of the **SAP Cloud SDK for AI (for Java)**, visit the official [GitHub repository](https://github.com/SAP/ai-sdk-java). This page provides comprehensive steps to set up and integrate the SDK effectively in your projects.
+
+[OPTION END]
+
[OPTION BEGIN [Bruno]]
#### Download and Import the Bruno Collection
- Download the [bruno_collections](img/Bruno_Collection.json) file
@@ -148,7 +182,7 @@ Go to the Configuration section within your chosen Resource Group.
[OPTION END]
-[OPTION BEGIN [Gen AI SDK]]
+[OPTION BEGIN [Python SDK]]
• Create a folder named orchestration, then navigate to this folder using VS Code.
@@ -206,7 +240,7 @@ print(f"Configuration created successfully with ID: {config.id} and Name: {confi
[OPTION END]
-[OPTION BEGIN [SAP Cloud SDK ]]
+[OPTION BEGIN [JavaScript SDK]]
In this step, we define a function to create an orchestration configuration using the ConfigurationApi from the SAP AI SDK. This configuration integrates various parameters needed for orchestration, such as the executable ID and scenario ID.
@@ -259,6 +293,45 @@ orchestrationConfig;
• config_name: Choose a unique name for the configuration (e.g., "config-new-orchestration")
+[OPTION END]
+
+[OPTION BEGIN [Java SDK]]
+
+In this step, we will create an orchestration configuration using the core module of the [SAP Cloud SDK for Java](https://github.com/SAP/cloud-sdk-java). This configuration integrates various parameters needed for orchestration, such as the executable ID and scenario ID.
+
+• Add the following code to your project to create an orchestration configuration:
+
+```java
+// Define the resource group, change this to your resource group name
+var RESOURCE_GROUP = "yourResourceGroup";
+
+// Define parameter and input artifact bindings you may need for orchestration
+var modelFilterList = AiParameterArgumentBinding.create()
+ .key("modelFilterList").value("null");
+var modelFilterListType = AiParameterArgumentBinding.create()
+ .key("modelFilterListType").value("allow");
+
+// Create a configuration data object for your configuration
+var configurationData = AiConfigurationBaseData.create()
+ .name("orchestration-config") // Choose a meaningful name
+ .executableId("orchestration") // Orchestration executable ID
+ .scenarioId("orchestration") // Orchestration scenario ID
+ .addParameterBindingsItem(modelFilterList)
+ .addParameterBindingsItem(modelFilterListType);
+
+// Create the configuration with your individual resource group
+var configuration = new ConfigurationApi().create(RESOURCE_GROUP, configurationData);
+
+// Print the configuration response message
+System.out.println(configuration.getMessage());
+```
+
+**Note**:
+
+• `scenarioId` and `executableId`: Both are set to "orchestration" for this tutorial.
+
+• `name`: Choose a unique name for the configuration (e.g., "config-new-orchestration")
+
[OPTION END]
[OPTION BEGIN [Bruno]]
@@ -299,7 +372,7 @@ Once the deployment begins, continue to the status page. Verify that the Deploym
[OPTION END]
-[OPTION BEGIN [Gen AI SDK]]
+[OPTION BEGIN [Python SDK]]
With the configuration ID, you can proceed to deploy the orchestration and monitor its progress.
@@ -357,7 +430,7 @@ Result: The code will display a loading spinner until the deployment status upda
[OPTION END]
-[OPTION BEGIN [SAP Cloud SDK ]]
+[OPTION BEGIN [JavaScript SDK]]
This step involves creating a deployment using the specified configuration and resource group. The deployment is handled via the DeploymentApi, which streamlines the process of activating the orchestration setup.
@@ -422,6 +495,26 @@ export async function deployOrchestration(
[OPTION END]
+[OPTION BEGIN [Java SDK]]
+
+In this step, we will create a deployment from the configuration created in the previous step using the core module of the [SAP Cloud SDK for Java](https://github.com/SAP/cloud-sdk-java).
+
+• Add the following code to your project to create an orchestration deployment:
+
+```java
+// Create a deployment creation request with the ID of the created configuration
+var deploymentCreationRequest =
+ AiDeploymentCreationRequest.create().configurationId(configuration.getId());
+
+// Create the deployment with the deployment creation request
+var deployment = new DeploymentApi().create(RESOURCE_GROUP, deploymentCreationRequest);
+
+// Print the deployment response message
+System.out.println(deployment.getMessage());
+```
+
+[OPTION END]
+
[OPTION BEGIN [Bruno]]
#### Update Configuration ID and Create Deployment
- Navigate to the create_deployment request.
@@ -602,7 +695,7 @@ Data masking and content filtering are available to enhance data privacy and saf
[OPTION END]
-[OPTION BEGIN [Gen AI SDK]]
+[OPTION BEGIN [Python SDK]]
To begin the consumption process for the orchestration you’ve deployed, follow the process below:
@@ -734,7 +827,7 @@ Data masking and content filtering are available to enhance data privacy and saf
[OPTION END]
-[OPTION BEGIN [SAP Cloud SDK ]]
+[OPTION BEGIN [JavaScript SDK]]
To begin the consumption process for the orchestration you’ve deployed, follow the process below:
@@ -775,7 +868,7 @@ const templateConfig = {
template: [
{
role: 'system',
- content: 'You are an AI assistant designed to screen resumes for HR purposes. Please assess the candidate qualifications based on the provided resume.',
+ content: 'You are a helpful AI assistant for HR. Summarize the following CV in 10 sentences, focusing on key qualifications, work experience, and achievements. Include personal contact information, organizational history, and personal interests.',
},
{
role: 'user',
@@ -895,6 +988,134 @@ Data masking and content filtering are available to enhance data privacy and saf
[OPTION END]
+[OPTION BEGIN [Java SDK]]
+
+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](https://github.com/SAP/cloud-sdk-java).
+
+To begin the consumption process, follow the steps below:
+
+
+**Prepare the CV File**
+
+• Download the [cv.txt](img/cv.txt) file, which contains the CV used this tutorial. Add it to your project.
+
+• Read the CV file from the correct path using the following code:
+
+```java
+// Adapt filepath to the location you stored the file
+var filePath = "path/to/cv.txt";
+
+// Read file into string
+String cvContent;
+try {
+ cvContent = new String(Files.readAllBytes(Paths.get(filePath)));
+} catch (IOException e) {
+ throw new RuntimeException(e);
+}
+
+// Print file content
+System.out.println(cvContent);
+
+```
+
+The next step involves creating the prompt for the LLM including both `SystemMessage` and `UserMessage` components.
+
+• `SystemMessage`: Defines the AI assistant's role and instructions.
+
+• `UserMessage`: Represents the user's input (i.e., the CV content) to be processed by the LLM.
+
+```java
+// Define system and user messages for prompt
+var systemMessage = Message.system(
+ """
+ You are a helpful AI assistant for HR. Summarize the following CV in 10 sentences,
+ using on key qualifications, work experience, and achievements. Include personal contact information,
+ organizational history, and personal interests.
+ """
+);
+var userMessage = Message.user("Candidate Resume: \n" + cvContent);
+
+// Define the prompt for resume screening
+var prompt = new OrchestrationPrompt(systemMessage, userMessage);
+
+```
+
+
+We can define model parameters and a list of models to use. Only use those models that are already deployed in your instances. For this example, we have selected the following parameters and models:
+
+```java
+// List of models with parameters to iterate through, can be adapted if desired
+var models = Stream.of(
+ OrchestrationAiModel.GPT_4O,
+ OrchestrationAiModel.MISTRAL_LARGE_INSTRUCT,
+ OrchestrationAiModel.CLAUDE_3_5_SONNET
+ ).map(model -> model.withParam(MAX_TOKENS, 1000).withParam(TEMPERATURE, 0.6)).toList();
+
+```
+
+
+The following function writes the responses from different models, stored in a list, to a file:
+
+```java
+// Function writitng responses to a file
+void createFileFromResponses (ArrayList