# Class EmbeddingConverter

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

public class EmbeddingConverter extends Object
Utility class for converting between embedding formats and performing vector operations.

This class provides helper methods for working with embeddings generated by Spring AI models, including format conversions and similarity calculations.

- ## 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`
  `createRequest(String text)`
  Create an EmbeddingRequest for a single text input.
  `static org.springframework.ai.embedding.EmbeddingRequest`
  `createRequest(List<String> texts)`
  Create an EmbeddingRequest for multiple text inputs.
  `static double`
  `euclideanDistance(float[] embedding1, float[] embedding2)`
  Calculate Euclidean distance between two embedding vectors.
  `static List<float[]>`
  `extractEmbeddings(org.springframework.ai.embedding.EmbeddingResponse response)`
  Extract embedding vectors from an EmbeddingResponse.
  `static float[]`
  `extractFirstEmbedding(org.springframework.ai.embedding.EmbeddingResponse response)`
  Extract the first embedding vector from an EmbeddingResponse.
  `static int`
  `findMostSimilar(float[] query, List<float[]> candidates)`
  Find the most similar embedding from a list of candidates.
  `static float[]`
  `normalize(float[] embedding)`
  Normalize an embedding vector to unit length.
  `static double[]`
  `toDoubleArray(float[] floatArray)`
  Convert float array to double array.
  `static float[]`
  `toFloatArray(double[] doubleArray)`
  Convert double array to float array.

  ### Methods inherited from class 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

  - ### 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[] 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

  - ### 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