Commit cf8affcd authored by alex yao's avatar alex yao

feat: API接口

parent e68cf43e
......@@ -8,6 +8,7 @@ import cn.com.poc.agent_application.utils.AgentApplicationTools;
import cn.com.poc.common.constant.CommonConstant;
import cn.com.poc.common.service.BosConfigService;
import cn.com.poc.common.utils.DateUtils;
import cn.com.poc.common.utils.UUIDTool;
import cn.com.poc.data_analyze.aggregate.DataAnalyzeReportService;
import cn.com.poc.data_analyze.constants.DataAnalyzeChannelEnum;
import cn.com.poc.data_analyze.constants.DataAnalyzeTypeEnum;
......@@ -21,17 +22,16 @@ import cn.com.poc.thirdparty.resource.demand.ai.constants.LLMRoleEnum;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.Message;
import cn.com.poc.thirdparty.resource.demand.ai.entity.dialogue.Tool;
import cn.com.yict.framemax.core.exception.BusinessException;
import cn.com.yict.framemax.core.i18n.I18nMessageException;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.*;
/**
* @author alex.yao
......@@ -107,7 +107,7 @@ public class AgentApplicationApiServiceImpl implements AgentApplicationApiServic
throw new BusinessException("无效的API Key或Secret");
}
BizAgentApplicationApiConversationEntity conversationEntity = bizAgentApplicationApiConversationService.getByConversationId(conversationId);
if (null == conversationEntity && DateUtils.getCurrDateTime().after(conversationEntity.getExpiredTime())) {
if (null == conversationEntity || DateUtils.getCurrDateTime().after(conversationEntity.getExpiredTime())) {
throw new BusinessException("无效的对话ID");
}
String agentId = conversationEntity.getAgentId();
......@@ -199,11 +199,20 @@ public class AgentApplicationApiServiceImpl implements AgentApplicationApiServic
if (!checkConversationEffectiveness(conversationId)) {
throw new BusinessException("无效的对话ID");
}
String contentType = file.getContentType();
String originalFilename = file.getOriginalFilename();
String prefix = originalFilename.substring(originalFilename.lastIndexOf(".")).replaceAll("\\.", "");
String uploadUrl = bosConfigService.upload(file.getInputStream(), prefix, contentType);
//校验文件类型
String type = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1, file.getOriginalFilename().length());
if (!(type.equals("pdf") || type.equals("doc") || type.equals("docx") || type.equals("md") || type.equals("txt"))) {
throw new BusinessException("文件类型不支持");
}
// 文件大小不能超过10M
long fileSizeInBytes = file.getSize();
double fileSizeInMB = (double) fileSizeInBytes / (1024 * 1024);
if (fileSizeInMB > 10) {
throw new BusinessException("文件不可超过10m");
}
File tempFile = File.createTempFile(UUIDTool.getUUID(), "." + type);
file.transferTo(tempFile);
String uploadUrl = bosConfigService.upload(file.getInputStream(), type, file.getContentType());
String fileId = UUID.randomUUID().toString();
BizAgentApplicationApiFilesEntity bizAgentApplicationApiFilesEntity = new BizAgentApplicationApiFilesEntity();
......@@ -254,7 +263,7 @@ public class AgentApplicationApiServiceImpl implements AgentApplicationApiServic
}
BizAgentApplicationApiFilesEntity filesEntity = filesEntities.get(0);
if (filesEntity.getExpiredTime().before(currDateTime)) {
if (filesEntity.getExpiredTime().after(currDateTime)) {
fileUrls.add(filesEntity.getFileUrl());
}
}
......
......@@ -245,8 +245,7 @@ public class AgentDataAnalyzeServiceImpl implements AgentDataAnalyzeService {
}
private static void setUsageDataAnalyze(DataAnalyze
dataAnalyze, List<AgentUsageQueryItem> agentUsageQueryItems, DataAnalyzeTimeDimensionEnum dimension) {
private static void setUsageDataAnalyze(DataAnalyze dataAnalyze, List<AgentUsageQueryItem> agentUsageQueryItems, DataAnalyzeTimeDimensionEnum dimension) {
if (dataAnalyze == null) {
dataAnalyze = new DataAnalyze();
}
......@@ -268,8 +267,7 @@ public class AgentDataAnalyzeServiceImpl implements AgentDataAnalyzeService {
dataAnalyze.setFluctuate(fluctuate);
}
private static void setUserCountDataAnalyze(DataAnalyze
dataAnalyze, List<AgentUsageQueryItem> agentUsageQueryItems, DataAnalyzeTimeDimensionEnum dimension) {
private static void setUserCountDataAnalyze(DataAnalyze dataAnalyze, List<AgentUsageQueryItem> agentUsageQueryItems, DataAnalyzeTimeDimensionEnum dimension) {
if (dataAnalyze == null) {
dataAnalyze = new DataAnalyze();
}
......@@ -291,8 +289,7 @@ public class AgentDataAnalyzeServiceImpl implements AgentDataAnalyzeService {
dataAnalyze.setFluctuate(fluctuate);
}
private static void setPointsDataAnalyze(List<PointsUsageQueryItem> pointsUsageQueryItems, DataAnalyze
dataAnalyze, DataAnalyzeTimeDimensionEnum dimension) {
private static void setPointsDataAnalyze(List<PointsUsageQueryItem> pointsUsageQueryItems, DataAnalyze dataAnalyze, DataAnalyzeTimeDimensionEnum dimension) {
if (dataAnalyze == null) {
dataAnalyze = new DataAnalyze();
}
......@@ -315,8 +312,7 @@ public class AgentDataAnalyzeServiceImpl implements AgentDataAnalyzeService {
}
private void channelUserConvert(String agentId, List<String> channel, Date
currDateTime, List<ChannelUsersCountEntity> result) {
private void channelUserConvert(String agentId, List<String> channel, Date currDateTime, List<ChannelUsersCountEntity> result) {
Date thisWeekBegin = DateUtils.getWeekBegin2(currDateTime);
Date thisWeekEnd = DateUtils.addDays(DateUtils.getWeekEnd2(currDateTime), -1);
ChannelUserCountQueryCondition thisWeekCondition = new ChannelUserCountQueryCondition();
......
package cn.com.poc.expose.dto.model_link_api;
import java.util.List;
/**
* @author alex.yao
* @date 2024/12/23
*/
public class CompletionsDto {
/**
* 对话ID
*/
private String conversationId;
/**
* 文件ID
*/
private String fileId;
/**
* 对话内容
*/
private String query;
/**
* 是否流式传输
*/
private Boolean stream;
public String getConversationId() {
return conversationId;
}
public void setConversationId(String conversationId) {
this.conversationId = conversationId;
}
public String getFileId() {
return fileId;
}
public void setFileId(String fileId) {
this.fileId = fileId;
}
public String getQuery() {
return query;
}
public void setQuery(String query) {
this.query = query;
}
public Boolean getStream() {
return stream;
}
public void setStream(Boolean stream) {
this.stream = stream;
}
}
package cn.com.poc.expose.dto.model_link_api;
/**
* 新建对话请求参数实体
*
* @author alex.yao
* @date 2024/12/23
*/
public class CreateConversationDto {
private String agentId;
public String getAgentId() {
return agentId;
}
public void setAgentId(String agentId) {
this.agentId = agentId;
}
}
package cn.com.poc.expose.rest;
import cn.com.poc.expose.dto.model_link_api.CompletionsDto;
import cn.com.poc.expose.dto.model_link_api.CreateConversationDto;
import cn.com.yict.framemax.core.rest.BaseRest;
import cn.com.yict.framemax.web.permission.Access;
import cn.com.yict.framemax.web.permission.Permission;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Model-Link Api 接口服务
*
* @author alex.yao
* @date 2024/12/23
*/
@Permission(Access.Anonymous)
public interface ModelLinkRest extends BaseRest {
/**
* 新建对话ID
* 有效期为7天,过期后将无法使用。
*
* @return 对话ID
*/
String conversation(@RequestBody CreateConversationDto dto, HttpServletRequest httpServletRequest) throws Exception;
/**
* API-对话
*
* @param dto
* @param httpServletRequest
* @param httpServletResponse
*/
void completions(@RequestBody CompletionsDto dto, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception;
/**
* 上传文件
* 该接口用于在对话中上传文件供大模型处理,文件的有效期为7天并且不超过对话的有效期。一次只能上传一个文件。
*
* @param agentId Agent ID
* @param conversationId 对话ID
* @param file 文件
* @param httpServletRequest
* @return 文件ID
*/
String uploadFile(@RequestParam String agentId, @RequestParam String conversationId, @RequestParam MultipartFile file, HttpServletRequest httpServletRequest) throws Exception;
}
package cn.com.poc.expose.rest.impl;
import cn.com.poc.common.utils.Assert;
import cn.com.poc.common.utils.StringUtils;
import cn.com.poc.expose.aggregate.AgentApplicationApiService;
import cn.com.poc.expose.dto.model_link_api.CompletionsDto;
import cn.com.poc.expose.dto.model_link_api.CreateConversationDto;
import cn.com.poc.expose.rest.ModelLinkRest;
import cn.com.yict.framemax.core.exception.BusinessException;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
/**
* @author alex.yao
* @date 2024/12/23
*/
@Component
public class ModelLinkRestImpl implements ModelLinkRest {
@Resource
private AgentApplicationApiService agentApplicationApiService;
@Override
public String conversation(CreateConversationDto dto, HttpServletRequest httpServletRequest) throws Exception {
Assert.notBlank(dto.getAgentId(), "Agent Id Can't Null");
String apiKey = httpServletRequest.getHeader("x-api-key");
String apiSecret = httpServletRequest.getHeader("x-api-secret");
if (apiKey == null || apiSecret == null) {
throw new BusinessException("Api Key or Api Secret Can't Null");
}
return agentApplicationApiService.conversation(apiKey, apiSecret, dto.getAgentId());
}
@Override
public void completions(CompletionsDto dto, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
String apiKey = httpServletRequest.getHeader("x-api-key");
String apiSecret = httpServletRequest.getHeader("x-api-secret");
if (apiKey == null || apiSecret == null) {
throw new BusinessException("Api Key or Api Secret Can't Null");
}
List<String> fileIds = new ArrayList<>();
if (StringUtils.isNotBlank(dto.getFileId())) {
fileIds.add(dto.getFileId());
}
agentApplicationApiService.completions(apiKey, apiSecret, dto.getConversationId(), fileIds, dto.getQuery(), dto.getStream(), httpServletResponse);
}
@Override
public String uploadFile(String agentId, String conversationId, MultipartFile file, HttpServletRequest httpServletRequest) throws Exception {
String apiKey = httpServletRequest.getHeader("x-api-key");
String apiSecret = httpServletRequest.getHeader("x-api-secret");
if (apiKey == null || apiSecret == null) {
throw new BusinessException("Api Key or Api Secret Can't Null");
}
return agentApplicationApiService.uploadFile(apiKey, apiSecret, agentId, conversationId, file);
}
}
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