当前位置: 首页 > news >正文

Spring AI Alibaba-ChatClient

一、ChatClient是什么?

二、ChatModel与ChatClient的区别

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

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

三、编码

依赖

<?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>

配置文件

server.port=9096server.servlet.encoding.enabled=trueserver.servlet.encoding.force=trueserver.servlet.encoding.charset=UTF-8spring.ai.dashscope.api-key=${qianwen}

Controller

packagecom.ai.controller;importjakarta.annotation.Resource;importorg.springframework.ai.chat.client.ChatClient;importorg.springframework.ai.chat.model.ChatModel;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.RestController;importreactor.core.publisher.Flux;@RestControllerpublicclassChatClientV2Controller{// 可共存@ResourceChatModelchatModel;@ResourceChatClientchatClient;@RequestMapping("/chat/normal")publicStringsay(@RequestParam(value="msg",defaultValue="你是谁")Stringmsg){returnchatModel.call(msg);}@RequestMapping("/chatClient/v2")publicStringchatClient(@RequestParam(value="msg",defaultValue="分析今日金价")Stringmsg){returnchatClient.prompt().user(msg).call().content();}}

总结

http://www.jsqmd.com/news/946956/

相关文章:

  • MATLAB环境下可直接运行的KNN分类代码包:含主程序、核心函数与调用说明
  • 2026学术写作新范式:Gemini 3.1 Pro、Claude 3.5与GPT-4o协同润色实战指南
  • Appium Inspector 保姆级配置指南:从Desired Capabilities到元素定位,一次搞定
  • 别再死记硬背CSRF原理了!用Pikachu靶场实战Get/Post/Token三种攻击,手把手教你复现
  • 保姆级教程:用C#和ABB PC SDK 6.08搞定机器人上位机通信(从环境配置到一键连接)
  • 别再到处找地图JSON了!手把手教你用ECharts-GL + 阿里云DataV下载并配置离线3D地图
  • 保姆级教程:I3C总线初始化与动态地址分配实战(基于SDR模式)
  • FlagOS实现DeepSeekV4八芯片Day0适配技术解析
  • Arduino读取FlySky接收机PWM信号:从硬件连接到代码实现
  • 5个关键步骤:使用FanControl实现Windows系统风扇的智能精准控制
  • ESP-Prog驱动安装避坑指南:从FT2232HL识别到VSCode成功连接ESP32的全流程
  • WeChatExporter终极指南:3步永久保存你的微信聊天记录,告别数据丢失
  • 快手无水印下载终极指南:KS-Downloader完整使用教程
  • Python 爬虫分布式实战:Redis + 多进程爬虫实现分布式数据采集与任务分片
  • 蓝桥杯5G仿真平台保姆级配置指南:从BBU到核心网,手把手带你打通第一个5G呼叫
  • 2026年实测AI写作辅助平台榜单(实测甄选版)
  • 从‘nvidia-smi’到跑通第一个CUDA核函数:给Python开发者的CentOS服务器GPU编程初体验
  • Halcon region转图像踩坑实录:region_to_bin、region_to_label、region_to_mean到底怎么选?
  • 京东自动下单工具终极指南:4步实现24小时智能购物监控
  • 自制Digispark开发板:从ATtiny85芯片到USB可编程硬件的完整实践
  • STK卫星仿真出的数据怎么用?手把手教你将STK轨道导出为TLE格式(MATLAB联动篇)
  • 从零开始组装电脑:硬件选型、兼容性检查与装机全流程实战指南
  • 别再只盯着GPS了!手把手教你用Arduino解析北斗/GPS模块的NMEA 0183数据(附完整代码)
  • 3步搞定Mac鼠标指针个性化:Mousecape完整使用指南
  • RK3568双网口开发板,u-boot下如何固定网络设备?一个env变量ethact就搞定
  • 告别Redis?用C++手把手教你玩转LMDB:一个嵌入式内存映射数据库的实战入门
  • Qwen3.6-Plus实战:8分钟生成可部署官网的前端工作流
  • SpringBoot项目OOM排查实录:一个10MB的max-http-header-size配置是如何吃光8G堆内存的
  • 创客教育中电路设计的多元应用:从模块化到生活场景实践
  • 深入对比:ZYNQ7000上EMMC与SD卡的裸机驱动性能实测与选型建议