AI Logic¶
This section guides you through integrating the Schedule Sessions Copilot with Azure OpenAI to automate the process of scheduling sessions based on user-defined parameters and AI-generated recommendations.
Before You Start¶
Ensure you have set up the new PromptDialog UI elements and understand how the part page functions, as these components are crucial for successfully integrating Azure OpenAI services.
Implementing AI Logic¶
1. Open Implementation Codeunit¶
- Location: Navigate to
"src\4-ScheduleSessionsCopilot\Implementation\ScheduleSessionsImpl.Codeunit.al".
2. Integrate Azure OpenAI¶
In the Generate procedure, you need to implement code that facilitates communication with Azure OpenAI to fetch AI-generated session schedules:
procedure Generate(ProjectCode: Code[20]; Duration: Enum "GPT Conference Duration"; GenerationId: Integer; var SessionScheduleProposal: Record "GPT Cop. Schedule Session")
begin
if not AzureOpenAI.IsEnabled(Enum::"Copilot Capability"::"GPT Schedule Sessions") then
exit;
AzureOpenAI.SetCopilotCapability(Enum::"Copilot Capability"::"GPT Schedule Sessions");
AzureOpenAI.SetAuthorization(Enum::"AOAI Model Type"::"Chat Completions", CopilotSetup.GetEndpoint(), CopilotSetup.GetDeployment(), CopilotSetup.GetSecretKey());
AOAIChatCompletionParams.SetTemperature(0);
AOAIChatMessages.AddSystemMessage(GetSystemMessage());
AOAIChatMessages.AddUserMessage(GenerateUserMessage(ProjectCode, Duration));
AzureOpenAI.GenerateChatCompletion(AOAIChatMessages, AOAIChatCompletionParams, AOAIOperationResponse);
if AOAIOperationResponse.IsSuccess() then
ResponseText := AOAIChatMessages.GetLastMessage()
else
Error(AOAIOperationResponse.GetError());
ResponseText := ResponseText.Replace('```json', '').Replace('```', '');
ConvertTextResponseToScheduleSessionsProposalRecords(ResponseText, ProjectCode, GenerationId, SessionScheduleProposal);
end;
3. System and User Messages¶
In this task, you are challenged to construct the system and user messages that guide the AI in creating a session schedule for an event. Your aim is to ensure the AI fills out the session dates, track numbers, start times, and end times based on provided event parameters.
System Message Guidelines¶
Your system message acts as instructions for the AI. It needs to explicitly direct the AI to adhere to the provided event schedule, including when the event starts each day, how long it lasts, and constraints like lunch breaks and times between sessions.
What to Include in Your System Message:
When crafting your system message for the AI, focus on outlining general guidelines and constraints for session scheduling without specifying exact values. Here’s a concise way to guide developers:
-
Event Times: Direct the AI to adhere to general event start and end times, ensuring no sessions are planned outside these hours.
-
Breaks and Lunch: Instruct the AI to include breaks between sessions and a lunch break, ensuring no session overlaps with these times.
-
Session Arrangement: Advise the AI to use multiple tracks if necessary to fit all sessions, but aim to minimize the number of tracks and avoid overlapping sessions within the same track.
-
Output Format: Specify that the output should be a JSON array and list the properties required for each session (e.g., session code, date, track number, start time, end time).
Example JSON Response:
[
{
"sessionCode": "S001",
"date": "2023-10-01",
"trackNo": 1,
"startTime": "09:00:00",
"endTime": "10:00:00"
},
{
"sessionCode": "S002",
"date": "2023-10-01",
"trackNo": 2,
"startTime": "09:45:00",
"endTime": "11:15:00"
},
...
]
User Message Construction¶
The user message should automatically gather data from the project and sessions and format it in a clean and precise way. This message should include all necessary details for the AI to create a viable session schedule.
Steps to Construct the User Message:
- Collect information from your project data, including the event's start and end dates, session durations, and other relevant settings.
- Format this data into a structured JSON object that includes the duration of the event, the specific times for sessions, breaks, and lunches.
- This JSON should be structured to ensure that the AI receives all the necessary details to process the scheduling effectively.
By constructing these messages properly, you help the AI generate a schedule that fits within the event’s constraints and meets the needs of your scenario, ultimately filling in the session schedule table accurately and effectively.
4. Convert AI Response¶
Inspect and utilize the ConvertTextResponseToScheduleSessionsProposalRecords procedure to parse the JSON response from Azure OpenAI and map it to Business Central records. This step involves converting JSON data into a format that populates the session schedule table.
5. Explore and Experiment¶
Play with the AI temperature settings and experiment with different system and user messages to observe how changes affect the generated schedule. Understanding these dynamics will aid in refining the copilot's effectiveness.
Conclusion¶
By completing this implementation, you will enable the Schedule Sessions Copilot to generate feasible and efficient session schedules that adhere to specified parameters, leveraging AI capabilities to automate complex scheduling tasks.
Next Steps¶
Once you are satisfied with the AI's output and have thoroughly tested the copilot under various scenarios, move on to applying the generated schedule to actual session records in Business Central. This involves taking the AI-generated proposal and updating session records accordingly.