提交生成rasa的yml文件的代码
parent
77ba20f827
commit
8edb5cab16
@ -0,0 +1,33 @@
|
|||||||
|
version: "3.1"
|
||||||
|
|
||||||
|
intents:
|
||||||
|
- self_introduction
|
||||||
|
- goodbye
|
||||||
|
- greet
|
||||||
|
- ask_bushufu
|
||||||
|
|
||||||
|
responses:
|
||||||
|
utter_self_introduction:
|
||||||
|
- text: "再见"
|
||||||
|
utter_goodbye:
|
||||||
|
- text: "你好医生"
|
||||||
|
utter_greet:
|
||||||
|
- text: "你好"
|
||||||
|
utter_ask_bushufu:
|
||||||
|
- text: "我最近感觉心跳特别快,喘不上气。"
|
||||||
|
utter_tool_tool_shizhen:
|
||||||
|
- text: "---tool---1"
|
||||||
|
utter_tool_tool_huxi:
|
||||||
|
- text: "---tool---10"
|
||||||
|
|
||||||
|
actions:
|
||||||
|
- utter_self_introduction
|
||||||
|
- utter_goodbye
|
||||||
|
- utter_greet
|
||||||
|
- utter_ask_bushufu
|
||||||
|
- utter_tool_tool_shizhen
|
||||||
|
- utter_tool_tool_huxi
|
||||||
|
|
||||||
|
session_config:
|
||||||
|
session_expiration_time: 60
|
||||||
|
carry_over_slots_to_new_session: true
|
@ -0,0 +1,28 @@
|
|||||||
|
version: "3.1"
|
||||||
|
|
||||||
|
nlu:
|
||||||
|
- intent: greet
|
||||||
|
examples: |
|
||||||
|
- 你好
|
||||||
|
- 你好啊
|
||||||
|
- 你好你好
|
||||||
|
- intent: goodbye
|
||||||
|
examples: |
|
||||||
|
- 再见
|
||||||
|
- 拜拜
|
||||||
|
- intent: self_introduction
|
||||||
|
examples: |
|
||||||
|
- 我是张医生
|
||||||
|
- intent: ask_bushufu
|
||||||
|
examples: |
|
||||||
|
- 今天您有什么不舒服?哪里不舒服?
|
||||||
|
- intent: tool_tool_shizhen
|
||||||
|
examples: |
|
||||||
|
- 1
|
||||||
|
- 22
|
||||||
|
- 333
|
||||||
|
- intent: tool_tool_huxi
|
||||||
|
examples: |
|
||||||
|
- 1
|
||||||
|
- 22
|
||||||
|
- 333
|
@ -0,0 +1,28 @@
|
|||||||
|
version: "3.1"
|
||||||
|
|
||||||
|
rules:
|
||||||
|
|
||||||
|
- rule: 自我介绍
|
||||||
|
steps:
|
||||||
|
- intent: self_introduction
|
||||||
|
- action: utter_self_introduction
|
||||||
|
- rule: 再见
|
||||||
|
steps:
|
||||||
|
- intent: goodbye
|
||||||
|
- action: utter_goodbye
|
||||||
|
- rule: 问候
|
||||||
|
steps:
|
||||||
|
- intent: greet
|
||||||
|
- action: utter_greet
|
||||||
|
- rule: 问不舒服
|
||||||
|
steps:
|
||||||
|
- intent: ask_bushufu
|
||||||
|
- action: utter_ask_bushufu
|
||||||
|
- rule: 视诊
|
||||||
|
steps:
|
||||||
|
- intent: tool_tool_shizhen
|
||||||
|
- action: utter_tool_tool_shizhen
|
||||||
|
- rule: 呼吸
|
||||||
|
steps:
|
||||||
|
- intent: tool_tool_huxi
|
||||||
|
- action: utter_tool_tool_huxi
|
@ -0,0 +1,27 @@
|
|||||||
|
package com.supervision.handler;
|
||||||
|
|
||||||
|
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class StringListTypeHandler extends JacksonTypeHandler {
|
||||||
|
|
||||||
|
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||||
|
|
||||||
|
|
||||||
|
public StringListTypeHandler(Class<Object> type) {
|
||||||
|
super(type);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Object parse(String json) {
|
||||||
|
try {
|
||||||
|
return objectMapper.readValue(json, new TypeReference<List<String>>() {});
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.supervision.controller;
|
||||||
|
|
||||||
|
import com.supervision.service.RasaService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("rasa")
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class RasaController {
|
||||||
|
|
||||||
|
private final RasaService rasaService;
|
||||||
|
|
||||||
|
@GetMapping("generateRasaYml")
|
||||||
|
public void generateRasaYml(String diseaseId){
|
||||||
|
rasaService.generateRasaYml(diseaseId);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -1,15 +1,28 @@
|
|||||||
package com.supervision.controller;
|
package com.supervision.controller;
|
||||||
|
|
||||||
|
import com.supervision.model.ConfigPhysicalTool;
|
||||||
|
import com.supervision.service.ConfigPhysicalToolService;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
@RequestMapping("test")
|
@RequestMapping("test")
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class TestController {
|
public class TestController {
|
||||||
|
|
||||||
|
private final ConfigPhysicalToolService configPhysicalToolService;
|
||||||
|
|
||||||
@GetMapping("testExpireTime")
|
@GetMapping("testExpireTime")
|
||||||
public String testExpireTime() {
|
public String testExpireTime() {
|
||||||
return "OK";
|
return "OK";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("testQueryJSON")
|
||||||
|
public List<ConfigPhysicalTool> testQueryJSON(){
|
||||||
|
return configPhysicalToolService.list();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,13 @@
|
|||||||
|
package com.supervision.pojo.rasa;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class RasaReqDTO {
|
||||||
|
|
||||||
|
private String sender;
|
||||||
|
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.supervision.pojo.rasa;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class RasaResDTO {
|
||||||
|
|
||||||
|
private String recipient_id;
|
||||||
|
|
||||||
|
private String text;
|
||||||
|
}
|
@ -0,0 +1,28 @@
|
|||||||
|
package com.supervision.pojo.rasa.train.intent;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class DomainYmlTemplate {
|
||||||
|
|
||||||
|
private List<String> intents;
|
||||||
|
|
||||||
|
private LinkedHashMap<String,List<String>> responses;
|
||||||
|
|
||||||
|
private List<String> actions;
|
||||||
|
|
||||||
|
private SessionConfig session_config = new SessionConfig();
|
||||||
|
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class SessionConfig{
|
||||||
|
|
||||||
|
private final int session_expiration_time = 60;
|
||||||
|
|
||||||
|
private final Boolean carry_over_slots_to_new_session = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,17 @@
|
|||||||
|
package com.supervision.pojo.rasa.train.intent;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public class Intent {
|
||||||
|
|
||||||
|
private String id;
|
||||||
|
|
||||||
|
private String code;
|
||||||
|
|
||||||
|
private String desc;
|
||||||
|
}
|
@ -0,0 +1,11 @@
|
|||||||
|
package com.supervision.pojo.rasa.train.intent;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class NluTemplate {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
package com.supervision.pojo.rasa.train.intent;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.yaml.snakeyaml.nodes.Tag;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class NluYmlTemplate {
|
||||||
|
|
||||||
|
private List<Nlu> nlu;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class Nlu{
|
||||||
|
private String intent;
|
||||||
|
|
||||||
|
private List<String> examples;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
package com.supervision.pojo.rasa.train.intent;
|
||||||
|
|
||||||
|
import cn.hutool.core.map.MapUtil;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public class RuleYmlTemplate {
|
||||||
|
|
||||||
|
private List<Rule> rules;
|
||||||
|
|
||||||
|
|
||||||
|
@Data
|
||||||
|
public static class Rule {
|
||||||
|
private String rule;
|
||||||
|
|
||||||
|
private List<Step> steps;
|
||||||
|
|
||||||
|
public Rule() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Rule(String rule, String intent, String action) {
|
||||||
|
this.rule = rule;
|
||||||
|
steps = new ArrayList<>();
|
||||||
|
Step step = new Step(intent, action);
|
||||||
|
steps.add(step);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@AllArgsConstructor
|
||||||
|
@NoArgsConstructor
|
||||||
|
public static class Step {
|
||||||
|
private String intent;
|
||||||
|
|
||||||
|
private String action;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
package com.supervision.service;
|
||||||
|
|
||||||
|
public interface RasaService {
|
||||||
|
|
||||||
|
void generateRasaYml(String diseaseId);
|
||||||
|
}
|
@ -0,0 +1,228 @@
|
|||||||
|
package com.supervision.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.collection.CollUtil;
|
||||||
|
import cn.hutool.core.map.MapUtil;
|
||||||
|
import cn.hutool.core.util.StrUtil;
|
||||||
|
import cn.hutool.json.JSONUtil;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
|
||||||
|
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
|
||||||
|
import com.supervision.model.*;
|
||||||
|
import com.supervision.pojo.rasa.train.intent.*;
|
||||||
|
import com.supervision.service.*;
|
||||||
|
import freemarker.template.Configuration;
|
||||||
|
import freemarker.template.Template;
|
||||||
|
import freemarker.template.TemplateException;
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@Slf4j
|
||||||
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
|
public class RasaServiceImpl implements RasaService {
|
||||||
|
|
||||||
|
private final AskIntentService askIntentService;
|
||||||
|
|
||||||
|
private final AskQuestionService askQuestionService;
|
||||||
|
|
||||||
|
private final AskDefaultIntentService askDefaultIntentService;
|
||||||
|
|
||||||
|
private final AskDefaultQuestionService askDefaultQuestionService;
|
||||||
|
|
||||||
|
private final AskDefaultAnswerService askDefaultAnswerService;
|
||||||
|
|
||||||
|
private final AskAnswerService askAnswerService;
|
||||||
|
|
||||||
|
private final ConfigPhysicalToolService configPhysicalToolService;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void generateRasaYml(String diseaseId) {
|
||||||
|
Map<String, Intent> defaultIntentCodeAndIdMap = new HashMap<>();
|
||||||
|
Map<String, Intent> questionCodeAndIdMap = new HashMap<>();
|
||||||
|
Map<String, ConfigPhysicalTool> toolCodeIdMap = new HashMap<>();
|
||||||
|
List<RuleYmlTemplate.Rule> ruleList = new ArrayList<>();
|
||||||
|
try {
|
||||||
|
generateNlu(diseaseId, defaultIntentCodeAndIdMap, questionCodeAndIdMap, toolCodeIdMap);
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
generateDomain(defaultIntentCodeAndIdMap, questionCodeAndIdMap, toolCodeIdMap, ruleList);
|
||||||
|
generateRule(ruleList);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void generateNlu(String diseaseId, Map<String, Intent> defaultIntentCodeAndIdMap,
|
||||||
|
Map<String, Intent> intentCodeAndIdMap,
|
||||||
|
Map<String, ConfigPhysicalTool> toolCodeIdMap) throws IOException, TemplateException {
|
||||||
|
// 首先生成根据意图查找到nlu文件
|
||||||
|
List<NluYmlTemplate.Nlu> nluList = new ArrayList<>();
|
||||||
|
// 默认意图
|
||||||
|
List<AskDefaultIntent> defaultIntentList = askDefaultIntentService.list();
|
||||||
|
// 根据默认意图找到所有的问题
|
||||||
|
if (CollUtil.isNotEmpty(defaultIntentList)) {
|
||||||
|
Set<String> defaultIntentIdSet = defaultIntentList.stream().map(AskDefaultIntent::getId).collect(Collectors.toSet());
|
||||||
|
// 去默认问题表找问题
|
||||||
|
List<AskDefaultQuestion> questionList = askDefaultQuestionService.lambdaQuery().in(AskDefaultQuestion::getDefaultIntentId, defaultIntentIdSet).list();
|
||||||
|
Map<String, List<AskDefaultQuestion>> defaultQuestionByDefaultIntentId = questionList.stream().collect(Collectors.groupingBy(AskDefaultQuestion::getDefaultIntentId));
|
||||||
|
// 生成nlu的节点
|
||||||
|
for (AskDefaultIntent askDefaultIntent : defaultIntentList) {
|
||||||
|
List<AskDefaultQuestion> questions = defaultQuestionByDefaultIntentId.get(askDefaultIntent.getId());
|
||||||
|
if (CollUtil.isNotEmpty(questions)) {
|
||||||
|
// 开始生成
|
||||||
|
NluYmlTemplate.Nlu nlu = new NluYmlTemplate.Nlu();
|
||||||
|
nlu.setIntent(askDefaultIntent.getCode());
|
||||||
|
// 注意,这里的格式应该是 "- 你好\n- 你好啊\n- 你好你好" 这种的格式,所以我们要拼接
|
||||||
|
nlu.setExamples(questions.stream().map(AskDefaultQuestion::getQuestion).collect(Collectors.toList()));
|
||||||
|
nluList.add(nlu);
|
||||||
|
// 添加到map中,key为意图编码,value为意图ID
|
||||||
|
defaultIntentCodeAndIdMap.put(askDefaultIntent.getCode(), new Intent(askDefaultIntent.getId(), askDefaultIntent.getCode(), askDefaultIntent.getDescription()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 然后处理该疾病对应的意图
|
||||||
|
List<AskIntent> askIntentList = askIntentService.lambdaQuery().eq(AskIntent::getDiseaseId, diseaseId).list();
|
||||||
|
// 根据默认意图找到所有的问题
|
||||||
|
if (CollUtil.isNotEmpty(askIntentList)) {
|
||||||
|
Set<String> askIntentListSet = askIntentList.stream().map(AskIntent::getId).collect(Collectors.toSet());
|
||||||
|
// 去默认问题表找问题
|
||||||
|
List<AskQuestion> questionList = askQuestionService.lambdaQuery().in(AskQuestion::getIntentId, askIntentListSet).list();
|
||||||
|
Map<String, List<AskQuestion>> questionByDefaultIntentId = questionList.stream().collect(Collectors.groupingBy(AskQuestion::getIntentId));
|
||||||
|
// 生成nlu的节点
|
||||||
|
for (AskIntent askIntent : askIntentList) {
|
||||||
|
List<AskQuestion> questions = questionByDefaultIntentId.get(askIntent.getId());
|
||||||
|
if (CollUtil.isNotEmpty(questions)) {
|
||||||
|
// 开始生成
|
||||||
|
NluYmlTemplate.Nlu nlu = new NluYmlTemplate.Nlu();
|
||||||
|
nlu.setIntent(askIntent.getCode());
|
||||||
|
nlu.setExamples(questions.stream().map(AskQuestion::getQuestion).collect(Collectors.toList()));
|
||||||
|
nluList.add(nlu);
|
||||||
|
// 添加到map中,key为意图编码,value为意图ID
|
||||||
|
intentCodeAndIdMap.put(askIntent.getCode(), new Intent(askIntent.getId(), askIntent.getCode(), askIntent.getDescription()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 这里处理呼出的问题(code和问题不能为空)
|
||||||
|
List<ConfigPhysicalTool> physicalToolList = configPhysicalToolService.lambdaQuery()
|
||||||
|
.isNotNull(ConfigPhysicalTool::getCode)
|
||||||
|
.isNotNull(ConfigPhysicalTool::getCallOutQuestion).list();
|
||||||
|
|
||||||
|
for (ConfigPhysicalTool tool : physicalToolList) {
|
||||||
|
// 把呼出的问题全部加进去
|
||||||
|
NluYmlTemplate.Nlu nlu = new NluYmlTemplate.Nlu();
|
||||||
|
String toolIntent = "tool_" + tool.getCode();
|
||||||
|
nlu.setIntent(toolIntent);
|
||||||
|
nlu.setExamples(tool.getCallOutQuestion());
|
||||||
|
nluList.add(nlu);
|
||||||
|
// 生成tool的map,key是code,value是工具对应的ID
|
||||||
|
toolCodeIdMap.put(toolIntent, tool);
|
||||||
|
}
|
||||||
|
// 生成后生成yml对象
|
||||||
|
|
||||||
|
// createYmlFile(nluYml, "nlu.yml");
|
||||||
|
// 加载模板配置
|
||||||
|
NluYmlTemplate nluYmlTemplate = new NluYmlTemplate();
|
||||||
|
nluYmlTemplate.setNlu(nluList);
|
||||||
|
createYmlFile(NluYmlTemplate.class, "nlu.ftl", nluYmlTemplate, "nlu.yml");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void generateDomain(Map<String, Intent> defaultQuestionCodeAndIdMap,
|
||||||
|
Map<String, Intent> questionCodeAndIdMap, Map<String, ConfigPhysicalTool> toolCodeIdMap,
|
||||||
|
List<RuleYmlTemplate.Rule> ruleList) {
|
||||||
|
LinkedHashMap<String, List<String>> responses = new LinkedHashMap<>();
|
||||||
|
// 首先根据默认意图找到所有的意图ID
|
||||||
|
Collection<Intent> defaultIntentIdColl = defaultQuestionCodeAndIdMap.values();
|
||||||
|
// 找到默认意图对应的回复
|
||||||
|
if (CollUtil.isNotEmpty(defaultIntentIdColl)) {
|
||||||
|
Set<String> defaultIntentIdSet = defaultIntentIdColl.stream().map(Intent::getId).collect(Collectors.toSet());
|
||||||
|
List<AskDefaultAnswer> defaultAnswerList = askDefaultAnswerService.lambdaQuery().in(AskDefaultAnswer::getDefaultIntentId, defaultIntentIdSet).list();
|
||||||
|
Map<String, String> answerMap = defaultAnswerList.stream().collect(Collectors.toMap(AskDefaultAnswer::getId, AskDefaultAnswer::getAnswer, (k1, k2) -> k1));
|
||||||
|
for (Map.Entry<String, Intent> entry : defaultQuestionCodeAndIdMap.entrySet()) {
|
||||||
|
String defaultIntentCode = entry.getKey();
|
||||||
|
Intent defaultIntent = entry.getValue();
|
||||||
|
String answer = answerMap.get(defaultIntent.getId());
|
||||||
|
String utter = "utter_" + defaultIntentCode;
|
||||||
|
responses.put(utter, CollUtil.newArrayList(answer));
|
||||||
|
ruleList.add(new RuleYmlTemplate.Rule(defaultIntent.getDesc(), defaultIntent.getCode(), utter));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 然后疾病意图对应的回复
|
||||||
|
Collection<Intent> intentIdColl = questionCodeAndIdMap.values();
|
||||||
|
if (CollUtil.isNotEmpty(intentIdColl)) {
|
||||||
|
Set<String> intentIdSet = intentIdColl.stream().map(Intent::getId).collect(Collectors.toSet());
|
||||||
|
List<AskAnswer> answerList = askAnswerService.lambdaQuery().in(AskAnswer::getIntentId, intentIdSet).list();
|
||||||
|
Map<String, String> answerMap = answerList.stream().collect(Collectors.toMap(AskAnswer::getId, AskAnswer::getAnswer, (k1, k2) -> k1));
|
||||||
|
for (Map.Entry<String, Intent> entry : questionCodeAndIdMap.entrySet()) {
|
||||||
|
String intentCode = entry.getKey();
|
||||||
|
Intent intent = entry.getValue();
|
||||||
|
String answer = answerMap.get(intent.getId());
|
||||||
|
String utter = "utter_" + intentCode;
|
||||||
|
responses.put(utter, CollUtil.newArrayList(answer));
|
||||||
|
ruleList.add(new RuleYmlTemplate.Rule(intent.getDesc(), intent.getCode(), utter));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// 生成呼出tool对应的回复
|
||||||
|
for (Map.Entry<String, ConfigPhysicalTool> entry : toolCodeIdMap.entrySet()) {
|
||||||
|
String intentCode = entry.getKey();
|
||||||
|
ConfigPhysicalTool tool = entry.getValue();
|
||||||
|
String utter = "utter_" + intentCode;
|
||||||
|
String answer = "---tool---" + tool.getId() ;
|
||||||
|
responses.put(utter, CollUtil.newArrayList(JSONUtil.toJsonStr(answer)));
|
||||||
|
ruleList.add(new RuleYmlTemplate.Rule(tool.getToolName(), intentCode, utter));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
DomainYmlTemplate domainYmlTemplate = new DomainYmlTemplate();
|
||||||
|
// 意图
|
||||||
|
List<String> intentList = new ArrayList<>();
|
||||||
|
intentList.addAll(defaultQuestionCodeAndIdMap.keySet());
|
||||||
|
intentList.addAll(questionCodeAndIdMap.keySet());
|
||||||
|
domainYmlTemplate.setIntents(intentList);
|
||||||
|
// 回复
|
||||||
|
domainYmlTemplate.setResponses(responses);
|
||||||
|
// action
|
||||||
|
List<String> actionList = new ArrayList<>(responses.keySet());
|
||||||
|
domainYmlTemplate.setActions(actionList);
|
||||||
|
// 生成yml文件
|
||||||
|
createYmlFile(DomainYmlTemplate.class, "domain.ftl", domainYmlTemplate, "domain.yml");
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成rule
|
||||||
|
*/
|
||||||
|
public void generateRule(List<RuleYmlTemplate.Rule> ruleList) {
|
||||||
|
RuleYmlTemplate ruleYmlTemplate = new RuleYmlTemplate();
|
||||||
|
ruleYmlTemplate.setRules(ruleList);
|
||||||
|
// 生成yml文件
|
||||||
|
createYmlFile(RuleYmlTemplate.class, "rules.ftl", ruleYmlTemplate, "rules.yml");
|
||||||
|
}
|
||||||
|
|
||||||
|
private void createYmlFile(Class<?> clazz, String ftlName, Object data, String ymlName) {
|
||||||
|
try {
|
||||||
|
// 这个版本和maven依赖的版本一致
|
||||||
|
Configuration configuration = new Configuration(Configuration.VERSION_2_3_31);
|
||||||
|
configuration.setClassForTemplateLoading(clazz, "/templates"); // 模板文件的所在目录
|
||||||
|
// 获取模板
|
||||||
|
Template template = configuration.getTemplate(ftlName);
|
||||||
|
// 创建输出文件
|
||||||
|
PrintWriter out = new PrintWriter(ymlName);
|
||||||
|
// 填充并生成输出
|
||||||
|
template.process(data, out);
|
||||||
|
|
||||||
|
// 关闭资源
|
||||||
|
out.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("导出模板失败", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,23 @@
|
|||||||
|
version: "3.1"
|
||||||
|
|
||||||
|
intents:
|
||||||
|
<#list intents as intent>
|
||||||
|
- ${intent}
|
||||||
|
</#list>
|
||||||
|
|
||||||
|
responses:
|
||||||
|
<#list responses?keys as response>
|
||||||
|
${response}:
|
||||||
|
<#list responses[response] as item>
|
||||||
|
- text: "${item}"
|
||||||
|
</#list>
|
||||||
|
</#list>
|
||||||
|
|
||||||
|
actions:
|
||||||
|
<#list actions as action>
|
||||||
|
- ${action}
|
||||||
|
</#list>
|
||||||
|
|
||||||
|
session_config:
|
||||||
|
session_expiration_time: 60
|
||||||
|
carry_over_slots_to_new_session: true
|
@ -0,0 +1,10 @@
|
|||||||
|
version: "3.1"
|
||||||
|
|
||||||
|
nlu:
|
||||||
|
<#list nlu as item>
|
||||||
|
- intent: ${item.intent}
|
||||||
|
examples: |
|
||||||
|
<#list item.examples as example>
|
||||||
|
- ${example}
|
||||||
|
</#list>
|
||||||
|
</#list>
|
@ -0,0 +1,12 @@
|
|||||||
|
version: "3.1"
|
||||||
|
|
||||||
|
rules:
|
||||||
|
|
||||||
|
<#list rules as item>
|
||||||
|
- rule: ${item.rule}
|
||||||
|
steps:
|
||||||
|
<#list item.steps as ss>
|
||||||
|
- intent: ${ss.intent}
|
||||||
|
- action: ${ss.action}
|
||||||
|
</#list>
|
||||||
|
</#list>
|
Loading…
Reference in New Issue