|
|
|
@ -11,11 +11,12 @@ import cc.hiver.mall.purchaseocr.vo.PurchaseOcrExample; |
|
|
|
import cc.hiver.mall.service.mybatis.*; |
|
|
|
import cn.hutool.core.date.StopWatch; |
|
|
|
import com.alibaba.dashscope.aigc.generation.Generation; |
|
|
|
import com.alibaba.dashscope.aigc.generation.GenerationParam; |
|
|
|
import com.alibaba.dashscope.aigc.generation.GenerationResult; |
|
|
|
import com.alibaba.dashscope.aigc.generation.models.QwenParam; |
|
|
|
import com.alibaba.dashscope.aigc.multimodalconversation.*; |
|
|
|
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation; |
|
|
|
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam; |
|
|
|
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult; |
|
|
|
import com.alibaba.dashscope.common.Message; |
|
|
|
import com.alibaba.dashscope.common.MessageManager; |
|
|
|
import com.alibaba.dashscope.common.MultiModalMessage; |
|
|
|
import com.alibaba.dashscope.common.Role; |
|
|
|
import com.alibaba.dashscope.exception.ApiException; |
|
|
|
@ -50,6 +51,61 @@ public class AliOcrUtil { |
|
|
|
private static final Pattern COMPILE = Pattern.compile("\\D+"); |
|
|
|
private static final Pattern SYYS = Pattern.compile("所有颜色", Pattern.LITERAL); |
|
|
|
private static final Pattern SYCM = Pattern.compile("所有尺码", Pattern.LITERAL); |
|
|
|
/** 图像识别:效果优先 */ |
|
|
|
private static final String MODEL_VISION = "qwen3.5-plus"; |
|
|
|
/** 语音/文本开单:速度优先(纯文本走 Generation,通常可压到 1s 左右) */ |
|
|
|
private static final String MODEL_TEXT = "qwen-flash"; |
|
|
|
|
|
|
|
private static Map<String, Object> mmText(String text) { |
|
|
|
final Map<String, Object> content = new HashMap<>(); |
|
|
|
content.put("text", text); |
|
|
|
return content; |
|
|
|
} |
|
|
|
|
|
|
|
private static Map<String, Object> mmImage(String image) { |
|
|
|
final Map<String, Object> content = new HashMap<>(); |
|
|
|
content.put("image", image); |
|
|
|
return content; |
|
|
|
} |
|
|
|
|
|
|
|
private static MultiModalMessage mmMessage(String role, List<Map<String, Object>> content) { |
|
|
|
return MultiModalMessage.builder().role(role).content(content).build(); |
|
|
|
} |
|
|
|
|
|
|
|
private static String extractMultiModalText(MultiModalConversationResult result) { |
|
|
|
final List<Map<String, Object>> content = result.getOutput().getChoices().get(0).getMessage().getContent(); |
|
|
|
if (content == null || content.isEmpty() || content.get(0).get("text") == null) { |
|
|
|
return ""; |
|
|
|
} |
|
|
|
return content.get(0).get("text").toString(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 文本开单走 Generation + 轻量模型,比 qwen3.5-plus 多模态快很多。 |
|
|
|
*/ |
|
|
|
private static String callTextFast(String systemPrompt, String userPrompt) |
|
|
|
throws ApiException, NoApiKeyException, InputRequiredException { |
|
|
|
final Generation gen = new Generation(); |
|
|
|
final List<Message> messages = new ArrayList<>(); |
|
|
|
messages.add(Message.builder().role(Role.SYSTEM.getValue()).content(systemPrompt).build()); |
|
|
|
messages.add(Message.builder().role(Role.USER.getValue()).content(userPrompt).build()); |
|
|
|
final GenerationParam param = GenerationParam.builder() |
|
|
|
.model(MODEL_TEXT) |
|
|
|
.messages(messages) |
|
|
|
.resultFormat(GenerationParam.ResultFormat.MESSAGE) |
|
|
|
.temperature(0.2F) |
|
|
|
.topP(0.8) |
|
|
|
.maxTokens(1024) |
|
|
|
.enableThinking(false) |
|
|
|
.build(); |
|
|
|
log.info("DashScope 文本调用开始, model={}", MODEL_TEXT); |
|
|
|
final long start = System.currentTimeMillis(); |
|
|
|
final GenerationResult result = gen.call(param); |
|
|
|
final String text = result.getOutput().getChoices().get(0).getMessage().getContent(); |
|
|
|
log.info("DashScope 文本调用结束, cost={}ms, textLength={}", System.currentTimeMillis() - start, |
|
|
|
text == null ? 0 : text.length()); |
|
|
|
return text == null ? "" : text; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* AI入库图像识别-多轮对话 |
|
|
|
@ -83,11 +139,9 @@ public class AliOcrUtil { |
|
|
|
Constants.apiKey = "sk-bcfa4865b89548acb8225f910f13d682"; |
|
|
|
final CopyOnWriteArrayList<MultiModalConversationResult> multiModalConversationResults = new CopyOnWriteArrayList<>(); |
|
|
|
final MultiModalConversation conv = new MultiModalConversation(); |
|
|
|
final MultiModalMessageItemText systemText = new MultiModalMessageItemText("You are a helpful assistant."); |
|
|
|
final MultiModalConversationMessage systemMessage = MultiModalConversationMessage.builder() |
|
|
|
.role(Role.SYSTEM.getValue()).content(Collections.singletonList(systemText)).build(); |
|
|
|
final MultiModalMessage systemMessage = mmMessage(Role.SYSTEM.getValue(), |
|
|
|
Collections.singletonList(mmText("You are a helpful assistant."))); |
|
|
|
|
|
|
|
final MultiModalMessageItemImage userImage = new MultiModalMessageItemImage(picturePath); |
|
|
|
final String firstQuestionMsg = "请把图片中的内容按照商品的不同规格拆分,返回" + count + "条JSON数据。\n" + |
|
|
|
" JSON示例:[{ \"productSn\": \"" + productSnExample + "\", \"productName\":\"" + productNameExample + "\", \"attributeList\": \"{'color':'" + purchaseOcrExample.getColor() + "','size':'" + purchaseOcrExample.getSize() + "'}\", \"productCount\": \"" + purchaseOcrExample.getProductCount() + "\", \"price\": \"" + purchaseOcrExample.getPrice() + "\" }]。" + |
|
|
|
"以下是几点要求: " + |
|
|
|
@ -99,23 +153,23 @@ public class AliOcrUtil { |
|
|
|
"6.请注意返回JSON符号解析格式正确 ,确保java程序能够正确解析。" + |
|
|
|
"7.严格返回" + count + "条JSON数据,如果数量有偏差按照实际条数返回,不能自动省略!"; |
|
|
|
log.info("AI提问内容:" + firstQuestionMsg); |
|
|
|
MultiModalMessageItemText userText = new MultiModalMessageItemText(firstQuestionMsg); |
|
|
|
final MultiModalConversationMessage userMessage = |
|
|
|
MultiModalConversationMessage.builder().role(Role.USER.getValue()) |
|
|
|
.content(Arrays.asList(userImage, userText)).build(); |
|
|
|
final CopyOnWriteArrayList<MultiModalConversationMessage> messages = new CopyOnWriteArrayList<>(); |
|
|
|
final MultiModalMessage userMessage = mmMessage(Role.USER.getValue(), |
|
|
|
Arrays.asList(mmImage(picturePath), mmText(firstQuestionMsg))); |
|
|
|
final CopyOnWriteArrayList<MultiModalMessage> messages = new CopyOnWriteArrayList<>(); |
|
|
|
messages.add(systemMessage); |
|
|
|
messages.add(userMessage); |
|
|
|
final MultiModalConversationParam param = MultiModalConversationParam.builder() |
|
|
|
.model("qwen-vl-max") |
|
|
|
.model(MODEL_VISION) |
|
|
|
.messages(messages) |
|
|
|
.enableThinking(false) |
|
|
|
.build(); |
|
|
|
stopWatch.start("一轮会话"); |
|
|
|
log.info("DashScope 图像识别开始, model={}, picture={}", MODEL_VISION, picturePath); |
|
|
|
MultiModalConversationResult result = conv.call(param); |
|
|
|
stopWatch.stop(); |
|
|
|
multiModalConversationResults.add(result); |
|
|
|
// 解析结果
|
|
|
|
String text = result.getOutput().getChoices().get(0).getMessage().getContent().get(0).get("text").toString(); |
|
|
|
String text = extractMultiModalText(result); |
|
|
|
log.info("一轮对话-的json======" + text); |
|
|
|
// 根据{}截取数据
|
|
|
|
final int startIndex = text.indexOf('{'); |
|
|
|
@ -170,21 +224,15 @@ public class AliOcrUtil { |
|
|
|
log.error(Thread.currentThread().getName() + "-一轮对话--->json识别报错喽,没关系,我来重新识别一下!"); |
|
|
|
//尝试重新识别一次
|
|
|
|
final String errorJsonQuestionMsg = "返回的json格式有误,请重新返回。"; |
|
|
|
final MultiModalMessageItemText assistentText = new MultiModalMessageItemText( |
|
|
|
text); |
|
|
|
final MultiModalConversationMessage assistentMessage = MultiModalConversationMessage.builder() |
|
|
|
.role(Role.ASSISTANT.getValue()).content(Collections.singletonList(assistentText)).build(); |
|
|
|
messages.add(assistentMessage); |
|
|
|
userText = new MultiModalMessageItemText(errorJsonQuestionMsg); |
|
|
|
messages.add(MultiModalConversationMessage.builder().role(Role.USER.getValue()) |
|
|
|
.content(Collections.singletonList(userText)).build()); |
|
|
|
param.setMessages(new CopyOnWriteArrayList<Object>(messages)); |
|
|
|
messages.add(mmMessage(Role.ASSISTANT.getValue(), Collections.singletonList(mmText(text)))); |
|
|
|
messages.add(mmMessage(Role.USER.getValue(), Collections.singletonList(mmText(errorJsonQuestionMsg)))); |
|
|
|
param.setMessages(new CopyOnWriteArrayList<>(messages)); |
|
|
|
stopWatch.start("一轮对话json格式错误重新识别!"); |
|
|
|
result = conv.call(param); |
|
|
|
multiModalConversationResults.add(result); |
|
|
|
stopWatch.stop(); |
|
|
|
// 解析结果
|
|
|
|
text = result.getOutput().getChoices().get(0).getMessage().getContent().get(0).get("text").toString(); |
|
|
|
text = extractMultiModalText(result); |
|
|
|
log.info("一轮对话--json格式错误,重新识别的结果======" + text); |
|
|
|
// 根据{}截取数据
|
|
|
|
final int secondStartIndex = text.indexOf('{'); |
|
|
|
@ -242,21 +290,15 @@ public class AliOcrUtil { |
|
|
|
final int missingCount = count - fristResultCount; |
|
|
|
if (missingCount > 0) { |
|
|
|
final String secondQuestionMsg = "缺少了" + missingCount + "条,请完整返回。"; |
|
|
|
final MultiModalMessageItemText assistentText = new MultiModalMessageItemText( |
|
|
|
text); |
|
|
|
final MultiModalConversationMessage assistentMessage = MultiModalConversationMessage.builder() |
|
|
|
.role(Role.ASSISTANT.getValue()).content(Collections.singletonList(assistentText)).build(); |
|
|
|
messages.add(assistentMessage); |
|
|
|
userText = new MultiModalMessageItemText(secondQuestionMsg); |
|
|
|
messages.add(MultiModalConversationMessage.builder().role(Role.USER.getValue()) |
|
|
|
.content(Collections.singletonList(userText)).build()); |
|
|
|
param.setMessages(new CopyOnWriteArrayList<Object>(messages)); |
|
|
|
messages.add(mmMessage(Role.ASSISTANT.getValue(), Collections.singletonList(mmText(text)))); |
|
|
|
messages.add(mmMessage(Role.USER.getValue(), Collections.singletonList(mmText(secondQuestionMsg)))); |
|
|
|
param.setMessages(new CopyOnWriteArrayList<>(messages)); |
|
|
|
stopWatch.start("二轮会话"); |
|
|
|
result = conv.call(param); |
|
|
|
multiModalConversationResults.add(result); |
|
|
|
stopWatch.stop(); |
|
|
|
// 解析结果
|
|
|
|
String secondText = result.getOutput().getChoices().get(0).getMessage().getContent().get(0).get("text").toString(); |
|
|
|
String secondText = extractMultiModalText(result); |
|
|
|
log.info("二轮对话识别结果:" + secondText); |
|
|
|
// 根据{}截取数据
|
|
|
|
final int secondStartIndex = secondText.indexOf('{'); |
|
|
|
@ -322,21 +364,15 @@ public class AliOcrUtil { |
|
|
|
log.error(Thread.currentThread().getName() + "-二轮对话--->json识别报错喽,没关系,我来重新识别一下!"); |
|
|
|
//尝试重新识别一次
|
|
|
|
final String errorJsonQuestionMsg = "返回的json格式有误,请重新返回。"; |
|
|
|
final MultiModalMessageItemText secondJsonErrorAssistentText = new MultiModalMessageItemText( |
|
|
|
secondText); |
|
|
|
final MultiModalConversationMessage secondJsonErrorAssistentMessage = MultiModalConversationMessage.builder() |
|
|
|
.role(Role.ASSISTANT.getValue()).content(Collections.singletonList(secondJsonErrorAssistentText)).build(); |
|
|
|
messages.add(secondJsonErrorAssistentMessage); |
|
|
|
userText = new MultiModalMessageItemText(errorJsonQuestionMsg); |
|
|
|
messages.add(MultiModalConversationMessage.builder().role(Role.USER.getValue()) |
|
|
|
.content(Collections.singletonList(userText)).build()); |
|
|
|
param.setMessages(new CopyOnWriteArrayList<Object>(messages)); |
|
|
|
messages.add(mmMessage(Role.ASSISTANT.getValue(), Collections.singletonList(mmText(secondText)))); |
|
|
|
messages.add(mmMessage(Role.USER.getValue(), Collections.singletonList(mmText(errorJsonQuestionMsg)))); |
|
|
|
param.setMessages(new CopyOnWriteArrayList<>(messages)); |
|
|
|
stopWatch.start("二轮会话json格式错误,重新识别!"); |
|
|
|
result = conv.call(param); |
|
|
|
multiModalConversationResults.add(result); |
|
|
|
stopWatch.stop(); |
|
|
|
// 解析结果
|
|
|
|
secondText = result.getOutput().getChoices().get(0).getMessage().getContent().get(0).get("text").toString(); |
|
|
|
secondText = extractMultiModalText(result); |
|
|
|
log.info("二轮对话-json格式错误,重新识别的结果======" + secondText); |
|
|
|
// 根据{}截取数据
|
|
|
|
final int errorSsecondStartIndex = secondText.indexOf('{'); |
|
|
|
@ -531,7 +567,7 @@ public class AliOcrUtil { |
|
|
|
} |
|
|
|
})).build(); |
|
|
|
final MultiModalConversationParam param = MultiModalConversationParam.builder() |
|
|
|
.model("qwen-vl-max") |
|
|
|
.model(MODEL_VISION) |
|
|
|
.message(userMessage) |
|
|
|
/*.stream(true) |
|
|
|
.temperature(1F) |
|
|
|
@ -543,6 +579,7 @@ public class AliOcrUtil { |
|
|
|
.seed(500)*/ |
|
|
|
// 是否使用增量输出。当使用增量输出时每次流式返回的序列仅包含最新生成的增量内容,默认值为false,即输出完整的全量内容
|
|
|
|
.incrementalOutput(true) |
|
|
|
.enableThinking(false) |
|
|
|
.build(); |
|
|
|
stopWatch.start("开始识别!"); |
|
|
|
final Flowable<MultiModalConversationResult> result = conv.streamCall(param); |
|
|
|
@ -833,10 +870,6 @@ public class AliOcrUtil { |
|
|
|
final JSONObject jsonObject = new JSONObject(); |
|
|
|
Constants.apiKey = "sk-bcfa4865b89548acb8225f910f13d682"; |
|
|
|
final StopWatch stopWatch = new StopWatch("Ai回答计时"); |
|
|
|
final Generation gen = new Generation(); |
|
|
|
final MessageManager msgManager = new MessageManager(10); |
|
|
|
final Message systemMsg = |
|
|
|
Message.builder().role(Role.SYSTEM.getValue()).content("You are a helpful assistant.").build(); |
|
|
|
//questionMsg += "请帮我把所有内容封装为JSON,json格式为:[{ \"productSn\": \"货号\", \"attributeList\": [{\"color\":\"颜色\",\"size\":\"尺码\",\"productCount\": \"数量\"}] }]。以下是几点要求: 1.“货号”两个字和颜色中间的内容代表productSn,productSn可能包含\"新\"、\"退\"、\"旧\"、\"换\"、\"补\"。 2.\"SYYS色\"代表“所有颜色”,\"color\"字段返回“SYYS色”。 3.\"SYCM码\"代表“所有尺码”,\"size\"字段返回“SYCM码”。 4.只输出JSON数据即可,不用返回字段描述和解析过程。";
|
|
|
|
questionMsg += "你是一个服装行业库管专家,请帮我把所有内容封装为JSON,json格式为:[{ \"productSn\": \"货号\", \"attributeList\": [{\"color\":\"颜色\",\"size\":\"尺码\",\"productCount\": \"数量\"}] , \"wholesalePrice\":\"价格\"}],\n" + |
|
|
|
"1.productSn的返回值中去掉颜色,尺码等信息,\n" + |
|
|
|
@ -848,22 +881,9 @@ public class AliOcrUtil { |
|
|
|
"7.如果没有识别到\"wholesalePrice\"的内容,则\"wholesalePrice\"赋值\"0\"。\n" + |
|
|
|
"8.只输出JSON数据即可,禁止返回除JSON数据外的任何内容!" + |
|
|
|
"9.返回的JSON数据不用换行,以最快的速度返回!"; |
|
|
|
final Message userMsg = Message.builder().role(Role.USER.getValue()).content(questionMsg).build(); |
|
|
|
msgManager.add(systemMsg); |
|
|
|
msgManager.add(userMsg); |
|
|
|
stopWatch.start("开始回答"); |
|
|
|
final QwenParam param = |
|
|
|
QwenParam.builder().model("qwen-turbo").messages(msgManager.get()) |
|
|
|
.resultFormat(QwenParam.ResultFormat.MESSAGE) |
|
|
|
.seed(1234) |
|
|
|
.temperature(0.7F) |
|
|
|
.topP(0.8) |
|
|
|
.repetitionPenalty(1.0F) |
|
|
|
.build(); |
|
|
|
final GenerationResult result = gen.call(param); |
|
|
|
log.info(result.toString()); |
|
|
|
// 解析结果
|
|
|
|
final String text = result.getOutput().getChoices().get(0).getMessage().getContent(); |
|
|
|
final String text = callTextFast("You are a helpful assistant.", questionMsg); |
|
|
|
log.info(text); |
|
|
|
// 根据{}截取数据
|
|
|
|
final int startIndex = text.indexOf('{'); |
|
|
|
final int endIndex = text.lastIndexOf('}'); |
|
|
|
@ -871,7 +891,7 @@ public class AliOcrUtil { |
|
|
|
stopWatch.stop(); |
|
|
|
log.info(stopWatch.prettyPrint()); |
|
|
|
jsonObject.put("resultContent", jsonStr); |
|
|
|
jsonObject.put("msg", result.getOutput().getChoices().get(0).getMessage().getContent()); |
|
|
|
jsonObject.put("msg", text); |
|
|
|
return jsonObject; |
|
|
|
} |
|
|
|
|
|
|
|
@ -887,10 +907,6 @@ public class AliOcrUtil { |
|
|
|
throws NoApiKeyException, ApiException, InputRequiredException { |
|
|
|
final JSONObject jsonObject = new JSONObject(); |
|
|
|
Constants.apiKey = "sk-bcfa4865b89548acb8225f910f13d682"; |
|
|
|
final Generation gen = new Generation(); |
|
|
|
final MessageManager msgManager = new MessageManager(10); |
|
|
|
final Message systemMsg = |
|
|
|
Message.builder().role(Role.SYSTEM.getValue()).content("You are a helpful assistant.").build(); |
|
|
|
// questionMsg += "请帮我把所有内容封装为JSON,json格式为:[{ \"productSn\": \"货号\", \"productName\": \"名称\" , \"price\":\"单价\",\"attributeList\": [{\"color\":\"颜色\",\"size\":\"尺码\",\"productCount\": \"数量\"}] }]。以下是几点要求: 1.“货号”两个字和\"名称\"两个字中间的内容代表productSn的值,如果没有名称则“货号”两个字和颜色中间的内容代表productSn的值,productSn可能包含\"新\"、\"退\"、\"旧\"、\"换\"、\"补\"。2.\"SYYS色\"代表“所有颜色”,\"color\"字段返回“SYYS色”。 3.\"SYCM码\"代表“所有尺码”,\"size\"字段返回“SYCM码”。4.如果没有名称,productName字段返回\"\"。5.只输出JSON数据即可,不用返回字段描述和解析过程。";
|
|
|
|
questionMsg += "你是一个服装行业库管专家,请帮我把所有内容封装为JSON,json格式为:[{ \"productSn\": \"货号\", \"productName\": \"名称\" , \"attributeList\": [{\"color\":\"颜色\",\"size\":\"尺码\",\"productCount\": \"数量\"}], \"purchasePrice\":\"单价\" , \"price\":\"零售价\" , \"wholesalePrice\":\"批发价\" }]\n" + |
|
|
|
"1.如果没有识别到\"purchasePrice\"的内容,则\"purchasePrice\"赋值\"0\",purchasePrice为单价!" + |
|
|
|
@ -903,27 +919,14 @@ public class AliOcrUtil { |
|
|
|
"8.如果指令中尺码是?码到?码,请完整返回跨度内所有尺码JSON、不要省略中间的尺码。(常见的尺码顺序:S码-M码-L码-XL码-2XL码-3XL码-4XL码-5XL码-6XL码-7XL码......)。\n" + |
|
|
|
"9.返回的JSON数据不用换行,以最快的速度返回!\n" + |
|
|
|
"10.只输出JSON数据即可,禁止返回除JSON数据外的任何内容!"; |
|
|
|
final Message userMsg = Message.builder().role(Role.USER.getValue()).content(questionMsg).build(); |
|
|
|
msgManager.add(systemMsg); |
|
|
|
msgManager.add(userMsg); |
|
|
|
final QwenParam param = |
|
|
|
QwenParam.builder().model("qwen-turbo").messages(msgManager.get()) |
|
|
|
.resultFormat(QwenParam.ResultFormat.MESSAGE) |
|
|
|
.seed(1234) |
|
|
|
.temperature(0.7F) |
|
|
|
.topP(0.8) |
|
|
|
.repetitionPenalty(1.0F) |
|
|
|
.build(); |
|
|
|
final GenerationResult result = gen.call(param); |
|
|
|
log.info(result.toString()); |
|
|
|
// 解析结果
|
|
|
|
final String text = result.getOutput().getChoices().get(0).getMessage().getContent(); |
|
|
|
final String text = callTextFast("You are a helpful assistant.", questionMsg); |
|
|
|
log.info(text); |
|
|
|
// 根据{}截取数据
|
|
|
|
final int startIndex = text.indexOf('{'); |
|
|
|
final int endIndex = text.lastIndexOf('}'); |
|
|
|
final String jsonStr = '[' + text.substring(startIndex, endIndex + 1) + ']'; |
|
|
|
jsonObject.put("resultContent", jsonStr); |
|
|
|
jsonObject.put("msg", result.getOutput().getChoices().get(0).getMessage().getContent()); |
|
|
|
jsonObject.put("msg", text); |
|
|
|
return jsonObject; |
|
|
|
} |
|
|
|
|
|
|
|
@ -932,10 +935,6 @@ public class AliOcrUtil { |
|
|
|
final JSONObject jsonObject = new JSONObject(); |
|
|
|
Constants.apiKey = "sk-bcfa4865b89548acb8225f910f13d682"; |
|
|
|
final StopWatch stopWatch = new StopWatch("Ai回答计时"); |
|
|
|
final Generation gen = new Generation(); |
|
|
|
final MessageManager msgManager = new MessageManager(10); |
|
|
|
final Message systemMsg = |
|
|
|
Message.builder().role(Role.SYSTEM.getValue()).content("You are a helpful assistant.").build(); |
|
|
|
// questionMsg += "请帮我把所有内容封装为JSON,json格式为:[{ \"productSn\": \"货号\", \"attributeList\": [{\"color\":\"颜色\",\"size\":\"尺码\",\"productCount\": \"数量\"}] }],只输出JSON数据即可,不用返回字段描述";
|
|
|
|
questionMsg += "你是一个服装行业库管专家,请帮我把所有内容封装为JSON,json格式为:[{ \"productSn\": \"货号\", \"attributeList\": [{\"color\":\"颜色\",\"size\":\"尺码\",\"productCount\": \"数量\"}] }],\n" + |
|
|
|
"1.productSn的返回值中去掉颜色,尺码等信息,\n" + |
|
|
|
@ -944,22 +943,9 @@ public class AliOcrUtil { |
|
|
|
"4.如果指令中有“各N件”,则每个尺码数量返回N。\n" + |
|
|
|
"5.如果指令中尺码是?码到?码,请完整返回跨度内所有尺码JSON、不要省略中间的尺码。(常见的尺码顺序:S码-M码-L码-XL码-2XL码-3XL码-4XL码-5XL码-6XL码-7XL码......)。\n" + |
|
|
|
"6.只输出JSON数据即可,禁止返回除JSON数据外的任何内容!"; |
|
|
|
final Message userMsg = Message.builder().role(Role.USER.getValue()).content(questionMsg).build(); |
|
|
|
msgManager.add(systemMsg); |
|
|
|
msgManager.add(userMsg); |
|
|
|
stopWatch.start("开始回答"); |
|
|
|
final QwenParam param = |
|
|
|
QwenParam.builder().model("qwen-turbo").messages(msgManager.get()) |
|
|
|
.resultFormat(QwenParam.ResultFormat.MESSAGE) |
|
|
|
.seed(1234) |
|
|
|
.temperature(0.85F) |
|
|
|
.topP(0.8) |
|
|
|
.repetitionPenalty(1.0F) |
|
|
|
.build(); |
|
|
|
final GenerationResult result = gen.call(param); |
|
|
|
log.info(result.toString()); |
|
|
|
// 解析结果
|
|
|
|
final String text = result.getOutput().getChoices().get(0).getMessage().getContent(); |
|
|
|
final String text = callTextFast("You are a helpful assistant.", questionMsg); |
|
|
|
log.info(text); |
|
|
|
// 根据{}截取数据
|
|
|
|
final int startIndex = text.indexOf('{'); |
|
|
|
final int endIndex = text.lastIndexOf('}'); |
|
|
|
@ -968,7 +954,7 @@ public class AliOcrUtil { |
|
|
|
stopWatch.stop(); |
|
|
|
log.info(stopWatch.prettyPrint()); |
|
|
|
jsonObject.put("resultContent", jsonStr); |
|
|
|
jsonObject.put("msg", result.getOutput().getChoices().get(0).getMessage().getContent()); |
|
|
|
jsonObject.put("msg", text); |
|
|
|
return jsonObject; |
|
|
|
} |
|
|
|
|
|
|
|
@ -1218,23 +1204,10 @@ public class AliOcrUtil { |
|
|
|
final JSONObject jsonObject = new JSONObject(); |
|
|
|
Constants.apiKey = "sk-bcfa4865b89548acb8225f910f13d682"; |
|
|
|
final StopWatch stopWatch = new StopWatch("Ai回答计时"); |
|
|
|
final Generation gen = new Generation(); |
|
|
|
final MessageManager msgManager = new MessageManager(10); |
|
|
|
final Message systemMsg = |
|
|
|
Message.builder().role(Role.SYSTEM.getValue()).content("You are a helpful assistant.").build(); |
|
|
|
questionMsg += "请帮我把所有内容封装为JSON,json格式为:[{ \"senderName\": \"发货人\", \"senderPhone\": \"发货人电话\" , \"receiverName\": \"收货人\", \"receiverPhone\": \"收货人电话\" , \"arrivalStation\":\"\"到达站,\"weight\":\"重量\",\"count\":\"件数\",\"methodOfSettlement\":\"结算方式\",\"tips\":\"小费\" }]。没有识别到的信息JSON字段返回\"\"。"; |
|
|
|
final Message userMsg = Message.builder().role(Role.USER.getValue()).content(questionMsg).build(); |
|
|
|
msgManager.add(systemMsg); |
|
|
|
msgManager.add(userMsg); |
|
|
|
stopWatch.start("开始回答"); |
|
|
|
final QwenParam param = |
|
|
|
QwenParam.builder().model("qwen-max").messages(msgManager.get()) |
|
|
|
.resultFormat(QwenParam.ResultFormat.MESSAGE) |
|
|
|
.build(); |
|
|
|
final GenerationResult result = gen.call(param); |
|
|
|
log.info(result.toString()); |
|
|
|
// 解析结果
|
|
|
|
final String text = result.getOutput().getChoices().get(0).getMessage().getContent(); |
|
|
|
final String text = callTextFast("You are a helpful assistant.", questionMsg); |
|
|
|
log.info(text); |
|
|
|
// 根据{}截取数据
|
|
|
|
final int startIndex = text.indexOf('{'); |
|
|
|
final int endIndex = text.lastIndexOf('}'); |
|
|
|
@ -1242,7 +1215,7 @@ public class AliOcrUtil { |
|
|
|
stopWatch.stop(); |
|
|
|
log.info(stopWatch.prettyPrint()); |
|
|
|
jsonObject.put("resultContent", jsonStr); |
|
|
|
jsonObject.put("msg", result.getOutput().getChoices().get(0).getMessage().getContent()); |
|
|
|
jsonObject.put("msg", text); |
|
|
|
return jsonObject; |
|
|
|
} |
|
|
|
|
|
|
|
@ -1258,10 +1231,6 @@ public class AliOcrUtil { |
|
|
|
final JSONObject jsonObject = new JSONObject(); |
|
|
|
Constants.apiKey = "sk-bcfa4865b89548acb8225f910f13d682"; |
|
|
|
final StopWatch stopWatch = new StopWatch("Ai回答计时"); |
|
|
|
final Generation gen = new Generation(); |
|
|
|
final MessageManager msgManager = new MessageManager(10); |
|
|
|
final Message systemMsg = |
|
|
|
Message.builder().role(Role.SYSTEM.getValue()).content("你是一个物流开票员(这句话是给你的角色设定,不要在json信息中返回)").build(); |
|
|
|
final List<LogisticsAddressBook> shipperLogisticsAddressBooks = logisticsAddressBooks.get("shipper"); |
|
|
|
final List<LogisticsAddressBook> receiverLogisticsAddressBooks = logisticsAddressBooks.get("receiver"); |
|
|
|
final StringBuilder endQuestionMsg = new StringBuilder(); |
|
|
|
@ -1295,18 +1264,9 @@ public class AliOcrUtil { |
|
|
|
.append("4.weight、count、tips、premium这几个字段只返回数字; ") |
|
|
|
.append("5.不要返回推理过程,只输出1条JSON数据即可,禁止返回除JSON数据外的任何内容!返回的JSON数据不用换行,以最快的速度返回!"); |
|
|
|
log.info("AI物流最终指令==="+endQuestionMsg); |
|
|
|
final Message userMsg = Message.builder().role(Role.USER.getValue()).content(endQuestionMsg.toString()).build(); |
|
|
|
msgManager.add(systemMsg); |
|
|
|
msgManager.add(userMsg); |
|
|
|
stopWatch.start("开始回答"); |
|
|
|
final QwenParam param = |
|
|
|
QwenParam.builder().model("qwen-max").messages(msgManager.get()) |
|
|
|
.resultFormat(QwenParam.ResultFormat.MESSAGE) |
|
|
|
.build(); |
|
|
|
final GenerationResult result = gen.call(param); |
|
|
|
log.info(result.toString()); |
|
|
|
// 解析结果
|
|
|
|
final String text = result.getOutput().getChoices().get(0).getMessage().getContent(); |
|
|
|
final String text = callTextFast("你是一个物流开票员(这句话是给你的角色设定,不要在json信息中返回)", endQuestionMsg.toString()); |
|
|
|
log.info(text); |
|
|
|
// 根据{}截取数据
|
|
|
|
final int startIndex = text.indexOf('{'); |
|
|
|
final int endIndex = text.lastIndexOf('}'); |
|
|
|
@ -1314,7 +1274,7 @@ public class AliOcrUtil { |
|
|
|
stopWatch.stop(); |
|
|
|
log.info(stopWatch.prettyPrint()); |
|
|
|
jsonObject.put("resultContent", jsonStr); |
|
|
|
jsonObject.put("msg", result.getOutput().getChoices().get(0).getMessage().getContent()); |
|
|
|
jsonObject.put("msg", text); |
|
|
|
return jsonObject; |
|
|
|
} |
|
|
|
} |