`Class EmbeddingConverter`

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

public final class EmbeddingConverter extends Object
这些静态工具用于在 Spring AI 与 ADK/GenAI 的嵌入格式之间进行转换,并执行向量运算。
  • 方法概述

    Modifier and Type
    Method
    Description
    static List<Double>
    calculateSimilarities(float[] query, List<float[]> candidates)
    计算查询与所有候选项之间的相似度得分。
    static double
    cosineSimilarity(float[] embedding1, float[] embedding2)
    计算两个嵌入向量之间的余弦相似度。
    static org.springframework.ai.embedding.EmbeddingRequest
    为单个文本输入创建一个 EmbeddingRequest。
    static org.springframework.ai.embedding.EmbeddingRequest
    为多个文本输入创建一个 EmbeddingRequest。
    static double
    euclideanDistance(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 int
    findMostSimilar(float[] query, List<float[]> candidates)
    从一组候选嵌入中找到最相似的那个嵌入。
    static org.springframework.ai.embedding.EmbeddingResponse
    fromEmbedContentResponse(com.google.genai.types.EmbedContentResponse response)
    将一个 GenAI 模型转换为另一种格式或类型。 EmbedContentResponse 发送到 Spring AI EmbeddingResponse.
    static float[]
    normalize(float[] embedding)
    将嵌入向量规范化为单位长度。
    static com.google.genai.types.ContentEmbedding
    toContentEmbedding(float[] vector)
    将一个浮点数数组转换为 GenAI 数据格式。 ContentEmbedding.
    static com.google.genai.types.ContentEmbedding
    toContentEmbedding(org.springframework.ai.embedding.Embedding embedding)
    将一个 Spring AI 对象转换为另一种格式或结构。 Embedding 发送给 GenAI ContentEmbedding.
    static double[]
    toDoubleArray(float[] floatArray)
    将浮点数数组转换为双精度数数组。
    static com.google.genai.types.EmbedContentResponse
    toEmbedContentResponse(org.springframework.ai.embedding.EmbeddingResponse response)
    将一个 Spring AI 对象转换为另一种格式或结构。 EmbeddingResponse 发送给 GenAI EmbedContentResponse按照……对批量嵌入数据进行排序 Embedding.getIndex() 当该字段存在时(即当 `when present` 条件为真时)。
    static org.springframework.ai.embedding.Embedding
    toEmbedding(float[] vector, int index)
    创建一个 Spring AI 应用程序。 Embedding 从一个浮点数向量以及批次索引中获取数据。
    static org.springframework.ai.embedding.Embedding
    toEmbedding(com.google.genai.types.ContentEmbedding contentEmbedding, int index)
    将一个 GenAI 模型转换为另一种格式或类型。 ContentEmbedding 发送到 Spring AI Embedding.
    static org.springframework.ai.embedding.EmbeddingResponse
    toEmbeddingResponse(float[] vector)
    创建一个 Spring AI 应用程序。 EmbeddingResponse 仅从一个嵌入向量中获取信息。
    static org.springframework.ai.embedding.EmbeddingResponse
    toEmbeddingResponse(List<float[]> vectors)
    创建一个 Spring AI 应用程序。 EmbeddingResponse 从一组嵌入向量中选取合适的向量。
    static float[]
    toFloatArray(double[] doubleArray)
    将双精度浮点数数组转换为单精度浮点数数组。

    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)
      为单个文本输入创建一个 EmbeddingRequest。
      Parameters:
      text - The text to embed
      Returns:
      EmbeddingRequest for the text
    • createRequest

      public static org.springframework.ai.embedding.EmbeddingRequest createRequest(List<String> texts)
      为多个文本输入创建一个 EmbeddingRequest。
      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)
      从 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 AI Embedding.
    • 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 发送给 GenAI ContentEmbedding.
    • 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 发送给 GenAI EmbedContentResponse按照……对批量嵌入数据进行排序 Embedding.getIndex() 当该字段存在时(即当 `when present` 条件为真时)。
    • fromEmbedContentResponse

      public static org.springframework.ai.embedding.EmbeddingResponse fromEmbedContentResponse(com.google.genai.types.EmbedContentResponse response)
      将一个 GenAI 模型转换为另一种格式或类型。 EmbedContentResponse 发送到 Spring AI EmbeddingResponse.
    • cosineSimilarity

      public static double cosineSimilarity(float[] embedding1, float[] embedding2)
      计算两个嵌入向量之间的余弦相似度。
      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)
      计算两个嵌入向量之间的欧几里得距离。
      Parameters:
      embedding1 - First embedding vector
      embedding2 - 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

      public static int findMostSimilar(float[] query, List<float[]> 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)
      计算查询与所有候选项之间的相似度得分。
      Parameters:
      query - The query embedding
      candidates - List of candidate embeddings
      Returns:
      List of similarity scores
    • 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