Skip to content

Implement AI Logic

Step 4 of 7

Overview

In this section, you go through setting up the AI logic for the Event Assistant Copilot.

Important

This tutorial assumes you have Business Central version 24.0 or 24.1. If you have 24.2 or later, please refer to this tutorial.

Setup Azure OpenAI Integration

The steps are very similar to the Event Ideas Copilot AI logic setup. However, the key difference is the functions you'll define and execute based on the AI's recommendations.

1. Open the Implementation Codeunit

Begin by navigating to the file "src\5-EventAssistantCopilot\Implementation\EventAssistImpl.Codeunit.al" in your development environment. This codeunit is where you'll integrate Azure OpenAI to process user queries.

2. Initialize Azure OpenAI Settings

Configure the initial settings for Azure OpenAI to ensure the copilot is enabled and authorized to use specific AI models:

if not AzureOpenAI.IsEnabled(Enum::"Copilot Capability"::"GPT Event Assistant") then
    exit;

AzureOpenAI.SetCopilotCapability(Enum::"Copilot Capability"::"GPT Event Assistant");
AzureOpenAI.SetAuthorization(Enum::"AOAI Model Type"::"Chat Completions", CopilotSetup.GetEndpoint(), CopilotSetup.GetDeployment(), CopilotSetup.GetSecretKey());
AOAIChatCompletionParams.SetTemperature(0);

Tip

When using function calling set the temperature to 0 to ensure consistent responses.

3. Add Tools to Chat Messages

Unlike other implementations where a system message dictates the interaction, here you add tools to enrich copilots skillset. These tools are predefined functions that the AI will recommend to invoke based on the user's question:

AOAIChatMessages.AddTool(EventAssistantToolsImpl.GetTotalSessionsTool());
AOAIChatMessages.AddTool(EventAssistantToolsImpl.GetTotalSessionsByTrackTool());
AOAIChatMessages.AddTool(EventAssistantToolsImpl.GetSessionDetailsByNameTool());
AOAIChatMessages.AddTool(EventAssistantToolsImpl.GetSpeakerScheduleTool());

Important

Azure OpentAI will NOT directly execute any of these functions. It will only recommend what function to call and with what parameters.

4. Process User Queries

Add the user's question into the chat messages and invoke the Azure OpenAI service to get the recommended function to execute:

AOAIChatMessages.AddUserMessage(Question);
AzureOpenAI.GenerateChatCompletion(AOAIChatMessages, AOAIChatCompletionParams, AOAIOperationResponse);

5. Handle AI Responses

Unlike typical implementations where the AI's response is directly displayed to the user, in this setup, the AI specifies which function to execute along with it's parameters. Your task is to parse this recommendation, execute the corresponding function within Business Central, and then present the result to the user:

if AOAIOperationResponse.IsSuccess() then begin
    EventAssistantToolsImpl.SetProjectCode(ProjectNo);
    Answer := EventAssistantToolsImpl.ProcessResponse(AOAIOperationResponse.GetResult());
end else
    Error(AOAIOperationResponse.GetError());

Important Considerations

  • Function Definitions: Ensure that you clearly define all the functions that the AI might recommend. These definitions include the function name, description, and expected parameters.

  • AI Recommendation: Understand that the AI does not trigger any functions inside Business Central directly. It only recommends what function should be called based on the user's input.

  • Execution of Function: Once you receive the AI's recommendation, it's your responsibility to execute this function within Business Central and then provide the result back to the user.

This approach ensures that the Event Assistant Copilot not only interprets user queries but also interacts with Business Central to provide accurate and useful responses based on real data.

Next Steps

Move on to the next step where you will define the functions, AI will choose, and implement them in your Dynamics 365 Business Central.