惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Security Affairs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
N
Netflix TechBlog - Medium
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
A
About on SuperTechFans
Last Week in AI
Last Week in AI
博客园 - 叶小钗
博客园 - Franky
腾讯CDC
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The GitHub Blog
The GitHub Blog
Google DeepMind News
Google DeepMind News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
小众软件
小众软件
The Hacker News
The Hacker News
C
Cisco Blogs
C
CXSECURITY Database RSS Feed - CXSecurity.com
L
LangChain Blog
WordPress大学
WordPress大学
美团技术团队
P
Proofpoint News Feed
T
Threat Research - Cisco Blogs
AWS News Blog
AWS News Blog
S
Securelist
T
Tenable Blog
I
Intezer
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
LINUX DO - 热门话题
博客园 - 三生石上(FineUI控件)
Y
Y Combinator Blog
Cisco Talos Blog
Cisco Talos Blog
T
Tor Project blog
Security Latest
Security Latest
Apple Machine Learning Research
Apple Machine Learning Research
C
Cybersecurity and Infrastructure Security Agency CISA
S
Schneier on Security
L
Lohrmann on Cybersecurity
P
Privacy & Cybersecurity Law Blog
月光博客
月光博客
P
Proofpoint News Feed
Vercel News
Vercel News
Simon Willison's Weblog
Simon Willison's Weblog
G
GRAHAM CLULEY
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
博客园 - 【当耐特】
A
Arctic Wolf
aimingoo的专栏
aimingoo的专栏

博客园 - cn2025

kubesphere-KDP 工具脚本【kubeSphere发布ruoyi-pro前端】20260720 HbuilderX 内置终端转为 gitBash 20260720 kubeSphere发布ruoyi-pro后端-用到脚本 kubeSphere发布ruoyi-web前端 20260711 kubeSphere发布ruoyi-pro后端 k8s集群-kubeShpere 20260710 安装Helm 20260710 k8s-portainer docker 镜像查询 k8集群一键重置 docker run OceanBase spring ai alibaba doc AI2.0 【多模态】 20260615 世界只有一个墨脱 AI2.0 【Mcp-client】 20260611 AI2.0 【Mcp-server】 20260611 GoLand 配Go SDK AI2.0 【redis向量-Rag】 问答顾问器QuestionAnswerAdvisor 20260608 AI2.0 【redis向量】redis-stack 20260608 AI2.0 【Embedding】嵌入模型 20260606 AI 工具 AI2.0 Tool调用20260604 AI2.0 对话chatMemory20260601 AI2.0 模型输出结构化【POJO、Record、List、Map 】20260530 AI2.0 Prompt【模板、流式 API、系统提示词】 20260529 AI2.0 自定义Advisor 20260528 AI2.0 ollama 20260528 AI2.0 dashscope-openai 20260527 ps 配Claude 20260527 spring-ai-alibaba-agent 260526 票务 Tool+Spring Security【动态tooLs:toolCallbacks】 20260525 spring-ai-alibaba-agent 260525 spring-ai-alibaba-agent 260523 票务 Tool接口 / 方法 / 参数【无意义、可读、业务化、参数数量过多】 20260521 票务 Tool参数幻觉 20260521 spring-ai-alibaba-agent 260521 ToolTemperature 温度过低,AI推算缺失自由度20260520 spring-ai-alibaba-agent 260520 票务Tool 20260519 票务助手 -多模型20260518 结构化输出 -原理【structuredconverter】20260518 spring-ai-alibaba-agent 260518 Dify 添加Ollma模型qwen2:0.5b 应用 20260516 docker 部【dify-api】/【dify-web】 20260514 spring-ai-alibaba-agent 260514 Chatmemory 多层(近、中、长期)20260513 ChatmemoryRedis 历史对话存【REDIS】20260512 ChatmemoryJdbc 历史对话存【JDBC】20260511 spring-ai-alibaba-agent 260511 ChatmemoryConversationId 多用户对话记忆 20260508 spring-ai-alibaba-agent 260508 ChatmemoryMax 历史对话长度20260507 2026SE Chatmemory 对话记忆20260506 spring-ai-alibaba-agent 260506 ChatClientPrompt 自定义拦截器【ReReadingAdvisor】重读提示词 20260430 docker-apache/kafka:4.1.2部暑 集群20260423 ChatClientPrompt Template.st20260428 ChatClientPrompt Template20260427 sb-KafkaListener 20260425 docker-apache/kafka:4.1.2部暑 20260425 dashscope-sb ChatClientPrompt20260425 SBAI-MultiPlatformAndModel 20260424 PlatformModel SB-ChatClient-DeepSeekDashScopeOllamaModel 20260424 dashscope-sb ChatClient20260420 ollama-sb 多态 图转文 20260418 【gemma3:4b 解析慢】 dashscope-sb 多模态(图片、语音识别) 文生视频 ollama-sb 20260414 dashscope-sb 阿里百炼-文生图20260413 dashscope-sb20260413 dashscope-sb20260411 docker-zabbix 20260410 deepseek-sb20260408 MQTT20260403 spring-ai-alibaba-agent 260403 MqttTest 20260401 spring-ai-alibaba-agent 260401 docker安装 EMQX spring-ai-alibaba-agent 260331 MQTT 20260331 spring-ai-alibaba-agent 三大 Java 生态 AI Agent 框架 CentOS7-静态 IP centos-stream10 安装 百炼-工作流-sb sb-flink1.13.1-jdk8-分隔字符串 20260125
Chatclient 结构化输出20260514
cn2025 · 2026-05-14 · via 博客园 - cn2025

1、pom

<properties>
<java.version>17</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>3.2.0</spring-boot.version>
<spring-ai.version>1.0.0</spring-ai.version>
<spring-ai-alibaba.version>1.0.0.2</spring-ai-alibaba.version>
    <jedis.version>5.2.0</jedis.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 阿里云通义千问(DashScope)starter -->
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter-dashscope</artifactId>
</dependency>

<!--对话记忆 chat-memory-->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-autoconfigure-model-chat-memory</artifactId>
</dependency>
<!-- Spring AI JDBC 聊天记忆核心依赖 -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-chat-memory-repository-jdbc</artifactId>
</dependency>
<!-- Spring Boot JDBC Starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- MySQL 驱动 -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Spring AI Alibaba Redis 记忆 Starter -->
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-starter-memory-redis</artifactId>
</dependency>

<!-- Jedis 客户端依赖 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
</dependency>

</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- 统一管理Spring AI依赖版本 -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud.ai</groupId>
<artifactId>spring-ai-alibaba-bom</artifactId>
<version>${spring-ai-alibaba.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- Spring AI 里程碑/快照仓库(必须配置,否则依赖无法下载) -->
<repositories>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<releases>
<enabled>false</enabled>
</releases>
</repository>
</repositories>

2、yml


server:
port: 18081
spring:
ai:
dashscope:
api-key: sk-8718a83408d7443b9544cXXXXXXXX
chat:
memory:
repository:
jdbc:
initialize-schema: always
schema: classpath:/sql/schema-mysql.sql
   memory:
   # Redis 配置必须放在 spring.ai 下,层级要和你的 @Value 匹配
   redis:
   host: 192.168.91.165 # 你的 Redis 地址
   port: 6379
   timeout: 5000
   # password: 123456 # 有密码再打开

datasource:
url: jdbc:mysql://192.168.91.165:3306/springai?useSSL=false&serverTimezone=UTC
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
logging:
level:
org.springframework.ai.chat.client.advisor.SimpleLoggerAdvisor: debug
#org.springframework.ai.chat.client.advisor: debug


3、pojo
public record Address(
String name, // 收件人姓名
String phone, // 联系电话
String province, // 省
String city, // 市
String district, // 区/县
String detail // 详细地址
) {}

 

4、controller
import com.sb.dashscope18081.pojo.Address;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/openai")
@ResponseBody
@Controller
public class ChatclientStructuredOutputController {
@Autowired
private ChatClient.Builder chatClientBuilder;

/**
* 以Boolean为例,在agent中可以用于判定用于的内容2个分支,不同的分支走不同的逻辑
* @return
*/
@GetMapping("/simple/chatclientsoutput")
public String chatclient () {
// 构建ChatClient并调用
ChatClient chatClient = chatClientBuilder
.build();
String content ="";
Boolean isComplain = chatClient
.prompt()
.system("""
请判断用户信息是否表达了投诉意图?
只能用 true 或 false 回答,不要输出多余内容
""")
.user("你们家的快递迟迟不到,我要退货!")
.call()
.entity(Boolean.class);

// 分支逻辑
if (Boolean.TRUE.equals(isComplain)) {
content="用户是投诉, 转接人工客服!";
System.out.println(content);
} else {
content="用户不是投诉, 自动流转客服机器人。";
System.out.println(content);
// todo 继续调用 客服ChatClient进行对话
}
return "【以Boolean为例】"+content;
}

/**
* 以Boolean为例,在agent中可以用于判定用于的内容2个分支,不同的分支走不同的逻辑
* @return
*/
@GetMapping("/simple/chatclientsoutput2")
public String chatclient2 () {
// 构建ChatClient并调用
ChatClient chatClient = chatClientBuilder
.build();
String content ="";
Boolean isComplain = chatClient
.prompt()
.system("""
请判断用户信息是否表达了投诉意图?
只能用 true 或 false 回答,不要输出多余内容
""")
.user("你好!")
.call()
.entity(Boolean.class);

// 分支逻辑
if (Boolean.TRUE.equals(isComplain)) {
content="用户是投诉, 转接人工客服!";
System.out.println(content);
} else {
content="用户不是投诉, 自动流转客服机器人。";
System.out.println(content);
// todo 继续调用 客服ChatClient进行对话
}
return "【以Boolean为例】"+content;
}

/**
* 从文本提取地址信息address
* @return
*/
@GetMapping("/simple/chatclientaddress")
public String chatclientaddress () {
// 构建ChatClient并调用
ChatClient chatClient = chatClientBuilder
.build();
String content ="";
Address address = chatClient.prompt()
.system("""
请从下面这条文本中提取收货信息
""")
.user("收货人:张三,电话13588888888,地址:浙江省杭州市西湖区文一西路100号8幢202室")
.call()
.entity(Address.class);
content=address.toString();
System.out.println(content);
return "【从文本提取地址信息address】"+content;
}

}

5、5.1
http://localhost:18081/openai/simple/chatclientsoutput

image

image

5.2

http://localhost:18081/openai/simple/chatclientsoutput2

image

image

 

5.3

image

image

http://localhost:18081/openai/simple/chatclientaddress

image

 

image

image