Class OpenAiSdkEmbeddingModel

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

public class OpenAiSdkEmbeddingModel extends AbstractEmbeddingModel
Embedding Model implementation using the OpenAI Java SDK.
Author:
Julien Dubois, Soby Chacko
  • Constructor Details

    • OpenAiSdkEmbeddingModel

      public OpenAiSdkEmbeddingModel()
      Creates a new OpenAiSdkEmbeddingModel with default options.
    • OpenAiSdkEmbeddingModel

      public OpenAiSdkEmbeddingModel(OpenAiSdkEmbeddingOptions options)
      Creates a new OpenAiSdkEmbeddingModel with the given options.
      Parameters:
      options - the embedding options
    • OpenAiSdkEmbeddingModel

      public OpenAiSdkEmbeddingModel(MetadataMode metadataMode, OpenAiSdkEmbeddingOptions options)
      Creates a new OpenAiSdkEmbeddingModel with the given metadata mode and options.
      Parameters:
      metadataMode - the metadata mode
      options - the embedding options
    • OpenAiSdkEmbeddingModel

      public OpenAiSdkEmbeddingModel(OpenAiSdkEmbeddingOptions options, io.micrometer.observation.ObservationRegistry observationRegistry)
      Creates a new OpenAiSdkEmbeddingModel with the given options and observation registry.
      Parameters:
      options - the embedding options
      observationRegistry - the observation registry
    • OpenAiSdkEmbeddingModel

      public OpenAiSdkEmbeddingModel(MetadataMode metadataMode, OpenAiSdkEmbeddingOptions options, io.micrometer.observation.ObservationRegistry observationRegistry)
      Creates a new OpenAiSdkEmbeddingModel with the given metadata mode, options, and observation registry.
      Parameters:
      metadataMode - the metadata mode
      options - the embedding options
      observationRegistry - the observation registry
    • OpenAiSdkEmbeddingModel

      public OpenAiSdkEmbeddingModel(com.openai.client.OpenAIClient openAiClient)
      Creates a new OpenAiSdkEmbeddingModel with the given OpenAI client.
      Parameters:
      openAiClient - the OpenAI client
    • OpenAiSdkEmbeddingModel

      public OpenAiSdkEmbeddingModel(com.openai.client.OpenAIClient openAiClient, MetadataMode metadataMode)
      Creates a new OpenAiSdkEmbeddingModel with the given OpenAI client and metadata mode.
      Parameters:
      openAiClient - the OpenAI client
      metadataMode - the metadata mode
    • OpenAiSdkEmbeddingModel

      public OpenAiSdkEmbeddingModel(com.openai.client.OpenAIClient openAiClient, MetadataMode metadataMode, OpenAiSdkEmbeddingOptions options)
      Creates a new OpenAiSdkEmbeddingModel with all configuration options.
      Parameters:
      openAiClient - the OpenAI client
      metadataMode - the metadata mode
      options - the embedding options
    • OpenAiSdkEmbeddingModel

      public OpenAiSdkEmbeddingModel(com.openai.client.OpenAIClient openAiClient, MetadataMode metadataMode, OpenAiSdkEmbeddingOptions options, io.micrometer.observation.ObservationRegistry observationRegistry)
      Creates a new OpenAiSdkEmbeddingModel with all configuration options.
      Parameters:
      openAiClient - the OpenAI client
      metadataMode - the metadata mode
      options - the embedding options
      observationRegistry - the observation registry
  • 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 embeddingRequest)
      Description copied from interface: Model
      Executes a method call to the AI model.
      Parameters:
      embeddingRequest - the request object to be sent to the AI model
      Returns:
      the response from the AI model
    • getOptions

      public OpenAiSdkEmbeddingOptions getOptions()
      Gets the embedding options for this model.
      Returns:
      the embedding options
    • setObservationConvention

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