Invoke Salesforce Prompt Template using Apex

Invoke Salesforce Prompt Template using Apex

Sample Prompt Template:

Sample Template Content:

You are a support representative who is tasked with creating a short summary of a Case that occurred between a customer and a company support rep. Make use of {!$Input:Case.Subject} and {!$RelatedList:Case.CaseComments.Records} for the summary.

I have used Flex Prompt Template Type in the Blog Post.

Sample Apex Code:

String objCaseId = '500Hn00001j4ulUIAQ';
Map < String, String > objCaseMap = new Map < String, String >();
//Passing Case Id since the Input object is Case
objCaseMap.put( 'id', objCaseId ); 
ConnectApi.WrappedValue objCaseValue = new ConnectApi.WrappedValue();
objCaseValue.value = objCaseMap;
Map < String, ConnectApi.WrappedValue > inputParamsMap = new Map < String, ConnectApi.WrappedValue >();
//Case is the API Name of the input
inputParamsMap.put( 'Input:Case', objCaseValue ); 

ConnectApi.EinsteinPromptTemplateGenerationsInput executeTemplateInput = new ConnectApi.EinsteinPromptTemplateGenerationsInput();
executeTemplateInput.additionalConfig = new ConnectApi.EinsteinLlmAdditionalConfigInput();
executeTemplateInput.additionalConfig.applicationName = 'PromptBuilderPreview';
executeTemplateInput.isPreview = false;
executeTemplateInput.inputParams = inputParamsMap;

//Case_Summary_Flex is the API Name of the Prompt Template
ConnectApi.EinsteinPromptTemplateGenerationsRepresentation generationsOutput = ConnectApi.EinsteinLLM.generateMessagesForPromptTemplate(
    'Case_Summary_Flex',
    executeTemplateInput
);

ConnectApi.EinsteinLLMGenerationItemOutput response = generationsOutput.generations[0]; 
String strResponse = response.text; 
System.debug( 
    'Response is ' + 
    strResponse 
);

Leave a Reply