Class EmbeddingConverter

java.lang.Object
com.google.adk.models.springai.EmbeddingConverter

public final class EmbeddingConverter extends Object
Static utilities for converting between Spring AI and ADK/GenAI embedding formats and performing vector operations.
  • Method Summary

    Modifier and Type
    Method
    Description
    static List<Double>
    calculateSimilarities(float[] query, List<float[]> candidates)
    Calculate similarity scores between a query and all candidates.
    static double
    cosineSimilarity(float[] embedding1, float[] embedding2)
    Calculate cosine similarity between two embedding vectors.
    static org.springframework.ai.embedding.EmbeddingRequest
    Create an EmbeddingRequest for a single text input.
    static org.springframework.ai.embedding.EmbeddingRequest
    Create an EmbeddingRequest for multiple text inputs.
    static double
    euclideanDistance(float[] embedding1, float[] embedding2)
    Calculate Euclidean distance between two embedding vectors.
    static float[]
    extractEmbedding(com.google.genai.types.ContentEmbedding contentEmbedding)
    Extracts a float array from a GenAI ContentEmbedding.
    static List<float[]>
    extractEmbeddings(org.springframework.ai.embedding.EmbeddingResponse response)
    Extract embedding vectors from an EmbeddingResponse.
    static float @Nullable []
    extractFirstEmbedding(org.springframework.ai.embedding.EmbeddingResponse response)
    Extract the first embedding vector from an EmbeddingResponse.
    static com.google.common.collect.ImmutableList<String>
    extractTexts(org.springframework.ai.embedding.EmbeddingRequest request)
    Extracts the input texts from a Spring AI EmbeddingRequest.
    static int
    findMostSimilar(float[] query, List<float[]> candidates)
    Find the most similar embedding from a list of candidates.
    static org.springframework.ai.embedding.EmbeddingResponse
    fromEmbedContentResponse(com.google.genai.types.EmbedContentResponse response)
    Converts a GenAI EmbedContentResponse to a Spring AI EmbeddingResponse.
    static float[]
    normalize(float[] embedding)
    Normalize an embedding vector to unit length.
    static com.google.genai.types.ContentEmbedding
    toContentEmbedding(float[] vector)
    Converts a float array to a GenAI ContentEmbedding.
    static com.google.genai.types.ContentEmbedding
    toContentEmbedding(org.springframework.ai.embedding.Embedding embedding)
    Converts a Spring AI Embedding to a GenAI ContentEmbedding.
    static double[]
    toDoubleArray(float[] floatArray)
    Convert float array to double array.
    static com.google.genai.types.EmbedContentResponse
    toEmbedContentResponse(org.springframework.ai.embedding.EmbeddingResponse response)
    Converts a Spring AI EmbeddingResponse to a GenAI EmbedContentResponse, ordering batch embeddings by Embedding.getIndex() when present.
    static org.springframework.ai.embedding.Embedding
    toEmbedding(float[] vector, int index)
    Creates a Spring AI Embedding from a float vector and batch index.
    static org.springframework.ai.embedding.Embedding
    toEmbedding(com.google.genai.types.ContentEmbedding contentEmbedding, int index)
    Converts a GenAI ContentEmbedding to a Spring AI Embedding.
    static org.springframework.ai.embedding.EmbeddingResponse
    toEmbeddingResponse(float[] vector)
    Creates a Spring AI EmbeddingResponse from a single embedding vector.
    static org.springframework.ai.embedding.EmbeddingResponse
    toEmbeddingResponse(List<float[]> vectors)
    Creates a Spring AI EmbeddingResponse from a list of embedding vectors.
    static float[]
    toFloatArray(double[] doubleArray)
    Convert double array to float array.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • createRequest

      public static org.springframework.ai.embedding.EmbeddingRequest createRequest(String text)
      Create an EmbeddingRequest for a single text input.
      Parameters:
      text - The text to embed
      Returns:
      EmbeddingRequest for the text
    • createRequest

      public static org.springframework.ai.embedding.EmbeddingRequest createRequest(List<String> texts)
      Create an EmbeddingRequest for multiple text inputs.
      Parameters:
      texts - The texts to embed
      Returns:
      EmbeddingRequest for the texts
    • extractTexts

      public static com.google.common.collect.ImmutableList<String> extractTexts(org.springframework.ai.embedding.EmbeddingRequest request)
      Extracts the input texts from a Spring AI EmbeddingRequest.
    • extractEmbeddings

      public static List<float[]> extractEmbeddings(org.springframework.ai.embedding.EmbeddingResponse response)
      Extract embedding vectors from an EmbeddingResponse.
      Parameters:
      response - The embedding response
      Returns:
      List of embedding vectors as float arrays
    • extractFirstEmbedding

      public static float @Nullable [] extractFirstEmbedding(org.springframework.ai.embedding.EmbeddingResponse response)
      Extract the first embedding vector from an EmbeddingResponse.
      Parameters:
      response - The embedding response
      Returns:
      The first embedding vector, or null if no embeddings
    • toEmbedding

      public static org.springframework.ai.embedding.Embedding toEmbedding(float[] vector, int index)
      Creates a Spring AI Embedding from a float vector and batch index.
    • toEmbedding

      public static org.springframework.ai.embedding.Embedding toEmbedding(com.google.genai.types.ContentEmbedding contentEmbedding, int index)
      Converts a GenAI ContentEmbedding to a Spring AI Embedding.
    • toEmbeddingResponse

      public static org.springframework.ai.embedding.EmbeddingResponse toEmbeddingResponse(float[] vector)
      Creates a Spring AI EmbeddingResponse from a single embedding vector.
    • toEmbeddingResponse

      public static org.springframework.ai.embedding.EmbeddingResponse toEmbeddingResponse(List<float[]> vectors)
      Creates a Spring AI EmbeddingResponse from a list of embedding vectors.
    • toContentEmbedding

      public static com.google.genai.types.ContentEmbedding toContentEmbedding(float[] vector)
      Converts a float array to a GenAI ContentEmbedding.
    • toContentEmbedding

      public static com.google.genai.types.ContentEmbedding toContentEmbedding(org.springframework.ai.embedding.Embedding embedding)
      Converts a Spring AI Embedding to a GenAI ContentEmbedding.
    • extractEmbedding

      public static float[] extractEmbedding(com.google.genai.types.ContentEmbedding contentEmbedding)
      Extracts a float array from a GenAI ContentEmbedding.
    • toEmbedContentResponse

      public static com.google.genai.types.EmbedContentResponse toEmbedContentResponse(org.springframework.ai.embedding.EmbeddingResponse response)
      Converts a Spring AI EmbeddingResponse to a GenAI EmbedContentResponse, ordering batch embeddings by Embedding.getIndex() when present.
    • fromEmbedContentResponse

      public static org.springframework.ai.embedding.EmbeddingResponse fromEmbedContentResponse(com.google.genai.types.EmbedContentResponse response)
      Converts a GenAI EmbedContentResponse to a Spring AI EmbeddingResponse.
    • cosineSimilarity

      public static double cosineSimilarity(float[] embedding1, float[] embedding2)
      Calculate cosine similarity between two embedding vectors.
      Parameters:
      embedding1 - First embedding vector
      embedding2 - Second embedding vector
      Returns:
      Cosine similarity score between -1 and 1
    • euclideanDistance

      public static double euclideanDistance(float[] embedding1, float[] embedding2)
      Calculate Euclidean distance between two embedding vectors.
      Parameters:
      embedding1 - First embedding vector
      embedding2 - Second embedding vector
      Returns:
      Euclidean distance
    • normalize

      public static float[] normalize(float[] embedding)
      Normalize an embedding vector to unit length.
      Parameters:
      embedding - The embedding vector to normalize
      Returns:
      Normalized embedding vector
    • findMostSimilar

      public static int findMostSimilar(float[] query, List<float[]> candidates)
      Find the most similar embedding from a list of candidates.
      Parameters:
      query - The query embedding
      candidates - List of candidate embeddings
      Returns:
      Index of the most similar embedding, or -1 if no candidates
    • calculateSimilarities

      public static List<Double> calculateSimilarities(float[] query, List<float[]> candidates)
      Calculate similarity scores between a query and all candidates.
      Parameters:
      query - The query embedding
      candidates - List of candidate embeddings
      Returns:
      List of similarity scores
    • toDoubleArray

      public static double[] toDoubleArray(float[] floatArray)
      Convert float array to double array.
      Parameters:
      floatArray - The float array
      Returns:
      Equivalent double array
    • toFloatArray

      public static float[] toFloatArray(double[] doubleArray)
      Convert double array to float array.
      Parameters:
      doubleArray - The double array
      Returns:
      Equivalent float array