Generic Model API
为了为所有 AI 模型客户端提供基础,创建了通用模型 API。这使得通过遵循通用模式向 Spring AI 添加新的 AI 模型支持变得容易。以下部分将介绍此 API。
In order to provide a foundation for all AI Model clients, the Generic Model API was created. This makes it easy to contribute new AI Model support to Spring AI by following a common pattern. The following sections walk through this API.
ModelClient
ModelClient 接口提供了一个用于调 AI 模型的通用 API。它旨在通过抽象发送请求和接收响应的过程来处理与各种类型的 AI 模型的交互。该接口使用 Java 泛型来适应不同类型的请求和响应,从而增强了不同 AI 模型实现的灵活性与适应性。
The ModelClient interface provides a generic API for invoking AI models. It is designed to handle the interaction with various types of AI models by abstracting the process of sending requests and receiving responses. The interface uses Java generics to accommodate different types of requests and responses, enhancing flexibility and adaptability across different AI model implementations.
该接口的定义如下:
The interface is defined below:
public interface ModelClient<TReq extends ModelRequest<?>, TRes extends ModelResponse<?>> {
/**
* Executes a method call to the AI model.
* @param request the request object to be sent to the AI model
* @return the response from the AI model
*/
TRes call(TReq request);
}
StreamingModelClient
StreamingModelClient 接口提供了一个用于使用流响应调 AI 模型的通用 API。它抽象了发送请求和接收流响应的过程。该接口使用 Java 泛型来适应不同类型的请求和响应,从而增强了不同 AI 模型实现的灵活性与适应性。
The StreamingModelClient interface provides a generic API for invoking a AI models with streaming response. It abstracts the process of sending requests and receiving a streaming responses. The interface uses Java generics to accommodate different types of requests and responses, enhancing flexibility and adaptability across different AI model implementations.
public interface StreamingModelClient<TReq extends ModelRequest<?>, TResChunk extends ModelResponse<?>> {
/**
* Executes a method call to the AI model.
* @param request the request object to be sent to the AI model
* @return the streaming response from the AI model
*/
Flux<TResChunk> stream(TReq request);
}
ModelRequest
用于表示向 AI 模型发出的请求的接口。此接口封装了与 AI 模型交互所需的信息,包括指令或输入(使用通用类型 T)和附加的模型选项。它提供了一种向 AI 模型发送请求的标准化方法,确保包含所有必需的详细信息并可以对其进行轻松管理。
Interface representing a request to an AI model. This interface encapsulates the necessary information required to interact with an AI model, including instructions or inputs (of generic type T) and additional model options. It provides a standardized way to send requests to AI models, ensuring that all necessary details are included and can be easily managed.
public interface ModelRequest<T> {
/**
* Retrieves the instructions or input required by the AI model.
* @return the instructions or input required by the AI model
*/
T getInstructions(); // required input
/**
* Retrieves the customizable options for AI model interactions.
* @return the customizable options for AI model interactions
*/
ModelOptions getOptions();
}
ModelOptions
用于表示 AI 模型交互可定制选项的接口。此标记接口允许指定各种设置和参数,这些设置和参数可以影响 AI 模型的行为和输出。它旨在为不同的 AI 场景提供灵活性与适应性,从而确保 AI 模型可以根据特定需求进行微调。
Interface representing the customizable options for AI model interactions. This marker interface allows for the specification of various settings and parameters that can influence the behavior and output of AI models. It is designed to provide flexibility and adaptability in different AI scenarios, ensuring that the AI models can be fine-tuned according to specific requirements.
public interface ModelOptions {
}
ModelResponse
用于表示从 AI 模型收到的响应的接口。此接口提供用于访问 AI 模型生成的主要结果或者结果列表的方法,以及响应元数据。它可作为封装和管理 AI 模型输出的标准化方法,从而确保轻松检索和处理生成的信息。
Interface representing the response received from an AI model. This interface provides methods to access the main result or a list of results generated by the AI model, along with the response metadata. It serves as a standardized way to encapsulate and manage the output from AI models, ensuring easy retrieval and processing of the generated information.
public interface ModelResponse<T extends ModelResult<?>> {
/**
* Retrieves the result of the AI model.
* @return the result generated by the AI model
*/
T getResult();
/**
* Retrieves the list of generated outputs by the AI model.
* @return the list of generated outputs
*/
List<T> getResults();
/**
* Retrieves the response metadata associated with the AI model's response.
* @return the response metadata
*/
ResponseMetadata getMetadata();
}
ModelResult
This interface provides methods to access the main output of the AI model and the metadata associated with this result. It is designed to offer a standardized and comprehensive way to handle and interpret the outputs generated by AI models.
public interface ModelResult<T> {
/**
* Retrieves the output generated by the AI model.
* @return the output generated by the AI model
*/
T getOutput();
/**
* Retrieves the metadata associated with the result of an AI model.
* @return the metadata associated with the result
*/
ResultMetadata getMetadata();
}