聊天模型 API

聊天模型 API 使开发人员能够将 AI 驱动的聊天补全功能集成到其应用程序中。它利用预先训练的语言模型,例如 GPT (Generative Pre-trained Transformer),以自然语言生成对用户输入的类似人类的响应。spring-doc.cadn.net.cn

API 的工作原理通常是向 AI 模型发送提示或部分对话,然后 AI 模型根据其训练数据和对自然语言模式的理解生成对话的完成或继续。然后,完成的响应将返回给应用程序,应用程序可以将其呈现给用户或将其用于进一步处理。spring-doc.cadn.net.cn

这Spring AI Chat Model API旨在成为一个简单且可移植的界面,用于与各种 AI 模型交互,允许开发人员以最少的代码更改在不同模型之间切换。 这种设计与 Spring 的模块化和可互换性理念一致。spring-doc.cadn.net.cn

此外,在诸如Prompt用于输入封装和ChatResponse对于输出处理,聊天模型 API 统一了与 AI 模型的通信。 它管理请求准备和响应解析的复杂性,提供直接且简化的 API 交互。spring-doc.cadn.net.cn

您可以在 可用实施 部分找到有关可用实施的更多信息,并在 聊天模型比较 部分找到详细比较。spring-doc.cadn.net.cn

API 概述

本节提供了 Spring AI Chat 模型 API 接口和相关类的指南。spring-doc.cadn.net.cn

聊天模型

以下是 ChatModel 接口定义:spring-doc.cadn.net.cn

public interface ChatModel extends Model<Prompt, ChatResponse> {

	default String call(String message) {...}

    @Override
	ChatResponse call(Prompt prompt);
}

这call()方法替换为String参数简化了初始使用,避免了更复杂的复杂性Prompt和ChatResponse类。 在实际应用中,使用call()方法,该方法采用Prompt实例并返回一个ChatResponse.spring-doc.cadn.net.cn

StreamingChatModel (流式聊天模型)

下面是 StreamingChatModel 接口定义:spring-doc.cadn.net.cn

public interface StreamingChatModel extends StreamingModel<Prompt, ChatResponse> {

    default Flux<String> stream(String message) {...}

    @Override
	Flux<ChatResponse> stream(Prompt prompt);
}

这stream()method 采用String或Prompt参数类似于ChatModel但它使用响应式 Flux API 来流化响应。spring-doc.cadn.net.cn

提示

Prompt 是一个ModelRequest封装了 Message 对象和可选模型请求选项的列表。 以下清单显示了Prompt类,不包括构造函数和其他实用程序方法:spring-doc.cadn.net.cn

public class Prompt implements ModelRequest<List<Message>> {

    private final List<Message> messages;

    private ChatOptions modelOptions;

	@Override
	public ChatOptions getOptions() {...}

	@Override
	public List<Message> getInstructions() {...}

    // constructors and utility methods omitted
}

消息

这Messageinterface 封装了一个Prompt文本内容、元数据属性的集合以及称为MessageType.spring-doc.cadn.net.cn

接口定义如下:spring-doc.cadn.net.cn

public interface Content {

	String getText();

	Map<String, Object> getMetadata();
}

public interface Message extends Content {

	MessageType getMessageType();
}

多模态消息类型还实现了MediaContent接口提供Mediacontent 对象。spring-doc.cadn.net.cn

public interface MediaContent extends Content {

	Collection<Media> getMedia();

}

这Messageinterface 具有各种实现,这些实现对应于 AI 模型可以处理的消息类别:spring-doc.cadn.net.cn

Spring AI 消息 API

聊天补全端点,根据对话角色区分消息类别,由MessageType.spring-doc.cadn.net.cn

例如,OpenAI 识别不同对话角色的消息类别,例如system,user,function或assistant.spring-doc.cadn.net.cn

虽然术语MessageType可能意味着特定的消息格式,在此上下文中,它有效地指定了消息在对话中扮演的角色。spring-doc.cadn.net.cn

对于不使用特定角色的 AI 模型,UserMessageimplementation 充当标准类别,通常表示用户生成的查询或说明。 了解实际应用及其关系Prompt和Message,尤其是在这些角色或消息类别的上下文中,请参阅 Prompts 部分中的详细说明。spring-doc.cadn.net.cn

聊天选项

表示可以传递给 AI 模型的选项。这ChatOptionsclass 是ModelOptions,用于定义一些可传递给 AI 模型的可移植选项。 这ChatOptions类定义如下:spring-doc.cadn.net.cn

public interface ChatOptions extends ModelOptions {

	String getModel();
	Float getFrequencyPenalty();
	Integer getMaxTokens();
	Float getPresencePenalty();
	List<String> getStopSequences();
	Float getTemperature();
	Integer getTopK();
	Float getTopP();
	ChatOptions copy();

}

此外,每个特定于模型的 ChatModel/StreamingChatModel 实现都可以有自己的选项,这些选项可以传递给 AI 模型。例如,OpenAI 聊天补全模型有自己的选项,例如logitBias,seed和user.spring-doc.cadn.net.cn

这是一项强大的功能,允许开发人员在启动应用程序时使用特定于模型的选项,然后在运行时使用Prompt请求。spring-doc.cadn.net.cn

Spring AI 提供了一个复杂的系统来配置和使用聊天模型。 它允许在启动时设置默认配置,同时还提供了根据每个请求覆盖这些设置的灵活性。 这种方法使开发人员能够轻松使用不同的 AI 模型并根据需要调整参数,所有这些都在 Spring AI 框架提供的一致界面中完成。spring-doc.cadn.net.cn

以程图说明了 Spring AI 如何处理聊天模型的配置和执行,并结合了启动和运行时选项:spring-doc.cadn.net.cn

聊天选项流程
  1. 启动配置 - ChatModel/StreamingChatModel 使用“启动”聊天选项进行初始化。 这些选项是在 ChatModel 初始化期间设置的,旨在提供默认配置。spring-doc.cadn.net.cn

  2. 运行时配置 - 对于每个请求,提示可以包含运行时聊天选项:这些选项可以覆盖启动选项。spring-doc.cadn.net.cn

  3. 选项合并过程 — “合并选项”步骤结合了启动和运行时选项。 如果提供了运行时选项,则它们优先于启动选项。spring-doc.cadn.net.cn

  4. 输入处理 - “转换输入”步骤将输入指令转换为特定于模型的原生格式。spring-doc.cadn.net.cn

  5. Output Processing - “Convert Output” 步骤将模型的响应转换为标准化的ChatResponse格式。spring-doc.cadn.net.cn

启动和运行时选项的分离允许全局配置和特定于请求的调整。spring-doc.cadn.net.cn

聊天响应

的结构ChatResponseclass 的 API 如下所示:spring-doc.cadn.net.cn

public class ChatResponse implements ModelResponse<Generation> {

    private final ChatResponseMetadata chatResponseMetadata;
	private final List<Generation> generations;

	@Override
	public ChatResponseMetadata getMetadata() {...}

    @Override
	public List<Generation> getResults() {...}

    // other methods omitted
}

ChatResponse 类保存 AI 模型的输出,每个Generation实例,其中包含单个提示可能产生的多个输出之一。spring-doc.cadn.net.cn

这ChatResponse类还带有一个ChatResponseMetadata有关 AI 模型响应的元数据。spring-doc.cadn.net.cn

生成

最后,Generation 类从ModelResult要表示模型输出 (Assistant 消息) 和相关元数据,请执行以下作:spring-doc.cadn.net.cn

public class Generation implements ModelResult<AssistantMessage> {

	private final AssistantMessage assistantMessage;
	private ChatGenerationMetadata chatGenerationMetadata;

	@Override
	public AssistantMessage getOutput() {...}

	@Override
	public ChatGenerationMetadata getMetadata() {...}

    // other methods omitted
}

可用的实现

此图说明了统一接口ChatModel和StreamingChatModel用于与来自不同提供商的各种 AI 聊天模型进行交互,从而允许在不同的 AI 服务之间轻松集成和切换,同时为客户端应用程序保持一致的 API。spring-doc.cadn.net.cn

Spring AI Chat 完成客户端
在 聊天模型比较 部分找到可用聊天模型的详细比较。

聊天模型 API

Spring AI 聊天模型 API 构建在 Spring AI 之上Generic Model API提供特定于 Chat 的抽象和实现。 这允许在不同 AI 服务之间轻松集成和切换,同时为客户端应用程序保持一致的 API。 下面的类图说明了 Spring AI Chat 模型 API 的主要类和接口。spring-doc.cadn.net.cn

Spring AI 聊天 API