Spring AI Alibaba-ChatClient

一、ChatClient是什么?

二、ChatModel与ChatClient的区别

  • ChatModel:对话模型(ChatModel)是底层接口,直接与具体大语言模型交互,提供call()和stream()方法,适合简单大模型交互场景。

  • ChatClient:ChatClient是高级封装,基于ChatModel构建,适合快速构建标准化复杂AI服务,支持同步和流式交互,集成多种高级功能。

三、编码

依赖

java 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.ai</groupId>
        <artifactId>SpringAI</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>SSA-03ChatClientModel</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.cloud.ai</groupId>
            <artifactId>spring-ai-alibaba-starter-dashscope</artifactId>
            <version>1.1.2.1</version>
        </dependency>
    </dependencies>

</project>

配置文件

java 复制代码
server.port=9096
server.servlet.encoding.enabled=true
server.servlet.encoding.force=true
server.servlet.encoding.charset=UTF-8
spring.ai.dashscope.api-key=${qianwen}

Controller

java 复制代码
package com.ai.controller;

import jakarta.annotation.Resource;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

@RestController
public class ChatClientV2Controller {
//    可共存
    @Resource
    ChatModel chatModel;
    @Resource
    ChatClient chatClient;
    @RequestMapping("/chat/normal")
    public String say(@RequestParam(value = "msg",defaultValue = "你是谁") String msg){
        return chatModel.call(msg);
    }

    @RequestMapping("/chatClient/v2")
    public String chatClient(@RequestParam(value = "msg",defaultValue = "分析今日金价") String msg){
        return chatClient.prompt().user(msg).call().content();
    }
}

总结

相关推荐
武子康1 小时前
Java-15 深入浅出MyBatis 分页与通用 Mapper 实战:PageHelper + tk.mybatis 从配置到分页查询
java·后端
MartinYeung51 小时前
[论文学习]使用使用者层级差分隐私(User-Level DP)微调大型语言模型(LLM)
人工智能·学习·语言模型
财经资讯数据_灵砚智能1 小时前
基于全球经济类多源新闻的NLP情感分析与数据可视化(夜间-次晨)2026年5月29日
人工智能·python·信息可视化·自然语言处理·ai编程
智慧景区与市集主理人1 小时前
巨有科技联营分账系统|多业态统一管控,破解景区分账结算难题
大数据·人工智能·科技
触底反弹1 小时前
从数据结构到 Prompt 设计:前端工程师的 AI 时代进阶指南
javascript·人工智能·python
z落落1 小时前
C# 虚方法(virtual)与抽象方法 +区别+new方法隐藏 & override方法重写
java·开发语言·c#
好好风格1 小时前
这个开源项目,把本地大模型做成会说话的 Live2D 桌宠
人工智能·python·开源
宋哥转AI1 小时前
Spring AI Graph:从0到Supervisor(二)并行执行+HITL实战
java·agent
程序猿阿伟1 小时前
《OpenClaw远程网关:密钥体系与长连接的深度拆解》
人工智能