Package com.google.adk.models.springai
Class EmbeddingConverter
java.lang.Object
com.google.adk.models.springai.EmbeddingConverter
Static utilities for converting between Spring AI and ADK/GenAI embedding formats and performing
vector operations.
-
Method Summary
Modifier and TypeMethodDescriptioncalculateSimilarities(float[] query, List<float[]> candidates) Calculate similarity scores between a query and all candidates.static doublecosineSimilarity(float[] embedding1, float[] embedding2) Calculate cosine similarity between two embedding vectors.static org.springframework.ai.embedding.EmbeddingRequestcreateRequest(String text) Create an EmbeddingRequest for a single text input.static org.springframework.ai.embedding.EmbeddingRequestcreateRequest(List<String> texts) Create an EmbeddingRequest for multiple text inputs.static doubleeuclideanDistance(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 GenAIContentEmbedding.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 AIEmbeddingRequest.static intfindMostSimilar(float[] query, List<float[]> candidates) Find the most similar embedding from a list of candidates.static org.springframework.ai.embedding.EmbeddingResponsefromEmbedContentResponse(com.google.genai.types.EmbedContentResponse response) Converts a GenAIEmbedContentResponseto a Spring AIEmbeddingResponse.static float[]normalize(float[] embedding) Normalize an embedding vector to unit length.static com.google.genai.types.ContentEmbeddingtoContentEmbedding(float[] vector) Converts a float array to a GenAIContentEmbedding.static com.google.genai.types.ContentEmbeddingtoContentEmbedding(org.springframework.ai.embedding.Embedding embedding) Converts a Spring AIEmbeddingto a GenAIContentEmbedding.static double[]toDoubleArray(float[] floatArray) Convert float array to double array.static com.google.genai.types.EmbedContentResponsetoEmbedContentResponse(org.springframework.ai.embedding.EmbeddingResponse response) Converts a Spring AIEmbeddingResponseto a GenAIEmbedContentResponse, ordering batch embeddings byEmbedding.getIndex()when present.static org.springframework.ai.embedding.EmbeddingtoEmbedding(float[] vector, int index) Creates a Spring AIEmbeddingfrom a float vector and batch index.static org.springframework.ai.embedding.EmbeddingtoEmbedding(com.google.genai.types.ContentEmbedding contentEmbedding, int index) Converts a GenAIContentEmbeddingto a Spring AIEmbedding.static org.springframework.ai.embedding.EmbeddingResponsetoEmbeddingResponse(float[] vector) Creates a Spring AIEmbeddingResponsefrom a single embedding vector.static org.springframework.ai.embedding.EmbeddingResponsetoEmbeddingResponse(List<float[]> vectors) Creates a Spring AIEmbeddingResponsefrom a list of embedding vectors.static float[]toFloatArray(double[] doubleArray) Convert double array to float array.
-
Method Details
-
createRequest
Create an EmbeddingRequest for a single text input.- Parameters:
text- The text to embed- Returns:
- EmbeddingRequest for the text
-
createRequest
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 AIEmbeddingRequest. -
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 AIEmbeddingfrom 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 GenAIContentEmbeddingto a Spring AIEmbedding. -
toEmbeddingResponse
public static org.springframework.ai.embedding.EmbeddingResponse toEmbeddingResponse(float[] vector) Creates a Spring AIEmbeddingResponsefrom a single embedding vector. -
toEmbeddingResponse
public static org.springframework.ai.embedding.EmbeddingResponse toEmbeddingResponse(List<float[]> vectors) Creates a Spring AIEmbeddingResponsefrom a list of embedding vectors. -
toContentEmbedding
public static com.google.genai.types.ContentEmbedding toContentEmbedding(float[] vector) Converts a float array to a GenAIContentEmbedding. -
toContentEmbedding
public static com.google.genai.types.ContentEmbedding toContentEmbedding(org.springframework.ai.embedding.Embedding embedding) Converts a Spring AIEmbeddingto a GenAIContentEmbedding. -
extractEmbedding
public static float[] extractEmbedding(com.google.genai.types.ContentEmbedding contentEmbedding) Extracts a float array from a GenAIContentEmbedding. -
toEmbedContentResponse
public static com.google.genai.types.EmbedContentResponse toEmbedContentResponse(org.springframework.ai.embedding.EmbeddingResponse response) Converts a Spring AIEmbeddingResponseto a GenAIEmbedContentResponse, ordering batch embeddings byEmbedding.getIndex()when present. -
fromEmbedContentResponse
public static org.springframework.ai.embedding.EmbeddingResponse fromEmbedContentResponse(com.google.genai.types.EmbedContentResponse response) Converts a GenAIEmbedContentResponseto a Spring AIEmbeddingResponse. -
cosineSimilarity
public static double cosineSimilarity(float[] embedding1, float[] embedding2) Calculate cosine similarity between two embedding vectors.- Parameters:
embedding1- First embedding vectorembedding2- 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 vectorembedding2- 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
Find the most similar embedding from a list of candidates.- Parameters:
query- The query embeddingcandidates- List of candidate embeddings- Returns:
- Index of the most similar embedding, or -1 if no candidates
-
calculateSimilarities
Calculate similarity scores between a query and all candidates.- Parameters:
query- The query embeddingcandidates- 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
-