Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save msrivastav13/6aaf3dcfc0cb72165277de33c532f56d to your computer and use it in GitHub Desktop.
Save msrivastav13/6aaf3dcfc0cb72165277de33c532f56d to your computer and use it in GitHub Desktop.
codeLive: A Deep Dive into Apex for Prompt Builder

What is Prompt Builder?

Prompt Builder from Salesforce is a powerful tool that enables organizations to add generative AI capabilities to their business application. It allows organizations to create and manage prompt templates.

What is Prompt Builder?

Prompts

Sets of questions or instructions provided to a large language model to generate relevant content. To learn more complete the trailhead module below

Prompting Techniques

What is Prompt Engineeting?

  • Zero Shot
  • Few-Shot
  • Chain of Thought (COT)
  • Tree of Thoughts (TOT)
  • Graph Prompting

A useful resource to learn about all of the above and much more is below

Prompt Examples

Advanced Automatic Prompt Generation Tools

Making LLMs ready for Enterprise Apps

  • Solve for Halucinations
    • Provide more context as a part of Prompt.
    • Ground prompts with current and latest information
  • Prevent information from leaking to LLM

How Do You Enrich Prompts with Your Data?

Prompt Builder License Requirements

Click on the below trailhead module to learn more about the licenses needed for enabling Prompt Builder for your Salesforce org.

Einstein Pricing: Quick Look Trailhead

How to access Prompt Builder Trial orgs

Below is link to get a free trial org with Prompt Builder access. Trial org expires in 5 days.

Sign up for a Trial Org

Prompt Templates Types

Prompt Builder offers a range of customizable templates, including:

  • Sales Email prompt templates
  • Field Generation prompt templates
  • Record Summary prompt templates
  • Flex prompt templates

Leverage AI Across Salesforce Applications

These templates can be used across different AI tools in Salesforce.

  • App Builder for Lightning Pages
  • Salesforce Flows (Including Screen Flows)
  • Apex
  • Einstein Copilot

How Do You Embed Generative AI in the Flow of Work?

Grounding Prompt Templates with Apex

Below is an example code that shows how to ground prompt templates with Apex

public with sharing class ExamplePromptTemplateResolver {
   
    @InvocableMethod(
        capabilityType='<this depends on the prompt template type>'
    )
    
    public static List<Response> getContactsPropertyInterest(List<Request> requests) {
        // Retrieve inputs
        Request input = requests[0];
        Object1__c input1 = input.input1;
        Object2__c input2 = input.input2;

        // Create expected response
        List<Response> responses = new List<Response>();
        Response output = new Response();
        
        // Example Business logic to add to the prompt
        output.Prompt = 'Recommended Products:';
        for(Product__c prd : ProductController.getSimilarProducts(input1.Id, input1.Product_Family__c)) {
            output.Prompt += '\n' + 'product name:' + prd.Name + ', Picture URL:' + prd.Picture_URL__c + ', MSRP:' + prd.MSRP__c;
        }
        output.Prompt += '\n';
        responses.add(output);
        
        return responses;
    }

    // Class that represents the template inputs that are passed to the class
    public class Request {
        @InvocableVariable
        // Ensure that the input1 variable is same name with proper case as you configured in the Prompt Builder
        public Object1__c input1;
        @InvocableVariable
        public Object2__c input2;
    }

    // Class that represents the data that will be added to the template
    // when it's resolved
    public class Response {
        @InvocableVariable
        public String Prompt;
    }
}

Capability Types

Capability Type Value
Sales Email PromptTemplateType://einstein_gpt__salesEmail
Field Completion PromptTemplateType://einstein_gpt__fieldCompletion
Flex Templates FlexTemplate://template_API_Name
Record Summary PromptTemplateType://einstein_gpt__recordSummary

For Flex template type make sure template_API_Name matches the prompt template API Name field defined in Prompt Builder

For more examples refer to the resources below

Apex Developer Guide Examples for Resolving Prompt Templates via Apex

Invoking Prompt Templates via Apex

Use Connect in Apex to call a prompt template from Apex code.

String promptTemplateDeveloperName = '';

// Prepare input for generating prompt template
ConnectApi.EinsteinPromptTemplateGenerationsInput promptGenerationsInput = new ConnectApi.EinsteinPromptTemplateGenerationsInput();

// Map for holding input parameters
Map<String, ConnectApi.WrappedValue> inputParams = new Map<String, ConnectApi.WrappedValue>();
Map<String, String> input = new Map<String, String>();

// Set input parameters. Usually set the record Id for resolving the template
input.put('id', inputId);

// Add wrapped values
ConnectApi.WrappedValue inputValueMap = new ConnectApi.WrappedValue();
inputValueMap.value = input;
inputParams.put('Input:<input_variable_from_prompt_builder>', inputValueMap);

// Set hyper parameters
promptGenerationsInput.inputParams = inputParams;
promptGenerationsInput.additionalConfig = new ConnectApi.EinsteinLlmAdditionalConfigInput();
promptGenerationsInput.additionalConfig.numGenerations = 1;
promptGenerationsInput.additionalConfig.enablePiiMasking = false;
promptGenerationsInput.additionalConfig.applicationName = ApplicationConstant.APP_NAME;

// Set if it's a preview or not
promptGenerationsInput.isPreview = false;

// Call the service to generate messages for the prompt template
ConnectApi.EinsteinPromptTemplateGenerationsRepresentation generationsOutput = ConnectApi.EinsteinLLM.generateMessagesForPromptTemplate(promptTemplateDeveloperName, promptGenerationsInput);

// Consume response
ConnectApi.EinsteinLLMGenerationItemOutput response = generationsOutput.generations[0];
System.debug('Response: ' + response.text);

For more examples refer to the resources below

Additional Resources

Blogs

Videos

Get Started with Prompt Templates for Developers

 Prompt Builder: Ask Me Anything with Salesforce Developers

 The Science of Prompt Engineering

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment