Appearance
Exercise 4: Create a Flex Template
In this exercise, you'll create a Flex template that generates a personalized schedule for a guest. The personalized schedule includes the time and location of resort experiences that are available today, and that match the guest's interests. You'll ground the template with data generated by calling an Apex method. This template can be invoked from anywhere. In this exercise, we invoke it from a screen flow on the guest record page.
Step 1: Create the Flex template
In Setup, search for Prompt Builder, and select Prompt Builder.
Click the New Prompt Template button.
Configure the prompt template as follows:
Parameter Value Prompt Template Type Flex Prompt Template Name Generate Personalized Schedule API Name Keep default Template Description Generate a personalized schedule that includes the time and location of resort experiences that are available today, and that match the guest's interests. Resource Name Contact API Name myContact Source Type Object Object Contact Click Next.
Paste the following text in the Prompt Template Workspace:
txtYour name is {!$User.FirstName}. You work in the guest success team at {!$Organization.Name}. Introduce yourself, then present the list of resort activities happening today that match the guest's interests. For clarity, use a bullet list. For each activity, include name, time, location and duration on a single line. Do not include the date. Tell the guest to reach out if they'd like to book any of these activities. List of activities happening today that match the guest's interests:
In the Preview panel, select Sofia Rodriguez as the Contact.
Click Save & Preview.
- In the Resolution panel, examine the prompt that was generated.
- In the Response panel, examine the personalized list that the LLM generated.
TIP
If the LLM generated a bullet list of resort experiences, this is an example of hallucination. Because the prompt didn't include resort experiences, the LLM decided to generate made-up experiences. To avoid hallucination, we need to ground the prompt in real customer data: in this case, a list of resort experience sessions that match the guest's interests.
Click Activate.
Step 2: Create an Apex class used to ground the template with data
In Code Builder, open the command palette (
CMD + SHIFT + P
on Mac orCTRL + SHIFT + P
on PC).Search for Apex and click Create Apex Class.
Enter PersonalizedGuestExperiences as the class name and accept the default directory.
Replace the default code with the following code:
apexpublic with sharing class PersonalizedGuestExperiences { // Make this method available for grounding // the Generate_Personalized_Schedule prompt template @InvocableMethod public static List<Response> getSessions(List<Request> requests) { Request input = requests[0]; Contact contact = input.myContact; List<Session__c> sessions = ExperienceSessionController.getSessions(contact); // Create expected response List<Response> responses = new List<Response>(); Response res = new Response(); res.prompt = JSON.serialize(sessions); responses.add(res); return responses; } // The variables in this class need to match the prompt template inputs, // that may be different on each prompt template public class Request { @InvocableVariable(required=true) public Contact myContact; } public class Response { @InvocableVariable public String Prompt; } }
Note that the
getSessions()
method is defined as an@InvocableMethod
so it can be invoked by your template.Save the file (
CMD + S
on Mac orCTRL + S
on PC).In the file explorer (left sidebar), click the PersonalizedGuestExperiences.cls-meta.xml file.
Make sure the class API version is at least 60.0. The file should now look like this:
xml<?xml version="1.0" encoding="UTF-8"?> <ApexClass xmlns="http://soap.sforce.com/2006/04/metadata"> <apiVersion>60.0</apiVersion> <status>Active</status> </ApexClass>
Save the file.
Right-click anywhere in the code and select SFDX: Deploy This Source to Org.
Step 3: Ground the template with Apex data
In Setup, open Prompt Builder.
Open the Generate Personalized Schedule template.
On the line following "Experiences that match the guest's interests," use the Resource search box and select: Apex > PersonalizedGuestExperiences.
Click Save As > Save as New Version.
In the Preview panel, select Sofia Rodriguez as the Contact.
Click Preview.
In the Resolution panel, examine the prompt that was generated. Notice that the Apex merge field in the template has been replaced with the list of experience sessions that match the guest's interests generated by the Invocable Apex method.
In the Response panel, examine the message generated by the LLM. Now that you grounded the prompt in real data, the list presented by the LLM shows the actual experiences offered at Coral Cloud Resorts today and that match the guest interests.
Step 4: Leverage the reasoning capabilities of large language models
Large language models (LLMs) can not only generate content, but they can also reason and orchestrate tasks. In this step, you'll modify the prompt to ask the LLM to create a suggested schedule for the guest, taking into account the start time and duration of each experience, making sure that suggested experiences don't overlap, and that the guest has at least 60 minutes between experiences.
Modify the prompt template as follows:
txtYour name is {!$User.FirstName}. You work in the guest success team at {!$Organization.Name}. Introduce yourself, then present the list of resort activities happening today that match the guest's interests. For clarity, use a bullet list. For each activity, include name, time, location and duration on a single line. Do not include the date. Then add a suggested schedule. Take into account the start time and duration of each activity. Make sure that you don't include activities that overlap. Make sure the guest has at least 60 minutes of free time between activities. Present the suggested schedule in chronological order. Tell the guest to reach out if they'd like to book any of these activities. List of activities happening today that match the guest's interests: {!$Apex:PersonalizedGuestExperiences.Prompt}
Click Save & Preview.
If the suggested schedule doesn't look right (for example, it features experiences that overlap), select the OpenAI GPT 4 Turbo model in the configuration panel on the right, and click Save & Preview again. LLMs keep getting better. OpenAI GPT 4 Turbo in particular is much better at reasoning and orchestrating tasks compared to OpenAI GPT 3.5 Turbo.
Click Activate when you are satisfied with your template.
Step 5: Use the prompt in a screen flow
In Setup, search for Flow and select Flows.
Open the Personalized Schedule flow.
Mouse over the circle under the Get Contact element, click +, search for Generate, and select Generate Personalized Schedule.
Configure the action as follows:
Field Value Label Invoke Prompt API Name Keep default Description Invoke the Generate Personalized Schedule Prompt Contact Select Contact from Get_Contact. Remove the dot that is automatically added after {!Get_Contact}
Use the screenshot below to confirm that you entered the right values.
Edit the Display Result element.
Click the Display Text Component (Insert Variable Here).
In the Resource Picker in the right panel, select Outputs from Invoke_Prompt > promptResponse (make sure that you delete "Insert Variable Here" if it appears in the rich edit area).
Click Done.
Click Save As > A New Version. Click Save.
Click Activate.
In the Coral Cloud Resorts app, click the Contacts tab and open Sofia Rodriguez.
Click the Personalized Schedule Quick Action.
The Quick Action displays the Personalized Schedule screen flow that you just updated. The screen flow invokes the Generate Personalized Schedule Flex template you created earlier in this exercise to generate a personalized schedule for the current guest. This is a great example showing how Flex templates can be used to bring generative AI anywhere into your applications.