Embeddings Model API
使用 Gemini 翻译后的文本如下:嵌入是文本、图像或视频的数值表示,它们捕捉输入之间的关系。
Embeddings are numerical representations of text, images, or videos that capture relationships between inputs.
以下是使用Gemini将这段文字翻译成中文的结果:嵌入的工作原理是将文本、图像和视频转换为浮点数数组,称为向量。这些向量旨在捕捉文本、图像和视频的含义。嵌入数组的长度称为向量的维度。
Embeddings work by converting text, image, and video into arrays of floating point numbers, called vectors. These vectors are designed to capture the meaning of the text, images, and videos. The length of the embedding array is called the vector’s dimensionality.
通过计算两段文本的向量表示之间的数值距离,应用程序可以确定用于生成嵌入向量的对象之间的相似性。
By calculating the numerical distance between the vector representations of two pieces of text, an application can determine the similarity between the objects used to generate the embedding vectors.
EmbeddingModel
接口旨在与 AI 和机器学习中的嵌入模型进行直接集成。其主要功能是将文本转换为数值向量,通常称为嵌入。这些嵌入对于语义分析和文本分类等各种任务至关重要。
The EmbeddingModel
interface is designed for straightforward integration with embedding models in AI and machine learning.
Its primary function is to convert text into numerical vectors, commonly referred to as embeddings.
These embeddings are crucial for various tasks such as semantic analysis and text classification.
EmbeddingModel 接口的设计围绕两个主要目标:
The design of the EmbeddingModel interface centers around two primary goals:
-
Portability:此接口确保了跨各种嵌入模型的轻松适应性。它允许开发人员在不同的嵌入技术或模型之间切换,同时仅需进行最少的代码更改。此设计符合 Spring 的模块化和可互换性理念。
-
Portability: This interface ensures easy adaptability across various embedding models. It allows developers to switch between different embedding techniques or models with minimal code changes. This design aligns with Spring’s philosophy of modularity and interchangeability.
-
Simplicity :EmbeddingModel 简化了将文本转换为嵌入的过程。通过提供
embed(String text)
和embed(Document document)
等简单方法,它消除了处理原始文本数据和嵌入算法的复杂性。这种设计选择使开发人员,特别是那些刚接触 AI 的开发人员,更容易在应用程序中利用嵌入,而无需深入了解底层机制。 -
Simplicity: EmbeddingModel simplifies the process of converting text to embeddings. By providing straightforward methods like
embed(String text)
andembed(Document document)
, it takes the complexity out of dealing with raw text data and embedding algorithms. This design choice makes it easier for developers, especially those new to AI, to utilize embeddings in their applications without delving deep into the underlying mechanics.
API Overview
Embedding Model API 构建在通用的 Spring AI Model API 之上,它是 Spring AI 库的一部分。因此,EmbeddingModel 接口扩展了 Model
接口,该接口提供了一组标准的用于与 AI 模型交互的方法。 EmbeddingRequest
和 EmbeddingResponse
类分别扩展自 ModelRequest
和 ModelResponse
,用于封装嵌入模型的输入和输出。
The Embedding Model API is built on top of the generic Spring AI Model API, which is a part of the Spring AI library.
As such, the EmbeddingModel interface extends the Model
interface, which provides a standard set of methods for interacting with AI models. The EmbeddingRequest
and EmbeddingResponse
classes extend from the ModelRequest
and ModelResponse
are used to encapsulate the input and output of the embedding models, respectively.
嵌入 API 又被更高级别的组件用于实现特定嵌入模型(例如 OpenAI、Titan、Azure OpenAI、Ollie 等)的嵌入模型。
The Embedding API in turn is used by higher-level components to implement Embedding Models for specific embedding models, such as OpenAI, Titan, Azure OpenAI, Ollie, and others.
下图说明了嵌入 API 及其与 Spring AI 模型 API 和嵌入模型的关系:
Following diagram illustrates the Embedding API and its relationship with the Spring AI Model API and the Embedding Models:
EmbeddingModel
本节提供了 EmbeddingModel
接口和相关类的指南。
This section provides a guide to the EmbeddingModel
interface and associated classes.
public interface EmbeddingModel extends Model<EmbeddingRequest, EmbeddingResponse> {
@Override
EmbeddingResponse call(EmbeddingRequest request);
/**
* Embeds the given document's content into a vector.
* @param document the document to embed.
* @return the embedded vector.
*/
float[] embed(Document document);
/**
* Embeds the given text into a vector.
* @param text the text to embed.
* @return the embedded vector.
*/
default float[] embed(String text) {
Assert.notNull(text, "Text must not be null");
return this.embed(List.of(text)).iterator().next();
}
/**
* Embeds a batch of texts into vectors.
* @param texts list of texts to embed.
* @return list of list of embedded vectors.
*/
default List<float[]> embed(List<String> texts) {
Assert.notNull(texts, "Texts must not be null");
return this.call(new EmbeddingRequest(texts, EmbeddingOptions.EMPTY))
.getResults()
.stream()
.map(Embedding::getOutput)
.toList();
}
/**
* Embeds a batch of texts into vectors and returns the {@link EmbeddingResponse}.
* @param texts list of texts to embed.
* @return the embedding response.
*/
default EmbeddingResponse embedForResponse(List<String> texts) {
Assert.notNull(texts, "Texts must not be null");
return this.call(new EmbeddingRequest(texts, EmbeddingOptions.EMPTY));
}
/**
* @return the number of dimensions of the embedded vectors. It is generative
* specific.
*/
default int dimensions() {
return embed("Test String").size();
}
}
嵌入式方法提供了将文本转换为嵌入的各种选项,包括容纳单个字符串、结构化 Document
对象或批处理文本。
The embed methods offer various options for converting text into embeddings, accommodating single strings, structured Document
objects, or batches of text.
提供了多种嵌入文本的快捷方式,包括 embed(String text)
方法,它采用单个字符串并返回相应的嵌入向量。所有快捷方式均围绕 call
方法实现,该方法是调用嵌入模型的主要方法。
Multiple shortcut methods are provided for embedding text, including the embed(String text)
method, which takes a single string and returns the corresponding embedding vector.
All shortcuts are implemented around the call
method, which is the primary method for invoking the embedding model.
通常,嵌入返回浮点数列表,以数值向量格式表示嵌入。
Typically the embedding returns a lists of floats, representing the embeddings in a numerical vector format.
embedForResponse
方法提供更全面的输出,可能包括有关嵌入的其他信息。
The embedForResponse
method provides a more comprehensive output, potentially including additional information about the embeddings.
dimensions
方法是开发者快速确定嵌入向量大小的便捷工具,这对于了解嵌入空间和后续处理步骤非常重要。
The dimensions method is a handy tool for developers to quickly ascertain the size of the embedding vectors, which is important for understanding the embedding space and for subsequent processing steps.
EmbeddingRequest
EmbeddingRequest
是一个接受文本对象列表和可选嵌入请求选项的 ModelRequest
。以下清单显示了 EmbeddingRequest 类的一个截断版本,不包括构造函数和其他实用方法:
The EmbeddingRequest
is a ModelRequest
that takes a list of text objects and optional embedding request options.
The following listing shows a truncated version of the EmbeddingRequest class, excluding constructors and other utility methods:
public class EmbeddingRequest implements ModelRequest<List<String>> {
private final List<String> inputs;
private final EmbeddingOptions options;
// other methods omitted
}
EmbeddingResponse
EmbeddingResponse
类的结构如下:
The structure of the EmbeddingResponse
class is as follows:
public class EmbeddingResponse implements ModelResponse<Embedding> {
private List<Embedding> embeddings;
private EmbeddingResponseMetadata metadata = new EmbeddingResponseMetadata();
// other methods omitted
}
EmbeddingResponse
类保存 AI 模型的输出,每个 Embedding
实例包含来自单个文本输入的结果向量数据。
The EmbeddingResponse
class holds the AI Model’s output, with each Embedding
instance containing the result vector data from a single text input.
EmbeddingResponse
类还携带有有关 AI 模型响应的元数据 EmbeddingResponseMetadata
。
The EmbeddingResponse
class also carries a EmbeddingResponseMetadata
metadata about the AI Model’s response.
Available Implementations
在内部,各种 EmbeddingModel
实现使用不同的低级库和 API 来执行嵌入任务。以下是一些可用的 EmbeddingModel
实现:
Internally the various EmbeddingModel
implementations use different low-level libraries and APIs to perform the embedding tasks. The following are some of the available implementations of the EmbeddingModel
implementations:
-
以下是使用Gemini将这段文字翻译成中文的结果: