Spring-Ai-Alibaba [02] chatclient-demo
Spring-Ai-Alibaba [02] chatclient-demo
- 概述
- 开发环境
- 项目结构
- pom.xml
- application.yml 配置文件
- config 配置类
- controller 层
- Hello01Controller.java
- Hello02Controller.java
- 启动类
- 验证
- [ChatClient]-方式1:Hello01Contrller
- [ChatClient]-方式2:Hello2Controller
概述
本文是 Spring AI Alibaba 框架学习系列第二篇,介绍chatclient的使用。
代码上传至 Gitee:https://gitee.com/xbjct/spring-ai-alibaba-demo
开发环境
- 基础框架: Spring Boot 3.5.14
- AI 框架: Spring AI 1.1.2 + Spring AI Alibaba 1.1.2.2
- 大模型: 阿里云通义千问 (qwen-plus)
- 构建工具: Maven 3.9.11
- JDK 版本: 21.0.10
项目结构
pom.xml
<projectxmlns="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.junjiu.spring.ai.alibaba.demo</groupId><artifactId>Spring-AI-Alibaba-Demo</artifactId><version>1.0-SNAPSHOT</version></parent><artifactId>02-chatclient-demo</artifactId><packaging>jar</packaging><name>02-chatclient-demo</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties></project>application.yml 配置文件
server:port:5826servlet:# 解决流式中文对话乱码问题.encoding:charset:utf-8enabled:trueforce:truespring:application:name:02-chatclient-demoai:dashscope:base-url:https://dashscope.aliyuncs.comapi-key:${AIALI_API_KEY}chat:options:model:qwen-plusconfig 配置类
packagecom.junjiu.spring.ai.alibaba.demo.config;importorg.springframework.ai.chat.client.ChatClient;importorg.springframework.ai.chat.model.ChatModel;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;/** * program: Spring-AI-Alibaba-Demo * ClassName: ChatClientConfig * description: * * @author: 君九 * @create: 2026-05-21 08:55 * @version: 1.0 **/@ConfigurationpublicclassChatClientConfig{/** * 创建 ChatClient 对象,并注入到 Spring 容器中。 * @param chatModel * @return */@BeanpublicChatClientchatClient(ChatModelchatModel){// return ChatClient.builder(chatModel).build();returnChatClient.create(chatModel);}}controller 层
Hello01Controller.java
packagecom.junjiu.spring.ai.alibaba.demo.controller;importorg.springframework.ai.chat.client.ChatClient;importorg.springframework.ai.chat.model.ChatModel;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;importreactor.core.publisher.Flux;/** * program: Spring-AI-Alibaba-Demo * ClassName: HelloController * description: * * @author: 君九 * @create: 2026-05-18 01:05 * @version: 1.0 **/@RestController@RequestMapping("/hello01")publicclassHello01Controller{@AutowiredprivateChatModelchatModel;/** * 聊天对话. * @param message * @return */@GetMapping("/chat")publicStringchat(@RequestParam(name="message",defaultValue="你好")Stringmessage){returnchatModel.call(message);}/** * 聊天对话. * @param message * @return */@GetMapping("/streamChat")publicFlux<String>streamChat(@RequestParam(name="message",defaultValue="你好")Stringmessage){returnchatModel.stream(message);}/********************************************************************************************************************/// ChatClient 模式/********************************************************************************************************************//** * ChatClient 第一种使用方式, * 在 SpringBoot 项目中,创建 ChatClient.Builder 对象,并注入到 Spring 容器中,然后使用 ChatClient 对象进行聊天对话。 */privateChatClientchatClient;publicHello01Controller(ChatClient.Builderbuilder){this.chatClient=builder.build();}/** * 聊天对话. * @param message * @return */@GetMapping("/chat01")publicStringchat01(@RequestParam(name="message",defaultValue="你好")Stringmessage){returnchatClient.prompt().user(message).call().content();}/** * 聊天对话. * @param message * @return */@GetMapping("/streamChat01")publicFlux<String>streamChat01(@RequestParam(name="message",defaultValue="你好")Stringmessage){returnchatClient.prompt().user(message).stream().content();}}Hello02Controller.java
packagecom.junjiu.spring.ai.alibaba.demo.controller;importorg.springframework.ai.chat.client.ChatClient;importorg.springframework.ai.chat.model.ChatModel;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;importreactor.core.publisher.Flux;/** * program: Spring-AI-Alibaba-Demo * ClassName: Hello02Controller * description: * * @author: 君九 * @create: 2026-05-21 08:54 * @version: 1.0 **/@RestController@RequestMapping("/hello02")publicclassHello02Controller{@AutowiredprivateChatModelchatModel;/** * 聊天对话. * @param message * @return */@GetMapping("/chat")publicStringchat(@RequestParam(name="message",defaultValue="你好")Stringmessage){returnchatModel.call(message);}/** * 聊天对话. * @param message * @return */@GetMapping("/streamChat")publicFlux<String>streamChat(@RequestParam(name="message",defaultValue="你好")Stringmessage){returnchatModel.stream(message);}/********************************************************************************************************************/// ChatClient 模式/********************************************************************************************************************//** * ChatClient 第二种使用方式: * 在配置文件 ChatClientconfig中,创建 ChatClient.Builder 对象,并注入到 Spring 容器中,然后使用 ChatClient 对象进行聊天对话。 */@AutowiredChatClientchatClient;/** * 聊天对话. * @param message * @return */@GetMapping("/chat02")publicStringchat02(@RequestParam(name="message",defaultValue="你好")Stringmessage){returnchatClient.prompt().user(message).call().content();}/** * 聊天对话. * @param message * @return */@GetMapping("/streamChat02")publicFlux<String>streamChat02(@RequestParam(name="message",defaultValue="你好")Stringmessage){returnchatClient.prompt().user(message).stream().content();}}启动类
packagecom.junjiu.spring.ai.alibaba.demo;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.boot.context.event.ApplicationReadyEvent;importorg.springframework.context.ApplicationListener;importorg.springframework.core.env.Environment;/** * program: Spring-AI-Alibaba-Demo * ClassName: CheckClientApplication * description: * * @author: 君九 * @create: 2026-05-21 08:23 * @version: 1.0 **/@SpringBootApplicationpublicclassCheckClientApplication{publicstaticvoidmain(String[]args){SpringApplication.run(CheckClientApplication.class,args);}publicApplicationListener<ApplicationReadyEvent>readyEventApplicationListener(Environmentenv){returnevent->{System.out.println("\n🎉========================================🎉");System.out.println("✅ Application is ready!");System.out.println("AIALI_API_KEY="+System.getenv("AIALI_API_KEY"));System.out.println("🎉========================================🎉\n");};}}验证
打开浏览器访问:
[ChatClient]-方式1:Hello01Contrller
基本对话
流式对话
[ChatClient]-方式2:Hello2Controller
基本对话
流式对话
代码上传至 Gitee:https://gitee.com/xbjct/spring-ai-alibaba-demo
若有转载,请标明出处:https://blog.csdn.net/CharlesYuangc/article/details/161273025
