Class OpenAiEmbeddingModel

java.lang.Object
org.springframework.ai.embedding.AbstractEmbeddingModel
org.springframework.ai.openai.OpenAiEmbeddingModel
All Implemented Interfaces:
EmbeddingModel, Model<EmbeddingRequest,EmbeddingResponse>

public class OpenAiEmbeddingModel extends AbstractEmbeddingModel
Open AI Embedding Model implementation.
Author:
Christian Tzolov, Thomas Vitale, Josh Long, Soby Chacko
  • Constructor Details

    • OpenAiEmbeddingModel

      public OpenAiEmbeddingModel(OpenAiApi openAiApi)
      Constructor for the OpenAiEmbeddingModel class.
      Parameters:
      openAiApi - The OpenAiApi instance to use for making API requests.
    • OpenAiEmbeddingModel

      public OpenAiEmbeddingModel(OpenAiApi openAiApi, MetadataMode metadataMode)
      Initializes a new instance of the OpenAiEmbeddingModel class.
      Parameters:
      openAiApi - The OpenAiApi instance to use for making API requests.
      metadataMode - The mode for generating metadata.
    • OpenAiEmbeddingModel

      public OpenAiEmbeddingModel(OpenAiApi openAiApi, MetadataMode metadataMode, OpenAiEmbeddingOptions openAiEmbeddingOptions)
      Initializes a new instance of the OpenAiEmbeddingModel class.
      Parameters:
      openAiApi - The OpenAiApi instance to use for making API requests.
      metadataMode - The mode for generating metadata.
      openAiEmbeddingOptions - The options for OpenAi embedding.
    • OpenAiEmbeddingModel

      public OpenAiEmbeddingModel(OpenAiApi openAiApi, MetadataMode metadataMode, OpenAiEmbeddingOptions options, org.springframework.core.retry.RetryTemplate retryTemplate)
      Initializes a new instance of the OpenAiEmbeddingModel class.
      Parameters:
      openAiApi - - The OpenAiApi instance to use for making API requests.
      metadataMode - - The mode for generating metadata.
      options - - The options for OpenAI embedding.
      retryTemplate - - The RetryTemplate for retrying failed API requests.
    • OpenAiEmbeddingModel

      public OpenAiEmbeddingModel(OpenAiApi openAiApi, MetadataMode metadataMode, OpenAiEmbeddingOptions options, org.springframework.core.retry.RetryTemplate retryTemplate, io.micrometer.observation.ObservationRegistry observationRegistry)
      Initializes a new instance of the OpenAiEmbeddingModel class.
      Parameters:
      openAiApi - - The OpenAiApi instance to use for making API requests.
      metadataMode - - The mode for generating metadata.
      options - - The options for OpenAI embedding.
      retryTemplate - - The RetryTemplate for retrying failed API requests.
      observationRegistry - - The ObservationRegistry used for instrumentation.
  • Method Details

    • getEmbeddingContent

      public String getEmbeddingContent(Document document)
      Description copied from interface: EmbeddingModel
      Extracts the text content from a Document to be used for embedding. By default, returns Document.getText(). Implementations that support MetadataMode should override this method to return Document.getFormattedContent(org.springframework.ai.document.MetadataMode) with the appropriate metadata mode, so that metadata is included in the text sent to the embedding API.
      Parameters:
      document - the document to extract embedding content from.
      Returns:
      the text content to embed.
    • embed

      public float[] embed(Document document)
      Description copied from interface: EmbeddingModel
      Embeds the given document's content into a vector.
      Parameters:
      document - the document to embed.
      Returns:
      the embedded vector.
    • call

      public EmbeddingResponse call(EmbeddingRequest request)
      Description copied from interface: Model
      Executes a method call to the AI model.
      Parameters:
      request - the request object to be sent to the AI model
      Returns:
      the response from the AI model
    • dimensions

      public int dimensions()
      Description copied from interface: EmbeddingModel
      Get the number of dimensions of the embedded vectors. Note that by default, this method will call the remote Embedding endpoint to get the dimensions of the embedded vectors. If the dimensions are known ahead of time, it is recommended to override this method.
      Specified by:
      dimensions in interface EmbeddingModel
      Overrides:
      dimensions in class AbstractEmbeddingModel
      Returns:
      the number of dimensions of the embedded vectors.
    • setObservationConvention

      public void setObservationConvention(EmbeddingModelObservationConvention observationConvention)
      Use the provided convention for reporting observation data
      Parameters:
      observationConvention - The provided convention