Spring AI Alibaba入门学习(三)

一、ChatClient VS ChatModel

ChatModelChatClient 是 Spring AI Alibaba 中两个核心的接口,它们服务于不同的开发需求。你可以把 ChatModel 看作是直接与AI模型对话的"基础电话",而 ChatClient 则是一个功能强大的"智能总机",能帮你处理更多复杂的沟通任务

二、两者对比

deepseek给出的对比:

  • 想快速实现一个功能完善、包含复杂逻辑的AI应用 (比如一个带记忆功能的客服机器人)。直接使用 ChatClient 。它内置的Advisors机制可以让你像"搭积木"一样轻松添加对话历史管理(MessageChatMemoryAdvisor)或RAG检索(QuestionAnswerAdvisor)等功能。

  • 需要精细控制模型参数或调用模型的某个特定功能 ?那就在需要的部分直接注入 ChatModel 来获得最大的灵活性。

三、测试实例

3.1 拷贝demo2新建配置类

复制代码
package com.wx.config;

import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description
 * @author: wangxin
 * @date: 2026/3/11 22:11
 */
@Configuration
public class LLMConfig {


    @Bean
    public DashScopeApi dashScopeApi() {
        return DashScopeApi.builder().apiKey(System.getenv("aliQwen-api")).build();
    }

    @Bean
    public ChatClient chatClient(ChatModel dashscopeChatModel)
    {
        return ChatClient.builder(dashscopeChatModel).build();
    }

}

3.2 yml配置

复制代码
server:
  port: 8003
  servlet:
    encoding:
      enabled: true
      force: true
      charset: utf-8

spring:
  application:
    name: demo3
  # ====ollama Config=============
  ai:
    dashscope:
      api-key: ${aliQwen-api}

3.3 controller

复制代码
package com.wx.controller;

import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

/**
 * @Description
 * @author: wangxin
 * @date: 2026/3/7 20:42
 */
@RestController
public class ChatClientController {

    @Resource(name = "dashscopeChatModel")
    private ChatModel chatModel;

    @Resource
    private ChatClient chatClient;


    @GetMapping("/chatclient/dochat")
    public String doChat(@RequestParam(name = "msg",defaultValue = "你是谁") String msg)
    {
        String result = chatClient.prompt().user(msg).call().content();
        System.out.println("ChatClient响应:" + result);
        return result;
    }

    /**
     * http://localhost:8003/chatmodelv2/dochat
     * @param msg
     * @return
     */
    @GetMapping("/chatmodel/dochat")
    public String doChat2(@RequestParam(name = "msg",defaultValue = "你是谁") String msg)
    {
        String result = chatModel.call(msg);
        System.out.println("ChatModel响应:" + result);
        return result;
    }





}

3.4 测试

相关推荐
hnlgzb15 小时前
Companion Object - 伴生对象 类比java中的什么?
java·开发语言
haiyangyiba16 小时前
学习Spring Ai的摸索实践
学习·spring ai
chase。16 小时前
【学习笔记】cuRoboV2——为高自由度机器人打造的动力学感知运动生成框架
笔记·学习·机器人
小红的布丁16 小时前
Redis 内存淘汰与过期策略
java·spring·mybatis
huihuihuanhuan.xin16 小时前
spring循环依赖以及补充相关知识
java·后端·spring
繁星星繁16 小时前
Docker(一)
java·c语言·数据结构·c++·docker·容器·eureka
泡泡鱼(敲代码中)16 小时前
C++-string学习笔记
c语言·开发语言·c++·笔记·学习·visualstudio
编程大师哥16 小时前
JAVA 动态代理
java·开发语言
圣光SG16 小时前
Java类与对象及面向对象基础核心详细笔记
java·前端·数据库
ACGkaka_16 小时前
ES 学习(六)设置账号密码(安全认证)
学习·安全·elasticsearch