How to integrate your live SAP HR Data into M365 Copilot - Part 2
Tags: ai, sap, m365, githubcopilot
Published: 2024-06-23 17:28:05

---
title: How to integrate your live SAP  HR Data into M365 Copilot - Part 2
published: true
date: 2024-06-01 11:00:06 UTC
tags: AI,SAP,M365,Copilot
canonical_url: https://blog.bajonczak.com/how-to-integrate-your-sap-hcm-data-into-you-ai-model-part-2/
---

> Read the [previous post](https://blog.bajonczak.com/how-to-integrate-you-sap-data-into-you-ai-model/) before you read this. At the end of this series, you will get the complete github project to play around with your data.

 ![How to integrate your live SAP HR Data into M365 Copilot - Part 2](https://blog.bajonczak.com/content/images/2024/06/blog-image.png)

In my last article, I wrote about how the m365 Copilot works and how we created a new Plugin / Bot that will later used by M365 Copilot. In this article, I will try to describe how we will extract the required Information from the description and fetch the required data from the SAP Systemto generate a matching result.

# How to extract the required data

Let's assume you create a model deployment within your Azure open AI instance. (I use the gpt 3.5 turbo, this will be enough for our needs).

So basically the technical flow looks like this:

![How to integrate your live SAP HR Data into M365 Copilot - Part 2](https://mermaid.ink/img/pako:eNptkMFuwyAMhl_F4ty-QA6T0ma3HaamuyzsYAWHICXAwGytqr77nI3tNC425vv_H7ipMRhSjZqW8DnOmBieTtqDrHZ4yZTgfI0EzoOhPCYX2QX_9gMchscLJxzZeQuJ3otLZASdQlpx4-DDIdjIsErGUlXH4SQoZQa06LzUvn0G8iYG5xk4gCUpM_1rWU26oRdFpXJZNkEKxc7fo7_IzW2TAgLThStbPdr9_uFQXyLtsV5P2k57tVMrSaYz8je37UgrsV5Jq0ZaQxOKk1ba3wXFwqG_-lE1nArtVIkGmTqHNuGqmgmXLNOI_jWE3_39C87_gNQ?type=png)

The end user types in the description from the tender, and the bot will take it and send it to a GPT 3.5 model to extract the required information (like skills, job title, duration, and so on). The funny thing is, that I can tell the model to act as an API and it will send me the JSON result back.

Next, I go to SAP and request the Employees that match the required skills (and hourly rates, as well as the availability).

The result will be sent to the GPT again to form a textual result. This last step is finally done automatically by the m365 copilot itself.

# Extend the code to fulfill the extraction

So at the moment, we use s.th. like "Hey search me c# developers" or s.th. But I want to insert a tender description and let the Azure AI extract the required skills and ask SAP against these skills.

So, I created a small helper method within the "MyDataSource" class.

```

    public async GetResult(message: string): Promise<string> {

        const messages = [
            { role: "system", content: `Du bist eine schnittstelle und gibst die Antworten in einem JSON zurück. Extrahiere aus den Eingaben in den '<context></context>' nur die technischen skills, zusätzlich die Laufzeiten und rechne diese in Monaten um. Gebe die Remotezeit in Prozent and. Des Weiteren der Einsatzort, und der Jobtitel. Beispiel JSON als Ausgabe referenz sieht wie folgt aus 
{
"RequiredSkills":[
    {
        "Name":".Net",
    },
    {
        "Name":"SAP",
    }
],
"OptionalSkills":[
    {
        "Name":"Documentation",
    },
    {
        "Name":"Music",
    }
]

"JobTitle": "C# .Net Developer",
"Duration": 2,
"PercentRemote": 15
} 
wenn die Eigenschaften nicht ausgelesen werden können, sind diese nicht einzutragen. Wenn Überhaupt keine Daten ermittelt werden können, dann ist folgendes JSON zurück zu liefern
{
    "JobTitle": "N/A",
}
Gebe NUR das JSON aus denn du bist eine API-Schnittstelle. 
` },
            { role: "user", content: message },
        ];

        let result: string = "";
        const events = await this.client.streamChatCompletions(deploymentId, messages, { maxTokens: 800 },);
        for await (const event of events) {
            for (const choice of event.choices) {
                if (choice.delta?.content != undefined) {
                    result += choice.delta?.content;
                }
            }
        }
        return result;
    }
```

Let me explain this. I will configure the Open AI service and tell him to act as an API that only results in JSONs. But not any JSON, I will give him the structure with an example. So, it can now parse the input and extract the data into the required result.

Let's take this example (it's German sorry, but it fits for now, AI can speak all languages ;))

```
Für unseren Kunden suchen wir einen .Net / Blazor Spezialisten (m/w/d).  
  
Start: Juni/ späterer Start, wenn von dir gewünscht möglich (Juli)  
Laufzeit: 6 Monate ++  
Einsatzort: Remote  
Auslastung: 40%  
  
Aufgaben und Anforderungen:  
- Technische Beratung und Umsetzng im Bereich der Webentwicklung mit dem Ziel, die Leistungsfähigkeit der aktuellen IT zu verbessern:  
? Entwicklung von Webanwendungen mit ASP.NET Blazor  
? Entwicklung von Benutzeroberflächen  
? Entwicklung mit .NET 7 und Steigerung der Performance mit den neuesten Features des Frameworks  
? Bereitstellung und Verwaltung von containerisierten Anwendungen in einer Kubernetes-Umgebung  
? Einrichten von CI/CD-Pipelines und Optimieren mit Azure DevOps  
? Idealerweise: SAP PI / ODATA Erfahrung - nicht selbst implenetiert, sonder mit einer SAP API schonmal kommuniziert (nice-to-have)  
? Fließend in Deutsch und Englisch  
  
Hast Du freie Kapazitäten und kannst unseren Kunden unterstützen?  
Ich freue mich auf Deine Rückmeldung mit der Projektnummer folgenden Informationen an :  
• aktuelle Projektverfügbarkeit (frühester Start)  
• maximale Auslastung/Woche insgesamt  
• Stundensatz  
• aktuelles Profil (idealerweise im pdf Format)  
• Einschätzung zu den geforderten Anforderungen  
  
  
Wir bearbeiten alle Rückmeldungen und geben immer unser Bestes, uns bei allen Kandidaten (m/w/d) zurückzumelden. Leider ist dies nicht immer möglich, wir bitten um Dein Verständnis. Wenn wir uns innerhalb von 5 Werktagen nicht bei Dir melden, gehe bitte davon aus, dass der Kunde sich für einen anderen Kandidaten entschieden hat.  
  
Beste Grüße  
```

So when Open AI gets this description, it will extract the information. Here is an example of what it looks like:

<video src="https://blog.bajonczak.com/content/media/2024/05/20240522-1913-43.0212950.mp4" poster="https://img.spacergif.org/v1/970x394/0a/spacer.png" width="970" height="394" playsinline preload="metadata" style="background: transparent url('https://blog.bajonczak.com/content/media/2024/05/20240522-1913-43.0212950_thumb.jpg') 50% 50% / cover no-repeat;"></video>
<button aria-label="Play video">
                        <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24">
                            <path d="M23.14 10.608 2.253.164A1.559 1.559 0 0 0 0 1.557v20.887a1.558 1.558 0 0 0 2.253 1.392L23.14 13.393a1.557 1.557 0 0 0 0-2.785Z"></path>
                        </svg>
                    </button>

<button aria-label="Play video">
                            <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24">
                                <path d="M23.14 10.608 2.253.164A1.559 1.559 0 0 0 0 1.557v20.887a1.558 1.558 0 0 0 2.253 1.392L23.14 13.393a1.557 1.557 0 0 0 0-2.785Z"></path>
                            </svg>
                        </button><button aria-label="Pause video">
                            <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24">
                                <rect x="3" y="1" width="7" height="22" rx="1.5" ry="1.5"></rect>
                                <rect x="14" y="1" width="7" height="22" rx="1.5" ry="1.5"></rect>
                            </svg>
                        </button>0:00
 /0:05
<input type="range" max="100" value="0"><button aria-label="Adjust playback speed">1×</button><button aria-label="Unmute">
                            <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24">
                                <path d="M15.189 2.021a9.728 9.728 0 0 0-7.924 4.85.249.249 0 0 1-.221.133H5.25a3 3 0 0 0-3 3v2a3 3 0 0 0 3 3h1.794a.249.249 0 0 1 .221.133 9.73 9.73 0 0 0 7.924 4.85h.06a1 1 0 0 0 1-1V3.02a1 1 0 0 0-1.06-.998Z"></path>
                            </svg>
                        </button><button aria-label="Mute">
                            <svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24">
                                <path d="M16.177 4.3a.248.248 0 0 0 .073-.176v-1.1a1 1 0 0 0-1.061-1 9.728 9.728 0 0 0-7.924 4.85.249.249 0 0 1-.221.133H5.25a3 3 0 0 0-3 3v2a3 3 0 0 0 3 3h.114a.251.251 0 0 0 .177-.073ZM23.707 1.706A1 1 0 0 0 22.293.292l-22 22a1 1 0 0 0 0 1.414l.009.009a1 1 0 0 0 1.405-.009l6.63-6.631A.251.251 0 0 1 8.515 17a.245.245 0 0 1 .177.075 10.081 10.081 0 0 0 6.5 2.92 1 1 0 0 0 1.061-1V9.266a.247.247 0 0 1 .073-.176Z"></path>
                            </svg>
                        </button><input type="range" max="100" value="100">

Now we get structured data, that we can use technically.

# Selecting data from SAP with the extracted requirement skills 

Previously we saw that we extracted the required information from the tender description. So next we must fetch the relevant data from the SAP System.

Let's take the example that I get a search for the skill "Java" In this case the query to the API looks like this

```
https://api.successfactors.com/odata/v2/User?$filter=skills/any(s:s/name eq 'Java')
```

This will result in the following answer (PII are replaced)

```
{
  "d": {
    "results": [
      {
        "__metadata": {
          "uri": "https://api.successfactors.com/odata/v2/User('user1')",
          "type": "SFOData.User"
        },
        "userId": "user1",
        "firstName": "John",
        "lastName": "Doe",
        "skills": [
          {
            "__metadata": {
              "uri": "https://api.successfactors.com/odata/v2/Skill('skill1')",
              "type": "SFOData.Skill"
            },
            "name": "Java",
            "proficiency": "Expert",
            "yearsOfExperience": 5
          }
        ]
      },
      {
        "__metadata": {
          "uri": "https://api.successfactors.com/odata/v2/User('user2')",
          "type": "SFOData.User"
        },
        "userId": "user2",
        "firstName": "Jane",
        "lastName": "Smith",
        "skills": [
          {
            "__metadata": {
              "uri": "https://api.successfactors.com/odata/v2/Skill('skill1')",
              "type": "SFOData.Skill"
            },
            "name": "Java",
            "proficiency": "Intermediate",
            "yearsOfExperience": 3
          }
        ]
      }
    ]
  }
}

```

So in this case I got two Employees that match these skills. It will also return the proficiency and the year of their experience. So my final code looks like this:

```
import { PersonalInformation, Skill } from "./Interfaces";
import axios from 'axios';

/**
 * Skill Response from SAP
 */
interface SapResponseSkill {
    name: string;
    proficiency: string;
    yearsOfExperience: number;
}

/**
 * User response from SAP
 */
interface SapResponseUser {
    userId: string;
    firstName: string;
    lastName: string;
    skills: SapResponseSkill[];
}
/**
 * Result response from SAP itself.
 */
interface SapResponseApiResponse {
    d: {
        results: SapResponseUser[];
    };
}

export default class SapHcmService {
    private token:string; 
    
    constructor() {

        this.token= "your_oauth_token_here";

    }

    /**
     * Get the data from SAP.
     * 
     * @param requiredSkills 
     * @returns 
     */
    public async Fetch(requiredSkills: Skill[]): Promise<PersonalInformation[]> {
        
        let result: PersonalInformation[] = [];

        const users = await this.getUsersBySkills(requiredSkills, this.token);

        users.forEach((_: SapResponseUser) => {
            let user: PersonalInformation = {
                Firstname: _.firstName,
                LastName: _.lastName,
                FullName: "".concat(_.lastName, ", ", _.firstName),
                Availbility: 100,
                Cv: '', // Future implementation
                EMail: "".concat(_.firstName, ".", _.lastName,"@domain.com"),
                ImageLocation: '' , // Future impplementation
                LastUpdate: new Date(),
                SkillsMatches: []
            };
            _.skills.forEach(_ => {
                user.SkillsMatches.push({
                    name: _.name,
                    proficiency: _.proficiency,
                    yearsOfExperience: _.yearsOfExperience
                })
            });

            result.push(user);
        });
        return result;
    }

    /**
     * Get the user by their skills from the SAP System
     * @param skills 
     * @param token 
     * @returns 
     */
    private async getUsersBySkills(skills: Skill[], token: string): Promise<SapResponseUser[]> {
        const url = 'https://api.successfactors.com/odata/v2/User';
        const filterQuery = skills.map(skill => `skills/any(s:s/name eq '${skill.Name}')`).join(' and ');
        const requestUrl = `${url}?$filter=${filterQuery}`;

        try {
            const response = await axios.get<SapResponseApiResponse>(requestUrl, {
                headers: {
                    Authorization: `Bearer ${token}`,
                    'Content-Type': 'application/json'
                }
            });

            if (response.status === 200) {
                return response.data.d.results;
            } else {
                throw new Error(`Error: ${response.status} - ${response.statusText}`);
            }
        } catch (error) {
            console.error('Error fetching users by skills:', error);
            throw error;
        }
    }
}
```

The main call will be the fetch method. This will get all skills to select for the current tender. So after that, I will build the query against the SAP System itself and fetch the desired users.

The result will then be transformed into the result object called PersonalInformation. This will later be used to generate the Herocard output.

# Generate an answer for the user

So in fact we use actually the bot plugin from Teams. So in this case, we generate an answer combined with the resulting data from the SAP System. So That is a small "grounding".

So in this case, you open up the bot itself, ask "Hey inspect this tender ..... and generate the matching employees as table output" It will then do the following procedure (like above)

1. Extract the required skills
2. Fetching the matches from SAP

Now it must be sent back to the user itself, but instead of sending him the plain objects, we must send them through the LLM itself. So that he gets a smooth nice output to the user.

So we generated the bot plugin earlier that will use an openai llm from Azure. This requires a system message, to tell how the LLM must act. In my case, It use this (sorry for the german wording):

```
Du bist ein Hilfsbereiter Agent. 
wenn <context></context> angegeben ist dann mache folgendes:

In <context></context> sind die Eckdaten über die Ausschreibung die einmal kurz zusammengefasst werden muss. In <personas></personas> stehen die dazu gefundenen Mitarbeiter. 
Gebe die Mitarbeiter als Tabelle aus und errechne ein Match score. Denk dir keine Daten aus und verwende nur die vorliegenden.

Wenn kein <context></context> und <personas></personas> angegeben ist, dann beantworte die Fragen auf den vorherigen Kontext.
```

So in general, it will act as a helpful agent, it will use the data in <context> as the tender data, to summarize it to the user. Then in the <personas> tags, there will be the matched employees.

So finally it will summarize the tender and write out the list of the matched employees.

So basically there is a "handler" the will workout all of it. Here is the code for this:

```
import { MemoryStorage } from "botbuilder";
import * as path from "path";
import config from "../config";

// See https://aka.ms/teams-ai-library to learn more about the Teams AI library.
import { Application, ActionPlanner, OpenAIModel, PromptManager, TurnState } from "@microsoft/teams-ai";
import { MyDataSource } from "./myDataSource";

// Create AI components
const model = new OpenAIModel({
  azureApiKey: config.azureOpenAIKey,
  azureDefaultDeployment: config.azureOpenAIDeploymentName,
  azureEndpoint: config.azureOpenAIEndpoint,

  useSystemMessages: true,
  logRequests: true,
});

// Get the promts
const prompts = new PromptManager({
  promptsFolder: path.join(__dirname, "../prompts"),
});

// Initialize the planner for the chat itselff
const planner = new ActionPlanner<TurnState>({
  model,
  prompts,
  defaultPrompt: "chat",
});

// Register your data source with planner
const myDataSource = new MyDataSource("my-ai-search");
myDataSource.init();
planner.prompts.addDataSource(myDataSource);

// Define storage and application
const storage = new MemoryStorage();

// Will handle all Chat requests
const app = new Application<TurnState>({
  storage,
  ai: {
    planner,
  },
});

export default app;

```

So at first, I create the openAi Model definition and tell the app to use the model from my Azure instance. Next, I will load the prompt (described above). After that, I will define a planner that will "orchestrate" the combination of the openai model, the prompt, and the data source itself.

So In this case the datasource will be the logic for extracting the skills from the tender (described above).

So this "app" will then used at the message endpoint and will handle from now on the chat requests

```
server.post("/api/messages", async (req, res) => {
  // Route received a request to adapter for processing
  await adapter.process(req, res as any, async (context) => {
    // Dispatch to application for routing
    await app.run(context);
  });
});
```

So for now you will have at the first step a new bot created that will use the natural language to generate a "natural" output for employee predictions.

But you might scream "Hey what about copilot? You promised that we integrate this into copilot?" .

This will be part of my next post, because I must learn basics first.