Browse Source

修改:

1、修改招呼语生成方式。
添加:
    1.实时通知业务开发
tags/B.2.7.0_20240201_base
10710 1 year ago
parent
commit
56c606aeda
6 changed files with 138 additions and 44 deletions
  1. +5
    -0
      xueyi-common/xueyi-common-web/pom.xml
  2. +4
    -0
      xueyi-modules/xueyi-nlt/pom.xml
  3. +16
    -0
      xueyi-modules/xueyi-nlt/src/main/java/com/xueyi/nlt/nlt/constant/PromptsConstants.java
  4. +23
    -28
      xueyi-modules/xueyi-nlt/src/main/java/com/xueyi/nlt/nlt/controller/DmIntentController.java
  5. +15
    -0
      xueyi-modules/xueyi-nlt/src/main/java/com/xueyi/nlt/nlt/domain/vo/WelcomeLabelVo.java
  6. +75
    -16
      xueyi-modules/xueyi-nlt/src/main/java/com/xueyi/nlt/nlt/template/GenerativeWelcomeTemplate.java

+ 5
- 0
xueyi-common/xueyi-common-web/pom.xml View File

@@ -51,6 +51,11 @@
<artifactId>xueyi-common-seata</artifactId>
</dependency>

<dependency>
<groupId>com.xueyi</groupId>
<artifactId>xueyi-common-redis</artifactId>
</dependency>

</dependencies>

</project>

+ 4
- 0
xueyi-modules/xueyi-nlt/pom.xml View File

@@ -80,6 +80,10 @@
<groupId>com.xueyi</groupId>
<artifactId>xueyi-api-system</artifactId>
</dependency>
<dependency>
<groupId>com.xueyi</groupId>
<artifactId>xueyi-common-mqtt</artifactId>
</dependency>


<dependency>


+ 16
- 0
xueyi-modules/xueyi-nlt/src/main/java/com/xueyi/nlt/nlt/constant/PromptsConstants.java View File

@@ -55,4 +55,20 @@ public class PromptsConstants {
}
return result;
}

public static String getPeopleTypeFormat(String peopleType){
String result = "";
if(StringUtils.isNotBlank(peopleType)){
result = "看到" + peopleType + "时,";
}
return result;
}

public static String getTimeTypeFormat(String timeType){
String result = "";
if (StringUtils.isNotBlank(timeType)){
result = "当你在" + timeType + "";
}
return result;
}
}

+ 23
- 28
xueyi-modules/xueyi-nlt/src/main/java/com/xueyi/nlt/nlt/controller/DmIntentController.java View File

@@ -18,6 +18,7 @@ import com.xueyi.common.core.web.validate.V_A;
import com.xueyi.common.core.web.validate.V_E;
import com.xueyi.common.log.annotation.Log;
import com.xueyi.common.log.enums.BusinessType;
import com.xueyi.common.mqtt.connection.MqttTemplate;
import com.xueyi.common.security.annotation.Logical;
import com.xueyi.common.security.annotation.RequiresPermissions;
import com.xueyi.common.web.entity.controller.BaseController;
@@ -64,6 +65,7 @@ import com.xueyi.system.api.organize.feign.RemoteEnterpriseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.validation.annotation.Validated;
@@ -170,6 +172,12 @@ public class DmIntentController extends BaseController<DmIntentQuery, DmIntentDt

@Autowired
private ISysLlmService sysLlmService;

@Autowired
MqttTemplate mqttTemplate;

@Value("${spring.profiles.active}")
private String activeProfile;
/**
* 意图请求
列表
@@ -952,42 +960,29 @@ public class DmIntentController extends BaseController<DmIntentQuery, DmIntentDt
return AjaxResult.error(res);
}

// @PostMapping("/api/testforwelcome")
// public AjaxResult test(){
// JSONObject jo = generativeWelcomeTemplate.handle("1", "");
// Map<Object, Object> devices = redisTemplate2.opsForHash().entries("group:dgman:device:");
// for(Object devId:devices.keySet()){
// mqttTemplate.sendToMqtt("/" + activeProfile + "/" + "digital_man" + "/" + devId.toString() + "/" + "notify" + "/" + "generate-welcome", jo.getJSONArray("data").toJSONString());
// }
// return AjaxResult.success();
// }

@PostMapping("/inner/generateContextTask")
@ResponseBody
public AjaxResult generateContextTask() {
LocalDateTime dateTime = LocalDateTime.now();
List<String> contentList = new ArrayList<>();
List<JSONObject> contentList = new ArrayList<>();
GenerativeWelcomeTemplate.lunar = "";
GenerativeWelcomeTemplate.term = null;
try {
for (int i = 0; i < 5 && contentList.size() < 10; i++) {
JSONObject jo = generativeWelcomeTemplate.handle("1", "");
String content = "";
if (jo != null) {
content = jo.getString("msg");
}
if (StringUtils.isNotEmpty(content)) {
JSONArray contents = JSONArray.parseArray(content);
for (int j = 0; j < contents.size(); j++) {
String contentStr = contents.getJSONObject(j).getString("content");
// 如过contentList中包含content,则不添加
if (contentList.contains(contentStr)) {
continue;
}
// 如果content的长度长于30,则不添加
if (contentStr.length() > 30) {
continue;
}
if (contentList.size() >= 10) {
break;
}
contentList.add(contentStr);
}
}
JSONObject jo = generativeWelcomeTemplate.handle("1", "");
Map<Object, Object> devices = redisTemplate2.opsForHash().entries("group:dgman:device:");
for(Object devId:devices.keySet()){
mqttTemplate.sendToMqtt("/" + activeProfile + "/" + "digital_man" + "/" + devId.toString() + "/" + "notify" + "/" + "generate-welcome", jo.getJSONArray("data").toJSONString());
}
//格式化日期:yyyy-MM-dd
String date = dateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
redisTemplate2.opsForHash().put("group:task" + ":generate", date, contentList);
} catch (Exception e) {
log.error("生成任务失败:{}",e.getMessage());
return AjaxResult.error("生成任务失败");


+ 15
- 0
xueyi-modules/xueyi-nlt/src/main/java/com/xueyi/nlt/nlt/domain/vo/WelcomeLabelVo.java View File

@@ -0,0 +1,15 @@
package com.xueyi.nlt.nlt.domain.vo;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@NoArgsConstructor
public class WelcomeLabelVo {
private String labelName;
private List<WelcomeLabelVo> labels;
private List<String> msgs;

}

+ 75
- 16
xueyi-modules/xueyi-nlt/src/main/java/com/xueyi/nlt/nlt/template/GenerativeWelcomeTemplate.java View File

@@ -12,6 +12,7 @@ import com.xueyi.nlt.nlt.constant.PromptsConstants;
import com.xueyi.nlt.nlt.domain.LlmContext;
import com.xueyi.nlt.nlt.domain.LlmParam;
import com.xueyi.nlt.nlt.domain.LlmResponse;
import com.xueyi.nlt.nlt.domain.vo.WelcomeLabelVo;
import com.xueyi.nlt.nlt.service.ISysLlmService;
import com.xueyi.system.api.holiday.domain.po.DmHolidayPo;
import com.xueyi.system.api.holiday.feign.FeignHolidayService;
@@ -24,6 +25,10 @@ import org.springframework.stereotype.Service;
import static com.xueyi.nlt.nlt.constant.PromptsConstants.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("generative-welcome")
@@ -52,7 +57,17 @@ public class GenerativeWelcomeTemplate implements BaseTemplate{

@Override
public JSONObject handle(String devId, String content) {
JSONObject jsonObject = new JSONObject();
JSONObject timeType = new JSONObject();
timeType.put("morning", "早上");
timeType.put("noon", "中午");
timeType.put("afternoon", "下午");
timeType.put("night", "晚上");

JSONObject peopleType = new JSONObject();
peopleType.put("employee", "员工");
peopleType.put("employer", "老板");
peopleType.put("visitor", "访客");

String holiday = "";
LocalDateTime now = LocalDateTime.now();
if (StringUtils.isBlank(lunar)) {
@@ -72,23 +87,67 @@ public class GenerativeWelcomeTemplate implements BaseTemplate{
DmHolidayPo po = JSON.parseObject(JSON.toJSONString(result.get("data")), DmHolidayPo.class);
holiday = po.getName();
}
List<WelcomeLabelVo> resultList = new ArrayList<>();
String prefix = "假设你是公司前台,";
String suffix = "请根据以上信息生成简短的话对他打招呼。生成十句打招呼内容,输出为一个JSON数组[{}],每个元素是一个JSON:{“content”:}。不要给出任何解释说明,不要带人名。";
for(String time:timeType.keySet()) {
String timeString = getTimeTypeFormat(timeType.getString(time));
WelcomeLabelVo labelVo = new WelcomeLabelVo();
labelVo.setLabelName(time);
List<WelcomeLabelVo> welcomeLabelVoList = new ArrayList<>();

//生成节日或节气的特殊招呼语
if(!holiday.isBlank()|| !term.isBlank()){
WelcomeLabelVo festivalWelcomeLabelVo = new WelcomeLabelVo();
festivalWelcomeLabelVo.setLabelName("festival");
log.info(prefix + getFestivalFormat(holiday) + getSolarTermFormat(term) + timeString + "看到员工时," + suffix);
LlmContext llmContext = new LlmContext(prefix + getFestivalFormat(holiday) + getSolarTermFormat(term) + timeString + "看到员工时," + suffix);
llmContext.setDevId(devId);
LlmParam llmParam = new LlmParam();
LlmResponse response = sysLlmService.chat(llmContext, llmParam);
JSONArray sentenceArray = JSONArray.parseArray(response.getContent());
List<String> responseList = new ArrayList<>();
for (JSONObject sentence : sentenceArray.toList(JSONObject.class)) {
responseList.add(sentence.getString("content"));
}
festivalWelcomeLabelVo.setMsgs(responseList);
welcomeLabelVoList.add(festivalWelcomeLabelVo);
}

// 根据content内容调用模版并返回结果
String prefix = "假设你是公司前台," + getDateFormat(now) + getFestivalFormat(holiday) + getSolarTermFormat(term) + "当你看到公司员工时,请根据以上信息生成简短的话和他打招呼。生成十句打招呼内容,输出为一个JSON数组[{}],每个元素是一个JSON:{“content”:}。不要给出任何解释说明。";
String suffix = "";
log.info(prefix + content + suffix);
LlmContext llmContext = new LlmContext(prefix + content + suffix);
llmContext.setDevId(devId);
LlmParam llmParam = new LlmParam();
LlmResponse response = sysLlmService.chat(llmContext,llmParam);
try {
jsonObject.put("msg",response.getContent());
return jsonObject;
} catch (JSONException je) {
// 返回结果错误,计日志,存log,返回空结果
log.error(je.getMessage(),je);
//按人员类型生成招呼语
for (String people : peopleType.keySet()) {
WelcomeLabelVo welcomeLabelVo = new WelcomeLabelVo();
welcomeLabelVo.setLabelName(people);
String peopleString = getPeopleTypeFormat(peopleType.getString(people));
log.info(prefix + timeString + peopleString + suffix);
LlmContext llmContext = new LlmContext(prefix + timeString + peopleString + suffix);
llmContext.setDevId(devId);
LlmParam llmParam = new LlmParam();
LlmResponse response = sysLlmService.chat(llmContext, llmParam);
JSONArray sentenceArray = JSONArray.parseArray(response.getContent());
List<String> responseList = new ArrayList<>();
for (JSONObject c : sentenceArray.toList(JSONObject.class)) {
responseList.add(c.getString("content"));
}
welcomeLabelVo.setMsgs(responseList);
welcomeLabelVoList.add(welcomeLabelVo);
}
labelVo.setLabels(welcomeLabelVoList);
resultList.add(labelVo);
}
return jsonObject;
JSONObject finalresult = new JSONObject();
finalresult.put("data", resultList);
return finalresult;

// 根据content内容调用模版并返回结果
// String prefix = "假设你是公司前台," + getDateFormat(now) + getFestivalFormat(holiday) + getSolarTermFormat(term) + "当你看到公司员工时,请根据以上信息生成简短的话和他打招呼。生成十句打招呼内容,输出为一个JSON数组[{}],每个元素是一个JSON:{“content”:}。不要给出任何解释说明。";
// String suffix = "";
// log.info(prefix + content + suffix);
// LlmContext llmContext = new LlmContext(prefix + content + suffix);
// llmContext.setDevId(devId);
// LlmParam llmParam = new LlmParam();
// LlmResponse response = sysLlmService.chat(llmContext,llmParam);
// return null;
}

@Override


Loading…
Cancel
Save