Skip to content

Implement the Copilot AI Logic

Step 4 of 4

This section will guide you on integrating your Event Ideas Copilot with Azure OpenAI to make it intelligent and capable of generating practical event planning suggestions.

Update the Generate Action

Replace the hardcoded response in the Generate action with a call to a new function GenerateIdeas():

trigger OnAction()
begin
    GenerateIdeas();
end;

Create the GenerateIdeas()

Open the relevant AL file and add the GenerateIdeas() procedure. Start by declaring the necessary variables:

procedure GenerateIdeas()
var
    AzureOpenAI: Codeunit "Azure OpenAI";
    AOAIOperationResponse: Codeunit "AOAI Operation Response";
    AOAIChatCompletionParams: Codeunit "AOAI Chat Completion Params";
    AOAIChatMessages: Codeunit "AOAI Chat Messages";
    CopilotSetup: Record "GPT Event Copilot Setup";
begin

Variable Descriptions for GenerateIdeas()

  • AzureOpenAI: This codeunit is central to integrating with the Azure OpenAI services. It facilitates setting up the necessary authorization credentials and sending requests to the Azure OpenAI models. It ensures that all interactions with Azure OpenAI follow the correct protocols and handle the data securely.

  • AOAI Operation Response: After making a request to Azure OpenAI, this codeunit processes the response. It checks whether the operation was successful and provides methods to access the result or error messages. This is crucial for error handling and providing feedback within the application.

  • AOAI Chat Completion Params: Before sending a request to Azure OpenAI, this codeunit allows you to set various parameters that influence the AI's response. These include the "temperature," which affects the creativity or randomness of the response, and the maximum length of the response measured in tokens (words or pieces of words).

  • AOAI Chat Messages: This codeunit manages the dialogue between your application and Azure OpenAI. It allows you to build a conversational context by adding system messages that guide the AI's behavior and user messages that contain the queries or statements the AI needs to respond to.

  • Copilot Setup: This table holds essential configuration data for connecting to Azure OpenAI, such as the endpoint URL, deployment details, and secret keys for authentication. Managing this data effectively ensures that your application can consistently connect to Azure OpenAI without exposing sensitive information.

1. Check Copilot Activation

Make sure the copilot is active before making API calls:

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

2. Set Copilot and Authorization

Inform Azure OpenAI about the copilot being used and set up authorization:

    AzureOpenAI.SetCopilotCapability(Enum::"Copilot Capability"::"GPT Ideas Copilot");
    AzureOpenAI.SetAuthorization(Enum::"AOAI Model Type"::"Chat Completions", CopilotSetup.GetEndpoint(), CopilotSetup.GetDeployment(), CopilotSetup.GetSecretKey());

3. Configure AI Parameters

Set the AI "temperature" (from 0 to 2) to adjust the creativity level of the AI responses:

    AOAIChatCompletionParams.SetTemperature(1);

4. Build the Chat Session

Define the system message that outlines the copilot's capabilities and limitations, and add the user's input:

    AOAIChatMessages.AddSystemMessage('You are an AI event assistant focused on generating creative and trendy ideas and planning. Offer suggestions for conference topics, names, attendee numbers, finances etc. Decline unrelated questions.');
    AOAIChatMessages.AddUserMessage(InputText);

Note

The system message is crucial for setting user expectations and guiding the AI's behavior. It informs users about the copilot's capabilities and limitations, ensuring they provide relevant input for the AI to process effectively.

Info

You didn't sent anything to Azure OpenAI yet. You just prepared the chat session in memory. The next step is to send it to Azure OpenAI and process the response.

5. Generate AI Response

Send the prepared chat session to Azure OpenAI and process the response:

    AzureOpenAI.GenerateChatCompletion(AOAIChatMessages, AOAIChatCompletionParams, AOAIOperationResponse);
    if AOAIOperationResponse.IsSuccess() then
        ResponseText := AOAIChatMessages.GetLastMessage()
    else
        Error(AOAIOperationResponse.GetError());

I get same response as request

Probably you have docker issue - Not able to access internet inside docker windows container.

Solution: In Docker Desktop go to Settings > Docker Engine and add the below line to the JSON config:

"dns": ["8.8.8.8"]

It should look like this:

{
"dns": [
    "8.8.8.8"
],
"experimental": false
}

Then restart Docker Desktop.

🎇 Congratulations!

You've successfully integrated AI logic into your copilot, allowing it to generate creative responses based on user queries about event planning.

Try asking:

I want to organize a live IT conference in Bangkok. 
Please suggest some trendy ideas, together with a list of catchy names, 
potential number of attendees, average ticket prices, and profit.

Experiment with different inputs and adjust your system message as needed to refine the AI's responses.