Access Models API with Apex
Access Models API with REST
LWC & Flow Examples
Rate Limits
Model API Names
Generation Feedback
Language & Locales
Data Masking
Toxicity Scoring
AI Models
Prompt Builder
Prompt Template Batch Processing
The Models API provides Apex classes that connect your application to large language models (LLMs). These Apex classes are autogenerated from the Models REST API specification using External Services.
All Models API requests are subject to Salesforce’s usage and billing rates for Einstein Requests. See Agentforce and Generative AI Usage and Billing and the Rate Card for Einstein Requests.
Note
The Apex methods of the Models API are also subject to the Apex Callout Limits and Limitations.
The ModelsAPI class in the aiplatform namespace provides a method for these generative AI capabilities.
| Capability | Method Name | Description |
|---|---|---|
| Generate Chat | createChatGenerations | Generate a response based on a list of messages representing a chat conversation. |
| Generate Embeddings | createEmbeddings | Create an embedding vector representing the input text. |
| Generate Text | createGenerations | Generate a response based on the prompt provided. |
| Submit Feedback | submitFeedback | Submit feedback for generated text. |
Each method, other than submitFeedback, requires the API name of the model as part of the request. For instance, if you want to use the OpenAI GPT-4o mini model for a generation request, specify the API name sfdc_ai__DefaultOpenAIGPT4OmniMini in the request.
The Models API supports models from various Salesforce-enabled providers. See Supported Models.
These examples demonstrate how to use Apex to communicate with the Models API.
To generate text, call the ModelsAPI.createGenerations() method with a prompt in your request body. If the request is successful, the response contains the generated text in the Code200.generation.generatedText field. If there’s an error, a ModelsAPI.createGenerations_ResponseException exception is thrown.
1// Create generate text request
2aiplatform.ModelsAPI.createGenerations_Request request = new aiplatform.ModelsAPI.createGenerations_Request();
3
4// Specify model
5request.modelName = 'sfdc_ai__DefaultOpenAIGPT4OmniMini';
6
7// Create request body
8aiplatform.ModelsAPI_GenerationRequest requestBody = new aiplatform.ModelsAPI_GenerationRequest();
9request.body = requestBody;
10
11// Add prompt to body
12requestBody.prompt = 'Generate a welcome email for the new developer on the team, Jane Doe.';
13
14try {
15 // Make request
16 aiplatform.ModelsAPI modelsAPI = new aiplatform.ModelsAPI();
17 aiplatform.ModelsAPI.createGenerations_Response response = modelsAPI.createGenerations(request);
18 System.debug('Models API response: ' + response.Code200.generation.generatedText);
19
20// Handle error
21} catch(aiplatform.ModelsAPI.createGenerations_ResponseException e) {
22 System.debug('Response code: ' + e.responseCode);
23 System.debug('The following exception occurred: ' + e);
24}To generate a chat message, call the ModelsAPI.createChatGenerations method with a chronological list of chat messages in your request body. If the request is successful, the response contains the chat messages in the Code200.generationDetails.generations field. If there’s an error, a ModelsAPI.createChatGenerations_ResponseException exception is thrown.
1// Create generate chat request
2aiplatform.ModelsAPI.createChatGenerations_Request request = new aiplatform.ModelsAPI.createChatGenerations_Request();
3
4// Specify model
5request.modelName = 'sfdc_ai__DefaultOpenAIGPT4OmniMini';
6
7// Create request body
8aiplatform.ModelsAPI_ChatGenerationsRequest body = new aiplatform.ModelsAPI_ChatGenerationsRequest();
9request.body = body;
10
11// Add chat messages to body
12List <aiplatform.ModelsAPI_ChatMessageRequest> messagesList = new List <aiplatform.ModelsAPI_ChatMessageRequest> ();
13aiplatform.ModelsAPI_ChatMessageRequest systemMessage = new aiplatform.ModelsAPI_ChatMessageRequest();
14systemMessage.content = 'Make this request a little silly.';
15systemMessage.role = 'system';
16messagesList.add(systemMessage);
17aiplatform.ModelsAPI_ChatMessageRequest userMessage = new aiplatform.ModelsAPI_ChatMessageRequest();
18userMessage.content = 'Can you give me a recipe for cherry pie?';
19userMessage.role = 'user';
20messagesList.add(userMessage);
21body.messages = messagesList;
22
23try {
24 // Make request
25 aiplatform.ModelsAPI modelsAPI = new aiplatform.ModelsAPI();
26 aiplatform.ModelsAPI.createChatGenerations_Response response = modelsAPI.createChatGenerations(request);
27 System.debug('Models API response: ' + response.Code200.generationDetails.generations);
28
29// Handle error
30} catch(aiplatform.ModelsAPI.createChatGenerations_ResponseException e) {
31 System.debug('Response code: ' + e.responseCode);
32 System.debug('The following exception occurred: ' + e);
33}To generate embeddings, call the ModelsAPI.createEmbeddings method with the input text in your request body. If the request is successful, the response contains the embeddings vector in the Code200.embeddings field. If there’s an error, a ModelsAPI.createEmbeddings_ResponseException exception is thrown.
1// Create generate embeddings request
2aiplatform.ModelsAPI.createEmbeddings_Request request = new aiplatform.ModelsAPI.createEmbeddings_Request();
3
4// Specify model
5request.modelName = 'sfdc_ai__DefaultOpenAITextEmbeddingAda_002';
6
7// Create request body
8aiplatform.ModelsAPI_EmbeddingRequest body = new aiplatform.ModelsAPI_EmbeddingRequest();
9request.body = body;
10
11// Add input to body
12List <String> inputList = new List <String> ();
13inputList.add('Every day, once a day, give yourself a present');
14body.input = inputList;
15
16try {
17 // Make request
18 aiplatform.ModelsAPI modelsAPI = new aiplatform.ModelsAPI();
19 aiplatform.ModelsAPI.createEmbeddings_Response response = modelsAPI.createEmbeddings(request);
20 System.debug('Models API response: ' + response.Code200.embeddings);
21
22// Handle error
23} catch(aiplatform.ModelsAPI.createEmbeddings_ResponseException e) {
24 System.debug('Response code: ' + e.responseCode);
25 System.debug('The following exception occurred: ' + e);
26}To submit feedback, call the ModelsAPI.submitFeedback method with feedback information in your request body. If there’s an error, a ModelsAPI.submitFeedback_ResponseException exception is thrown.
1// Create submit feedback request
2aiplatform.ModelsAPI.submitFeedback_Request request = new aiplatform.ModelsAPI.submitFeedback_Request();
3
4// Provide feedback information in body
5aiplatform.ModelsAPI_FeedbackRequest feedbackRequest = new aiplatform.ModelsAPI_FeedbackRequest();
6feedbackRequest.id = '78w60870345-2335840385-4857348-803483';
7feedbackRequest.generationId = '89t608-46756v66y-23r4wqxe43-096mi';
8feedbackRequest.feedback = 'GOOD';
9feedbackRequest.feedbackText = 'The generation was helpful and did not require any changes';
10feedbackRequest.source = 'HUMAN';
11request.body = feedbackRequest;
12
13try {
14 // Submit feedback
15 aiplatform.ModelsAPI modelsAPI = new aiplatform.ModelsAPI();
16 aiplatform.ModelsAPI.submitFeedback_Response response = modelsAPI.submitFeedback(request);
17 System.debug('Models API response: ' + response.Code202.message);
18
19// Handle error
20} catch(aiplatform.ModelsAPI.submitFeedback_ResponseException e) {
21 System.debug('Response code: ' + e.responseCode);
22 System.debug('The following exception occurred: ' + e);
23}These Apex classes are based on an External Services framework. For guidance on testing your Apex, refer to the Apex Developer Guide, and information about External Services.
To optimize your implementation, refer to these topics.