ABAP GEN AI: Accessing LLM Capabilities within ABAP-Stack using ABAP AI SDK powered by ISLM-Part 2

Estimated read time 14 min read

In Part 1 of this series, we successfully established ISLM connectivity in S/4 Hana Public cloud and able to call it using ABAP AI SDK. With this connectivity in place, we’re now ready to build our own custom use case.

Customer Requirement: Whenever a Sales Order is rejected, the system should automatically trigger a custom notification E-Mail to the respective Sold-To Party (Customer) informing them about the rejection. Since customers may belong to different regions, it is essential that this E-Mail communication is sent in the Sold-To Party’s preferred language.GAP: S/4 Public Cloud does not provide any feature yet to maintain, access & translate E-Mail contents for CUSTOM business scenario.High Level Solution Proposal: ABAP GENAI-Powered Solution using ISLM (Intelligent Scenario Lifecycle Management). 

High-Level Architecture for ISLM custom AI scenario  

 

Solution Overview —

Step-1) Creating Intelligent Scenario and Models for our use case.

Create Intelligent Scenario in ADT –>

 

Once Intelligent Scenario is created, Create the intelligent scenario model on top of the above scenario.

In the intelligent model, provide the below information.

Intelligent Scenario name which we have created in first stepLarge language Model Name and its versionModel prompts templates (pre-written prompt structure where variables or placeholders will be filled to generate consistent and accurate responses from the AI model.)

Two User prompt has been created for the custom use case –

Note –> For demonstration purposes, we have created two separate user prompts for the custom use case. However, this task can also be accomplished with a single prompt.

1) Prompt-1 -Translating E-mail body from source language to target language.

Prompt used –> 

Task: Convert the following E-Mail body string {ISLM_EMAILBODY} into {ISLM_LANGUAGE} language, preserving all HTML tags as-is present in the body.
Response Expectation:
Return only the translated E-Mail body string in the response.

2) Prompt-2 – Replacing the placeholders from E-mail body with actual sales order data.

Task:
Replace the placeholders in the given E-Mail body ({ISLM_EMAILBODY}) with actual runtime values provided in ISLM variables.

Replacement Rules:

Replace ##Salesorder## with {ISLM_SALESORDER}

Replace ##SalesOrderItem## with {ISLM_SALESORDERITEM}

Replace ##Material## with {ISLM_MATERIAL}

Replace ##ReasonRejection## with {ISLM_REASONREJECTION}

Data Format Notes:

{ISLM_SALESORDERITEM}, {ISLM_MATERIAL}, and {ISLM_REASONREJECTION} may contain multiple values, separated by commas (,).

For multiple Sales Order items, output each record on a new line in the following format:
{SalesOrderItem1}, {Material1}, {ReasonRejection1}
{SalesOrderItem2}, {Material2}, {ReasonRejection2}
…and so on.

Response Expectation:
Return only the Final converted E-Mail body string after all placeholders have been dynamically replaced. No additional text or explanation is needed.

Intelligent Scenario Management App

Published Intelligent Scenarios will be visible here in Intelligent Scenario Management App

 

 

You can select the Scenario and deploy it.

 

Step -2) Creating custom Fiori App for storing E-Mail-template in source language.

Custom Fiori App has been created which will be used by Business users/ Functional to maintain E-Mail-Body in source language. (Just like we do in standard Fiori App – Maintain E-mail templates). This will be one time activity which need to be done by functional.

Since this was only a POC, we created a simple RAP-based application to store the e-mail body. In a real scenario, this application could be replaced with a freestyle UI5 application, or the template can be stored in DMS.

Source E-mail content translated to multiple target languages –> 

User can create translation to new language by clicking on create and by just providing target language. In the background ISLM will translate all the E-mail content from source language to target language.

In the above screenshot we are creating a new E-mail body in Hindi target language (ISLM will translate all the content from English to Hindi).

All the content has been successfully translated into Hindi language, except for the two items listed below. Additionally, users have the option to manually update any content in case of incorrect word translations.

HTML tags –> This will be used while sending the mail. Placeholders like ##SalesOrderItem## , ##Material## , ##ReasonRejection## , these values will be replaced by ISLM in runtime by the actual Sales order data while sending the mail.

The application also provides a built-in mechanism to retrigger an E-Mail in the desired target language. Users can select the required E-Mail template and initiate the ‘Send Mail using AI Template’ action. Upon doing so, the system will prompt the user to enter the following details:

Sales Order NumberTarget Template Language

An E-Mail is generated in the selected target language, populated with the actual Sales Order values.

Now the first step of storing the default E-mail template in source language is completed. 

Step – 3) Creating local event for Sales order -> Create Event

A local event will be triggered automatically whenever a new Sales Order is created. Within this event, custom logic can be implemented to utilize ISLM for translating the E-Mail body based on the Sold-to Party’s language and to send the translated E-Mail to the user.

a) Fetch the Sold-to Party’s language (this will be the target language for the translation).

b) Once we have the target language, we will be using ABAP AI SDK to translate E-Mail body to target language using “fetch_store_email_template” 

Instantiate the factory via class CL_AIC_ISLM_COMPL_API_FACTORY and method GET. Use the factory method CREATE_INSTANCE which provides you with a new instance of the completion API. Provide the intelligent scenario name which we have created in step1 as this carries the information about the connection and the model. Create the message container which will be used to hold system and user prompt.

 

 

Fetch the target language, then pass both the language and the e-mail content to be translated as prompt parameters defined during the creation of the intelligent scenario. 

 

ISLM-Variables in prompt 1–> 

Use EXECUTE_FOR_MESSAGES to provide the prompt as a message list wrapped in a message container instance. Each message is classified as system roleuser message, or assistant message. You can get an instance of the message container using the method CREATE_MESSAGE_CONTAINERExecute method will be called to get the response of the LLM as a string 

Lv_emailstringtarget will hold the E-mail body in target language with placeholder, now the next step will be to remove the place holders from the E-MAIL body with actual Sales Order data.

For example –> 

Replacing the place holder from E-Mail. –>

All the steps will be same, except for the number of parameters passed to the prompt.

 

Prompt variable used –>

iv_targetemailstring –> This will hold the E-mail body in target language which is generated from above step.

iv_salesorderno –> This will contain Sales order number.

lv_lineno –> This will contain all the line-item numbers of Sales order separated by ‘,’ delimiter

lv_material –> This will contain all the material numbers of Sales Order separated by ‘,’ delimiter.

lv_reasnrjn –> This will contain all the rejection reasons for each line in Sales Order separated by ‘,’ delimiter.

Final -Mail Output –>

The E-Mail is translated into the Sold-to Party’s language (French), and all placeholders are replaced with the actual sales order values whenever sales order line items are rejected.

Conclusion 

I hope this post gave you a clear understanding of how to integrate AI into a business scenario using ISLM. Share your thoughts in the comments if you found it helpful.

Useful Links

Local Events –> Working with RAP BO Events end-to-end – Event-driv… – SAP Community

ISLM Overview –> https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwjoz6KwlO6OAxVyxzgGHW30LSIQFnoECB8QAQ&url=https%3A%2F%2Fhelp.sap.com%2Fdocs%2Fabap-ai%2Fgenerative-ai-in-abap-cloud%2Fabap-ai-sdk-powered-by-intelligent-scenario-lifecycle-management&usg=AOvVaw1NV61P7QjWM68fPchKW_nR&opi=89978449

 

 

 

 

 

 

 

 

 

 

 

​ In Part 1 of this series, we successfully established ISLM connectivity in S/4 Hana Public cloud and able to call it using ABAP AI SDK. With this connectivity in place, we’re now ready to build our own custom use case.Customer Requirement: Whenever a Sales Order is rejected, the system should automatically trigger a custom notification E-Mail to the respective Sold-To Party (Customer) informing them about the rejection. Since customers may belong to different regions, it is essential that this E-Mail communication is sent in the Sold-To Party’s preferred language.GAP: S/4 Public Cloud does not provide any feature yet to maintain, access & translate E-Mail contents for CUSTOM business scenario.High Level Solution Proposal: ABAP GENAI-Powered Solution using ISLM (Intelligent Scenario Lifecycle Management). High-Level Architecture for ISLM custom AI scenario   Solution Overview –Step-1) Creating Intelligent Scenario and Models for our use case.Create Intelligent Scenario in ADT –> Once Intelligent Scenario is created, Create the intelligent scenario model on top of the above scenario.In the intelligent model, provide the below information.Intelligent Scenario name which we have created in first stepLarge language Model Name and its versionModel prompts templates (pre-written prompt structure where variables or placeholders will be filled to generate consistent and accurate responses from the AI model.)Two User prompt has been created for the custom use case -Note –> For demonstration purposes, we have created two separate user prompts for the custom use case. However, this task can also be accomplished with a single prompt.1) Prompt-1 -Translating E-mail body from source language to target language.Prompt used –> Task: Convert the following E-Mail body string {ISLM_EMAILBODY} into {ISLM_LANGUAGE} language, preserving all HTML tags as-is present in the body.Response Expectation:Return only the translated E-Mail body string in the response.2) Prompt-2 – Replacing the placeholders from E-mail body with actual sales order data.Task:Replace the placeholders in the given E-Mail body ({ISLM_EMAILBODY}) with actual runtime values provided in ISLM variables.Replacement Rules:Replace ##Salesorder## with {ISLM_SALESORDER}Replace ##SalesOrderItem## with {ISLM_SALESORDERITEM}Replace ##Material## with {ISLM_MATERIAL}Replace ##ReasonRejection## with {ISLM_REASONREJECTION}Data Format Notes:{ISLM_SALESORDERITEM}, {ISLM_MATERIAL}, and {ISLM_REASONREJECTION} may contain multiple values, separated by commas (,).For multiple Sales Order items, output each record on a new line in the following format:{SalesOrderItem1}, {Material1}, {ReasonRejection1}{SalesOrderItem2}, {Material2}, {ReasonRejection2}…and so on.Response Expectation:Return only the Final converted E-Mail body string after all placeholders have been dynamically replaced. No additional text or explanation is needed.Intelligent Scenario Management AppPublished Intelligent Scenarios will be visible here in Intelligent Scenario Management App  You can select the Scenario and deploy it. Step -2) Creating custom Fiori App for storing E-Mail-template in source language.Custom Fiori App has been created which will be used by Business users/ Functional to maintain E-Mail-Body in source language. (Just like we do in standard Fiori App – Maintain E-mail templates). This will be one time activity which need to be done by functional.Since this was only a POC, we created a simple RAP-based application to store the e-mail body. In a real scenario, this application could be replaced with a freestyle UI5 application, or the template can be stored in DMS.Source E-mail content translated to multiple target languages –> User can create translation to new language by clicking on create and by just providing target language. In the background ISLM will translate all the E-mail content from source language to target language.In the above screenshot we are creating a new E-mail body in Hindi target language (ISLM will translate all the content from English to Hindi).All the content has been successfully translated into Hindi language, except for the two items listed below. Additionally, users have the option to manually update any content in case of incorrect word translations.HTML tags –> This will be used while sending the mail. Placeholders like ##SalesOrderItem## , ##Material## , ##ReasonRejection## , these values will be replaced by ISLM in runtime by the actual Sales order data while sending the mail.The application also provides a built-in mechanism to retrigger an E-Mail in the desired target language. Users can select the required E-Mail template and initiate the ‘Send Mail using AI Template’ action. Upon doing so, the system will prompt the user to enter the following details:Sales Order NumberTarget Template LanguageAn E-Mail is generated in the selected target language, populated with the actual Sales Order values.Now the first step of storing the default E-mail template in source language is completed. Step – 3) Creating local event for Sales order -> Create EventA local event will be triggered automatically whenever a new Sales Order is created. Within this event, custom logic can be implemented to utilize ISLM for translating the E-Mail body based on the Sold-to Party’s language and to send the translated E-Mail to the user.a) Fetch the Sold-to Party’s language (this will be the target language for the translation).b) Once we have the target language, we will be using ABAP AI SDK to translate E-Mail body to target language using “fetch_store_email_template” Instantiate the factory via class CL_AIC_ISLM_COMPL_API_FACTORY and method GET. Use the factory method CREATE_INSTANCE which provides you with a new instance of the completion API. Provide the intelligent scenario name which we have created in step1 as this carries the information about the connection and the model. Create the message container which will be used to hold system and user prompt.  Fetch the target language, then pass both the language and the e-mail content to be translated as prompt parameters defined during the creation of the intelligent scenario.  ISLM-Variables in prompt 1–> Use EXECUTE_FOR_MESSAGES to provide the prompt as a message list wrapped in a message container instance. Each message is classified as system role, user message, or assistant message. You can get an instance of the message container using the method CREATE_MESSAGE_CONTAINER. Execute method will be called to get the response of the LLM as a string Lv_emailstringtarget will hold the E-mail body in target language with placeholder, now the next step will be to remove the place holders from the E-MAIL body with actual Sales Order data.For example –> Replacing the place holder from E-Mail. –>All the steps will be same, except for the number of parameters passed to the prompt. Prompt variable used –>iv_targetemailstring –> This will hold the E-mail body in target language which is generated from above step.iv_salesorderno –> This will contain Sales order number.lv_lineno –> This will contain all the line-item numbers of Sales order separated by ‘,’ delimiterlv_material –> This will contain all the material numbers of Sales Order separated by ‘,’ delimiter.lv_reasnrjn –> This will contain all the rejection reasons for each line in Sales Order separated by ‘,’ delimiter.Final -Mail Output –>The E-Mail is translated into the Sold-to Party’s language (French), and all placeholders are replaced with the actual sales order values whenever sales order line items are rejected.Conclusion I hope this post gave you a clear understanding of how to integrate AI into a business scenario using ISLM. Share your thoughts in the comments if you found it helpful.Useful LinksLocal Events –> Working with RAP BO Events end-to-end – Event-driv… – SAP CommunityISLM Overview –> https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=&cad=rja&uact=8&ved=2ahUKEwjoz6KwlO6OAxVyxzgGHW30LSIQFnoECB8QAQ&url=https%3A%2F%2Fhelp.sap.com%2Fdocs%2Fabap-ai%2Fgenerative-ai-in-abap-cloud%2Fabap-ai-sdk-powered-by-intelligent-scenario-lifecycle-management&usg=AOvVaw1NV61P7QjWM68fPchKW_nR&opi=89978449             Read More Technology Blog Posts by SAP articles 

#SAP

#SAPTechnologyblog

You May Also Like

More From Author