Access Models API with Apex

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.

note: 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.

The Apex methods of the Models API are also subject to the Apex Callout Limits and Limitations.

Pick a Method

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.

Choose a Model

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.

Write Apex Code

These examples demonstrate how to use Apex to communicate with the Models API.

Generate Text

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.

// Create generate text request
aiplatform.ModelsAPI.createGenerations_Request request = new aiplatform.ModelsAPI.createGenerations_Request();

// Specify model
request.modelName = 'sfdc_ai__DefaultOpenAIGPT4OmniMini';

// Create request body
aiplatform.ModelsAPI_GenerationRequest requestBody = new aiplatform.ModelsAPI_GenerationRequest();
request.body = requestBody;

// Add prompt to body
requestBody.prompt = 'Generate a welcome email for the new developer on the team, Jane Doe.';

try {
    // Make request
    aiplatform.ModelsAPI modelsAPI = new aiplatform.ModelsAPI();
    aiplatform.ModelsAPI.createGenerations_Response response = modelsAPI.createGenerations(request);
    System.debug('Models API response: ' + response.Code200.generation.generatedText);

// Handle error
} catch(aiplatform.ModelsAPI.createGenerations_ResponseException e) {
    System.debug('Response code: ' + e.responseCode);
    System.debug('The following exception occurred: ' + e);
}

Generate Chat

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.

// Create generate chat request
aiplatform.ModelsAPI.createChatGenerations_Request request = new aiplatform.ModelsAPI.createChatGenerations_Request();

// Specify model
request.modelName = 'sfdc_ai__DefaultOpenAIGPT4OmniMini';

// Create request body
aiplatform.ModelsAPI_ChatGenerationsRequest body = new aiplatform.ModelsAPI_ChatGenerationsRequest();
request.body = body;

// Add chat messages to body
List <aiplatform.ModelsAPI_ChatMessageRequest> messagesList = new List <aiplatform.ModelsAPI_ChatMessageRequest> ();
aiplatform.ModelsAPI_ChatMessageRequest systemMessage = new aiplatform.ModelsAPI_ChatMessageRequest();
systemMessage.content = 'Make this request a little silly.';
systemMessage.role = 'system';
messagesList.add(systemMessage);
aiplatform.ModelsAPI_ChatMessageRequest userMessage = new aiplatform.ModelsAPI_ChatMessageRequest();
userMessage.content = 'Can you give me a recipe for cherry pie?';
userMessage.role = 'user';
messagesList.add(userMessage);
body.messages = messagesList;

try {
    // Make request
    aiplatform.ModelsAPI modelsAPI = new aiplatform.ModelsAPI();
    aiplatform.ModelsAPI.createChatGenerations_Response response = modelsAPI.createChatGenerations(request);
    System.debug('Models API response: ' + response.Code200.generationDetails.generations);

// Handle error
} catch(aiplatform.ModelsAPI.createChatGenerations_ResponseException e) {
    System.debug('Response code: ' + e.responseCode);
    System.debug('The following exception occurred: ' + e);
}

Generate Embeddings

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.

// Create generate embeddings request
aiplatform.ModelsAPI.createEmbeddings_Request request = new aiplatform.ModelsAPI.createEmbeddings_Request();

// Specify model
request.modelName = 'sfdc_ai__DefaultOpenAITextEmbeddingAda_002';

// Create request body
aiplatform.ModelsAPI_EmbeddingRequest body = new aiplatform.ModelsAPI_EmbeddingRequest();
request.body = body;

// Add input to body
List <String> inputList = new List <String> ();
inputList.add('Every day, once a day, give yourself a present');
body.input = inputList;

try {
    // Make request
    aiplatform.ModelsAPI modelsAPI = new aiplatform.ModelsAPI();
    aiplatform.ModelsAPI.createEmbeddings_Response response = modelsAPI.createEmbeddings(request);
    System.debug('Models API response: ' + response.Code200.embeddings);

// Handle error
} catch(aiplatform.ModelsAPI.createEmbeddings_ResponseException e) {
    System.debug('Response code: ' + e.responseCode);
    System.debug('The following exception occurred: ' + e);
}

Submit Feedback

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.

// Create submit feedback request
aiplatform.ModelsAPI.submitFeedback_Request request = new aiplatform.ModelsAPI.submitFeedback_Request();

// Provide feedback information in body
aiplatform.ModelsAPI_FeedbackRequest feedbackRequest = new aiplatform.ModelsAPI_FeedbackRequest();
feedbackRequest.id = '78w60870345-2335840385-4857348-803483';
feedbackRequest.generationId = '89t608-46756v66y-23r4wqxe43-096mi';
feedbackRequest.feedback = 'GOOD';
feedbackRequest.feedbackText = 'The generation was helpful and did not require any changes';
feedbackRequest.source = 'HUMAN';
request.body = feedbackRequest;

try {
    // Submit feedback
    aiplatform.ModelsAPI modelsAPI = new aiplatform.ModelsAPI();
    aiplatform.ModelsAPI.submitFeedback_Response response = modelsAPI.submitFeedback(request);
    System.debug('Models API response: ' + response.Code202.message);

// Handle error
} catch(aiplatform.ModelsAPI.submitFeedback_ResponseException e) {
    System.debug('Response code: ' + e.responseCode);
    System.debug('The following exception occurred: ' + e);
}

Test Your Apex

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.

Next Steps

To optimize your implementation, refer to these topics.

See Also