From 79a094be02072c0a0f650d5162f2bcb55593ac31 Mon Sep 17 00:00:00 2001 From: cherishsince Date: Wed, 5 Jun 2024 10:27:08 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E5=A2=9E=E5=8A=A0=E3=80=91MidjourneyP?= =?UTF-8?q?roxy=20=E5=A2=9E=E5=8A=A0=20action=20=E6=93=8D=E4=BD=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ai/client/MidjourneyProxyClient.java | 37 +++++++++++++++---- .../ai/client/vo/MidjourneyActionReqVO.java | 33 +++++++++++++++++ 2 files changed, 62 insertions(+), 8 deletions(-) create mode 100644 yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/client/vo/MidjourneyActionReqVO.java diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/client/MidjourneyProxyClient.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/client/MidjourneyProxyClient.java index b68310700..6230aa5b7 100644 --- a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/client/MidjourneyProxyClient.java +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/client/MidjourneyProxyClient.java @@ -1,6 +1,7 @@ package cn.iocoder.yudao.module.ai.client; import cn.iocoder.yudao.framework.common.util.json.JsonUtils; +import cn.iocoder.yudao.module.ai.client.vo.MidjourneyActionReqVO; import cn.iocoder.yudao.module.ai.client.vo.MidjourneyImagineReqVO; import cn.iocoder.yudao.module.ai.client.vo.MidjourneySubmitRespVO; import jakarta.validation.constraints.NotNull; @@ -23,9 +24,12 @@ import org.springframework.web.client.RestTemplate; public class MidjourneyProxyClient { private static final String URI_IMAGINE = "/submit/imagine"; + private static final String URI_ACTON = "/submit/action"; @Value("${ai.midjourney-proxy.url:http://127.0.0.1:8080/mj}") private String url; + @Value("${ai.midjourney-proxy.key}") + private String key; @Autowired private RestTemplate restTemplate; @@ -37,15 +41,32 @@ public class MidjourneyProxyClient { * @return */ public MidjourneySubmitRespVO imagine(@Validated @NotNull MidjourneyImagineReqVO imagineReqVO) { - // 创建 HttpHeaders 对象 - HttpHeaders headers = new HttpHeaders(); - headers.setContentType(MediaType.APPLICATION_JSON); - headers.set("Authorization", "Bearer sk-c3qxUCVKsPfdQiYU8440E3Fc8dE5424d9cB124A4Ee2489E3"); - // 创建 HttpEntity 对象,将 HttpHeaders 和请求体传递给它 - HttpEntity requestEntity = new HttpEntity<>(JsonUtils.toJsonString(imagineReqVO), headers); - // 发送 post 请求 - ResponseEntity response = restTemplate.exchange(url.concat(URI_IMAGINE), HttpMethod.POST, requestEntity, String.class); + // 1、发送 post 请求 + ResponseEntity response = post(URI_IMAGINE, imagineReqVO); + // 2、转换 resp return JsonUtils.parseObject(response.getBody(), MidjourneySubmitRespVO.class); } + /** + * action - 放大、缩小、U1、U2... + * + * @param actionReqVO + */ + public MidjourneySubmitRespVO action(@Validated @NotNull MidjourneyActionReqVO actionReqVO) { + // 1、发送 post 请求 + ResponseEntity response = post(URI_ACTON, actionReqVO); + // 2、转换 resp + return JsonUtils.parseObject(response.getBody(), MidjourneySubmitRespVO.class); + } + + private ResponseEntity post(String uri, Object body) { + // 1、创建 HttpHeaders 对象 + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + headers.set("Authorization", "Bearer ".concat(key)); + // 2、创建 HttpEntity 对象,将 HttpHeaders 和请求体传递给它 + HttpEntity requestEntity = new HttpEntity<>(JsonUtils.toJsonString(body), headers); + // 3、发送 post 请求 + return restTemplate.exchange(url.concat(uri), HttpMethod.POST, requestEntity, String.class); + } } diff --git a/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/client/vo/MidjourneyActionReqVO.java b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/client/vo/MidjourneyActionReqVO.java new file mode 100644 index 000000000..a5e3e2a47 --- /dev/null +++ b/yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/client/vo/MidjourneyActionReqVO.java @@ -0,0 +1,33 @@ +package cn.iocoder.yudao.module.ai.client.vo; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotNull; +import lombok.Data; + +import java.util.List; + +/** + * Midjourney:action 请求 + * + * @author fansili + * @time 2024/5/30 14:02 + * @since 1.0 + */ +@Data +public class MidjourneyActionReqVO { + + @Schema(description = "操作按钮id", required = true) + @NotNull(message = "customId 不能为空!") + private String customId; + + @Schema(description = "操作按钮id", required = true) + @NotNull(message = "customId 不能为空!") + private String taskId; + + @Schema(description = "通知地址", required = false) + @NotNull(message = "回调地址不能为空!") + private String notifyHook; + + @Schema(description = "自定义参数", required = false) + private String state; +}