0% found this document useful (0 votes)
25 views16 pages

Microsoft Ai SDK For Sap Abap

Uploaded by

minup01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views16 pages

Microsoft Ai SDK For Sap Abap

Uploaded by

minup01
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

MICROSOFT AI SDK FOR SAP ABAP

INTRODUCTION

Microsoft AI SDK for SAP ABAP is designed to be user-friendly, with an intuitive interface that
allows developers to easily integrate AI capabilities into ABAP applications. In current versions,
it provides ability to integrate and use Open AI and Azure Open AI capabilities.

Using AI SDK for SAP ABAP and integrating Azure Open AI and Open AI with SAP ABAP,
developers can create innovative applications that can automate manual tasks, make smarter
business decisions, and provide more personalized customer experiences. For example, an e-
commerce application could use Azure Open AI to provide product recommendations based on
a customer’s past purchases, while a customer service application could use natural language
processing to understand customer inquiries and provide automated responses. Possibilities
are endless and imagination is truly the limit.

Some of the key features of Microsoft AI SDK for SAP ABAP are:

· Models, Deployment, Files, Fine-Tuning, Completions, Chat completions and Embeddings.

· ABAP Ready data types significantly lowers barrier of use for ABAP developers.

· Supports Azure Open AI versions: “2022–12–01” and “2022–06–01-preview”

· Enterprise Control with 3 levels of control granularity — Enterprise control provides access
control, to ensure the privacy and security of sensitive data. Organizations can control the SDK
usage by implementing policies to allow/block specific functionalities. For example, an
organization may opt to use authorizations to determine a group of users who can perform
Setup operations (Deployment, Files and Fine-Tuning) — but allow all users to use the
Completions functionality.

· Authentication using either Azure Open AI Keys, or Azure Active Directory (AAD).

Microsoft AI SDK for SAP ABAP was introduced in April 2023 and till date it has two versions. In
AI SDK for SAP ABAP V1.0 it provided the ability to integrate and use Azure Open AI capabilities
only. Later in V2.0 Microsoft has introduced its integration with OpenAI and can also generate
embeddings through ABAP code. So the ABAP programs can use either Azure OpenAI as well as
OpenAI.

The documentation is located at: https://microsoft.github.io/aisdkforsapabap/ . The link is also


available from README file in GitHub repository located
here: https://github.com/microsoft/aisdkforsapabap .

Azure OpenAI Service gives customers advanced language AI with OpenAI GPT-4, GPT-3, Codex,
and DALL-E models with the security and enterprise promise of Azure. Azure OpenAI co-
develops the APIs with OpenAI, ensuring compatibility and a smooth transition from one to the
other.

With Azure OpenAI, customers get the security capabilities of Microsoft Azure while running the
same models as OpenAI. Azure OpenAI offers private networking, regional availability, and
responsible AI content filtering.

What is Azure Open AI?

Azure Open AI is a comprehensive set of AI services and tools provided by Microsoft Azure. It
includes powerful machine learning algorithms, natural language processing tools, and
cognitive services that can be used to build intelligent applications that can recognize patterns,
process natural language, and make predictions based on data. Azure Open AI features pre-
built AI models and algorithms, as well as tools for custom model training and deployment — all
with strong security, compliance, and data privacy.

What is Open AI?

OpenAI is an artificial intelligence research organization that aims to create safe and beneficial
AI for humanity. It was founded in 2015 by a group of prominent tech entrepreneurs and
researchers. OpenAI conducts cutting-edge research on topics such as generative models,
natural language processing, computer vision, and reinforcement learning. It also develops
products and platforms that enable anyone to access and use its AI models, such as the
OpenAI API and ChatGPT. OpenAI’s vision is to create artificial general intelligence (AGI) that
can benefit all of humanity without causing harm or being influenced by vested interests.

INSTALLING AI SDK AND CERTIFICATES ON SAP SYSTEM:

1. Run ABAPGit and pull the AI SDK code from “main” branch to SAP Application Server. If you
don’t have it, you can get it from Installing Online Repo | abapGit Docs .

2. We also need to add GITHUB and AZURE certificates in STRUST Tcode. We can get the AI SDK
certificate by exporting it from https://github.com/microsoft/aisdkforsapabap .
1. Navigate to SE80 tcode. Select package and enter the package name of the ai sdk. We
can see all the objects of the ai sdk as below.

Objects from AI SDK package

2. AI SDK package has a “Demo” package and programs which show how to instantiate the SDK
as well as use different features. In the coming days, we will discuss details of each through this
article series. However, if you would like to take a look at the demo programs right away, here
are the program names — all in the package ZPENGG_AZOAI_SDK_DEMOS (For Azure OpenAI)
and ZPENGG_OAI_DEMOS (For OpenAI)

3. All the demo programs show how to initialize the SDK.

4. BREAK-POINTs are set between each of the key operations, so that the input and output
structures can be inspected in ABAP/ADT Debugger.

Data Types has been explicitly declared so that people who are looking at code can understand
the data type to be used for specific operations. In a real-world application, we may use implicit
declaration ( Data(<variablename>) ).

Following are the demo programmes for all different features with Azure OpenAI and OpenAI
respectively,

• ZP_MSAISDKDEMO_MODELS, ZP_MSAISDKDEMO_MODELS_OAI — Models Capabilities


(Get, List).

• ZP_MSAISDKDEMO_DEPLOYMENTS — Deployment Capabilities (Create, List, Get,


Delete).

• ZP_MSAISDKDEMO_FILES, ZP_MSAISDKDEMO_FILES_OAI — File Capabilities (Upload,


Import, List, Get, Get Content, Delete).

• ZP_MSAISDKDEMO_FINETUNES, ZP_MSAISDKDEMO_FILES_OAI — FineTunes


Capabilities (Create, List, Get, Get Events, Cancel and Delete).
• ZP_PENGG_AZOAI_DEMO_COMPLETION, ZP_MSAISDKDEMO_COMPLETION_OAI —
Completion Capabilities (This program demonstrates the interaction with a UI. But,
under the hood, the code illustrates how to send the prompts and get results back.)

• ZP_AISDKDEMO_CHATCOMPL_SIMPLE MSPENG,
ZP_AISDKDEMO_CHTCMPL_SMPL_OAI — Chat Completion (This program
demonstrates how the feature helps to enhance the naturalness and coherence of the
conversation between the user and the chatbot.)

• ZP_AISDKDEMO_EMBEDDINGS, ZP_AISDKDEMO_EMBEDDINGS_OAI — Embeddings


(This program helps to generate embeddings)

WORKING WITH AI SDK

At a high level, working with the SDK has 3 steps.

1. Initializing and creating an SDK object instance.

2. Using the SDK instance to access capabilities like Models, Deployments, Files, Fine-Tunes or
Completions, and perform operations.

3. (Optional) Depending on organization’s policies, use Enterprise Control feature of SDK to


restrict operations that can be performed.

INITIALIZING AND CREATING SDK OBJECT

Instance creation for AI SDK

PERFORM A TEST RUN USING MODELS DEMO PROGRAM

1. Run program ZP_MSAISDKDEMO_MODELS.


2. Enter the end-point URL and key that was noted down earlier when setting up Azure Open AI
resource. For “Version”, enter “2022–12–01” exactly.

3. Run the program by pressing “F8” or clicking on the “execute” button from toolbar.

4. In the program you can find that there are break-points between different actions. So the
debugger is triggered when we execute it.
5. Double-Click on “model_list” variable and inspect it in debugger. Double-click on “DATA” field
to see the list of models available in your Azure Open AI. One of the features of AI SDK is the
ABAP Friendly Data Types.

6. Compare this list to the list of models available in Azure Open AI. Open the Azure Open AI
resource from Azure Portal and click on “Go to Azure Open AI Studio”. Once in Azure Open AI
studio, click on “Models” menu on left hand side, and review the list of models available.
Deployments

Once you create an Azure OpenAI Resource, you must deploy a model before you can start
making API calls and generating text. This action can be done using the Deployment APIs. These
APIs allow you to specify the model you wish to use.

Models

The service provides users access to several different models. Each model provides a different
capability and price point. GPT-4 models are the latest available models. Due to high demand
access to this model series is currently only available by request.

Prompts & completions

The completions endpoint is the core component of the API service. Completions endpoint
provides the completion for a single prompt and takes a single string as an input.

Here’s an example of a simple prompt and completion:


Embeddings

Usually the embeddings are introduced in Version 2.0 of AI SDK. So, we can generate
embeddings through ABAP code itself.

In Azure OpenAI, embeddings refer to the process of mapping words or phrases to numerical
vectors in a high-dimensional space. This allows for more efficient processing of text data, as
well as the ability to analyze relationships between words and phrases. For example, two words
that have similar meanings would be mapped to vectors that are closer together in the space,
while words that are unrelated would be mapped to vectors that are further apart. This
technique is commonly used in natural language processing (NLP) tasks such as text
classification, sentiment analysis, and language translation. Embeddings are typically learned
from large amounts of text data, using algorithms like Word2Vec, GloVe, and fastText.

Fine Tunes

Fine-tuning in AI SDK refers to the process of training a pre-trained machine learning model with
a custom dataset specific to a particular use case. It involves updating the weights of the pre-
trained model by feeding it with new data and making adjustments to the model’s parameters to
make it fit into the new dataset. It is often used when the available data for a task is not
sufficient to train a model from scratch or when the task requires learning context-specific
features. In Azure OpenAI, finetuning may refer to similar techniques applied to AI models
offered as services or provided in SDKs for specific use cases or domains.

Files

Files in Azure OpenAI refer to data or information stored in the Azure cloud platform that can be
accessed and used by OpenAI APIs and tools. These files can be in various formats such as text,
image, video, audio, and more. Files can be uploaded to Azure Storage, Azure Blob Storage, or
Azure ML Workspace Storage for storage and management. Azure OpenAI supports various data
processing and analysis tools that can help to extract insights and knowledge from files, which
can be useful in training machine learning models, natural language processing, computer
vision, and other AI applications.

Chat Completion

Chat completions in Azure OpenAI are a feature that generates human-like responses to text
prompts. This feature helps to enhance the naturalness and coherence of the conversation
between the user and the chatbot. It uses advanced machine learning algorithms to recognize
and analyze patterns in the input text and generate appropriate responses accordingly. Chat
completions can be used in a wide range of applications such as chatbots, virtual assistants,
customer service applications, and many more.

RUN A TEST PROGRAM

Let’s see how we can use Microsoft AI SDK in ABAP programme.


1. I have created an ABAP program for Chat Completion with the name of
Z3180_CHAT_COMPLETIONS.
Following is the code,

REPORT z3180_chat_completions.

SELECTION-SCREEN BEGIN OF block b1 WITH FRAME TITLE text-001.


PARAMETERS:
p_role TYPE string ,
p_cont TYPE string OBLIGATORY.

SELECTION-SCREEN end OF block b1.

SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE text-002.


PARAMETERS:
p_url TYPE string OBLIGATORY LOWER CASE,
p_ver TYPE string DEFAULT '2023-03-15-preview' OBLIGATORY LOWER CASE,
p_key TYPE string OBLIGATORY LOWER CASE,
p_depid TYPE string OBLIGATORY LOWER CASE.

SELECTION-SCREEN END OF BLOCK b2.

DATA:
sdk_instance TYPE REF TO zif_peng_azoai_sdk, "MS AI SDK for SAP Instance
status_code TYPE i, "Return Status Code
status_reason TYPE string, "Return Status Reason
returnjson TYPE string, "Return JSON. The content of this JSON string is parsed
and made available through ABAP data types.
error TYPE zif_peng_azoai_sdk_types=>ty_error."ABAP Type for Error

DATA:
chatcompl_input TYPE zif_peng_azoai_sdk_types=>ty_chatcompletion_input,
chatcompl_output TYPE zif_peng_azoai_sdk_types=>ty_chatcompletion_output.

START-OF-SELECTION.

TRY.
* Create an instance of MS AI SDK for SAP.
sdk_instance = zcl_peng_azoai_sdk_factory=>get_instance( )->get_sdk(
api_version = p_ver
api_base = p_url
api_type = zif_peng_azoai_sdk_constants=>c_apitype-azure
api_key = p_key
).

* Construct the prompt with system and user roles.


APPEND INITIAL LINE TO chatcompl_input-messages ASSIGNING FIELD-
SYMBOL(<fs_message>).
<fs_message>-role = zif_peng_azoai_sdk_constants=>c_chatcompletion_role-system.
<fs_message>-content = p_cont.

if p_role is NOT INITIAL.


APPEND INITIAL LINE TO chatcompl_input-messages ASSIGNING <fs_message>.
<fs_message>-role = zif_peng_azoai_sdk_constants=>c_chatcompletion_role-user.
<fs_message>-content = p_role.
ENDIF.

* Invoke Chat completion.


sdk_instance->chat_completions( )->create(
EXPORTING
deploymentid = p_depid
prompts = chatcompl_input
IMPORTING
statuscode = status_code " Status Code
statusreason = status_reason " HTTP status description
json = returnjson " JSON String returned from AI Resource
response = chatcompl_output
error = error " ABAP Ready error details
).

* Display the results.


cl_demo_output=>display_text( text = chatcompl_output-choices[ 1 ]-message-content ).

CATCH zcx_peng_azoai_sdk_exception INTO DATA(ex). " MSPENG:Azure Open AI ABAP SDK


Exception
MESSAGE ex TYPE 'I'.

CATCH cx_root.
MESSAGE |An error occured - perhaps the model name is incorrect? Or Access key is
incorrect?| TYPE 'I'.
ENDTRY.

2. When we execute it, we need to give the input query along with the Azure OpenAI connection
details.

3. We can also provide the role description for better results.

Given query is Write an abap code for fetching MARA table details
Following is the output,

FEW EXAMPLES ON HOW WE CAN USE AI

We can give instructions to the prompt on how to view your data and get the output. In the
below example the user has given instructions to display the given data as a table.
Direct SAP Table Query to Use of AI SDK for SAP ABAP with Azure OpenAI(AOAI)

Lets explore how to make direct table queries in SAP ABAP using AI SDK in the ABAP
environment.

Let’s consider an example of a Purchase Order ID query, demonstrating how AI can enhance the
process of retrieving data from SAP tables. Again, I have been using the SNWD_PO standard
Transparent Table which stores EPM: Purchase Order Item Table data.

Our main objective is to retrieve the total gross amount, Net amount and Tax amount of a given
Purchase Order ID from the SAP table. This process also can carried out manually by ABAP
developers or simply run of few manual T-codes, but with the help of the AI SDK, we can
automate it and provide instant, accurate result with customized ABAP UI.

In ABAP program, utilize the AI SDK to create a chatbot-like solution that can interpret a user’s
request for a particular Purchase Order ID’s gross amount. The program directly queries the SAP
table using the input ID and retrieves the corresponding gross amount, net amount, and tax
amount.
Upon executing the completion program, the AI SDK interprets the user’s request, retrieves the
requested data from the SAP table, and displays it. For instance, if the user requests the total
gross amount of Purchase Order ID 0300000000, the program has displayed “Gross amount:
33835.27, Net amount: 28433.00, Tax amount: 5402.27”.

· Use the get_text_as_r3table method to retrieve the text from the input field as a table of strings.

· Loop through the table, convert each line into a string, and concatenate these into a single
string, which forms user’s request.

· Process this request using the AI model (for this demonstration, I used Azure OpenAI’s text-
davinci-003 model).

Using AI SDK for generating program

Let us see how can we use this AI SDK for generating code.

In the example, developer can put pseudocode in the ABAP editor as comments and pass it on
to Open AI and get back Open AI’s recommendations in the same editor.
The programmer gave their query in the comment. After clicking on ask ai it returns us with abap
code as response.

You can see that the AI has returned with some response for the given query.

Here they have used Codex AI model for this example. It can understand the query from the
comment and turn it into code.
We can refer to the following blog post to understand how we can implement this in ABAP
system.
https://blogs.sap.com/2023/05/24/a-cool-use-of-open-ai-in-sap-gui-editor/ .

Microsoft AI SDK for SAP Update: V2.0 — Embeddings & Open AI Integration:

Microsoft had announced the release of AI SDK for SAP Version 2.0 . This new update introduces
generating embeddings through ABAP code, as well as integration with OpenAI. With this
update, ABAP programs, based on the needs and organization policies, can use either Azure
Open AI, or Open AI. SAP being an enterprise system, the recommendation would still be to use
Azure Open AI due to its focus on privacy and data security.

So, what’s new in Version 2.0:

Embeddings

In the context of artificial intelligence (AI), embeddings refer to a technique used to convert
categorical variables or text into a form that can be fed into machine learning algorithms. These
embeddings are essentially a dense vector representation of a categorical variable or text that
are learned during the training of the model.

For instance, in natural language processing (NLP), word embeddings are a popular
methodology. Each word or phrase from the vocabulary is mapped to a high-dimensional vector
that represents it. Similar words have similar vectors, which means that the geometric
relationship in the vector space captures the semantic relationship between the words.

Embeddings have proven to be a powerful tool in AI as they can capture complex relationships
in data and are capable of handling large categorical domains or vast vocabularies in a
compact, dense representation. This makes them suitable for a wide range of applications
including text analysis, recommendation systems, and more.

OpenAI Integration

While Version 1.0 supported Azure Open AI operations (Models, Deployments, Files, Fine-
Tunes, Completions (GPT-3) and Chat-Completions (GPT-4) ), with version 2.0 release,
Microsoft AI SDK for SAP now supports Open AI as well for all the operations, except for
Deployments. Deployments are not supported in open AI — since the model’s name/id itself is
used for Embeddings, Completions and Chat-Completions.

LIMITATIONS

It is important to note that while AI can provide many benefits to ABAP development, it is not a
silver bullet and has its own limitations and challenges. Some of these limitations may include:

1. Data quality: AI models rely on high-quality data to provide accurate results. If the data
used to train the model is incomplete, inconsistent, or biased, the model’s predictions
may be inaccurate or unreliable.

2. Model complexity: AI models can be complex and difficult to understand, making it


challenging to troubleshoot issues or modify the model as needed.

3. Security and privacy: AI models may require access to sensitive data, which can raise
security and privacy concerns. It is important to ensure that appropriate security
measures are in place to protect sensitive data.
Even though there are limitations while using ai sdk but in further updates we can expect new
developments along with them.

CONCLUSION

AI SDK for SAP ABAP aims to be the best AI integration mechanism for ABAP developers, with its
high quality and ease of use. Additionally, as new AI engines are added (eg: Open AI in Version
2.0), the goal is to keep the programming interface the same as much as possible and
encapsulate the AI Engine API differences within the SDK. This allows ABAP Developers to not
only easily integrate AI capabilities into ABAP code (eg: Reports/BADIs…etc), but also to easily
switch between different AI engines.
Furthermore, once an ABAP developer knows how to use AI SDK, the same programming model
knowledge can be used for different AI Engines without worrying about differences in each of
these AI Engine APIs.

REFERENCES

AI SDK FOR SAP ABAP: Documentation and Installations:

• https://microsoft.github.io/aisdkforsapabap/

• Announcing — Microsoft AI SDK for SAP | SAP Blogs

• OpenAI #AISDKforSAPABAP #SAPonAzure #MicrosoftAISDK

• https://github.com/microsoft/aisdkforsapabap — For Git repository

• https://portal.azure.com/ — To create Azure account and Azure Open AI resource

• Installing Online Repo | abapGit Docs — ABAPGIT installation

• https://openai.com/ — To create Open AI resource

• Announcing Microsoft AI SDK for SAP (linkedin.com)

You might also like