Function Calling Implementation¶
Overview¶
Function calling is a powerful feature that lets the AI Assistant interact directly with your application's functionalities. This guide will walk you through implementing function calling capabilities for the Event Assistant Copilot in Dynamics 365 Business Central.
About Function Calling¶
The concept of function calling enables you to provide a description of functions to the AI. In response, the AI identifies the appropriate function that needs to be invoked, along with the necessary arguments. The specific function and its corresponding arguments are determined based on the user's input. The function calling tool is designed to parse essential details from user's natural language input and use this as arguments for the functions that you've defined. This way, it bridges the gap between human language and programmatic functions, making the interaction more intuitive and user-friendly. Read more
Start Implementing¶
1. Open File¶
Navigate to the file "\src\5-EventAssistantCopilot\Implementation\EventAssistToolsImpl.Codeunit.al". This is where you will define the functions that the AI can recommend to call.
2. Define Basic Function¶
Start by defining a basic function that doesn’t require any parameters:
The function should be defined in the following format:
{
"type": "function",
"function": {
"name": "GetTotalSessions",
"description": "Returns the total number of sessions planned for the conference.",
"parameters": {}
}
}
It's up to you how pass this function to the AOAIChatMessages.AddTool() method. Here is an example of how we will do it in AL for the Event Assistant Copilot:
procedure GetTotalSessionsTool() ToolJObj: JsonObject
var
FunctionJObject: JsonObject;
begin
ToolJObj.Add('type', 'function');
FunctionJObject.Add('name', 'GetTotalSessions');
FunctionJObject.Add('description', 'Returns the total number of sessions planned for the conference.');
FunctionJObject.Add('parameters', NullValue());
ToolJObj.Add('function', FunctionJObject);
end;
This function, when called by the AI, will return the total number of sessions planned for the conference.
Important
Clearly describe the function and its purpose to ensure the AI can recommend it accurately.
3. Define Parametric Function¶
Next, define a function that requires parameters:
{
"type": "function",
"function": {
"name": "GetTotalSessionsByTrack",
"description": "Returns the number of sessions planned in a specified track.",
"parameters": {
"type": "object",
"properties": {
"trackNumber": {
"type": "integer",
"description": "The track number to query the number of sessions."
}
},
"required": ["trackNumber"]
}
}
}
Here is an example of how to define this function in AL:
procedure GetTotalSessionsByTrackTool() ToolJObj: JsonObject
var
FunctionJObject: JsonObject;
ParametersJObject: JsonObject;
PropertiesJObject: JsonObject;
TrackNumberJObject: JsonObject;
RequiredArray: JsonArray;
begin
ToolJObj.Add('type', 'function');
FunctionJObject.Add('name', 'GetTotalSessionsByTrack');
FunctionJObject.Add('description', 'Returns the number of sessions planned in a specified track.');
ParametersJObject.Add('type', 'object');
TrackNumberJObject.Add('type', 'integer');
TrackNumberJObject.Add('description', 'The track number to query the number of sessions.');
PropertiesJObject.Add('trackNumber', TrackNumberJObject);
ParametersJObject.Add('properties', PropertiesJObject);
RequiredArray.Add('trackNumber');
ParametersJObject.Add('required', RequiredArray);
FunctionJObject.Add('parameters', ParametersJObject);
ToolJObj.Add('function', FunctionJObject);
end;
This function takes a track number, and returns the number of sessions scheduled for that track.
Tip
Alternatively, you can define these functions in a separate text file and import them using Azure Key Vaults or Blob Storage.
Add Additional Functions¶
Your task is to further implement the following functions:
-
GetSessionDetailsByNameTool: This function should return the date and time details for a specified session. The parameter should be the session name.
-
GetSpeakerScheduleTool: This function should return the schedule for a specified speaker. The parameter should be the speaker's name.
Each function should be clearly described and have well-defined parameters so that the AI can correctly interpret user queries and call these functions with the right arguments.
Execute Functions (next step)¶
Once the AI determines which function to call and constructs the appropriate parameters from the user's input, it's your job to ensure that these functions are executed in the backend and their results are returned to the user. This interaction creates a seamless bridge between natural language queries and actionable data within your application.
Conclusion¶
By defining functions and their parameters, you added skills to the AI Event Assistant, enabling it to interact with your application's functionalities.
Next Steps¶
Move to the next step to implement the functions logic in AL.