`Class EmbeddingConverter`
java.lang.Object
com.google.adk.models.springai.EmbeddingConverter
这些静态工具用于在 Spring AI 与 ADK/GenAI 的嵌入格式之间进行转换,并执行向量运算。
-
方法概述
Modifier and TypeMethodDescriptioncalculateSimilarities(float[] query, List<float[]> candidates) 计算查询与所有候选项之间的相似度得分。static doublecosineSimilarity(float[] embedding1, float[] embedding2) 计算两个嵌入向量之间的余弦相似度。static org.springframework.ai.embedding.EmbeddingRequestcreateRequest(String text) 为单个文本输入创建一个 EmbeddingRequest。static org.springframework.ai.embedding.EmbeddingRequestcreateRequest(List<String> texts) 为多个文本输入创建一个 EmbeddingRequest。static doubleeuclideanDistance(float[] embedding1, float[] embedding2) 计算两个嵌入向量之间的欧几里得距离。static float[]extractEmbedding(com.google.genai.types.ContentEmbedding contentEmbedding) 从 GenAI 中提取一个浮点数数组。ContentEmbedding.static List<float[]> extractEmbeddings(org.springframework.ai.embedding.EmbeddingResponse response) 从 `EmbeddingResponse` 中提取嵌入向量(embedding vectors)。static float @Nullable []extractFirstEmbedding(org.springframework.ai.embedding.EmbeddingResponse response) 从 `EmbeddingResponse` 中提取第一个嵌入向量。static com.google.common.collect.ImmutableList<String> extractTexts(org.springframework.ai.embedding.EmbeddingRequest request) 从 Spring AI 系统中提取输入的文本。EmbeddingRequest.static intfindMostSimilar(float[] query, List<float[]> candidates) 从一组候选嵌入中找到最相似的那个嵌入。static org.springframework.ai.embedding.EmbeddingResponsefromEmbedContentResponse(com.google.genai.types.EmbedContentResponse response) 将一个 GenAI 模型转换为另一种格式或类型。EmbedContentResponse发送到 Spring AIEmbeddingResponse.static float[]normalize(float[] embedding) 将嵌入向量规范化为单位长度。static com.google.genai.types.ContentEmbeddingtoContentEmbedding(float[] vector) 将一个浮点数数组转换为 GenAI 数据格式。ContentEmbedding.static com.google.genai.types.ContentEmbeddingtoContentEmbedding(org.springframework.ai.embedding.Embedding embedding) 将一个 Spring AI 对象转换为另一种格式或结构。Embedding发送给 GenAIContentEmbedding.static double[]toDoubleArray(float[] floatArray) 将浮点数数组转换为双精度数数组。static com.google.genai.types.EmbedContentResponsetoEmbedContentResponse(org.springframework.ai.embedding.EmbeddingResponse response) 将一个 Spring AI 对象转换为另一种格式或结构。EmbeddingResponse发送给 GenAIEmbedContentResponse按照……对批量嵌入数据进行排序Embedding.getIndex()当该字段存在时(即当 `when present` 条件为真时)。static org.springframework.ai.embedding.EmbeddingtoEmbedding(float[] vector, int index) 创建一个 Spring AI 应用程序。Embedding从一个浮点数向量以及批次索引中获取数据。static org.springframework.ai.embedding.EmbeddingtoEmbedding(com.google.genai.types.ContentEmbedding contentEmbedding, int index) 将一个 GenAI 模型转换为另一种格式或类型。ContentEmbedding发送到 Spring AIEmbedding.static org.springframework.ai.embedding.EmbeddingResponsetoEmbeddingResponse(float[] vector) 创建一个 Spring AI 应用程序。EmbeddingResponse仅从一个嵌入向量中获取信息。static org.springframework.ai.embedding.EmbeddingResponsetoEmbeddingResponse(List<float[]> vectors) 创建一个 Spring AI 应用程序。EmbeddingResponse从一组嵌入向量中选取合适的向量。static float[]toFloatArray(double[] doubleArray) 将双精度浮点数数组转换为单精度浮点数数组。
-
方法详细信息(Method Details)
-
createRequest
为单个文本输入创建一个 EmbeddingRequest。- Parameters:
text- The text to embed- Returns:
- EmbeddingRequest for the text
-
createRequest
-
extractTexts
public static com.google.common.collect.ImmutableList<String> extractTexts(org.springframework.ai.embedding.EmbeddingRequest request) 从 Spring AI 系统中提取输入的文本。EmbeddingRequest. -
extractEmbeddings
public static List<float[]> extractEmbeddings(org.springframework.ai.embedding.EmbeddingResponse response) 从 `EmbeddingResponse` 中提取嵌入向量(embedding vectors)。- 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) 从 `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) 创建一个 Spring AI 应用程序。Embedding从一个浮点数向量以及批次索引中获取数据。 -
toEmbedding
public static org.springframework.ai.embedding.Embedding toEmbedding(com.google.genai.types.ContentEmbedding contentEmbedding, int index) 将一个 GenAI 模型转换为另一种格式或类型。ContentEmbedding发送到 Spring AIEmbedding. -
toEmbeddingResponse
public static org.springframework.ai.embedding.EmbeddingResponse toEmbeddingResponse(float[] vector) 创建一个 Spring AI 应用程序。EmbeddingResponse仅从一个嵌入向量中获取信息。 -
toEmbeddingResponse
public static org.springframework.ai.embedding.EmbeddingResponse toEmbeddingResponse(List<float[]> vectors) 创建一个 Spring AI 应用程序。EmbeddingResponse从一组嵌入向量中选取合适的向量。 -
toContentEmbedding
public static com.google.genai.types.ContentEmbedding toContentEmbedding(float[] vector) 将一个浮点数数组转换为 GenAI 数据格式。ContentEmbedding. -
toContentEmbedding
public static com.google.genai.types.ContentEmbedding toContentEmbedding(org.springframework.ai.embedding.Embedding embedding) 将一个 Spring AI 对象转换为另一种格式或结构。Embedding发送给 GenAIContentEmbedding. -
extractEmbedding
public static float[] extractEmbedding(com.google.genai.types.ContentEmbedding contentEmbedding) 从 GenAI 中提取一个浮点数数组。ContentEmbedding. -
toEmbedContentResponse
public static com.google.genai.types.EmbedContentResponse toEmbedContentResponse(org.springframework.ai.embedding.EmbeddingResponse response) 将一个 Spring AI 对象转换为另一种格式或结构。EmbeddingResponse发送给 GenAIEmbedContentResponse按照……对批量嵌入数据进行排序Embedding.getIndex()当该字段存在时(即当 `when present` 条件为真时)。 -
fromEmbedContentResponse
public static org.springframework.ai.embedding.EmbeddingResponse fromEmbedContentResponse(com.google.genai.types.EmbedContentResponse response) 将一个 GenAI 模型转换为另一种格式或类型。EmbedContentResponse发送到 Spring AIEmbeddingResponse. -
cosineSimilarity
public static double cosineSimilarity(float[] embedding1, float[] embedding2) 计算两个嵌入向量之间的余弦相似度。- 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) 计算两个嵌入向量之间的欧几里得距离。- Parameters:
embedding1- First embedding vectorembedding2- Second embedding vector- Returns:
- Euclidean distance
-
normalize
public static float[] normalize(float[] embedding) 将嵌入向量规范化为单位长度。- Parameters:
embedding- The embedding vector to normalize- Returns:
- Normalized embedding vector
-
findMostSimilar
从一组候选嵌入中找到最相似的那个嵌入。- Parameters:
query- The query embeddingcandidates- List of candidate embeddings- Returns:
- Index of the most similar embedding, or -1 if no candidates
-
calculateSimilarities
-
toDoubleArray
public static double[] toDoubleArray(float[] floatArray) 将浮点数数组转换为双精度数数组。- Parameters:
floatArray- The float array- Returns:
- Equivalent double array
-
toFloatArray
public static float[] toFloatArray(double[] doubleArray) 将双精度浮点数数组转换为单精度浮点数数组。- Parameters:
doubleArray- The double array- Returns:
- Equivalent float array
-