Commit 998368a1 authored by shiyu's avatar shiyu

ai会话

parent 6e60cc2d
...@@ -12,17 +12,17 @@ public class SendChatMsgRequestDto implements Entity { ...@@ -12,17 +12,17 @@ public class SendChatMsgRequestDto implements Entity {
* 2:消息不同步至 From_Account * 2:消息不同步至 From_Account
* 若不填写默认情况下会将消息存 From_Account 漫游 * 若不填写默认情况下会将消息存 From_Account 漫游
*/ */
private Integer SyncOtherMachine; private Integer syncOtherMachine;
/** /**
* 消息发送方 UserID(用于指定发送消息方账号) * 消息发送方 UserID(用于指定发送消息方账号)
*/ */
private String From_Account; private String fromAccount;
/** /**
* 消息接收方 UserID * 消息接收方 UserID
*/ */
private String To_Account; private String toAccount;
/** /**
* 消息离线保存时长(单位:秒),最长为7天(604800秒) * 消息离线保存时长(单位:秒),最长为7天(604800秒)
...@@ -30,28 +30,28 @@ public class SendChatMsgRequestDto implements Entity { ...@@ -30,28 +30,28 @@ public class SendChatMsgRequestDto implements Entity {
* 若设置该字段超过7天(604800秒),仍只保存7天 * 若设置该字段超过7天(604800秒),仍只保存7天
* 若不设置该字段,则默认保存7天 * 若不设置该字段,则默认保存7天
*/ */
private Integer MsgLifeTime; private Integer msgLifeTime;
/** /**
* 消息序列号(32位无符号整数),后台会根据该字段去重及进行同秒内消息的排序, * 消息序列号(32位无符号整数),后台会根据该字段去重及进行同秒内消息的排序,
* 详细规则请看本接口的功能说明。若不填该字段,则由后台填入随机数 * 详细规则请看本接口的功能说明。若不填该字段,则由后台填入随机数
*/ */
private Integer MsgSeq; private Integer msgSeq;
/** /**
* 消息随机数(32位无符号整数),后台用于同一秒内的消息去重。请确保该字段填的是随机 * 消息随机数(32位无符号整数),后台用于同一秒内的消息去重。请确保该字段填的是随机
*/ */
private Integer MsgRandom; private Integer msgRandom;
/** /**
* 消息对象类型 * 消息对象类型
* 枚举转换成字符串文本 * 枚举转换成字符串文本
*/ */
private Integer MsgType; private Integer msgType;
/** /**
* 消息内容 * 消息内容
*/ */
private Object MsgContent; private Object msgContent;
} }
...@@ -65,7 +65,7 @@ public class ConversationApi { ...@@ -65,7 +65,7 @@ public class ConversationApi {
OkHttpUtil okHttpUtil = OkHttpUtil.builder().url(url); OkHttpUtil okHttpUtil = OkHttpUtil.builder().url(url);
IMChatMsg imChatMsg = imUtil.convert(sendChatMsgRequestDto); IMChatMsg imChatMsg = imUtil.convert(sendChatMsgRequestDto);
//消息同步至发送方 //消息同步至发送方
imChatMsg.setSyncOtherMachine(IMEnum.SyncOtherMachineEnum.SYNC.getCode()); imChatMsg.setSyncOtherMachine(sendChatMsgRequestDto.getSyncOtherMachine()==null ? IMEnum.SyncOtherMachineEnum.SYNC.getCode() : sendChatMsgRequestDto.getSyncOtherMachine());
imChatMsg.setMsgRandom(randomNum); imChatMsg.setMsgRandom(randomNum);
logger.info("============ 发送对话信息内容 : {}", JsonUtils.from(imChatMsg)); logger.info("============ 发送对话信息内容 : {}", JsonUtils.from(imChatMsg));
okHttpUtil.addParams(MapUtils.fromEntity(imChatMsg)); okHttpUtil.addParams(MapUtils.fromEntity(imChatMsg));
......
...@@ -46,8 +46,8 @@ public class IMUtil { ...@@ -46,8 +46,8 @@ public class IMUtil {
public IMChatMsg convert(SendChatMsgRequestDto dto) { public IMChatMsg convert(SendChatMsgRequestDto dto) {
IMChatMsg imChatMsg = new IMChatMsg(); IMChatMsg imChatMsg = new IMChatMsg();
//userid需要转换成IM的用户ID //userid需要转换成IM的用户ID
imChatMsg.setTo_Account(formatUserId(dto.getTo_Account())); imChatMsg.setTo_Account(formatUserId(dto.getToAccount()));
imChatMsg.setFrom_Account(formatUserId(dto.getFrom_Account())); imChatMsg.setFrom_Account(formatUserId(dto.getFromAccount()));
Map<String, Object> msgContentMap = new HashMap<>(); Map<String, Object> msgContentMap = new HashMap<>();
msgContentMap.put("MsgType", IMEnum.MsgTypeEnum.getValueByCode(dto.getMsgType())); msgContentMap.put("MsgType", IMEnum.MsgTypeEnum.getValueByCode(dto.getMsgType()));
msgContentMap.put("MsgContent", new HashMap(){{put("Text", dto.getMsgContent());}}); msgContentMap.put("MsgContent", new HashMap(){{put("Text", dto.getMsgContent());}});
......
...@@ -9,12 +9,13 @@ import com.wwdz.ch.core.type.Result; ...@@ -9,12 +9,13 @@ import com.wwdz.ch.core.type.Result;
import com.wwdz.ch.core.util.IMUtil; import com.wwdz.ch.core.util.IMUtil;
import com.wwdz.ch.db.dao.AiAssistantDao; import com.wwdz.ch.db.dao.AiAssistantDao;
import com.wwdz.ch.db.domain.AiAssistant; import com.wwdz.ch.db.domain.AiAssistant;
import com.wwdz.ch.wx.api.ChatApi;
import com.wwdz.ch.wx.service.IMService; import com.wwdz.ch.wx.service.IMService;
import com.xxdxxs.utils.JsonUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
...@@ -38,6 +39,9 @@ public class IMServiceImpl implements IMService { ...@@ -38,6 +39,9 @@ public class IMServiceImpl implements IMService {
@Autowired @Autowired
IMUtil imUtil; IMUtil imUtil;
@Autowired
ChatApi chatApi;
/** /**
* 新增ai会话,发送默认打招呼的消息 * 新增ai会话,发送默认打招呼的消息
* @param dto * @param dto
...@@ -46,7 +50,7 @@ public class IMServiceImpl implements IMService { ...@@ -46,7 +50,7 @@ public class IMServiceImpl implements IMService {
@Override @Override
public Result addAiChat(SendChatMsgRequestDto dto) { public Result addAiChat(SendChatMsgRequestDto dto) {
try { try {
AiAssistant aiAssistant = aiAssistantDao.findByCode(dto.getFrom_Account()); AiAssistant aiAssistant = aiAssistantDao.findByCode(dto.getFromAccount());
String msgContent = aiAssistant.getProfile(); String msgContent = aiAssistant.getProfile();
dto.setMsgContent(msgContent); dto.setMsgContent(msgContent);
dto.setMsgType(IMEnum.MsgTypeEnum.TIMTextElem.getCode()); dto.setMsgType(IMEnum.MsgTypeEnum.TIMTextElem.getCode());
...@@ -87,6 +91,46 @@ public class IMServiceImpl implements IMService { ...@@ -87,6 +91,46 @@ public class IMServiceImpl implements IMService {
@Override @Override
public Result sendChatMsg(SendChatMsgRequestDto dto) { public Result sendChatMsg(SendChatMsgRequestDto dto) {
return null; try {
Result result = conversationApi.sendChatMsg(dto);
return result;
} catch (Exception e) {
logger.error("发送聊天消息失败:{}", e);
}
return Result.failed();
}
@Override
public Result sendAiChatMsg(SendChatMsgRequestDto dto) {
try {
Result askResult = conversationApi.sendChatMsg(dto);
if (!askResult.getSuccess()) {
return askResult;
}
logger.info("=========== userid:{}, 提问ai的消息发送成功 ==========", dto.getFromAccount());
if (dto.getMsgType() == IMEnum.MsgTypeEnum.TIMTextElem.getCode()) {
String question = (String) dto.getMsgContent();
String response = chatApi.chatWithModel(question);
String answer = JsonUtils.getValueByPath(response, "response");
//将回答也发送给IM
SendChatMsgRequestDto answerDto = new SendChatMsgRequestDto();
answerDto.setFromAccount(dto.getToAccount());
answerDto.setToAccount(dto.getFromAccount());
answerDto.setMsgType(IMEnum.MsgTypeEnum.TIMTextElem.getCode());
answerDto.setMsgContent(answer);
Result answerResult = conversationApi.sendChatMsg(answerDto);
if (!answerResult.getSuccess()) {
logger.info("=============== 发送问题的ai答复消息失败 =============");
return answerResult;
}
} else {
return Result.failed("目前尚不支持除文本外的消息发送");
}
return Result.success();
} catch (Exception e) {
logger.error("发送聊天消息失败:{}", e);
}
return Result.failed();
} }
} }
...@@ -35,4 +35,12 @@ public interface IMService { ...@@ -35,4 +35,12 @@ public interface IMService {
* @return * @return
*/ */
Result sendChatMsg(SendChatMsgRequestDto dto); Result sendChatMsg(SendChatMsgRequestDto dto);
/**
* 发送ai聊天消息
* 将问题发送给im,并把ai的回答也发送给im
* @param dto
* @return
*/
Result sendAiChatMsg(SendChatMsgRequestDto dto);
} }
...@@ -47,7 +47,7 @@ public class IMController { ...@@ -47,7 +47,7 @@ public class IMController {
@PostMapping("/addChat") @PostMapping("/addChat")
public Result addChat(@RequestBody SendChatMsgRequestDto dto) { public Result addChat(@RequestBody SendChatMsgRequestDto dto) {
logger.info("【请求开始】新增AI会话,请求参数:{}", JSON.toJSONString(dto)); logger.info("【请求开始】新增AI会话,请求参数:{}", JSON.toJSONString(dto));
if (!StringUtils.isAllNotEmpty(dto.getFrom_Account(), dto.getTo_Account())) { if (!StringUtils.isAllNotEmpty(dto.getFromAccount(), dto.getToAccount())) {
return Result.failed(ResultCode.PARAM_ERROR); return Result.failed(ResultCode.PARAM_ERROR);
} }
return imService.addAiChat(dto); return imService.addAiChat(dto);
...@@ -69,11 +69,24 @@ public class IMController { ...@@ -69,11 +69,24 @@ public class IMController {
@PostMapping("/sendChatMsg") @PostMapping("/sendChatMsg")
public Result sendChatMsg(@RequestBody SendChatMsgRequestDto dto) { public Result sendChatMsg(@RequestBody SendChatMsgRequestDto dto) {
logger.info("【请求开始】发送聊天消息,请求参数:{}", JSON.toJSONString(dto)); logger.info("【请求开始】发送聊天消息,请求参数:{}", JSON.toJSONString(dto));
if (!StringUtils.isAllNotEmpty(dto.getFrom_Account(), dto.getTo_Account(), dto.getMsgType())) { if (!StringUtils.isAllNotEmpty(dto.getFromAccount(), dto.getToAccount(), dto.getMsgType())) {
return Result.failed(ResultCode.PARAM_ERROR); return Result.failed(ResultCode.PARAM_ERROR);
} }
return imService.sendChatMsg(dto); return imService.sendChatMsg(dto);
} }
@ApiOperation(value = "发送Ai聊天消息")
@PostMapping("/sendAiChatMsg")
public Result sendAiChatMsg(@RequestBody SendChatMsgRequestDto dto) {
logger.info("【请求开始】发送聊天消息,请求参数:{}", JSON.toJSONString(dto));
if (!StringUtils.isAllNotEmpty(dto.getFromAccount(), dto.getToAccount(), dto.getMsgType())) {
return Result.failed(ResultCode.PARAM_ERROR);
}
return imService.sendAiChatMsg(dto);
}
} }
...@@ -11,7 +11,6 @@ import com.wwdz.ch.db.dao.UserDao; ...@@ -11,7 +11,6 @@ import com.wwdz.ch.db.dao.UserDao;
import com.wwdz.ch.db.domain.AiAssistant; import com.wwdz.ch.db.domain.AiAssistant;
import com.wwdz.ch.db.domain.User; import com.wwdz.ch.db.domain.User;
import com.wwdz.ch.db.dto.request.AiAssistantRequestDto; import com.wwdz.ch.db.dto.request.AiAssistantRequestDto;
import com.wwdz.ch.wx.service.AiAssistantService;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -90,8 +89,8 @@ public class ChatApiTest { ...@@ -90,8 +89,8 @@ public class ChatApiTest {
@Test @Test
public void sendChatMsg() { public void sendChatMsg() {
SendChatMsgRequestDto sendChatMsgRequestDto = new SendChatMsgRequestDto(); SendChatMsgRequestDto sendChatMsgRequestDto = new SendChatMsgRequestDto();
sendChatMsgRequestDto.setTo_Account("241"); sendChatMsgRequestDto.setToAccount("241");
sendChatMsgRequestDto.setFrom_Account("ai10000"); sendChatMsgRequestDto.setFromAccount("ai10000");
sendChatMsgRequestDto.setMsgType(IMEnum.MsgTypeEnum.TIMTextElem.getCode()); sendChatMsgRequestDto.setMsgType(IMEnum.MsgTypeEnum.TIMTextElem.getCode());
long timestamp = System.currentTimeMillis(); long timestamp = System.currentTimeMillis();
String msgContent = timestamp + " >> ai测试消息的数据, 是否可以接收到"; String msgContent = timestamp + " >> ai测试消息的数据, 是否可以接收到";
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment