Function Calling Implementation¶
This tutorial demonstrates how to implement function calling capabilities for the Event Assistant Copilot in Dynamics 365 Business Central, enabling direct interaction between the AI Assistant and your application's functionalities.
About Function Calling¶
Function calling lets you describe functions to the AI, which then identifies the appropriate function to invoke based on the user's input. This feature parses details from the user's natural language input to use as arguments for the predefined functions, bridging the gap between human language and programmatic functions.
Implementation Overview¶
Each function the AI can select is encapsulated in a separate codeunit that implements the AOAI Function interface. This setup ensures modular, maintainable, and scalable code.
1. Open File¶
Open the file "\src\5-EventAssistantCopilot\Implementation\Tools\GetTotalSessions.Codeunit.al" where you will define the first function.
2. Define Functions as Separate Codeunits¶
Each function should be implemented as a separate codeunit that implements the AOAI Function interface. Here’s how to structure these codeunits:
Basic Function Example:
codeunit 50227 "GPT GetTotalSessions" implements "AOAI Function"
{
procedure GetName(): Text
begin
exit('GetTotalSessions');
end;
procedure GetPrompt() ToolJObj: JsonObject
var
FunctionJObject: JsonObject;
begin
ToolJObj.Add('type', 'function');
FunctionJObject.Add('name', GetName());
FunctionJObject.Add('description', 'Returns the total number of sessions planned for the conference.');
FunctionJObject.Add('parameters', NullValue());
ToolJObj.Add('function', FunctionJObject);
end;
procedure Execute(Arguments: JsonObject): Variant
var
Session: Record "GPT Session";
begin
Session.SetRange("Project Code", ProjectCode);
exit('The total number of sessions planned for the conference is ' + Format(Session.Count) + '.');
end;
}
This will define the GetTotalSessions function, in the next json object format:
{
"type": "function",
"function": {
"name": "GetTotalSessions",
"description": "Returns the total number of sessions planned for the conference.",
"parameters": {}
}
}
If the AI recommends this function, the Copilot toolkit will call the Execute method to retrieve the total number of sessions planned for the conference.
Parametric Function Example:
codeunit 50228 "GPT GetTotalSessionsByTrack" implements "AOAI Function"
{
procedure GetName(): Text
begin
exit('GetTotalSessionsByTrack');
end;
procedure GetPrompt() ToolJObj: JsonObject
var
FunctionJObject: JsonObject;
ParametersJObject: JsonObject;
PropertiesJObject: JsonObject;
TrackNumberJObject: JsonObject;
RequiredArray: JsonArray;
begin
ToolJObj.Add('type', 'function');
FunctionJObject.Add('name', GetName());
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;
procedure Execute(Arguments: JsonObject): Variant
var
Token: JsonToken;
TrackNumber: Integer;
Session: Record "GPT Session";
begin
if not Arguments.Get('trackNumber', Token) then
exit('Please provide the track number');
TrackNumber := Token.AsValue().AsInteger();
Session.SetRange("Track Number", TrackNumber);
exit('The number of sessions on track ' + Format(TrackNumber) + ' is ' + Format(Session.Count) + '.');
end;
}
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.
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 on to the next step to understand how many skills you can add to the AI Event Assistant, what are the limits, and how to manage them.