Invoke Salesforce Einstein Work Summary using Apex

Invoke Salesforce Einstein Work Summary using Apex

Salesforce Einstein Work Summary makes use of Prompt Template. So, we can invoke the Prompt Template using Apex.

Check the following Apex Code for Enhanced Messaging Einstein Work Summary.

Sample Apex Code:

// Messaging Session Id
String objMSId = '0Mwau000002LaAbCAK';
Map < String, String > objMSMap = new Map < String, String >();
//Passing Messaging Session Id since the Input object is Messaging Session
objMSMap.put( 'id', objMSId ); 
ConnectApi.WrappedValue objMSValue = new ConnectApi.WrappedValue();
objMSValue.value = objMSMap;
Map < String, ConnectApi.WrappedValue > inputParamsMap 
    = new Map < String, ConnectApi.WrappedValue >();
//Messaging Session is the API Name of the input
inputParamsMap.put( 'Input:MessagingSession', objMSValue ); 

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

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

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

// Parse the JSON string
Map < String, Object > jsonMap
    = ( Map < String, Object > )JSON.deserializeUntyped( strResponse );

// Access issue_resolution array
List < Object > issueResolutionList
    = ( List< Object > )jsonMap.get( 'issue_resolution' );

// Access the first item in the array
Map < String, Object > issueResolutionMap 
    = ( Map < String, Object > )issueResolutionList[ 0 ];

// Get the natural, issue and resolution
String natural = ( String ) jsonMap.get( 'natural' );
String issue = ( String ) issueResolutionMap.get( 'issue' );
String resolution = ( String ) issueResolutionMap.get( 'resolution' );

// Print the values
System.debug( 'Issue: ' + issue );
System.debug( 'Natural: ' + natural );
System.debug( 'Resolution: ' + resolution);

Leave a Reply