|
@@ -1,20 +1,16 @@
|
|
|
-package cn.iocoder.yudao.framework.ai.midjourney.interactions;
|
|
|
+package cn.iocoder.yudao.framework.ai.midjourney.api;
|
|
|
|
|
|
-import cn.hutool.core.util.IdUtil;
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyConfig;
|
|
|
-import cn.iocoder.yudao.framework.ai.midjourney.constants.MjConstants;
|
|
|
-import cn.iocoder.yudao.framework.ai.midjourney.util.MjUtil;
|
|
|
-import cn.iocoder.yudao.framework.ai.midjourney.vo.Attachments;
|
|
|
-import cn.iocoder.yudao.framework.ai.midjourney.vo.Describe;
|
|
|
-import cn.iocoder.yudao.framework.ai.midjourney.vo.ReRoll;
|
|
|
-import cn.iocoder.yudao.framework.ai.midjourney.vo.UploadAttachmentsRes;
|
|
|
+import cn.iocoder.yudao.framework.ai.midjourney.util.MidjourneyUtil;
|
|
|
+import cn.iocoder.yudao.framework.ai.midjourney.api.req.AttachmentsReq;
|
|
|
+import cn.iocoder.yudao.framework.ai.midjourney.api.req.DescribeReq;
|
|
|
+import cn.iocoder.yudao.framework.ai.midjourney.api.req.ReRollReq;
|
|
|
+import cn.iocoder.yudao.framework.ai.midjourney.api.res.UploadAttachmentsRes;
|
|
|
import com.alibaba.fastjson.JSON;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.google.common.collect.Lists;
|
|
|
-import com.google.common.collect.Maps;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.jetbrains.annotations.NotNull;
|
|
|
import org.springframework.core.io.FileSystemResource;
|
|
|
import org.springframework.http.*;
|
|
|
import org.springframework.util.LinkedMultiValueMap;
|
|
@@ -24,6 +20,7 @@ import org.springframework.web.client.RestTemplate;
|
|
|
import java.io.IOException;
|
|
|
import java.util.HashMap;
|
|
|
|
|
|
+// TODO @fansili:按照 spring ai 的封装习惯,这个类是不是 MidjourneyApi
|
|
|
/**
|
|
|
* 图片生成
|
|
|
*
|
|
@@ -31,17 +28,13 @@ import java.util.HashMap;
|
|
|
* time: 2024/4/3 17:36
|
|
|
*/
|
|
|
@Slf4j
|
|
|
-public class MjInteractions {
|
|
|
-
|
|
|
+public class MidjourneyInteractionsApi extends MidjourneyInteractions {
|
|
|
|
|
|
private final String url;
|
|
|
- private final MidjourneyConfig midjourneyConfig;
|
|
|
- private final RestTemplate restTemplate = new RestTemplate();
|
|
|
- private static final String HEADER_REFERER = "https://discord.com/channels/%s/%s";
|
|
|
-
|
|
|
+ private final RestTemplate restTemplate = new RestTemplate(); // TODO @fansili:优先级低:后续搞到统一的管理
|
|
|
|
|
|
- public MjInteractions(MidjourneyConfig midjourneyConfig) {
|
|
|
- this.midjourneyConfig = midjourneyConfig;
|
|
|
+ public MidjourneyInteractionsApi(MidjourneyConfig midjourneyConfig) {
|
|
|
+ super(midjourneyConfig);
|
|
|
this.url = midjourneyConfig.getServerUrl().concat(midjourneyConfig.getApiInteractions());
|
|
|
}
|
|
|
|
|
@@ -49,45 +42,38 @@ public class MjInteractions {
|
|
|
// 获取请求模板
|
|
|
String requestTemplate = midjourneyConfig.getRequestTemplates().get("imagine");
|
|
|
// 设置参数
|
|
|
- HashMap<String, String> requestParams = Maps.newHashMap();
|
|
|
- requestParams.put("guild_id", midjourneyConfig.getGuildId());
|
|
|
- requestParams.put("channel_id", midjourneyConfig.getChannelId());
|
|
|
- requestParams.put("session_id", midjourneyConfig.getSessionId());
|
|
|
- requestParams.put("nonce", String.valueOf(IdUtil.getSnowflakeNextId()));
|
|
|
+ HashMap<String, String> requestParams = getDefaultParams();
|
|
|
requestParams.put("prompt", prompt);
|
|
|
// 解析 template 参数占位符
|
|
|
- String requestBody = MjUtil.parseTemplate(requestTemplate, requestParams);
|
|
|
+ String requestBody = MidjourneyUtil.parseTemplate(requestTemplate, requestParams);
|
|
|
// 获取 header
|
|
|
- HttpHeaders httpHeaders = getHttpHeaders();
|
|
|
+ HttpHeaders httpHeaders = getHeadersOfAppJson();
|
|
|
// 发送请求
|
|
|
HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, httpHeaders);
|
|
|
String res = restTemplate.postForObject(url, requestEntity, String.class);
|
|
|
// 这个 res 只要不返回值,就是成功!
|
|
|
- boolean isSuccess = StrUtil.isBlank(res);
|
|
|
- if (isSuccess) {
|
|
|
+ // TODO @fansili:可以直接 if (StrUtil.isBlank(res))
|
|
|
+ if (StrUtil.isBlank(res)) {
|
|
|
return true;
|
|
|
+ } else {
|
|
|
+ log.error("请求失败! 请求参数:{} 返回结果! {}", requestBody, res);
|
|
|
+ return false;
|
|
|
}
|
|
|
- log.error("请求失败! 请求参数:{} 返回结果! {}", requestBody, res);
|
|
|
- return isSuccess;
|
|
|
}
|
|
|
+ // TODO done @fansili:方法和方法之间,空一行哈;
|
|
|
|
|
|
|
|
|
-
|
|
|
- public Boolean reRoll(ReRoll reRoll) {
|
|
|
+ public Boolean reRoll(ReRollReq reRoll) {
|
|
|
// 获取请求模板
|
|
|
String requestTemplate = midjourneyConfig.getRequestTemplates().get("reroll");
|
|
|
// 设置参数
|
|
|
- HashMap<String, String> requestParams = Maps.newHashMap();
|
|
|
- requestParams.put("guild_id", midjourneyConfig.getGuildId());
|
|
|
- requestParams.put("channel_id", midjourneyConfig.getChannelId());
|
|
|
- requestParams.put("session_id", midjourneyConfig.getSessionId());
|
|
|
- requestParams.put("nonce", String.valueOf(IdUtil.getSnowflakeNextId()));
|
|
|
+ HashMap<String, String> requestParams = getDefaultParams();
|
|
|
requestParams.put("custom_id", reRoll.getCustomId());
|
|
|
requestParams.put("message_id", reRoll.getMessageId());
|
|
|
// 获取 header
|
|
|
- HttpHeaders httpHeaders = getHttpHeaders();
|
|
|
+ HttpHeaders httpHeaders = getHeadersOfAppJson();
|
|
|
// 设置参数
|
|
|
- String requestBody = MjUtil.parseTemplate(requestTemplate, requestParams);
|
|
|
+ String requestBody = MidjourneyUtil.parseTemplate(requestTemplate, requestParams);
|
|
|
// 发送请求
|
|
|
HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, httpHeaders);
|
|
|
String res = restTemplate.postForObject(url, requestEntity, String.class);
|
|
@@ -100,12 +86,13 @@ public class MjInteractions {
|
|
|
return isSuccess;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- public UploadAttachmentsRes uploadAttachments(Attachments attachments) {
|
|
|
+ // TODO @fansili:搞成私有方法,可能会好点;
|
|
|
+ public UploadAttachmentsRes uploadAttachments(AttachmentsReq attachments) {
|
|
|
// file
|
|
|
JSONObject fileObj = new JSONObject();
|
|
|
fileObj.put("id", "0");
|
|
|
fileObj.put("filename", attachments.getFileSystemResource().getFilename());
|
|
|
+ // TODO @fansili:这块用 lombok 哪个异常处理,简化下代码;
|
|
|
try {
|
|
|
fileObj.put("file_size", attachments.getFileSystemResource().contentLength());
|
|
|
} catch (IOException e) {
|
|
@@ -115,12 +102,7 @@ public class MjInteractions {
|
|
|
MultiValueMap<String, Object> multipartRequest = new LinkedMultiValueMap<>();
|
|
|
multipartRequest.put("files", Lists.newArrayList(fileObj));
|
|
|
// 设置header值
|
|
|
- HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
- httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
|
|
- httpHeaders.set("Authorization", midjourneyConfig.getToken());
|
|
|
- httpHeaders.set("User-Agent", midjourneyConfig.getUserAage());
|
|
|
- httpHeaders.set("Cookie", MjConstants.HTTP_COOKIE);
|
|
|
- httpHeaders.set("Referer", String.format(HEADER_REFERER, midjourneyConfig.getGuildId(), midjourneyConfig.getChannelId()));
|
|
|
+ HttpHeaders httpHeaders = getHeadersOfAppJson();
|
|
|
// 创建HttpEntity对象,包含表单数据和头部信息
|
|
|
HttpEntity<MultiValueMap<String, Object>> multiValueMapHttpEntity = new HttpEntity<>(multipartRequest, httpHeaders);
|
|
|
// 发送POST请求并接收响应
|
|
@@ -128,38 +110,26 @@ public class MjInteractions {
|
|
|
String response = restTemplate.postForObject(midjourneyConfig.getServerUrl().concat(uri), multiValueMapHttpEntity, String.class);
|
|
|
UploadAttachmentsRes uploadAttachmentsRes = JSON.parseObject(response, UploadAttachmentsRes.class);
|
|
|
|
|
|
-
|
|
|
//
|
|
|
// 上传文件
|
|
|
String uploadUrl = uploadAttachmentsRes.getAttachments().getFirst().getUploadUrl();
|
|
|
- String uploadAttachmentsUrl = midjourneyConfig.getApiAttachmentsUpload().concat(uploadUrl);
|
|
|
httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA);
|
|
|
HttpEntity<FileSystemResource> fileSystemResourceHttpEntity = new HttpEntity<>(attachments.getFileSystemResource(), httpHeaders);
|
|
|
ResponseEntity<String> exchange = restTemplate.exchange(uploadUrl, HttpMethod.PUT, fileSystemResourceHttpEntity, String.class);
|
|
|
String uploadRes = exchange.getBody();
|
|
|
-
|
|
|
return uploadAttachmentsRes;
|
|
|
}
|
|
|
|
|
|
- public Boolean describe(Describe describe) {
|
|
|
+ public Boolean describe(DescribeReq describe) {
|
|
|
// 获取请求模板
|
|
|
String requestTemplate = midjourneyConfig.getRequestTemplates().get("describe");
|
|
|
// 设置参数
|
|
|
- HashMap<String, String> requestParams = Maps.newHashMap();
|
|
|
- requestParams.put("guild_id", midjourneyConfig.getGuildId());
|
|
|
- requestParams.put("channel_id", midjourneyConfig.getChannelId());
|
|
|
- requestParams.put("session_id", midjourneyConfig.getSessionId());
|
|
|
- requestParams.put("nonce", String.valueOf(IdUtil.getSnowflakeNextId()));
|
|
|
+ HashMap<String, String> requestParams = getDefaultParams();
|
|
|
requestParams.put("file_name", describe.getFileName());
|
|
|
requestParams.put("final_file_name", describe.getFinalFileName());
|
|
|
// 设置 header
|
|
|
- HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
- httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); // 设置内容类型为JSON
|
|
|
- httpHeaders.set("Authorization", midjourneyConfig.getToken());
|
|
|
- httpHeaders.set("User-Agent", midjourneyConfig.getUserAage());
|
|
|
- httpHeaders.set("Cookie", MjConstants.HTTP_COOKIE);
|
|
|
- httpHeaders.set("Referer", String.format(HEADER_REFERER, midjourneyConfig.getGuildId(), midjourneyConfig.getChannelId()));
|
|
|
- String requestBody = MjUtil.parseTemplate(requestTemplate, requestParams);
|
|
|
+ HttpHeaders httpHeaders = getHeadersOfFormData();
|
|
|
+ String requestBody = MidjourneyUtil.parseTemplate(requestTemplate, requestParams);
|
|
|
// 创建表单数据
|
|
|
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
|
|
|
formData.add("payload_json", requestBody);
|
|
@@ -175,14 +145,4 @@ public class MjInteractions {
|
|
|
return isSuccess;
|
|
|
}
|
|
|
|
|
|
- @NotNull
|
|
|
- private HttpHeaders getHttpHeaders() {
|
|
|
- HttpHeaders httpHeaders = new HttpHeaders();
|
|
|
- httpHeaders.setContentType(MediaType.APPLICATION_JSON); // 设置内容类型为JSON
|
|
|
- httpHeaders.set("Authorization", midjourneyConfig.getToken());
|
|
|
- httpHeaders.set("User-Agent", midjourneyConfig.getUserAage());
|
|
|
- httpHeaders.set("Cookie", MjConstants.HTTP_COOKIE);
|
|
|
- httpHeaders.set("Referer", String.format(HEADER_REFERER, midjourneyConfig.getGuildId(), midjourneyConfig.getChannelId()));
|
|
|
- return httpHeaders;
|
|
|
- }
|
|
|
}
|