GeneralKnowledgeDialoguePolicy.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package com.fs.company.service.workflow.deepdialogue;
  2. import java.util.regex.Pattern;
  3. /**
  4. * General-knowledge questions unrelated to tenant products may be answered via web research.
  5. */
  6. public final class GeneralKnowledgeDialoguePolicy {
  7. private static final Pattern PRODUCT_INTENT = Pattern.compile(
  8. "价格|多少钱|多少钸|怎么买|怎么卖|套餐|产品|商品|服务|课程|订单|下单|购买|优惠|折扣|"
  9. + "你们的|咱们的|我们的|店里|公司的|咨询一下.*卖|怎么卖|怎么办|怎么开通");
  10. private static final Pattern GENERAL_KNOWLEDGE = Pattern.compile(
  11. "能否|能不能|可以吗|好吗|有用吗|有效吗|怎么样|是否|是不是|什么是|为什么|怎么回事|有什么好处|能治|能否治|对.*有用|对.*有好处");
  12. public static final String WEB_RESEARCH_MARKER = "网页摘要参考";
  13. /** Public health / general-knowledge answers (not tenant product KB). */
  14. public static final String GENERAL_KNOWLEDGE_MARKER = "【通识作答】";
  15. private GeneralKnowledgeDialoguePolicy() {
  16. }
  17. public static boolean isProductRelatedQuestion(String message) {
  18. return message != null && !message.isBlank() && PRODUCT_INTENT.matcher(message.trim()).find();
  19. }
  20. public static boolean isGeneralKnowledgeQuestion(String message) {
  21. if (message == null || message.isBlank()) {
  22. return false;
  23. }
  24. if (MedicalEfficacyDialoguePolicy.isMedicalEfficacyConsultation(message)) {
  25. return true;
  26. }
  27. if (isProductRelatedQuestion(message)) {
  28. return false;
  29. }
  30. return GENERAL_KNOWLEDGE.matcher(message.trim()).find();
  31. }
  32. /**
  33. * @return true when web snippets or public-knowledge context should supplement the answer
  34. */
  35. public static boolean shouldEnrichWithWebResearch(String message, String knowledgeContext) {
  36. if (message == null || message.isBlank()) {
  37. return false;
  38. }
  39. if (!isGeneralKnowledgeQuestion(message)) {
  40. return false;
  41. }
  42. if (knowledgeContext != null) {
  43. if (knowledgeContext.contains(WEB_RESEARCH_MARKER)
  44. || knowledgeContext.contains(GENERAL_KNOWLEDGE_MARKER)) {
  45. return false;
  46. }
  47. }
  48. // 通识/公域问题且 KB 未命中时,允许走网页摘要补充回答
  49. return true;
  50. }
  51. public static boolean allowsPublicKnowledgeAnswer(String knowledgeContext) {
  52. if (knowledgeContext == null || knowledgeContext.isBlank()) {
  53. return false;
  54. }
  55. return knowledgeContext.contains(WEB_RESEARCH_MARKER)
  56. || knowledgeContext.contains(GENERAL_KNOWLEDGE_MARKER);
  57. }
  58. }