The Models API plays a significant role in providing access to and facilitating interaction with large language models (LLMs) from various Salesforce partners such as Anthropic, Google, and OpenAI. It enables applications to leverage these models for various tasks, including text generation and other AI-powered capabilities.

As a user, you can configure any Salesforce-enabled model with Einstein Studio. If you want to customize model hyperparameters, create a custom-configured model in Einstein Studio and later use it with the Models API. 

Key Capabilities To Consider:

  1. Generate Chat
  2. Generate Embeddings
  3. Generate Text
  4. Submit Feedback

Steps to Generate Chat Using Model API

The Models API requires access to Einstein Generative AI on your Salesforce Org. If you don’t have an Org with this feature, you can get an Einstein AI Playground org from Trailhead to experiment or you can get a Developer Edition org with Agentforce and Data Cloud to experiment.

Step 1: Create a Salesforce Connected App

  • Log in to your newly created Salesforce org.
  • Go to the setup Button.
unnamed 24
  • Search for ‘Generative AI’ in the Quick Find box and select ‘Einstein Setup’
blogImg.1
  • After turning on Einstein, search for ‘App Manager’ in the Quick Find box, select ‘App Manager’, and click on ‘New Connected App’.
blogImg.2 1
  • Select “Create a Connected App” from the given two points mentioned below:
  1. Connected apps support plugins other than OAuth 2.0.
  2. ‘Create an External Client App’ supports only OAuth 2.0 plugins.
  •  Fill in the following details:
blogImg.3

Enable ‘OAuth settings’,
Fill ‘Callback URL’

  • Select the mentioned ‘OAuth Scopes’.
blogImg.4
blogImg.5
  • Click on the Save button, then continue.
  • A new Page will open, then click on the ‘Manage’ button.
m
  • Click on ‘Edit Policies’.
blogImg.7
  • Click on ‘Run as’, select the user you want to assign, check the appropriate checkbox, and save.
unnamed 32
  • After saving, you need to open the newly created Connected App from ‘App Manager’. Once the app is open, click on ‘Manage Consumer Details’.
unnamed 33
  • When you click on the ‘Manage Consumer Details’ button, you’ll be prompted to enter a verification code. Once it is done, you’ll be redirected to the Consumer Details Page, where you can find the Key and Secret.
blogImg.10

Step 2: Now go to Auth Providers and click on “New”

blogImg.11
  • Select ‘Provider Type’ as ‘Salesforce/Open id Connect’.
  • Fill in the Name (URL suffix will fill automatically), Consumer key, and consumer secret, and leave the rest. Then click on save.
s
  • Click on ‘Save’, and then copy the Callback URL provided.
unnamed 38
  • Now go back to your created App, and click on edit.
blogImg.14
  • And paste the URL in the Callback URL section and click on save.
blogImg.15

P.S. – You’ll get this message, just click continue. 

unnamed 41

Step 3: Set Up Named Credentials:

Now we have to create a Named Credential in Source ORG (In this example, in our org).

  • First, go to my domain and copy the Current My Domain URL and copy it.
blogImg.17
  • Now, go to the Named Credentials tab and click on the ‘New Legacy’ option.
blogImg.18
  • Fill in the details below (The URL will contain the Current My Domain URL that we copied and add “https://” before that), click on the little search icon before Authentication Provider, and click on save. Once you click on save.
blogImg.19
  • You’ll be redirected to the login page, log in to the target org, and click on Allow.
ss
  • Now that we’ve set up everything in our org, we can test it in Postman.

Step 4: Set Up in Postman

Now that we’ve set up everything in our org, we can have a demo in the Postman.

blogImg.20
  • Once you’ve created a fork, now it’s time to generate an authentication code.
  • Fill in Variable values after creating your workspace in Postman as mentioned below.

authDomain: Current My Domain URL( in My Domain)

clientId: Consumer Key from Connected App.

clientSecret: Consumer Secret from Connected App.

modelName: You can select any model Name from the given link( Supported Model).

_baseUrl: remain it as it is.

unnamed 49
  • Now, generate an authentication code if you want to test it directly in Postman. Inside ‘Authentication’ Click on the ‘ Get new Access Token’ button,  then ‘Proceed’, and then  ‘Use Token’.
blogImg.23
  • From the above Page inside the ‘Authentication’ tab, copy ‘Access Token URL’ and then paste it into ‘the Token Endpoint URL’ field of ‘Auth. Provider you created above, and enter the ‘Authorize Endpoint URL’ field value as mentioned below.

Authorize Endpoint URL: https://hostname/services/oauth2/authorize

Replace the hostname with My Domain URL(as mentioned in the below image).

blogImg.24
blogImg.25

Step 5: Development Work

  • Now you can go into any of the 4 sections of the Models API, we will explore all 4 options and what they do.

You can use the below chat based on your preference:

1. Generate text (standalone prompt)

In this, you will ask the chatbot only one question, and it will give you the answer based on that. 

blogImg.26

For example, we will ask “Tell me a developer Joke?” (Like why not, we are developers and we deserve a good developer joke)

blogImg.27

2. Generate chat (conversation with context)

In this, you will have a conversation with the chatbot, and the model will consider the previous chat and reply accordingly.

blogImg.28

As you can see, the chatbot has replied accordingly, but this is only 1 message and its reply, so what now? Now we can use the result it gave us and add our message.

blogImg.29

I have added the response from the assistant and gave my following message as well; now it will reply accordingly.

blogImg.30

3. Generate embeddings

An embedding is a numerical representation of a chunk of content. Sometimes an embedding is called an embedding vector. To measure the semantic similarity between two chunks of content, you can use mathematical operations on their embedding vectors, such as cosine similarity, Euclidean distance, or dot product.

blogImg.31

Submit Feedback

This is based on the above text or chat, we can provide feedback if the chat is good or bad.

blogImg.32

We need to fill in the following details. In this example, we will provide the selected field and the ID from the above text.

blogImg.331 2

5.1 Create Apex class and Lightning component which uses the models API:

Now that we know how to use these models, we can create an intuitive UI to display the data.

For that, we need a lightning web component and an apex class.

  • Create Lightning Web Component (LWC). Open your Visual Studio Code and create a project, and in that:
unnamed 62
  • Create Apex Class in a similar way (Apex class name callModelAPIdata and Lwc name chatWindow).
  • Now copy the entire code.
  • LWC
  • CSS
  • JS
  • Apex code

Step 6: The Final Output

Now that we have put all the pieces together, we can finally see what it looks like.

unnamed 63
  • The green bubble represents the message sent to us, while the blue bubble represents the API’s response in a conversation format.
  • I have put this component on the Account record page, but you can put it on any page depending on your preference.

Demo Video

Related Articles
How to Integrate Salesforce Data Cloud with Amazon Redshift

Companies can finally connect Amazon Redshift to Salesforce Data Cloud without using the ordinary username & password authentication systems ever since its collaboration with AWS has happened. Business owners no longer have to be concerned about managing static credentials or breaching data security. By leveraging Salesforce IDP, companies can easily access their data lakehouse in […]

Read More
Trigger Prompt Templates Using Flow, Apex, or REST API

A prompt template is a pre-designed structure in Salesforce, specially designed to create personalized prompts by filling the substitute with particular details of customers, products, and more. This approach strengthens the creation of prompts and lets teams generate data-driven content. Every time a prompt template is used, it automatically merges the data to create a […]

Read More
Step-by-Step Guide on Adding Opportunity Team Members to Slack Channel

Salesforce allows you to improve communication by effortlessly connecting Opportunity Team Members to Slack channels. This step-by-step blog will show you how to enable Opportunity Teams, configure Slack integrations, and automate the process with Salesforce Flow. Real-time Slack notifications may help you streamline communication, increase productivity, and keep your sales team on the same page. […]

Read More
How to Use Transform Element in Salesforce Flow

Salesforce introduced the transform element in their winter ‘24 release. This feature simplifies data processing in flows by eliminating the need for loops and assignment elements. With the Transform Element, you can seamlessly map source data to target data, making your flows more efficient and easier to maintain. Transform element is available for screen flows, […]

Read More
How to Use TypeScript in Salesforce Lightning Web Components (LWC)

TypeScript is a programming language developed by Microsoft; used for enhancing JavaScript by adding different static types. With the help of TypeScript, developers can easily find errors during the development process before the execution. It also makes the code more readable and maintainable. With the successful integration of Salesforce Lightning Web Components (LWC), TypeScript improves […]

Read More
Expert Guide on Seamless Integration of Salesforce and QuickBooks Online

Integrating Salesforce with QuickBooks Online can significantly streamline your business operations, improving data accuracy and efficiency. QB Sync Made Easy offers a seamless solution to synchronize customer and financial data between the two platforms. This expert QuickBooks Online, on the other hand, is a leading accounting tool specifically developed for small and medium-sized businesses that […]

Read More
Our Location worldwide
Indian Flag India
3rd Floor, A-10, Pegasus Tower, Sector 68, Noida, Uttar Pradesh 201301 +91-1203117884
United States of America Flag USA
333 West Brown Deer Road Unit G – 366 Milwaukee WI, USA 53217 +1(262) 310-7818
United Kingdom Flag UK
7 Bell Yard, London, WC2A 2JR +44 20 3239 9428
Canada Canada
HIC Global Solutions INC
43 Lafferty Lane, Richmond Hill, L4C 3N8, CA +1(262) 310-7818