| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package com.fs.company.service.workflow.deepdialogue;
- import java.util.regex.Pattern;
- /**
- * General-knowledge questions unrelated to tenant products may be answered via web research.
- */
- public final class GeneralKnowledgeDialoguePolicy {
- private static final Pattern PRODUCT_INTENT = Pattern.compile(
- "价格|多少钱|多少钸|怎么买|怎么卖|套餐|产品|商品|服务|课程|订单|下单|购买|优惠|折扣|"
- + "你们的|咱们的|我们的|店里|公司的|咨询一下.*卖|怎么卖|怎么办|怎么开通");
- private static final Pattern GENERAL_KNOWLEDGE = Pattern.compile(
- "能否|能不能|可以吗|好吗|有用吗|有效吗|怎么样|是否|是不是|什么是|为什么|怎么回事|有什么好处|能治|能否治|对.*有用|对.*有好处");
- public static final String WEB_RESEARCH_MARKER = "网页摘要参考";
- /** Public health / general-knowledge answers (not tenant product KB). */
- public static final String GENERAL_KNOWLEDGE_MARKER = "【通识作答】";
- private GeneralKnowledgeDialoguePolicy() {
- }
- public static boolean isProductRelatedQuestion(String message) {
- return message != null && !message.isBlank() && PRODUCT_INTENT.matcher(message.trim()).find();
- }
- public static boolean isGeneralKnowledgeQuestion(String message) {
- if (message == null || message.isBlank()) {
- return false;
- }
- if (MedicalEfficacyDialoguePolicy.isMedicalEfficacyConsultation(message)) {
- return true;
- }
- if (isProductRelatedQuestion(message)) {
- return false;
- }
- return GENERAL_KNOWLEDGE.matcher(message.trim()).find();
- }
- /**
- * @return true when web snippets or public-knowledge context should supplement the answer
- */
- public static boolean shouldEnrichWithWebResearch(String message, String knowledgeContext) {
- if (message == null || message.isBlank()) {
- return false;
- }
- if (!isGeneralKnowledgeQuestion(message)) {
- return false;
- }
- if (knowledgeContext != null) {
- if (knowledgeContext.contains(WEB_RESEARCH_MARKER)
- || knowledgeContext.contains(GENERAL_KNOWLEDGE_MARKER)) {
- return false;
- }
- }
- // 通识/公域问题且 KB 未命中时,允许走网页摘要补充回答
- return true;
- }
- public static boolean allowsPublicKnowledgeAnswer(String knowledgeContext) {
- if (knowledgeContext == null || knowledgeContext.isBlank()) {
- return false;
- }
- return knowledgeContext.contains(WEB_RESEARCH_MARKER)
- || knowledgeContext.contains(GENERAL_KNOWLEDGE_MARKER);
- }
- }
|