Using Field Generation Prompt Builder and Flow to generate a recommendation
The recent addition of Prompt Builder to the Salesforce admin toolbelt allows admins to use Einstein generative AI with Salesforce data.
I set out to learn some of this new functionality with a fun little use case: generate an outfit recommendation based on the current weather conditions for the current contact’s mailing city and use the generated output to populate a field on the contact called: What Should I Wear? The current weather information is obtained by using a flow that makes an HTTP callout to a weather API. The flow will be triggered when the template is run, and will return the current weather and temperature back to the prompt based on the input of the contact mailing city.
The first thing you need to do is get access to a special Salesforce trial org, that is enabled for Einstein AI. The easiest way to get access is to go to any of the quick start projects for Einstein AI found in Trailhead. From there you can create a special Einstein AI playground. Keep in mind that the trial org comes with an expiration date and you will lose access to it and all your configurations once it expires.
After logging into the org, you need to Turn on Einstein from the Einstein Setup page.
Only after you do that, will you be able to access Prompt Builder from the setup menu.
The first thing I did was to create a receptacle field for my generated content. I created a Long Text Area field on the contact object called What Should I Wear?
Before starting on my template I created a new Template-Triggered Prompt Flow. In this flow I created an HTTP callout to a publicly available weather API to get the current weather for the inputted city. There are multiple other resources available that explain on how to create and use an HTTP callout action in a flow so I will not add details here.
There are two things unique about this new flow type:
- You need to specify the type of prompt template this flow will fit, and also specify the object available for input. The input object must match the input object specified in the prompt template. This introduces 2 new global variables in the flow called $Input, and $Output. You can use them to interact with the calling prompt.
- There is a new element for returning output instructions back to the prompt, called Add Prompt Instructions. It will send text back to the prompt and you can merge fields from the flow in it. It is important to handle both positive and negative instructions in case an error occurs and the desired output cannot be generated. In my case I added an instruction saying that an error occurred while getting the weather and also added a formula in my flow that will send a dummy city in case there was no mailing city on the contact record. This handled flow errors from the callout missing a required input parameter. Another option to handle errors would be to have a decision that skips sending a callout if there is no mailing city available.
Here is the configuration of the flow
After the flow is created and activated it can be used as a resource in the prompt instructions.
When you click New Prompt Template, you need to select one of the available templates that fit your use case and the starting object it will use as input. I chose a field generation template, the contact object, and the field I created to hold the output.
the builder workspace will open and it is there that you enter the prompt instructions and test generated responses
This is my Field generation text instructions, using both contact merge fields and the flow that makes the callout
You’re a sales representative who needs to create an outfit recommendation for {!$Input:Contact.FirstName} based on his location {!$Input:Contact.MailingCity}.
When I ask you to create a recommendation, you must strictly follow my instructions below.
Instructions:
“””
Generate an outfit recommendation in one paragraph no longer than 1000 characters.
Use clear, concise, and straightforward language using the active voice and strictly avoiding the use of filler words and phrases and redundant language.
Start the paragraph with a line mentioning the contact’s mailing city and describing the current weather and the current temperature in the contact’s mailing city.
If no value is available for the mailing city , say you cannot recommend an outfit currently.
Based on the weather and temperature, recommend an appropriate outfit to wear.
The recommended outfit should be gender neutral. You can mention accessories such as an umbrella or a hat in the recommendation.
Use the following information to get the current weather: {!$Flow:Contact_GetWeatherByCity_PromptTriggered.Prompt}.
If an error was returned for the current weather, say you cannot recommend an outfit currently.
Do not attribute any positive or negative traits in the recommendation.
“””
Now create the recommendation.
Before activating a new prompt template it is important to test it out repeatedly using various input records to see how it generates the response text, and make any adjustments to the instructions.
Again it is important to handle negative scenarios, in my case if no mailing city is found I instructed the prompt to say a recommendation could not be made.
The last step after activating the prompt template is to add it to the field on the Lightning record page. This will only work with dynamic record pages.
Finally this is a demo of the field generation prompt in action
