重新修改提示词和返回值
This commit is contained in:
@@ -111,11 +111,11 @@ public class WmsImageRecognitionController extends BaseController {
|
||||
if (imgUrl == null || imgUrl.trim().isEmpty()) {
|
||||
return R.fail("图片URL不能为空");
|
||||
}
|
||||
|
||||
|
||||
ImageRecognitionBo bo = new ImageRecognitionBo();
|
||||
bo.setImageUrl(imgUrl);
|
||||
bo.setRecognitionType("text");
|
||||
|
||||
|
||||
ImageRecognitionVo result = iImageRecognitionService.recognizeText(bo);
|
||||
return R.ok(result);
|
||||
}
|
||||
@@ -129,12 +129,12 @@ public class WmsImageRecognitionController extends BaseController {
|
||||
if (imgUrl == null || imgUrl.trim().isEmpty()) {
|
||||
return R.fail("图片URL不能为空");
|
||||
}
|
||||
|
||||
|
||||
ImageRecognitionBo bo = new ImageRecognitionBo();
|
||||
bo.setImageUrl(imgUrl);
|
||||
bo.setRecognitionType("bom");
|
||||
|
||||
|
||||
ImageRecognitionVo result = iImageRecognitionService.recognizeBom(bo);
|
||||
return R.ok(result);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,6 +49,11 @@ public class ImageRecognitionVo implements Serializable {
|
||||
*/
|
||||
private List<BomItemVo> bomItems;
|
||||
|
||||
/**
|
||||
* 识别结果属性列表
|
||||
*/
|
||||
private List<AttributeVo> attributes;
|
||||
|
||||
/**
|
||||
* 识别置信度
|
||||
*/
|
||||
@@ -110,4 +115,20 @@ public class ImageRecognitionVo implements Serializable {
|
||||
*/
|
||||
private String remark;
|
||||
}
|
||||
|
||||
/**
|
||||
* 属性信息
|
||||
*/
|
||||
@Data
|
||||
public static class AttributeVo {
|
||||
/**
|
||||
* 属性名称
|
||||
*/
|
||||
private String attrKey;
|
||||
|
||||
/**
|
||||
* 属性值
|
||||
*/
|
||||
private String attrValue;
|
||||
}
|
||||
}
|
||||
@@ -114,7 +114,7 @@ public class ImageRecognitionServiceImpl implements IImageRecognitionService {
|
||||
result.setRecognitionType("bom");
|
||||
result.setRecognizedText(aiResponse);
|
||||
|
||||
// 解析BOM信息
|
||||
// 解析识别结果
|
||||
try {
|
||||
Map<String, Object> structuredResult = parseBomResponse(aiResponse);
|
||||
result.setStructuredResult(structuredResult);
|
||||
@@ -123,8 +123,12 @@ public class ImageRecognitionServiceImpl implements IImageRecognitionService {
|
||||
List<ImageRecognitionVo.BomItemVo> bomItems = extractBomItems(structuredResult);
|
||||
result.setBomItems(bomItems);
|
||||
|
||||
// 提取属性列表
|
||||
List<ImageRecognitionVo.AttributeVo> attributes = extractAttributes(structuredResult);
|
||||
result.setAttributes(attributes);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.warn("解析BOM响应失败: {}", e.getMessage());
|
||||
log.warn("解析识别响应失败: {}", e.getMessage());
|
||||
result.setRecognizedText(aiResponse);
|
||||
}
|
||||
|
||||
@@ -341,13 +345,13 @@ public class ImageRecognitionServiceImpl implements IImageRecognitionService {
|
||||
*/
|
||||
private String buildBomPrompt(ImageRecognitionBo bo) {
|
||||
StringBuilder prompt = new StringBuilder();
|
||||
prompt.append("请仔细分析这张图片中的BOM(物料清单)信息,并提取以下内容:\n\n");
|
||||
prompt.append("请仔细分析这张图片中的内容,并提取所有相关信息:\n\n");
|
||||
prompt.append("【识别要求】\n");
|
||||
prompt.append("1. 识别图片中的所有物料信息,包括原材料ID、名称、数量、单位等\n");
|
||||
prompt.append("2. 如果图片中包含表格,请按表格结构提取信息\n");
|
||||
prompt.append("3. 如果图片中包含列表,请按列表格式提取信息\n");
|
||||
prompt.append("4. 确保数量信息的准确性,包括数字和单位\n");
|
||||
prompt.append("5. 识别规格、备注等附加信息\n\n");
|
||||
prompt.append("1. 如果图片包含BOM(物料清单)信息,请提取原材料ID、名称、数量、单位等\n");
|
||||
prompt.append("2. 如果图片包含其他类型的信息(如检验报告、产品信息等),请提取所有相关属性\n");
|
||||
prompt.append("3. 识别图片中的所有文字信息,包括标题、内容、表格数据等\n");
|
||||
prompt.append("4. 提取关键信息点,如产品名称、规格参数、检验结果等\n");
|
||||
prompt.append("5. 确保信息的准确性和完整性\n\n");
|
||||
|
||||
if (bo.getProductId() != null) {
|
||||
prompt.append("【产品信息】\n");
|
||||
@@ -372,10 +376,16 @@ public class ImageRecognitionServiceImpl implements IImageRecognitionService {
|
||||
prompt.append(" \"remark\": \"备注\"\n");
|
||||
prompt.append(" }\n");
|
||||
prompt.append(" ],\n");
|
||||
prompt.append(" \"summary\": \"BOM清单总结\",\n");
|
||||
prompt.append(" \"attributes\": [\n");
|
||||
prompt.append(" {\n");
|
||||
prompt.append(" \"attrKey\": \"属性名称\",\n");
|
||||
prompt.append(" \"attrValue\": \"属性值\"\n");
|
||||
prompt.append(" }\n");
|
||||
prompt.append(" ],\n");
|
||||
prompt.append(" \"summary\": \"内容总结\",\n");
|
||||
prompt.append(" \"totalItems\": 总项目数\n");
|
||||
prompt.append("}\n\n");
|
||||
prompt.append("如果图片中没有BOM信息,请返回空数组。");
|
||||
prompt.append("请将识别到的所有信息整理成属性数组,每个属性包含attrKey(属性名称)和attrValue(属性值)。");
|
||||
|
||||
return prompt.toString();
|
||||
}
|
||||
@@ -487,4 +497,73 @@ public class ImageRecognitionServiceImpl implements IImageRecognitionService {
|
||||
|
||||
return bomItems;
|
||||
}
|
||||
|
||||
/**
|
||||
* 提取属性列表
|
||||
*/
|
||||
private List<ImageRecognitionVo.AttributeVo> extractAttributes(Map<String, Object> structuredResult) {
|
||||
List<ImageRecognitionVo.AttributeVo> attributes = new ArrayList<>();
|
||||
|
||||
try {
|
||||
List<Map<String, Object>> attrList = (List<Map<String, Object>>) structuredResult.get("attributes");
|
||||
if (attrList != null) {
|
||||
for (Map<String, Object> attr : attrList) {
|
||||
ImageRecognitionVo.AttributeVo attribute = new ImageRecognitionVo.AttributeVo();
|
||||
attribute.setAttrKey((String) attr.get("attrKey"));
|
||||
attribute.setAttrValue((String) attr.get("attrValue"));
|
||||
attributes.add(attribute);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有attributes字段,尝试从其他字段生成属性
|
||||
if (attributes.isEmpty()) {
|
||||
attributes = generateAttributesFromResult(structuredResult);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("提取属性失败: {}", e.getMessage());
|
||||
}
|
||||
|
||||
return attributes;
|
||||
}
|
||||
|
||||
/**
|
||||
* 从识别结果生成属性列表
|
||||
*/
|
||||
private List<ImageRecognitionVo.AttributeVo> generateAttributesFromResult(Map<String, Object> structuredResult) {
|
||||
List<ImageRecognitionVo.AttributeVo> attributes = new ArrayList<>();
|
||||
|
||||
try {
|
||||
// 提取summary作为内容总结
|
||||
String summary = (String) structuredResult.get("summary");
|
||||
if (summary != null && !summary.isEmpty()) {
|
||||
ImageRecognitionVo.AttributeVo summaryAttr = new ImageRecognitionVo.AttributeVo();
|
||||
summaryAttr.setAttrKey("内容总结");
|
||||
summaryAttr.setAttrValue(summary);
|
||||
attributes.add(summaryAttr);
|
||||
}
|
||||
|
||||
// 提取totalItems
|
||||
Object totalItems = structuredResult.get("totalItems");
|
||||
if (totalItems != null) {
|
||||
ImageRecognitionVo.AttributeVo totalAttr = new ImageRecognitionVo.AttributeVo();
|
||||
totalAttr.setAttrKey("总项目数");
|
||||
totalAttr.setAttrValue(String.valueOf(totalItems));
|
||||
attributes.add(totalAttr);
|
||||
}
|
||||
|
||||
// 提取bomItems数量
|
||||
List<Map<String, Object>> bomItems = (List<Map<String, Object>>) structuredResult.get("bomItems");
|
||||
if (bomItems != null && !bomItems.isEmpty()) {
|
||||
ImageRecognitionVo.AttributeVo bomCountAttr = new ImageRecognitionVo.AttributeVo();
|
||||
bomCountAttr.setAttrKey("BOM项目数");
|
||||
bomCountAttr.setAttrValue(String.valueOf(bomItems.size()));
|
||||
attributes.add(bomCountAttr);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.warn("生成属性失败: {}", e.getMessage());
|
||||
}
|
||||
|
||||
return attributes;
|
||||
}
|
||||
}
|
||||
@@ -104,6 +104,13 @@ public class ImageRecognitionTest implements CommandLineRunner {
|
||||
item.getSpecification());
|
||||
}
|
||||
}
|
||||
|
||||
if (result.getAttributes() != null) {
|
||||
log.info("属性数量: {}", result.getAttributes().size());
|
||||
for (ImageRecognitionVo.AttributeVo attr : result.getAttributes()) {
|
||||
log.info("属性: {} = {}", attr.getAttrKey(), attr.getAttrValue());
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("BOM识别测试异常", e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user