|
|
@@ -5,9 +5,7 @@ import com.zsjz.ai.common.enums.AiParseStatusEnum;
|
|
|
import com.zsjz.ai.common.enums.FileCategoryEnum;
|
|
|
import com.zsjz.ai.common.model.dm.entity.FileAiProfile;
|
|
|
import com.zsjz.ai.common.model.dm.query.FileAiProfileQuery;
|
|
|
-import com.zsjz.ai.common.utils.Json;
|
|
|
import com.zsjz.ai.module.dm.ai.FileAiProfileService;
|
|
|
-import com.zsjz.ai.module.dm.ai.FileSampleExtractor;
|
|
|
import io.agentscope.core.message.ToolResultBlock;
|
|
|
import io.agentscope.core.tool.Tool;
|
|
|
import io.agentscope.core.tool.ToolParam;
|
|
|
@@ -24,7 +22,7 @@ import java.util.TreeSet;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
- * 非表格材料档案类 Agent 工具集(3 个工具),把 {@code file_ai_profile} 表暴露给 Agent。
|
|
|
+ * 非表格材料档案类 Agent 工具集(2 个工具),把 {@code file_ai_profile} 表暴露给 Agent。
|
|
|
*
|
|
|
* <h3>这张表是什么</h3>
|
|
|
* 用户上传的 txt / doc / docx / pdf / ppt / 压缩包 这类文件<b>不落盘</b>
|
|
|
@@ -33,21 +31,16 @@ import java.util.stream.Collectors;
|
|
|
* {@code category}(业务分类)+ {@code summary}(一句话摘要)+ {@code keywords} + {@code sampleText}。
|
|
|
* 详见 {@code module/dm/ai/FileAiProfileService}。
|
|
|
*
|
|
|
- * <h3>为什么「分析」只能是这三个动作</h3>
|
|
|
+ * <h3>为什么「分析」只能是这两个动作</h3>
|
|
|
* 原文件没落盘,能复用的只有表里那点东西,所以工具不做「重新解析文件」这类承诺:
|
|
|
* <ol>
|
|
|
* <li>{@code list_file_profiles}:按分类/关键词检索材料清单;</li>
|
|
|
- * <li>{@code stat_file_profiles_by_category}:摸清材料构成(各类多少份、什么格式);</li>
|
|
|
- * <li>{@code get_file_sample}:把某份材料的正文片段(≤4000 字)读给模型 ——
|
|
|
- * 这是全链路里<b>唯一</b>能看到材料原文的入口,也是「基于材料内容回答」的关键。</li>
|
|
|
+ * <li>{@code stat_file_profiles_by_category}:摸清材料构成(各类多少份、什么格式)。</li>
|
|
|
* </ol>
|
|
|
* 表格类文件(xls/xlsx/csv)解析出的业务数据走各自域的工具,与这里无关。
|
|
|
*
|
|
|
- * <h3>三个必须踩对的点</h3>
|
|
|
+ * <h3>两个必须踩对的点</h3>
|
|
|
* <ul>
|
|
|
- * <li><b>绝不把 {@code sampleText} 放进表格</b>:单条样本上限 4000 字,50 行就是 20 万字,
|
|
|
- * 直接冲爆模型上下文。列表工具只输出元信息,正文必须显式用 {@code get_file_sample} 取,
|
|
|
- * 且一次只取一份 —— 这是刻意的「按需下钻」。</li>
|
|
|
* <li><b>只返回头部预览</b>:结果集只回传列定义 + 前 50 行 + 全量行数,
|
|
|
* 超出行数用 {@code totalRows} 如实告知,避免让模型误以为只有这些材料。</li>
|
|
|
* <li><b>分类/状态入参宽松解析</b>:模型经常回传中文(「笔录」「识别成功」),
|
|
|
@@ -72,17 +65,6 @@ public class FileAnalysisTool {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 读取正文样本的默认长度
|
|
|
- */
|
|
|
- private static final int SAMPLE_DEFAULT_CHARS = 2000;
|
|
|
-
|
|
|
- /**
|
|
|
- * 读取正文样本的上限:直接复用采集侧的常量,避免两处 4000 各写一遍后漂移 ——
|
|
|
- * 采集时就截断到这个长度,要更多也没有。
|
|
|
- */
|
|
|
- private static final int SAMPLE_MAX_CHARS = FileSampleExtractor.MAX_SAMPLE_CHARS;
|
|
|
-
|
|
|
/**
|
|
|
* 分类统计里「识别不出分类」的兜底标签
|
|
|
*/
|
|
|
@@ -100,7 +82,7 @@ public class FileAnalysisTool {
|
|
|
* 检索非表格材料清单
|
|
|
*/
|
|
|
@Tool(name = "list_file_profiles",
|
|
|
- description = "检索非表格材料清单(txt/doc/pdf/ppt/压缩包),返回分类与摘要;看正文用当前工具列表中的材料正文工具。",
|
|
|
+ description = "检索非表格材料清单(txt/doc/pdf/ppt/压缩包),返回分类与摘要。",
|
|
|
readOnly = true)
|
|
|
public ToolResultBlock listFileProfiles(
|
|
|
@ToolParam(name = "query", description = "筛选条件,全部可空(不传即查全部)")
|
|
|
@@ -142,32 +124,6 @@ public class FileAnalysisTool {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 读取某份材料的正文样本
|
|
|
- */
|
|
|
- @Tool(name = "get_file_sample",
|
|
|
- description = "读取某份非表格材料的正文片段(Tika 抽取的文本样本);fileId 从当前工具列表中的材料清单工具取。",
|
|
|
- readOnly = true)
|
|
|
- public ToolResultBlock getFileSample(
|
|
|
- @ToolParam(name = "query", description = "材料 ID 与返回长度")
|
|
|
- FileToolSpecs.FileSampleSpec query) {
|
|
|
- FileToolSpecs.FileSampleSpec spec = query == null ? new FileToolSpecs.FileSampleSpec() : query;
|
|
|
- try {
|
|
|
- if (spec.fileId == null) {
|
|
|
- return ToolResults.error("fileId 必传,取自 list_file_profiles 返回表格的 fileId 列");
|
|
|
- }
|
|
|
- FileAiProfile profile = profileService.getByFileId(spec.fileId);
|
|
|
- if (profile == null) {
|
|
|
- return ToolResults.error("未找到 fileId=" + spec.fileId + " 的材料档案。"
|
|
|
- + "请先用 list_file_profiles 确认该材料存在并取到正确的 fileId"
|
|
|
- + "(注意:表格类文件 xls/xlsx/csv 不在这张表里)");
|
|
|
- }
|
|
|
- return ToolResults.ok(Json.toStr(buildSamplePayload(profile, spec.maxLength)));
|
|
|
- } catch (Exception e) {
|
|
|
- return fail("读取材料正文样本", e);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
// ==================== 内部实现 ====================
|
|
|
|
|
|
/**
|
|
|
@@ -246,50 +202,6 @@ public class FileAnalysisTool {
|
|
|
return rows;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 单份材料的正文样本载荷。
|
|
|
- */
|
|
|
- private static Map<String, Object> buildSamplePayload(FileAiProfile profile, Number maxLength) {
|
|
|
- int limit = normalizeSampleLength(maxLength);
|
|
|
- String text = profile.getSampleText();
|
|
|
- boolean truncated = text != null && text.length() > limit;
|
|
|
-
|
|
|
- Map<String, Object> payload = new LinkedHashMap<>();
|
|
|
- payload.put("fileId", profile.getFileId());
|
|
|
- payload.put("fileName", profile.getFileName());
|
|
|
- payload.put("category", categoryLabel(profile.getCategory()));
|
|
|
- payload.put("summary", profile.getSummary());
|
|
|
- payload.put("keywords", profile.getKeywords());
|
|
|
- payload.put("mimeType", profile.getMimeType());
|
|
|
- payload.put("sampleLength", profile.getSampleLength());
|
|
|
- payload.put("sampleText", text == null ? null : (truncated ? text.substring(0, limit) : text));
|
|
|
- payload.put("truncated", truncated);
|
|
|
- payload.put("failReason", profile.getFailReason());
|
|
|
- if (text == null) {
|
|
|
- payload.put("note", "该材料没有可用的文本样本(扫描件、加密压缩包、纯图片等),"
|
|
|
- + "无法提供正文;原因见 failReason。");
|
|
|
- } else if (truncated) {
|
|
|
- payload.put("note", "样本已被截断:完整样本共 " + text.length() + " 字符,本次返回前 " + limit
|
|
|
- + " 字符(上限 " + SAMPLE_MAX_CHARS + ")。需要看后面的内容请说明,"
|
|
|
- + "或改从 list_file_profiles 的 summary / keywords 判断材料内容。");
|
|
|
- }
|
|
|
- return payload;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 样本长度归一化:null 取默认,非法取默认,超上限截断到上限。
|
|
|
- */
|
|
|
- private static int normalizeSampleLength(Number maxLength) {
|
|
|
- if (maxLength == null) {
|
|
|
- return SAMPLE_DEFAULT_CHARS;
|
|
|
- }
|
|
|
- int value = maxLength.intValue();
|
|
|
- if (value <= 0) {
|
|
|
- return SAMPLE_DEFAULT_CHARS;
|
|
|
- }
|
|
|
- return Math.min(value, SAMPLE_MAX_CHARS);
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 分类名 → 中文(落库存的是 {@code FileCategoryEnum} 的 name)。
|
|
|
*/
|