Embeddings API

EmbeddingClient 接口旨在直接与 AI 和机器学习中的嵌入模型集成。其主要功能是将文本转换为数字向量,通常称为嵌入。这些嵌入对于语义分析和文本分类等各种任务至关重要。

The EmbeddingClient 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.

EmbeddingClient 接口的设计围绕着两个主要目标:

The design of the EmbeddingClient interface centers around two primary goals:

  • 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: EmbeddingClient simplifies the process of converting text to embeddings. By providing straightforward methods like embed(String text) and embed(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 API 构建在通用 Spring AI Model API之上,这是 Spring AI 库的一部分。因此,EmbeddingClient 接口扩展了 `ModelClient`接口,其中提供了用于与人工智能模型进行交互的一组标准方法。`EmbeddingRequest`和 `EmbeddingResponse`类从 `ModelRequest`和 `ModelResponse`扩展而来,分别用于封装嵌入模型的输入和输出。

The Embedding API is built on top of the generic Spring AI Model API, which is a part of the Spring AI library. As such, the EmbeddingClient interface extends the ModelClient 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.

Embedding API 反过来被更高级别的组件用于为特定的嵌入模型(如 OpenAI、Titan、Azure OpenAI、Ollie 等)实现嵌入客户端。

The Embedding API in turn is used by higher-level components to implement Embedding Clients 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 Clients:

embeddings api

EmbeddingClient

本部分提供了 EmbeddingClient 接口和相关类的指南。

This section provides a guide to the EmbeddingClient interface and associated classes.

public interface EmbeddingClient extends ModelClient<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.
	 */
	List<Double> embed(Document document);

	/**
	 * Embeds the given text into a vector.
	 * @param text the text to embed.
	 * @return the embedded vector.
	 */
	default List<Double> 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<List<Double>> 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 doubles, 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.

Embedding

Embedding 代表单个嵌入向量。

The Embedding represents a single embedding vector.

public class Embedding implements ModelResult<List<Double>> {
	private List<Double> embedding;
	private Integer index;
	private EmbeddingResultMetadata metadata;
	// other methods omitted
}

Available Implementations

在内部,各种 EmbeddingClient 实现使用不同的底层库和 API 来执行嵌入任务。以下是一些 EmbeddingClient 实现的可用实现:

Internally the various EmbeddingClient implementations use different low-level libraries and APIs to perform the embedding tasks. The following are some of the available implementations of the EmbeddingClient implementations: