Spring AI Alibaba入门学习(三)

一、ChatClient VS ChatModel

ChatModel 和 ChatClient 是 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 测试

相关推荐
小坏讲微服务4 小时前
刚发布一个月的JDK 27,为什么我建议你立刻升级?
java·开发语言·spring boot·spring·架构·求职招聘·springboot4
kunicoding4 小时前
多线程编程核心概念详解:从线程基础到并发安全
java·jvm·安全
snow@li4 小时前
SpringBoot:核心注解全景梳理 / @xxx 叫“注解”(Annotation)
java·spring
我命由我123454 小时前
智能助手通用问答边界
人工智能·python·学习·ai·学习方法·ai编程·智能体
devilnumber4 小时前
避坑_IDEA 提取方法(Extract Method)工具
java·ide·intellij-idea
鬼手点金5 小时前
Agent 的学习机制:后训练、上下文学习与外部化学习
java·人工智能·git·学习·自动化·nanogpt
小陈不好吃5 小时前
equals 与 hashCode 的契约关系:从 HashMap 到 Lombok 注解
java·hashmap·lombok
几个几个n5 小时前
LaTeX 入门学习
学习
xiaogai_gai5 小时前
金蝶收料通知单查询策略实战教程:从源系统拉取到中间层落地的完整路径
java·前端·数据库
打工仔折腾 AI5 小时前
用 Dashlet 把极空间 NAS 服务入口整理成统一导航首页的实践
java·开发语言·人工智能·后端·python·rpc·ai agent 实战