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.
All Models API requests are subject to Salesforce’s usage and billing rates. See Einstein Usage.
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 GPT-3.5 Turbo model for a generation request, specify the API name sfdc_ai__DefaultGPT35Turbo
in the request.
The Models API supports models from various Salesforce-enabled providers. See Supported Models for Models API.
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.
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.
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.
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.
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.
- Apex Developer Guide: Testing Apex
- Salesforce Help: Testing External Services
To optimize your implementation, refer to these topics.
- Build Lightning Web Components and Flows with Models API
- Supported Models for Models API
- Rate Limits for Models API
- Models API Apex Reference (Beta)
- Specify Languages and Locales with Models API
- Salesforce Help: About Einstein Generative AI
- Models API Developer Guide: Access Models API with REST
- Apex Developer Guide: Getting Started with Apex
- Trailhead: Get Started with the Models API