SAP MDK integration with SAP AI Core services: Part 5 – Retrieval Augmented Generation

Estimated read time 10 min read

In this series of blogs, I will showcase how to integrate SAP AI Core services into SAP Mobile Development Kit (MDK) to develop mobile applications with AI capabilities.

Part 1: SAP Mobile Development Kit (MDK) integration with SAP AI Core services – SetupPart 2: SAP Mobile Development Kit (MDK) integration with SAP AI Core services – Business Use CasesPart 3: SAP Mobile Development Kit (MDK) integration with SAP AI Core services – Measurement ReadingsPart 4: SAP MDK integration with SAP AI Core services – Anomaly Detection and Maintenance GuidelinesPart 5: SAP Mobile Development Kit (MDK) integration with SAP AI Core services- Retrieval Augmented Generation (Current Blog)

In the previous blog, we showcased how to detect anomalies and generate maintenance guidelines using the SAP AI Core service. In the following blogs, we will explore how to record orders and operations using voice and free text input. To do that, we first need to understand the SAP HANA Cloud vector engine and Retrieval Augmented Generation (RAG)

The SAP HANA Cloud vector engine offers multiple use cases in AI scenarios. Recent advances in Generative AI (GenAI) and Large Language Models (LLM) have led to increased awareness and popularity of vector databases. Similarity search, a key functionality of vector databases, complements traditional relational databases and full-text search systems.

Using natural language text as an example, embedding functions map data to high-dimensional vectors to preserve their semantic similarity. Developers can then use vector-based semantic search to find similarities between different passages of text. Because the data within an LLM is current only up to a specific point in time, vector databases can offer additional relevant text to make searches more accurate – known as Retrieval Augmented Generation (RAG).

Therefore, adding RAG to an LLM using a vector database like SAP HANA Cloud provides an effective approach to increase the quality of responses from an LLM. In this blog, we will showcase how to use the SAP HANA Cloud vector engine to enable our MDK app with Retrieval Augmented Generation (RAG) capability.

 

1. Create an instance of SAP HANA Cloud.

Please refer to the tutorial below to create an instance of the SAP HANA Cloud in SAP BTP trial or free tier.

Deploy SAP HANA Cloud

Please refer to the tutorial below to create a database that supports vectors.

CAP with SAP HANA Vector Engine & SAP Core AI

 

 2. Configure new destination for SAP Mobile Development Kit (MDK) Web Client to consume the new SAP HANA Cloud vector engine instance

an example of destination configuration is as below

 

#Tue Aug 20 07:48:36 UTC 2024
tokenServiceURLType=Dedicated
clientId=sb-vectortest-Demo_int-dev!12345
#clientSecret=<< Existing password/certificate removed on export >>
tokenServiceURL=https://demo-int.authentication.sap.hana.ondemand.com/oauth/token
WebIDEEnabled=true
URL=https://demo-int-dev-vectortest-srv.cfapps.sap.hana.ondemand.com/service/vectortestService
WebIDEUsage=odata_gen
Name=CAPVectorEngine
Type=HTTP
HTML5.DynamicDestination=true
Authentication=OAuth2UserTokenExchange
ProxyType=Internet

 

3. Configure your SAP Mobile Service instance to consume the SAP HANA Cloud vector engine

You can create a new mobile service instance or modify your existing instance to add new mobile connectivity for your SAP HANA Cloud vector engine instance.

 

 

4. Create a SAP HANA Cloud vector engine service configuration file in your MDK project

Under the Services folder of your MDK project, create a new service file, e.g., named CAPVectorEngine.service , with the following content

 

{

“DestinationName”: “CAPVectorEngine”,

“OfflineEnabled”: false

}

 

 5. Create JavaScript rules in your MDK project to populate the SAP HANA Cloud vector database

Create a JavaScript rule PopulateEmbeddings.js to populate the SAP HANA Cloud vector database with domain-specific or customer-specific knowledge. For example, in the case below, equipment types and descriptions are considered domain-specific knowledge, while the schema definition of SAPPump is customer-specific knowledge.

 

// Define the knowledge base as a single string
const knowledgeBase = `
Unique Name: SAPElectricityMeter
Equipment Type: Electricity meter
Description: A device used to measure the amount of electrical energy consumed by a residence, business, or electrically powered device. It records the amount of electricity used in kilowatt-hours (kWh) and is typically installed by utility companies to monitor and bill for electricity usage.

Unique Name: SAPPump
Equipment Type: Water pump
Description: A device used to move water from one place to another. It is commonly powered by electricity, manual operation, gasoline, or solar power. Water pumps are essential in various applications such as residential water supply, agricultural irrigation, industrial processes, and municipal water systems. Key components of a water pump typically include an inlet, outlet, impeller, motor or engine, and pump housing.

Common Schema
[
{“Title”: “taskTitle”, “Description”: “Operation Title”, “Type”: “Text”},
{“Title”: “shortDescription”, “Description”: “Operation short description”, “Type”: “Text”},
{“Title”: “description”, “Description”: “Detailed operation description”, “Type”: “Text”},
{“Title”: “priority”, “Description”: “priority (high, medium, low)”, “Type”: “Text”},
]

Specific Schema for SAPPump:
[
{“Title”: “yearOfInstallation”, “Description”: “Year of installation”, “Type”: “Text”},
{“Title”: “length”, “Description”: “Length of the water pump in meters”, “Type”: “Text”}
]
`;

// Function to split the knowledge base into chunks
function splitKnowledgeBase(knowledgeBase) {
// Split the knowledge base into sections based on double newlines
const chunks = knowledgeBase.trim().split(/nn/).map(chunk => {
return { chunk: chunk.trim() };
});
return chunks;
}

export default async function PopulateEmbeddings(context) {
// Populate RAG Data
const functionName = ‘storeEmbeddings’;
const serviceName = ‘/MDKDevApp/Services/CAPVectorEngine.service’;

const chunks = splitKnowledgeBase(knowledgeBase);
context.showActivityIndicator(“Populating embeddings…”);
for (const chunk of chunks) {
const parameters = {
“value”: chunk.chunk,
“chunkSize”: “1000” // adjustable
};
let oFunction = { Name: functionName, Parameters: parameters };
try {
await context.callFunction(serviceName, oFunction);
} catch (error) {
console.log(“Error:”, error.message);
}
}
context.dismissActivityIndicator();
console.log(“Embeddings populated!”);
const section = context.getPageProxy().getControl(‘SectionedTable’).getSection(‘Section’);
section.redraw();
return;
}

 

After completing the steps mentioned above, our next blog will explore how to use Retrieval Augmented Generation (RAG) capability and the SAP AI Core service to achieve the use case of work order and operation recording.

 

 

 

​ In this series of blogs, I will showcase how to integrate SAP AI Core services into SAP Mobile Development Kit (MDK) to develop mobile applications with AI capabilities.Part 1: SAP Mobile Development Kit (MDK) integration with SAP AI Core services – SetupPart 2: SAP Mobile Development Kit (MDK) integration with SAP AI Core services – Business Use CasesPart 3: SAP Mobile Development Kit (MDK) integration with SAP AI Core services – Measurement ReadingsPart 4: SAP MDK integration with SAP AI Core services – Anomaly Detection and Maintenance GuidelinesPart 5: SAP Mobile Development Kit (MDK) integration with SAP AI Core services- Retrieval Augmented Generation (Current Blog)In the previous blog, we showcased how to detect anomalies and generate maintenance guidelines using the SAP AI Core service. In the following blogs, we will explore how to record orders and operations using voice and free text input. To do that, we first need to understand the SAP HANA Cloud vector engine and Retrieval Augmented Generation (RAG)The SAP HANA Cloud vector engine offers multiple use cases in AI scenarios. Recent advances in Generative AI (GenAI) and Large Language Models (LLM) have led to increased awareness and popularity of vector databases. Similarity search, a key functionality of vector databases, complements traditional relational databases and full-text search systems.Using natural language text as an example, embedding functions map data to high-dimensional vectors to preserve their semantic similarity. Developers can then use vector-based semantic search to find similarities between different passages of text. Because the data within an LLM is current only up to a specific point in time, vector databases can offer additional relevant text to make searches more accurate – known as Retrieval Augmented Generation (RAG).Therefore, adding RAG to an LLM using a vector database like SAP HANA Cloud provides an effective approach to increase the quality of responses from an LLM. In this blog, we will showcase how to use the SAP HANA Cloud vector engine to enable our MDK app with Retrieval Augmented Generation (RAG) capability. 1. Create an instance of SAP HANA Cloud.Please refer to the tutorial below to create an instance of the SAP HANA Cloud in SAP BTP trial or free tier.Deploy SAP HANA CloudPlease refer to the tutorial below to create a database that supports vectors.CAP with SAP HANA Vector Engine & SAP Core AI  2. Configure new destination for SAP Mobile Development Kit (MDK) Web Client to consume the new SAP HANA Cloud vector engine instancean example of destination configuration is as below #Tue Aug 20 07:48:36 UTC 2024
tokenServiceURLType=Dedicated
clientId=sb-vectortest-Demo_int-dev!12345
#clientSecret=<< Existing password/certificate removed on export >>
tokenServiceURL=https://demo-int.authentication.sap.hana.ondemand.com/oauth/token
WebIDEEnabled=true
URL=https://demo-int-dev-vectortest-srv.cfapps.sap.hana.ondemand.com/service/vectortestService
WebIDEUsage=odata_gen
Name=CAPVectorEngine
Type=HTTP
HTML5.DynamicDestination=true
Authentication=OAuth2UserTokenExchange
ProxyType=Internet 3. Configure your SAP Mobile Service instance to consume the SAP HANA Cloud vector engine You can create a new mobile service instance or modify your existing instance to add new mobile connectivity for your SAP HANA Cloud vector engine instance.  4. Create a SAP HANA Cloud vector engine service configuration file in your MDK projectUnder the Services folder of your MDK project, create a new service file, e.g., named CAPVectorEngine.service , with the following content {

“DestinationName”: “CAPVectorEngine”,

“OfflineEnabled”: false

}  5. Create JavaScript rules in your MDK project to populate the SAP HANA Cloud vector databaseCreate a JavaScript rule PopulateEmbeddings.js to populate the SAP HANA Cloud vector database with domain-specific or customer-specific knowledge. For example, in the case below, equipment types and descriptions are considered domain-specific knowledge, while the schema definition of SAPPump is customer-specific knowledge. // Define the knowledge base as a single string
const knowledgeBase = `
Unique Name: SAPElectricityMeter
Equipment Type: Electricity meter
Description: A device used to measure the amount of electrical energy consumed by a residence, business, or electrically powered device. It records the amount of electricity used in kilowatt-hours (kWh) and is typically installed by utility companies to monitor and bill for electricity usage.

Unique Name: SAPPump
Equipment Type: Water pump
Description: A device used to move water from one place to another. It is commonly powered by electricity, manual operation, gasoline, or solar power. Water pumps are essential in various applications such as residential water supply, agricultural irrigation, industrial processes, and municipal water systems. Key components of a water pump typically include an inlet, outlet, impeller, motor or engine, and pump housing.

Common Schema
[
{“Title”: “taskTitle”, “Description”: “Operation Title”, “Type”: “Text”},
{“Title”: “shortDescription”, “Description”: “Operation short description”, “Type”: “Text”},
{“Title”: “description”, “Description”: “Detailed operation description”, “Type”: “Text”},
{“Title”: “priority”, “Description”: “priority (high, medium, low)”, “Type”: “Text”},
]

Specific Schema for SAPPump:
[
{“Title”: “yearOfInstallation”, “Description”: “Year of installation”, “Type”: “Text”},
{“Title”: “length”, “Description”: “Length of the water pump in meters”, “Type”: “Text”}
]
`;

// Function to split the knowledge base into chunks
function splitKnowledgeBase(knowledgeBase) {
// Split the knowledge base into sections based on double newlines
const chunks = knowledgeBase.trim().split(/nn/).map(chunk => {
return { chunk: chunk.trim() };
});
return chunks;
}

export default async function PopulateEmbeddings(context) {
// Populate RAG Data
const functionName = ‘storeEmbeddings’;
const serviceName = ‘/MDKDevApp/Services/CAPVectorEngine.service’;

const chunks = splitKnowledgeBase(knowledgeBase);
context.showActivityIndicator(“Populating embeddings…”);
for (const chunk of chunks) {
const parameters = {
“value”: chunk.chunk,
“chunkSize”: “1000” // adjustable
};
let oFunction = { Name: functionName, Parameters: parameters };
try {
await context.callFunction(serviceName, oFunction);
} catch (error) {
console.log(“Error:”, error.message);
}
}
context.dismissActivityIndicator();
console.log(“Embeddings populated!”);
const section = context.getPageProxy().getControl(‘SectionedTable’).getSection(‘Section’);
section.redraw();
return;
} After completing the steps mentioned above, our next blog will explore how to use Retrieval Augmented Generation (RAG) capability and the SAP AI Core service to achieve the use case of work order and operation recording.     Read More Technology Blogs by SAP articles 

#SAP

#SAPTechnologyblog

You May Also Like

More From Author