Play Games

Search This Blog

Tuesday, March 11, 2025

Call the prompt template from Apex Salesforce

 

Solution: You can call the prompt template from Apex as shown in example below

Note: Test_Template is the sample flex template created and Lead is the object associated with it.

                                       

public class PromptTemplateController {
    public static void callPromptTemplate() {
        Map<String, String> property = new Map<String, String>();
        List<Lead> lstLead = [Select id,name from Lead];
        property.put('id', lstLead[0].id); // parameters to the prompt template
        ConnectApi.WrappedValue propertyValue = new ConnectApi.WrappedValue();
        propertyValue.value = property;
        Map<String, ConnectApi.WrappedValue> inputParams = new Map<String, ConnectApi.WrappedValue>();
        inputParams.put('Input:Lead', propertyValue); // Property is the API Name we gave to the input
        
        ConnectApi.EinsteinPromptTemplateGenerationsInput executeTemplateInput = new ConnectApi.EinsteinPromptTemplateGenerationsInput();
        executeTemplateInput.additionalConfig = new ConnectApi.EinsteinLlmAdditionalConfigInput();
        executeTemplateInput.additionalConfig.applicationName = 'PromptBuilderPreview';
        executeTemplateInput.isPreview = false;
        executeTemplateInput.inputParams = inputParams; 
        String promptTemplateName = 'Test_Template'; //prompt template api name
        
        ConnectApi.EinsteinPromptTemplateGenerationsRepresentation generationsOutput = ConnectApi.EinsteinLLM.generateMessagesForPromptTemplate(
            promptTemplateName,
            executeTemplateInput
        );
        if(generationsOutput != null && generationsOutput.generations != null){
            ConnectApi.EinsteinLLMGenerationItemOutput response = generationsOutput.generations[0]; 
            String res = response.text;
            System.debug('Response:::'+res);
        }
    }
}

call the method from the Developer console using the code below
PromptTemplateController.callPromptTemplate();

Output:



No comments:

Post a Comment