聊天接口
This commit is contained in:
parent
438ac49fba
commit
76037d4756
@ -71,4 +71,6 @@ public interface ErrorCodeConstants {
|
||||
|
||||
ErrorCode TRAINING_NOT_EXISTS = new ErrorCode(10029, "训练不存在");
|
||||
|
||||
ErrorCode MODEL_COMPLETIONS_ERROR = new ErrorCode(10030, "模型推理失败");
|
||||
|
||||
}
|
||||
|
@ -85,7 +85,7 @@ public class BaseModelController {
|
||||
@Operation(summary = "获得基座模型")
|
||||
@Parameter(name = "id", description = "编号", required = true, example = "1024")
|
||||
@PreAuthorize("@ss.hasPermission('llm:base-model:query')")
|
||||
public CommonResult<BaseModelRespVO> getBaseModel(@RequestParam("id") Integer id) {
|
||||
public CommonResult<BaseModelRespVO> getBaseModel(@RequestParam("id") Long id) {
|
||||
BaseModelDO baseModel = baseModelService.getBaseModel(id);
|
||||
return success(BeanUtils.toBean(baseModel, BaseModelRespVO.class));
|
||||
}
|
||||
@ -94,7 +94,7 @@ public class BaseModelController {
|
||||
@Operation(summary = "获取参数模板")
|
||||
@Parameter(name = "id", description = "编号", required = true, example = "1024")
|
||||
@PreAuthorize("@ss.hasPermission('llm:base-model:query')")
|
||||
public CommonResult<JSONObject> getParamModel(@RequestParam("id") Integer id) {
|
||||
public CommonResult<JSONObject> getParamModel(@RequestParam("id") Long id) {
|
||||
BaseModelDO baseModel = baseModelService.getBaseModel(id);
|
||||
//todo 获取参数模板api
|
||||
return success(new JSONObject());
|
||||
|
@ -92,4 +92,9 @@ public class ConversationController {
|
||||
BeanUtils.toBean(list, ConversationRespVO.class));
|
||||
}
|
||||
|
||||
@PostMapping("/chat")
|
||||
@Operation(summary = "对话推理接口")
|
||||
public CommonResult<ChatRespVO> chat(@Valid @RequestBody ChatReqVO chatReqVO) {
|
||||
return success(conversationService.chat(chatReqVO));
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package cn.iocoder.yudao.module.llm.controller.admin.conversation.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
|
||||
@Schema(description = "管理后台 - 大模型对话推理聊天 Request VO")
|
||||
@Data
|
||||
public class ChatReqVO {
|
||||
|
||||
@Schema(description = "模型类型", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotEmpty(message = "模型类型不能为空")
|
||||
private Integer modelType;
|
||||
@Schema(description = "模型ID", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotEmpty(message = "模型ID不能为空")
|
||||
private Long modelId;
|
||||
@Schema(description = "应用ID", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotEmpty(message = "应用ID不能为空")
|
||||
private Long applicationId;
|
||||
@Schema(description = "对话的内容", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotEmpty(message = "对话的内容不能为空")
|
||||
private String prompt;
|
||||
@Schema(description = "对话ID", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
private String uuid;
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
package cn.iocoder.yudao.module.llm.controller.admin.conversation.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
|
||||
@Schema(description = "管理后台 - 大模型对话推理聊天 Response VO")
|
||||
@Data
|
||||
public class ChatRespVO {
|
||||
|
||||
@Schema(description = "对话ID", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
private String uuid;
|
||||
@Schema(description = "模型类型", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotEmpty(message = "模型类型不能为空")
|
||||
private Integer modelType;
|
||||
@Schema(description = "模型ID", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotEmpty(message = "模型ID不能为空")
|
||||
private Long modelId;
|
||||
@Schema(description = "应用ID", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotEmpty(message = "应用ID不能为空")
|
||||
private Long applicationId;
|
||||
@Schema(description = "对话的内容", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotEmpty(message = "对话的内容不能为空")
|
||||
private String prompt;
|
||||
@Schema(description = "回答的内容", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||
@NotEmpty(message = "回答的内容不能为空")
|
||||
private String response;
|
||||
|
||||
}
|
@ -42,7 +42,7 @@ public interface BaseModelService {
|
||||
* @param id 编号
|
||||
* @return 基座模型
|
||||
*/
|
||||
BaseModelDO getBaseModel(Integer id);
|
||||
BaseModelDO getBaseModel(Long id);
|
||||
|
||||
/**
|
||||
* 获得基座模型分页
|
||||
|
@ -63,7 +63,7 @@ public class BaseModelServiceImpl implements BaseModelService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseModelDO getBaseModel(Integer id) {
|
||||
public BaseModelDO getBaseModel(Long id) {
|
||||
return baseModelMapper.selectById(id);
|
||||
}
|
||||
|
||||
|
@ -52,4 +52,5 @@ public interface ConversationService {
|
||||
*/
|
||||
PageResult<ConversationDO> getConversationPage(ConversationPageReqVO pageReqVO);
|
||||
|
||||
ChatRespVO chat(@Valid ChatReqVO chatReqVO);
|
||||
}
|
@ -1,11 +1,21 @@
|
||||
package cn.iocoder.yudao.module.llm.service.conversation;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
||||
import cn.iocoder.yudao.module.llm.dal.dataobject.basemodel.BaseModelDO;
|
||||
import cn.iocoder.yudao.module.llm.service.basemodel.BaseModelService;
|
||||
import cn.iocoder.yudao.module.llm.service.http.ModelService;
|
||||
import cn.iocoder.yudao.module.llm.service.http.vo.ModelCompletionsReqVO;
|
||||
import cn.iocoder.yudao.module.llm.service.http.vo.ModelCompletionsRespVO;
|
||||
import com.alibaba.excel.util.StringUtils;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import javax.annotation.Resource;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import cn.iocoder.yudao.module.llm.controller.admin.conversation.vo.*;
|
||||
import cn.iocoder.yudao.module.llm.dal.dataobject.conversation.ConversationDO;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
@ -28,6 +38,17 @@ public class ConversationServiceImpl implements ConversationService {
|
||||
|
||||
@Resource
|
||||
private ConversationMapper conversationMapper;
|
||||
@Resource
|
||||
private StringRedisTemplate stringRedisTemplate;
|
||||
@Resource
|
||||
private ModelService modelService;
|
||||
@Resource
|
||||
private BaseModelService baseModelService;
|
||||
|
||||
// 聊天会话历史记录缓存Key
|
||||
private final static String CHAT_HIStORY_REDIS_KEY = "llm:chat:history";
|
||||
// 聊天会话历史记录缓存时间
|
||||
private final static Long CHAT_HISTORY_REDIS_EXPIRE_SECONDS = 60 * 60 * 24L;
|
||||
|
||||
@Override
|
||||
public Integer createConversation(ConversationSaveReqVO createReqVO) {
|
||||
@ -71,4 +92,41 @@ public class ConversationServiceImpl implements ConversationService {
|
||||
return conversationMapper.selectPage(pageReqVO);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChatRespVO chat(ChatReqVO chatReqVO) {
|
||||
if (StringUtils.isBlank(chatReqVO.getUuid())) {
|
||||
// 如果没有uuid,就生成一个
|
||||
chatReqVO.setUuid(UUID.randomUUID().toString());
|
||||
}
|
||||
String model = null;
|
||||
if (Objects.equals(1, chatReqVO.getModelType())) {
|
||||
// 预制模型
|
||||
BaseModelDO baseModelDO = baseModelService.getBaseModel(chatReqVO.getModelId());
|
||||
if (baseModelDO == null) {
|
||||
throw exception(BASE_MODEL_NOT_EXISTS);
|
||||
}
|
||||
model = baseModelDO.getModelName();
|
||||
}
|
||||
ModelCompletionsReqVO.ModelCompletionsMessage message = new ModelCompletionsReqVO.ModelCompletionsMessage();
|
||||
message.setRole("user");
|
||||
message.setContent(chatReqVO.getPrompt());
|
||||
// TODO: 聊天推理
|
||||
List<String> messageHistoryList = stringRedisTemplate.opsForList().range(CHAT_HIStORY_REDIS_KEY + ":" + chatReqVO.getUuid(), 0, -1);
|
||||
List<ModelCompletionsReqVO.ModelCompletionsMessage> messages = new ArrayList<>();
|
||||
if (messageHistoryList != null && !messageHistoryList.isEmpty()) {
|
||||
for (String messageHistory : messageHistoryList) {
|
||||
messages.add(JsonUtils.parseObject(messageHistory, ModelCompletionsReqVO.ModelCompletionsMessage.class));
|
||||
}
|
||||
}
|
||||
messages.add(message);
|
||||
ModelCompletionsReqVO modelCompletionsReqVO = new ModelCompletionsReqVO();
|
||||
modelCompletionsReqVO.setMessages(messages);
|
||||
modelCompletionsReqVO.setModel(model);
|
||||
ModelCompletionsRespVO modelCompletionsRespVO = modelService.modelCompletions(modelCompletionsReqVO);
|
||||
if (modelCompletionsRespVO == null) {
|
||||
throw exception(MODEL_COMPLETIONS_ERROR);
|
||||
}
|
||||
stringRedisTemplate.opsForList().rightPush(CHAT_HIStORY_REDIS_KEY + ":" + chatReqVO.getUuid(), JsonUtils.toJsonString(message));
|
||||
return null;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user