Understanding the Process
To call a Prompt Template from Apex, you need to follow a series of steps. First, you need to define the Prompt Template developer name and the record ID that you want to use as input. Then, you need to prepare the input variables and wrap them in a ConnectApi.WrappedValue object.
Next, you need to create the input structure using the ConnectApi.EinsteinPromptTemplateGenerationsInput class and set the input parameters and other options such as preview mode. Finally, you can call the Prompt Template using the ConnectApi.EinsteinLLM.generateMessagesForPromptTemplate method and access the generated text from the output.
Sample Code
ApexCode.cls
String promptTemplateDevName = 'Your_Template_API_Name';
String recordId = '001XXXXXXXXXXXXXXX';
Map<String, String> inputParams = new Map<String, String>();
inputParams.put('id', recordId);
ConnectApi.WrappedValue wrappedInput = new ConnectApi.WrappedValue();
wrappedInput.value = inputParams;
ConnectApi.EinsteinPromptTemplateGenerationsInput promptGenerationsInput = new ConnectApi.EinsteinPromptTemplateGenerationsInput();
promptGenerationsInput.inputParams = new Map<String, ConnectApi.WrappedValue>{
'Input:Input_Variable_Name' => wrappedInput
};
promptGenerationsInput.isPreview = false;
ConnectApi.EinsteinPromptTemplateGenerationsRepresentation output = ConnectApi.EinsteinLLM.generateMessagesForPromptTemplate(promptTemplateDevName, promptGenerationsInput);
String generatedText = output.generations[0].text;
System.debug('Generated Text: ' + generatedText);
The root cause of the issue is the lack of understanding of how to call a Prompt Template from Apex. By using the ConnectApi class and following the steps outlined above, you can easily call a Prompt Template and retrieve the generated text.
Best Practices
To get the most out of calling a Prompt Template from Apex, make sure to follow best practices such as testing your code in a developer edition org before deploying it to production.
Heads up: Make sure to replace the placeholder values in the sample code with your actual Prompt Template developer name and record ID.
Checklist
- Define the Prompt Template developer name and record ID
- Prepare the input variables and wrap them in a ConnectApi.WrappedValue object
- Create the input structure using the ConnectApi.EinsteinPromptTemplateGenerationsInput class
- Call the Prompt Template using the ConnectApi.EinsteinLLM.generateMessagesForPromptTemplate method
- Access the generated text from the output
- Test your code in a developer edition org before deploying it to production
- Replace placeholder values in the sample code with your actual values
Frequently Asked Questions
What is the ConnectApi class?
The ConnectApi class is a set of APIs that provide access to Agentforce features such as Prompt Templates.
How do I define the Prompt Template developer name?
You can define the Prompt Template developer name by going to the Prompt Template settings in your Agentforce org and copying the API name.
What is the purpose of the ConnectApi.WrappedValue object?
The ConnectApi.WrappedValue object is used to wrap the input variables and pass them to the Prompt Template.
How do I access the generated text from the output?
You can access the generated text from the output by using the output.generations[0].text property.
What is the purpose of the isPreview property?
The isPreview property is used to set whether the Prompt Template should be called in preview mode or not. If set to true, the Prompt Template will be called in preview mode and the generated text will not be saved.
Need help shipping this in production?
Genetrix builds and untangles Salesforce Marketing Cloud and Agentforce setups for teams that want it done right the first time. If anything in this post sounds familiar, talk to us before it ships.