ChatModel 是 SpringAI 的底层核心接口,它定义了所有大模型统一的调用规范,是最原始、最基础的调用层。

ChatClient 是 SpringAI 基于 ChatModel 封装的高阶工具

它不是替代 ChatModel,而是对底层能力进行包装,简化编码、增强语法、扩展能力,让开发者以极简链式语法完成 AI 调用。

使用示例:

@RestController
public class ChatClientController {
    private final ChatClient dashScopeChatClient;
    public ChatClientController(ChatModel dashScopeChatModel) {
        this.dashScopeChatClient = ChatClient.builder(dashScopeChatModel).build();
    }
    @GetMapping("/chatClient/doChat")
    public String doChat(@RequestParam(value = "msg", defaultValue = "你好")String msg) {
        return dashScopeChatClient
                .prompt()       // 创建一个对话
                .user(msg)      // 用户输入的问题
                .call()         // 发送请求给 AI
                .content();     // 获取返回的回答文本
    }
}

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐