Categories
Uncategorized

AI and BC: chat with your data using MS Teams

This article will outline a method for creating virtual agents (chatbots) that can respond to user questions about business data.

To achieve our goal, we’ll utilize the following tools:

  • Microsoft Dynamics 365 Business Central, our favorite ERP system.
  • Microsoft Teams where users will ask their questions.
  • Power Virtual Agent to create a virtual assistant, which will ultimately be deployed within Teams.
  • Power Automate will be responsible for triggering the entire logic for each question that the user asks in the virtual agent’s chat.
  • Azure Functions will manage the logic for retrieving user questions and calling the necessary APIs from Business Central and OpenAI to generate responses.
  • OpenAI API for accessing AI models developed by OpenAI, specifically we will use the function calling feature.

Our aim is to create a virtual assistant adept at responding to highly specific questions concerning items data stored in Business Central. This model can be expanded to include other entities as desired. Below are some sample test questions our virtual assistant will address:

  • What is the unit price of item 1896-S?
  • What is the profitability of item 1896-S?
  • Give me all items with item category SERVIZI.
  • What is the on-stock quantity for item 1896-S?
  • Which item has the highest stock level?
  • Give me the total stock of items with category SEDIA.
  • Give me the list of item codes and quantities which are in the process of purchasing.

The virtual assistant will be able to tell when the user asks for information from the ERP system and will make a query on its own to get the needed data for the answer. Before we dive into the implementation details, let’s first watch a video demonstrating the final outcome: 

Solution

Below is a diagram summarizing how the various systems interact with each other.

The user inputs their question into a virtual assistant created using Power Virtual Agent and installed on Microsoft Teams. Through Power Automate, an Azure function is invoked to handle processing until the response is returned to the user. The logic of the Azure Function is outlined in the central part of the diagram and can be summarized as follows:

  1. For each user question, the OpenAI API is invoked. This initial call to the AI model aims to determine if the user question requires interaction with external functions, such as querying an external system. This is achieved using OpenAI’s function calling feature.
  2. In our case, the function will be responsible for invoking the Business Central APIs with a specific Odata query, also provided by the AI model.
  3. Next, the designated Business Central API is invoked with the provided query.
  4. Another call is made to the OpenAI API to refine the user question, incorporating the context obtained from the previous Business Central query.
  5. Equipped with the context, the AI service crafts a natural language response to the user’s question. This final message is then sent to the user via the Teams chat interface.

The main functionality, described above in points 1 and 2, is the function calling. Through this capability, two critical pieces of information can be provided to the AI model:

  1. A prompt containing the user’s question, along with any additional information deemed relevant for the model (e.g., constraints or specific formatting requirements for the response).
  2. A detailed description of all external functions that can be invoked, including their respective parameters. Functions can be more than one, and the AI model will “decide” based on the provided description of each function, which one to choose.

The following part of the diagram describes this.

In our specific example, we instructed the AI model with a single function named get_item_data with a simple description: “Use this function to answer user questions about items in the company master data.” Additionally, we provided details of the parameter required by the function, which is a string containing the OData query as its value. This is certainly the most challenging part, as it requires specifying with utmost precision how the parameter should be provided by the AI model. 

Example: let’s suppose we ask, “What is the unit price of item 1896-S?” This is what will happen:

The AI model suggests invoking the get_item_data function with the query “?$filter=itemCode eq ‘1896-S’&$select=unitPrice”. At this point, in our Azure Function, we can follow the suggestion and invoke the function responsible for querying Business Central.

The source code of the Azure function will not be publicly available for now. However, if there is sufficient interest, I may consider providing more in-depth technical details. Please feel free to reach out to me on my LinkedIn to express your interest.

Conclusions 

In this article, we have explored the power of function calling. Below are some points of consideration if you intend to implement such a solution:

  • It is crucial not to share sensitive, personal, or company information with external systems, such as OpenAI APIs. For this reason, it is advisable to use Azure OpenAI, which allows the use of AI models within the corporate tenant, where data will not be shared externally.
  • Evaluate if the solution you are implementing is scalable. In our case, the solution requires invoking both OpenAI and Business Central APIs to function. Assess the API limits before deploying the solution. For instance, Business Central’s limits are well defined in the following Microsoft documentation. It might be a good idea to query a Data Lake instead of directly accessing the ERP, or it may be necessary to create a balance between multiple instances of Azure OpenAI (follow this blog by Stefano Demiliani on this topic).

Thank you for reading this blog. Stay tuned for new experiments soon and follow me on LinkedIn.

By Mario Longo

I am currently working as Head of Development of a team that develops apps for Microsoft Dynamics 365 Business Central. When I’m not working I enjoy making music, reading about news, science and technology.

Leave a Reply