Commit 2c689394 authored by alex yao's avatar alex yao

feat:Agent应用 头像生成

parent d1c508bf
......@@ -57,7 +57,7 @@ public interface AgentApplicationInfoService {
* @param agentDesc 应用描述
* @return Agent 应用图标URL
*/
String createAgentIcon(String agentTitle, String agentDesc);
String createAgentIcon(String agentTitle, String agentDesc) throws Exception;
/**
* Agent 应用标题,描述生成
......
......@@ -13,15 +13,19 @@ import cn.com.poc.agent_application.service.BizAgentApplicationPublishService;
import cn.com.poc.common.utils.BlContext;
import cn.com.poc.common.utils.JsonUtils;
import cn.com.poc.support.security.oauth.entity.UserBaseEntity;
import cn.com.poc.thirdparty.resource.demand.ai.aggregate.AICreateImageService;
import cn.com.poc.thirdparty.resource.demand.ai.aggregate.DemandKnowledgeService;
import cn.com.poc.thirdparty.resource.demand.ai.common.domain.Message;
import cn.com.poc.thirdparty.resource.demand.ai.common.domain.MultiContent;
import cn.com.poc.thirdparty.resource.demand.ai.entity.generations.BaiduAISailsText2ImageRequest;
import cn.com.poc.thirdparty.resource.demand.ai.entity.generations.BaiduAISailsText2ImageResult;
import cn.com.poc.thirdparty.resource.demand.ai.entity.largemodel.LargeModelDemandResult;
import cn.com.poc.thirdparty.resource.demand.ai.entity.largemodel.LargeModelResponse;
import cn.com.poc.thirdparty.service.LLMService;
import cn.com.yict.framemax.core.exception.BusinessException;
import cn.hutool.core.bean.BeanUtil;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
......@@ -61,6 +65,9 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
@Resource
private DemandKnowledgeService demandKnowledgeService;
@Resource
private AICreateImageService aiCreateImageService;
@Resource
private LLMService llmService;
......@@ -85,11 +92,8 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
}
@Override
public String callAgentApplication(String largeModel, String[] unitIds, String agentSystem,
String[] knowledgeIds, Integer communicationTurn, Float topP,
List<Message> messages, HttpServletResponse httpServletResponse) throws Exception {
logger.info("--------- Call Agent Application large model:{},unitIds:{},agentSystem:{},knowledgeIds:{}" +
" communicationTurn:{},topP:{},messages:{}--------------", largeModel, unitIds, agentSystem, knowledgeIds, communicationTurn, topP, messages);
public String callAgentApplication(String largeModel, String[] unitIds, String agentSystem, String[] knowledgeIds, Integer communicationTurn, Float topP, List<Message> messages, HttpServletResponse httpServletResponse) throws Exception {
logger.info("--------- Call Agent Application large model:{},unitIds:{},agentSystem:{},knowledgeIds:{}" + " communicationTurn:{},topP:{},messages:{}--------------", largeModel, unitIds, agentSystem, knowledgeIds, communicationTurn, topP, messages);
//todo 调用组件
String promptTemplate = buildDialogsPrompt(messages, agentSystem, knowledgeIds);
......@@ -214,11 +218,11 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
}
@Override
public String createAgentIcon(String agentTitle, String agentDesc) {
Assert.isTrue(StringUtils.isNotBlank(agentTitle) || StringUtils.isNotBlank(agentDesc), "请输入标题或者描述再生成应用头像");
public String createAgentIcon(String agentTitle, String agentDesc) throws Exception {
BizAgentApplicationGcConfigEntity configEntity = bizAgentApplicationGcConfigService.getByConfigCode(AgentApplicationGCConfigConstants.AGENT_ICON);
if (null == configEntity || StringUtils.isBlank(configEntity.getConfigSystem())) {
throw new BusinessException("创建[头像]配置不存在");
throw new BusinessException("创建[头像提示语]配置不存在,请联系管理员");
}
List<MultiContent> multiContents = new ArrayList<>();
......@@ -247,12 +251,35 @@ public class AgentApplicationInfoServiceImpl implements AgentApplicationInfoServ
largeModelResponse.setUser("POC-CREATE-AGENT-PROMPT");
LargeModelDemandResult largeModelDemandResult = llmService.chat(largeModelResponse);
if (largeModelDemandResult == null || !"0".equals(largeModelDemandResult.getCode())) {
logger.error("create preamble error, largeModelResponse:{}", largeModelResponse);
throw new BusinessException("创建[开场白]失败");
if (largeModelDemandResult == null || !"0".equals(largeModelDemandResult.getCode()) || StringUtils.isEmpty(largeModelDemandResult.getMessage())) {
logger.error("create agent icon prompt error, largeModelResponse:{}", largeModelResponse);
throw new BusinessException("创建[头像]失败,请稍后再试");
}
return null;
logger.info("create agent application icon prompt, result:{}", largeModelDemandResult);
String createIconPrompt = largeModelDemandResult.getMessage();
BaiduAISailsText2ImageRequest baiduAISailsText2ImageRequest = new BaiduAISailsText2ImageRequest();
baiduAISailsText2ImageRequest.setPrompt(createIconPrompt);
baiduAISailsText2ImageRequest.setNegativePrompt(null);
baiduAISailsText2ImageRequest.setSize("768x768");
baiduAISailsText2ImageRequest.setN(1);
baiduAISailsText2ImageRequest.setSteps(15);
baiduAISailsText2ImageRequest.setSamplerIndex("Euler a");
baiduAISailsText2ImageRequest.setSeed(null);
baiduAISailsText2ImageRequest.setCfgCcale(null);
baiduAISailsText2ImageRequest.setStyle("Base");
baiduAISailsText2ImageRequest.setUserId("AGENT-APPLICATION-ICON-CREATE");
baiduAISailsText2ImageRequest.setAcctId(null);
BaiduAISailsText2ImageResult baiduAISailsText2ImageResult = aiCreateImageService.executeSailsText2Image(baiduAISailsText2ImageRequest);
if (baiduAISailsText2ImageResult == null
|| CollectionUtils.isEmpty(baiduAISailsText2ImageResult.getData())
|| StringUtils.isEmpty(baiduAISailsText2ImageResult.getData().get(0).getUrl())) {
logger.error("create agent icon error, baiduAISailsText2ImageRequest:{} ", baiduAISailsText2ImageRequest);
throw new BusinessException("创建[头像]失败,请稍后再试");
}
return baiduAISailsText2ImageResult.getData().get(0).getUrl();
}
private String buildDialogsPrompt(List<Message> messages, String agentSystem, String[] knowledgeIds) {
......
......@@ -72,5 +72,8 @@ public interface AgentApplicationInfoRest extends BaseRest {
*/
String createPreamble(@RequestBody AgentApplicationGCDto dto) throws Exception;
/**
* AI创建应用头像
*/
String createAgentApplicationAvatar(@RequestBody AgentApplicationGCDto dto) throws Exception;
}
\ No newline at end of file
......@@ -160,5 +160,9 @@ public class AgentApplicationInfoRestImpl implements AgentApplicationInfoRest {
return agentApplicationInfoService.createPreamble(dto.getAgentTitle(), dto.getAgentDesc());
}
@Override
public String createAgentApplicationAvatar(AgentApplicationGCDto dto) throws Exception {
Assert.isTrue(StringUtils.isNotBlank(dto.getAgentDesc()) || StringUtils.isNotBlank(dto.getAgentTitle()), "请输入标题或者描述再生成应用头像");
return agentApplicationInfoService.createAgentIcon(dto.getAgentTitle(), dto.getAgentDesc());
}
}
\ No newline at end of file
......@@ -11,5 +11,12 @@ public interface BosConfigService extends BaseService {
String uploadFileByByteArray2Oss(byte[] decodedByte, String fileName, String fileType) throws IOException;
String upload(InputStream inputStream,String fileType,String contentType) throws IOException;
String upload(InputStream inputStream, String fileType, String contentType) throws IOException;
String uploadImageByBase64(String base64) throws IOException;
String uploadImageByUrl2Bos(String url) throws IOException;
}
package cn.com.poc.common.service.impl;
import cn.com.poc.common.service.BosConfigService;
import cn.com.poc.common.utils.Assert;
import cn.com.poc.common.utils.UUIDTool;
import cn.com.yict.framemax.core.config.Config;
import cn.com.yict.framemax.core.exception.BusinessException;
import cn.com.yict.framemax.frame.service.FmxParamConfigService;
......@@ -15,8 +17,13 @@ import org.apache.http.client.utils.DateUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Base64;
import java.util.Date;
import java.util.Optional;
......@@ -122,6 +129,35 @@ public class BosConfigServiceImpl implements BosConfigService {
}
}
@Override
public String uploadImageByBase64(String base64) throws IOException {
Assert.notBlank(base64);
byte[] decode = Base64.getDecoder().decode(base64);
String fileType = "jpeg";
return this.uploadFileByByteArray2Oss(decode, UUIDTool.getUUID(), fileType);
}
@Override
public String uploadImageByUrl2Bos(String path) throws IOException {
StringBuffer resultUrl = new StringBuffer();
String fileType = "jpeg";
URL url = new URL(path);
HttpURLConnection httpUrl = (HttpURLConnection) url.openConnection();
httpUrl.setRequestMethod("GET");
httpUrl.usingProxy();
httpUrl.connect();
InputStream inputStream = new BufferedInputStream(httpUrl.getInputStream());
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = inputStream.read(buffer))) {
output.write(buffer, 0, n);
}
byte[] content = output.toByteArray();
return this.uploadFileByByteArray2Oss(content, UUIDTool.getUUID(), fileType);
}
//返回即将创建的文件名
private String createFileName(String fileType) {
StringBuilder stringBuilder = new StringBuilder();
......
package cn.com.poc.thirdparty.resource.demand.ai.aggregate;
import cn.com.poc.thirdparty.resource.demand.ai.entity.generations.*;
import javax.servlet.http.HttpServletResponse;
/**
* @author alex.yao
* @date 2023/5/30
*
* 图片创建
**/
public interface AICreateImageService {
/**
* 调用OpenAi创建图片接口
*/
GenerationsResult invokeCreateImage(GenerationsRequest request) throws Exception;
/**
* 调用Baidu创建图片接口(普通版)
*/
GenerationsResult invokeCreateImage(BaiduGenerationsRequest request) throws Exception;
/**
* 调用Baidu创建图片接口(高级版)
*/
GenerationsResult invokeCreateImageV2(BaiduGenerationsV2Request request) throws Exception;
/**
* 调用Baidu千帆大模型文生图
* Stable-Diffusion-XL是业内知名的跨模态大模型,由StabilityAI研发并开源,有着业内领先的图像生成能力。
*/
BaiduAISailsText2ImageResult executeSailsText2Image(BaiduAISailsText2ImageRequest request) throws Exception;
/**
* 千帆大模型图生文
* Fuyu-8B是由Adept AI训练的多模态图像理解模型,可以支持多样的图像分辨率,回答图形图表有关问题。模型在视觉问答和图像描述等任务上表现良好。
*/
BaiduAISailsImage2TextResult executeSailsImage2Text(BaiduAISailsImage2TextRequest request, HttpServletResponse httpServletResponse) throws Exception;
}
......@@ -206,4 +206,20 @@ public class BaiduAISailsText2ImageRequest extends AbstractRequest<BaiduAISailsT
public String getMethod() throws Exception {
return null;
}
@Override
public String toString() {
return "BaiduAISailsText2ImageRequest{" +
"prompt='" + prompt + '\'' +
", negativePrompt='" + negativePrompt + '\'' +
", size='" + size + '\'' +
", n=" + n +
", steps=" + steps +
", samplerIndex='" + samplerIndex + '\'' +
", seed=" + seed +
", cfgCcale=" + cfgCcale +
", style='" + style + '\'' +
", userId='" + userId + '\'' +
'}';
}
}
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