fix(ai-web): 完成feedback AI流程

This commit is contained in:
v-zhangjc9
2025-06-16 20:37:14 +08:00
parent e6a1bc5383
commit 45da452f18
7 changed files with 318 additions and 104 deletions

View File

@@ -32,7 +32,6 @@ public class FeedbackController {
@PostMapping("add")
public void add(@RequestBody CreateItem item) {
log.info("Enter method: add[item]. item:{}", item);
feedbackService.add(item.source, ObjectUtil.defaultIfNull(item.pictures, Lists.immutable.empty()));
}
@@ -58,6 +57,8 @@ public class FeedbackController {
private String source;
private ImmutableList<String> pictures;
private Feedback.Status status;
private String analysis;
private String analysisShort;
public ListItem(FileStoreProperties fileStoreProperties, Feedback feedback) {
this.id = feedback.getId();
@@ -65,6 +66,8 @@ public class FeedbackController {
this.pictures = feedback.getPictureIds()
.collect(id -> StrUtil.format("{}/upload/download/{}", fileStoreProperties.getDownloadPrefix(), id));
this.status = feedback.getStatus();
this.analysis = feedback.getAnalysis();
this.analysisShort = feedback.getAnalysisShort();
}
}
}

View File

@@ -9,7 +9,7 @@ public class Feedback {
private String source;
private String analysisShort;
private String analysis;
private ImmutableList<String> pictureIds;
private ImmutableList<Long> pictureIds;
private Status status;
private Long createdTime;
private Long modifiedTime;

View File

@@ -0,0 +1,16 @@
package com.lanyuanxiaoyao.service.ai.web.entity.context;
import com.lanyuanxiaoyao.service.ai.web.entity.Feedback;
import java.util.List;
import lombok.Data;
import org.eclipse.collections.api.factory.Lists;
/**
* @author lanyuanxiaoyao
* @version 20250616
*/
@Data
public class FeedbackContext {
private Feedback feedback;
private List<String> pictureDescriptions = Lists.mutable.empty();
}

View File

@@ -1,18 +1,24 @@
package com.lanyuanxiaoyao.service.ai.web.service.feedback;
import club.kingon.sql.builder.SqlBuilder;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.EnumUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.ai.core.configuration.SnowflakeId;
import com.lanyuanxiaoyao.service.ai.web.entity.Feedback;
import com.lanyuanxiaoyao.service.ai.web.entity.context.FeedbackContext;
import com.lanyuanxiaoyao.service.common.Constants;
import com.yomahub.liteflow.core.FlowExecutor;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.collections.api.factory.Lists;
import org.eclipse.collections.api.list.ImmutableList;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@@ -20,6 +26,7 @@ import org.springframework.transaction.annotation.Transactional;
@Service
public class FeedbackService {
public static final String FEEDBACK_TABLE_NAME = Constants.DATABASE_NAME + ".service_ai_feedback";
public static final String[] FEEDBACK_COLUMNS = new String[]{"id", "source", "analysis_short", "analysis", "pictures", "status", "created_time", "modified_time"};
private static final RowMapper<Feedback> feedbackMapper = (rs, row) -> {
Feedback feedback = new Feedback();
feedback.setId(rs.getLong(1));
@@ -29,7 +36,7 @@ public class FeedbackService {
feedback.setPictureIds(
StrUtil.isBlank(rs.getString(5))
? Lists.immutable.empty()
: Lists.immutable.ofAll(StrUtil.split(rs.getString(5), ","))
: Lists.immutable.ofAll(StrUtil.split(rs.getString(5), ",")).collect(Long::parseLong)
);
feedback.setStatus(EnumUtil.fromString(Feedback.Status.class, rs.getString(6)));
feedback.setCreatedTime(rs.getTimestamp(7).getTime());
@@ -37,9 +44,38 @@ public class FeedbackService {
return feedback;
};
private final JdbcTemplate template;
private final FlowExecutor executor;
public FeedbackService(JdbcTemplate template) {
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
public FeedbackService(JdbcTemplate template, FlowExecutor executor) {
this.template = template;
this.executor = executor;
}
@Scheduled(initialDelay = 1, fixedDelay = 1, timeUnit = TimeUnit.MINUTES)
public void analysis() {
List<Feedback> feedbacks = template.query(
SqlBuilder.select(FEEDBACK_COLUMNS)
.from(FEEDBACK_TABLE_NAME)
.whereEq("status", Feedback.Status.ANALYSIS_PROCESSING.name())
.build(),
feedbackMapper
);
for (Feedback feedback : feedbacks) {
FeedbackContext context = new FeedbackContext();
context.setFeedback(feedback);
executor.execute2Resp("feedback_analysis", null, context);
}
}
public Feedback get(Long id) {
return template.queryForObject(
SqlBuilder.select(FEEDBACK_COLUMNS)
.from(FEEDBACK_TABLE_NAME)
.whereEq("id", id)
.build(),
feedbackMapper
);
}
@Transactional(rollbackFor = Exception.class)
@@ -55,9 +91,37 @@ public class FeedbackService {
);
}
@Transactional(rollbackFor = Exception.class)
public void updateAnalysis(Long id, String analysis, String analysisShort) {
Assert.notNull(id, "ID cannot be null");
template.update(
SqlBuilder.update(FEEDBACK_TABLE_NAME)
.set("analysis", "?")
.addSet("analysis_short", "?")
.whereEq("id", "?")
.precompileSql(),
analysis,
analysisShort,
id
);
}
@Transactional(rollbackFor = Exception.class)
public void updateStatus(Long id, Feedback.Status status) {
Assert.notNull(id, "ID cannot be null");
template.update(
SqlBuilder.update(FEEDBACK_TABLE_NAME)
.set("status", "?")
.whereEq("id", "?")
.precompileSql(),
status.name(),
id
);
}
public ImmutableList<Feedback> list() {
return template.query(
SqlBuilder.select("id", "source", "analysis_short", "analysis", "pictures", "status", "created_time", "modified_time")
SqlBuilder.select(FEEDBACK_COLUMNS)
.from(FEEDBACK_TABLE_NAME)
.orderByDesc("created_time")
.build(),

View File

@@ -1,14 +1,181 @@
package com.lanyuanxiaoyao.service.ai.web.service.node;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.lanyuanxiaoyao.service.ai.web.entity.Feedback;
import com.lanyuanxiaoyao.service.ai.web.entity.context.FeedbackContext;
import com.lanyuanxiaoyao.service.ai.web.entity.vo.DataFileVO;
import com.lanyuanxiaoyao.service.ai.web.service.DataFileService;
import com.lanyuanxiaoyao.service.ai.web.service.feedback.FeedbackService;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.annotation.LiteflowMethod;
import com.yomahub.liteflow.core.NodeComponent;
import com.yomahub.liteflow.enums.LiteFlowMethodEnum;
import com.yomahub.liteflow.enums.NodeTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.core.io.FileSystemResource;
import org.springframework.util.MimeType;
import org.springframework.util.MimeTypeUtils;
@Slf4j
@LiteflowComponent
public class FeedbackNodes {
private final ChatClient.Builder chatClientBuilder;
private final ChatClient.Builder visualChatClientBuilder;
private final DataFileService dataFileService;
private final FeedbackService feedbackService;
public FeedbackNodes(@Qualifier("chat") ChatClient.Builder chatClientBuilder) {
public FeedbackNodes(
@Qualifier("chat") ChatClient.Builder chatClientBuilder,
@Qualifier("visual") ChatClient.Builder visualClientBuilder, DataFileService dataFileService, FeedbackService feedbackService
) {
this.chatClientBuilder = chatClientBuilder;
this.visualChatClientBuilder = visualClientBuilder;
this.dataFileService = dataFileService;
this.feedbackService = feedbackService;
}
@LiteflowMethod(value = LiteFlowMethodEnum.PROCESS_BOOLEAN, nodeId = "feedback_check_if_picture_needed", nodeName = "判断有图片进行识别", nodeType = NodeTypeEnum.BOOLEAN)
public boolean checkIfPictureReadNeeded(NodeComponent node) {
FeedbackContext context = node.getContextBean(FeedbackContext.class);
Feedback feedback = context.getFeedback();
return ObjectUtil.isNotEmpty(feedback.getPictureIds());
}
@LiteflowMethod(value = LiteFlowMethodEnum.PROCESS, nodeId = "image_read", nodeName = "读取图片", nodeType = NodeTypeEnum.COMMON)
public void imageRead(NodeComponent node) {
FeedbackContext context = node.getContextBean(FeedbackContext.class);
Feedback feedback = context.getFeedback();
ChatClient client = visualChatClientBuilder
// language=TEXT
.defaultSystem("""
你是一个专业的OCR解析助手。请严格按以下步骤处理用户上传的图片
1. 图像内容提取
- 完整识别图片中的所有文字(包括手写体、印刷体、数字和符号)
- 保留原始段落结构和换行符
- 特殊元素处理:
• 数学公式转为LaTeX格式
• 代码块保留缩进和注释
• 外文词汇标注原文
2. 表格解析优化
- 识别所有表格区域
- 转换为Markdown表格格式对齐表头与单元格
- 补充缺失的表格线
- 用▲标注合并单元格▲跨3列▲
3. 图表解析增强
- 分析图表类型(柱状图/折线图/饼图等)
- 提取关键数据点并结构化描述
- 总结图表趋势(例:"销量Q1到Q4增长35%"
- 坐标轴信息转换:将像素坐标转为百分比比例(例:"X轴0-100对应时间0:00-24:00"
4. 输出规范
- 按[文本][表格][图表]分区块输出
- 表格/图表区域标注原始位置(例:"[左上区域表格]"
- 模糊内容用[?]标注并给出备选(例:"年收[?]入(可能为'入'或'人'"
- 保持原始数据精度(不四舍五入)
立即开始处理用户图片,无需确认步骤。
""")
.build();
for (Long pictureId : feedback.getPictureIds()) {
DataFileVO file = dataFileService.downloadFile(pictureId);
log.info("Parse picture: {} {}", file.getFilename(), file.getPath());
MimeType type = switch (StrUtil.blankToDefault(file.getType(), "").toLowerCase()) {
case "jpg", "jpeg" -> MimeTypeUtils.IMAGE_JPEG;
default -> MimeTypeUtils.IMAGE_PNG;
};
String content = client.prompt()
.user(spec -> spec
.text("输出图片内容")
.media(type, new FileSystemResource(file.getPath())))
.call()
.content();
log.info("Picture: {}", content);
context.getPictureDescriptions().add(content);
}
}
@LiteflowMethod(value = LiteFlowMethodEnum.PROCESS, nodeId = "feedback_suggest", nodeName = "大模型建议", nodeType = NodeTypeEnum.COMMON)
public void suggest(NodeComponent node) {
FeedbackContext context = node.getContextBean(FeedbackContext.class);
Feedback feedback = context.getFeedback();
ChatClient client = chatClientBuilder.build();
String description = client.prompt()
// language=TEXT
.system("""
你是一名专业的IT系统运维工程师对于用户输入的关于系统的报障信息你会严格遵循以下步骤进行处理
1.输入
[故障描述]
(这里是用户遇到的系统故障的详细描述)
[相关截图]
(这里是用户遇到的系统故障相关的截图的文字描述,如果没有相关截图,这里会写“无”;如果有多张图片,图片和图片之间会使用“---”分隔)
2.处理逻辑
解析输入
读取并解析用户提供的故障描述和相关截图描述。
识别关键元素:包括故障类型(如硬件故障、软件错误)、受影响系统组件(如服务器、网络设备)、错误消息、发生时间、重现步骤、影响范围等。
如果截图描述存在,提取关键细节(如错误弹窗文本、系统状态截图),并将其作为辅助证据;如果无截图,则忽略此部分。
分析与重写故障描述
专业化改写使用标准IT术语替换非专业用语例如“电脑死机”改为“系统无响应”“连不上网”改为“网络连接中断”并确保描述符合行业规范。
排除歧义澄清模糊描述如添加具体时间戳、系统版本、IP地址或错误代码移除主观语言如“我觉得”或“可能”并添加必要上下文如操作系统环境、相关服务运行状态
结构化组织:将故障描述重写为逻辑段落,格式包括:
问题概述:简明总结故障本质(例如,“数据库服务异常导致应用无法访问”)。
详细症状描述具体现象包括错误消息、发生频率和影响范围如“影响用户登录功能错误代码500”
重现步骤列出可复现故障的操作序列如“1. 访问URL X2. 触发操作 Y”
相关环境添加系统细节如“运行在Linux Ubuntu 20.04, Java 11环境”
整合截图信息如果截图描述存在将其嵌入重写中作为证据例如“根据截图错误弹窗显示Connection timeout
质量校验
检查重写后的内容是否完整、一致且无歧义:确保所有用户输入细节都被涵盖,添加缺失信息(如建议的故障分类),并验证专业术语的准确性。
如果输入信息不足(如缺少时间戳或系统版本),在输出中添加注释提示用户补充。
3.输出
输出一个专业、结构化的故障报告,格式清晰,可直接用于运维团队诊断。
重写的故障描述,以结构化段落呈现,涵盖问题概述、详细症状、重现步骤和相关环境。
输出将使用中性、客观语言,避免任何个人意见或建议,以确保报告专注于事实描述。
""")
.call()
.content();
Assert.notBlank(description, "Description cannot be blank");
String analysis = client.prompt()
.system("""
你是一名专业的IT系统运维工程师对于用户输入的报障信息你会给出专业的意见
""")
.user(StrUtil.format("""
[故障描述]
{}
[相关截图]
{}
""",
description,
ObjectUtil.isEmpty(context.getPictureDescriptions()) ? "" : StrUtil.join(",", context.getPictureDescriptions())
))
.call()
.content();
feedback.setAnalysis(analysis);
Assert.notBlank(description, "Analysis cannot be blank");
String analysisShort = client.prompt()
.system("""
你是一名专业的文字编辑,对用户输入的内容,用一段话总结内容
""")
.user(analysis)
.call()
.content();
feedback.setAnalysisShort(analysisShort);
}
@LiteflowMethod(value = LiteFlowMethodEnum.PROCESS, nodeId = "feedback_save", nodeName = "保存分析内容", nodeType = NodeTypeEnum.COMMON)
public void saveFeedback(NodeComponent node) {
FeedbackContext context = node.getContextBean(FeedbackContext.class);
Feedback feedback = context.getFeedback();
feedbackService.updateAnalysis(feedback.getId(), feedback.getAnalysis(), feedback.getAnalysisShort());
feedbackService.updateStatus(feedback.getId(), Feedback.Status.ANALYSIS_SUCCESS);
}
}

View File

@@ -23,4 +23,14 @@
<chain id="embedding_submit_directly">
SER(import_vector_source)
</chain>
<chain id="feedback_analysis">
SER(
IF(
feedback_check_if_picture_needed,
image_read
),
feedback_suggest,
feedback_save
)
</chain>
</flow>

View File

@@ -1,114 +1,68 @@
package com.lanyuanxiaoyao.service.ai.web;
import java.net.http.HttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.lanyuanxiaoyao.service.ai.core.configuration.WebClientConfiguration;
import com.lanyuanxiaoyao.service.ai.web.configuration.LlmConfiguration;
import com.lanyuanxiaoyao.service.ai.web.configuration.LlmProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.ai.openai.api.OpenAiApi;
import org.springframework.http.client.JdkClientHttpRequestFactory;
import org.springframework.http.client.reactive.JdkClientHttpConnector;
import org.springframework.web.client.RestClient;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.core.io.FileSystemResource;
import org.springframework.util.MimeTypeUtils;
/**
* @author lanyuanxiaoyao
* @version 20250526
*/
@Slf4j
public class TestLlm {
private static final Logger log = LoggerFactory.getLogger(TestLlm.class);
public static void main(String[] args) {
HttpClient httpClient = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.build();
ChatClient client = ChatClient.builder(
OpenAiChatModel.builder()
.openAiApi(
OpenAiApi.builder()
.baseUrl("http://132.121.206.65:10086")
.apiKey("*XMySqV%>hR&v>>g*NwCs3tpQ5FVMFEF2VHVTj<MYQd$&@$sY7CgqNyea4giJi4")
.webClientBuilder(WebClient.builder().clientConnector(new JdkClientHttpConnector(httpClient)))
.restClientBuilder(RestClient.builder().requestFactory(new JdkClientHttpRequestFactory(httpClient)))
.build()
)
.defaultOptions(
OpenAiChatOptions.builder()
.model("Qwen3-1.7-vllm")
.build()
)
.build()
)
.build();
LlmConfiguration configuration = new LlmConfiguration();
LlmProperties properties = new LlmProperties();
LlmProperties.ChatProperties chatProperties = new LlmProperties.ChatProperties();
chatProperties.setBaseUrl("http://132.121.206.65:10086");
chatProperties.setApiKey("*XMySqV%>hR&v>>g*NwCs3tpQ5FVMFEF2VHVTj<MYQd$&@$sY7CgqNyea4giJi4");
chatProperties.setModel("Qwen2.5/qwen2.5-vl-7b");
properties.setVisual(chatProperties);
ChatClient client = configuration.visualClientBuilder(
properties,
WebClientConfiguration.generateWebClientBuilder(),
WebClientConfiguration.generateRestClientBuilder()
).build();
String content = client.prompt()
// language=TEXT
.system("""
对用户输入的文本,生成多组高质量的问答对。请遵循以下指南
1. 问题部分:
为同一个主题创建尽可能多的不同表述的问题,确保问题的多样性。
每个问题应考虑用户可能的多种问法,例如:
直接询问(如“什么是...?”)
请求确认(如“是否可以说...?”)
寻求解释(如“请解释一下...的含义。”)
假设性问题(如“如果...会怎样?”)
例子请求(如“能否举个例子说明...?”)
问题应涵盖文本中的关键信息、主要概念和细节,确保不遗漏重要内容。
2. 答案部分:
提供一个全面、信息丰富的答案,涵盖问题的所有可能角度,确保逻辑连贯。
答案应直接基于给定文本,确保准确性和一致性。
包含相关的细节,如日期、名称、职位等具体信息,必要时提供背景信息以增强理解。
3. 格式:
使用"问:"标记问题集合的开始,所有问题应在一个段落内,问题之间用空格分隔。
使用"答:"标记答案的开始,答案应清晰分段,便于阅读。
问答对之间用“---”分隔,以提高可读性。
4. 内容要求:
确保问答对紧密围绕文本主题,避免偏离主题。
避免添加文本中未提及的信息,确保信息的真实性。
一个问题搭配一个答案,避免一组问答对中同时涉及多个问题。
如果文本信息不足以回答某个方面,可以在答案中说明 "根据给定信息无法确定",并尽量提供相关的上下文。
""")
.user("""
Apache Hudi 是一款开源的数据湖管理框架,专注于高效处理大规模数据集的增量更新和实时操作。以下是其核心要点介绍:
核心功能与特性
增量数据处理支持插入Insert、更新Update、删除Delete操作并通过“Upsert”插入或更新机制高效处理变更数据避免重写整个数据集。
事务支持:提供 ACID 事务保证,确保数据一致性和可靠性,支持原子提交和回滚。
存储优化:自动合并小文件并维护文件最佳大小,减少存储碎片化问题,提升查询性能。
多查询类型:
快照查询:获取最新数据状态(对 MoR 表合并基础文件和增量文件)。
增量查询:捕获自某次提交后的变更数据流,适用于增量管道。
读取优化查询:针对 MoR 表展示最新压缩后的数据。
存储模型
写入时复制Copy-on-Write, CoW数据以列式格式如 Parquet存储每次更新生成新版本文件适合读多写少的场景。
读取时合并Merge-on-Read, MoR结合列式Parquet和行式Avro存储更新写入增量文件后异步合并适合写频繁的场景。
适用场景
合规性需求:支持 GDPR、CCPA 等法规要求的数据删除和更新。
实时数据流处理:如 IoT 设备数据、CDC变更数据捕获系统实现近实时分析。
数据湖管理:优化大规模数据集的存储和查询效率,支持时间旅行查询和历史版本回溯。
技术生态与兼容性
多引擎支持:与 Spark、Flink、Hive、Presto、Trino 等计算和查询引擎集成。
存储格式:基于 Parquet 和 Avro兼容 Hadoop 生态及云存储(如 Amazon S3
优势对比
相较于同类产品(如 Apache Iceberg、Delta LakeHudi 在实时更新和增量处理方面表现突出,尤其适合需要频繁数据变更的场景。其独特的 MoR 模型在写入性能上优于传统批处理方案。
总结
Apache Hudi 通过灵活的存储模型、高效的事务管理和广泛的生态系统集成,成为构建现代化数据湖的核心工具,适用于金融、物联网、实时分析等对数据新鲜度和操作效率要求高的领域。
你是一个专业的OCR解析助手。请严格按以下步骤处理用户上传的图片
1. 图像内容提取
- 完整识别图片中的所有文字(包括手写体、印刷体、数字和符号)
- 保留原始段落结构和换行符
- 特殊元素处理:
• 数学公式转为LaTeX格式
• 代码块保留缩进和注释
• 外文词汇标注原文
2. 表格解析优化
- 识别所有表格区域
- 转换为Markdown表格格式对齐表头与单元格
- 补充缺失的表格线
- 用▲标注合并单元格▲跨3列▲
3. 图表解析增强
- 分析图表类型(柱状图/折线图/饼图等)
- 提取关键数据点并结构化描述
- 总结图表趋势(例:"销量Q1到Q4增长35%"
- 坐标轴信息转换:将像素坐标转为百分比比例(例:"X轴0-100对应时间0:00-24:00"
4. 输出规范
- 按[文本][表格][图表]分区块输出
- 表格/图表区域标注原始位置(例:"[左上区域表格]"
- 模糊内容用[?]标注并给出备选(例:"年收[?]入(可能为'入'或'人'"
- 保持原始数据精度(不四舍五入)
立即开始处理用户图片,无需确认步骤。
""")
.user(spec -> spec
// language=TEXT
.text("输出图片内容")
.media(MimeTypeUtils.IMAGE_PNG, new FileSystemResource("/Users/lanyuanxiaoyao/Downloads/图片_002_07.png")))
.call()
.content();
log.info(content);