---
schema_version: "1"
id: 03ac3897765fe37a82fa0f3cb7fc2f81c18f8bbf98b9222ea1c61d53b00d2c0c
title: Access Models API with Apex
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/genai/agentforce/guides/models/models-api/access-models-api-with-apex.md
canonical_url: https://developer.salesforce.com/docs/ai/agentforce/guide/access-models-api-with-apex.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: 0a4e2d08e2b330d24f6c93f130daacef6de1cb99f49d02e92ae0fc86ff35f9f4
products:
  - Agentforce Developer Guide
  - Agentforce and Einstein Generative AI
---
# 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](https://help.salesforce.com/s/articleView?id=platform.external_services.htm).

**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](https://help.salesforce.com/s/articleView?id=ai.generative_ai_usage.htm) and the [Rate Card for Einstein Requests](https://www.salesforce.com/products/einstein/skus/).

The Apex methods of the Models API are also subject to the Apex [Callout Limits and Limitations](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_timeouts.htm).

## Pick a Method

The [ModelsAPI](/docs/ai/agentforce/references/models-apex-api/models-apex-reference.html#modelsapi) class in the `aiplatform` namespace provides a method for these generative AI capabilities.

| Capability          | Method Name                                                                                                                | Description                                                                       |
| ------------------- | -------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------- |
| Generate Chat       | [`createChatGenerations`](/docs/ai/agentforce/references/models-apex-api/models-apex-reference.html#createchatgenerations) | Generate a response based on a list of messages representing a chat conversation. |
| Generate Embeddings | [`createEmbeddings`](/docs/ai/agentforce/references/models-apex-api/models-apex-reference.html#createembeddings)           | Create an embedding vector representing the input text.                           |
| Generate Text       | [`createGenerations`](/docs/ai/agentforce/references/models-apex-api/models-apex-reference.html#creategenerations)         | Generate a response based on the prompt provided.                                 |
| Submit Feedback     | [`submitFeedback`](/docs/ai/agentforce/references/models-apex-api/models-apex-reference.html#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](https://developer.salesforce.com/docs/ai/agentforce/supported-models.md).

## 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.

```apex
// 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](/docs/ai/agentforce/references/models-apex-api/models-apex-reference.html#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](/docs/ai/agentforce/references/models-apex-api/models-apex-reference.html#createchatgenerations_responseexception) exception is thrown.

```apex
// 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](/docs/ai/agentforce/references/models-apex-api/models-apex-reference.html#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](/docs/ai/agentforce/references/models-apex-api/models-apex-reference.html#createembeddings_responseexception) exception is thrown.

```apex
// 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](/docs/ai/agentforce/references/models-apex-api/models-apex-reference.html#submitfeedback) method with feedback information in your request body. If there’s an error, a [ModelsAPI.submitFeedback_ResponseException](/docs/ai/agentforce/references/models-apex-api/models-apex-reference.html#createembeddings_responseexception) exception is thrown.

```apex
// 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](https://help.salesforce.com/s/articleView?id=platform.external_services.htm) framework. For guidance on testing your Apex, refer to the Apex Developer Guide, and information about External Services.

- _Apex Developer Guide_: [Testing Apex](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing.htm)
- _Salesforce Help_: [Testing External Services](https://help.salesforce.com/s/articleView?id=platform.external_services_test_actions_in_flow.htm)

## Next Steps

To optimize your implementation, refer to these topics.

- [Build Lightning Web Components and Flows with Models API](https://developer.salesforce.com/docs/ai/agentforce/guide/models-api-build-lwc-flow.md)
- [Supported Models](https://developer.salesforce.com/docs/ai/agentforce/supported-models.md)
- [Rate Limits for Models API](https://developer.salesforce.com/docs/ai/agentforce/guide/models-api-rate-limits.md)
- [Models API Apex Reference](/docs/ai/agentforce/references/models-apex-api/models-apex-reference.html)
- [Specify Languages and Locales with Models API](https://developer.salesforce.com/docs/ai/agentforce/guide/models-api-languages-and-locales.md)

## See Also

- _Salesforce Help_: [About Einstein Generative AI](https://help.salesforce.com/s/articleView?id=ai.generative_ai_about.htm)
- _Models API Developer Guide_: [Access Models API with REST](https://developer.salesforce.com/docs/ai/agentforce/guide/access-models-api-with-rest.md)
- _Apex Developer Guide_: [Getting Started with Apex](https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_intro_get_started.htm)
- _Trailhead_: [Get Started with the Models API](https://trailhead.salesforce.com/content/learn/modules/get-started-with-einstein-models-api)
