Access Models API with Apex

This feature is a Beta Service. Customer may opt to try such Beta Service in its sole discretion. Any use of the Beta Service is subject to the applicable Beta Services Terms provided at Agreements and Terms.

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.

CapabilityMethod NameDescription
Generate ChatcreateChatGenerationsGenerate a response based on a list of messages representing a chat conversation.
Generate EmbeddingscreateEmbeddingsCreate an embedding vector representing the input text.
Generate TextcreateGenerationsGenerate a response based on the prompt provided.
Submit FeedbacksubmitFeedbackSubmit 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 OpenAI’s GPT-3.5 Turbo model for a generation request, model for a generation request, you must specify the API name sfdc_ai__DefaultOpenAIGPT35Turbo 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.

To optimize your implementation, refer to these topics.