Commit b1158fb1a7e26985ab5041a84340a576915f1cfe
1 parent
f2b9c148
增加图片选择,和图片放大相关操作
Showing
6 changed files
with
82 additions
and
15 deletions
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/MidjourneyConfig.java
| ... | ... | @@ -4,6 +4,7 @@ import lombok.Data; |
| 4 | 4 | import lombok.experimental.Accessors; |
| 5 | 5 | |
| 6 | 6 | import java.util.Map; |
| 7 | +import java.util.UUID; | |
| 7 | 8 | |
| 8 | 9 | /** |
| 9 | 10 | * Midjourney 配置 |
| ... | ... | @@ -15,16 +16,6 @@ import java.util.Map; |
| 15 | 16 | @Accessors(chain = true) |
| 16 | 17 | public class MidjourneyConfig { |
| 17 | 18 | |
| 18 | - public MidjourneyConfig(String token, String guildId, String channelId, Map<String, String> requestTemplates) { | |
| 19 | - this.token = token; | |
| 20 | - this.guildId = guildId; | |
| 21 | - this.channelId = channelId; | |
| 22 | - this.serverUrl = serverUrl; | |
| 23 | - this.apiInteractions = apiInteractions; | |
| 24 | - this.userAage = userAage; | |
| 25 | - this.requestTemplates = requestTemplates; | |
| 26 | - } | |
| 27 | - | |
| 28 | 19 | /** |
| 29 | 20 | * token信息 |
| 30 | 21 | * |
| ... | ... | @@ -64,4 +55,23 @@ public class MidjourneyConfig { |
| 64 | 55 | |
| 65 | 56 | |
| 66 | 57 | private Map<String, String> requestTemplates; |
| 58 | + | |
| 59 | + // | |
| 60 | + // | |
| 61 | + | |
| 62 | + private String sessionId; | |
| 63 | + | |
| 64 | + public MidjourneyConfig(String token, String guildId, String channelId, Map<String, String> requestTemplates) { | |
| 65 | + this.token = token; | |
| 66 | + this.guildId = guildId; | |
| 67 | + this.channelId = channelId; | |
| 68 | + this.serverUrl = serverUrl; | |
| 69 | + this.apiInteractions = apiInteractions; | |
| 70 | + this.userAage = userAage; | |
| 71 | + this.requestTemplates = requestTemplates; | |
| 72 | + | |
| 73 | + // 生成 session id | |
| 74 | + sessionId = UUID.randomUUID().toString().replaceAll("-", ""); | |
| 75 | + } | |
| 76 | + | |
| 67 | 77 | } | ... | ... |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/MjMessage.java
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/interactions/MjClient.java
| ... | ... | @@ -32,8 +32,7 @@ public class MjClient { |
| 32 | 32 | // 封装请求体和头部信息 |
| 33 | 33 | HttpEntity<String> requestEntity = new HttpEntity<>(body, headers); |
| 34 | 34 | // 发送请求 |
| 35 | - String result = restTemplate.postForObject(url, requestEntity, String.class); | |
| 36 | - return result; | |
| 35 | + return restTemplate.postForObject(url, requestEntity, String.class); | |
| 37 | 36 | } |
| 38 | 37 | |
| 39 | 38 | ... | ... |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/interactions/MjImagineInteractions.java
| ... | ... | @@ -4,6 +4,8 @@ import cn.hutool.core.util.IdUtil; |
| 4 | 4 | import cn.hutool.core.util.StrUtil; |
| 5 | 5 | import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyConfig; |
| 6 | 6 | import cn.iocoder.yudao.framework.ai.midjourney.constants.MjInteractionsEnum; |
| 7 | +import cn.iocoder.yudao.framework.ai.midjourney.vo.ReRoll; | |
| 8 | +import com.google.common.collect.Lists; | |
| 7 | 9 | import com.google.common.collect.Maps; |
| 8 | 10 | import lombok.extern.slf4j.Slf4j; |
| 9 | 11 | |
| ... | ... | @@ -12,7 +14,7 @@ import java.util.List; |
| 12 | 14 | import java.util.UUID; |
| 13 | 15 | |
| 14 | 16 | /** |
| 15 | - * | |
| 17 | + * 图片生成 | |
| 16 | 18 | * |
| 17 | 19 | * author: fansili |
| 18 | 20 | * time: 2024/4/3 17:36 |
| ... | ... | @@ -28,7 +30,7 @@ public class MjImagineInteractions implements MjInteractions { |
| 28 | 30 | |
| 29 | 31 | @Override |
| 30 | 32 | public List<MjInteractionsEnum> supperInteractions() { |
| 31 | - return null; | |
| 33 | + return Lists.newArrayList(MjInteractionsEnum.IMAGINE); | |
| 32 | 34 | } |
| 33 | 35 | |
| 34 | 36 | @Override |
| ... | ... | @@ -40,7 +42,7 @@ public class MjImagineInteractions implements MjInteractions { |
| 40 | 42 | HashMap<String, String> requestParams = Maps.newHashMap(); |
| 41 | 43 | requestParams.put("guild_id", midjourneyConfig.getGuildId()); |
| 42 | 44 | requestParams.put("channel_id", midjourneyConfig.getChannelId()); |
| 43 | - requestParams.put("session_id", UUID.randomUUID().toString().replaceAll("-", "")); | |
| 45 | + requestParams.put("session_id", midjourneyConfig.getSessionId()); | |
| 44 | 46 | requestParams.put("nonce", String.valueOf(IdUtil.getSnowflakeNextId())); |
| 45 | 47 | requestParams.put("prompt", prompt); |
| 46 | 48 | // 设置参数 |
| ... | ... | @@ -55,4 +57,29 @@ public class MjImagineInteractions implements MjInteractions { |
| 55 | 57 | log.error("请求失败! 请求参数:{} 返回结果! {}", requestBody, res); |
| 56 | 58 | return isSuccess; |
| 57 | 59 | } |
| 60 | + | |
| 61 | + public Boolean reRoll(ReRoll reRoll) { | |
| 62 | + String url = midjourneyConfig.getServerUrl().concat(midjourneyConfig.getApiInteractions()); | |
| 63 | + // 获取请求模板 | |
| 64 | + String requestTemplate = midjourneyConfig.getRequestTemplates().get("reroll"); | |
| 65 | + // 设置参数 | |
| 66 | + HashMap<String, String> requestParams = Maps.newHashMap(); | |
| 67 | + requestParams.put("guild_id", midjourneyConfig.getGuildId()); | |
| 68 | + requestParams.put("channel_id", midjourneyConfig.getChannelId()); | |
| 69 | + requestParams.put("session_id", midjourneyConfig.getSessionId()); | |
| 70 | + requestParams.put("nonce", String.valueOf(IdUtil.getSnowflakeNextId())); | |
| 71 | + requestParams.put("custom_id", reRoll.getCustomId()); | |
| 72 | + requestParams.put("message_id", reRoll.getMessageId()); | |
| 73 | + // 设置参数 | |
| 74 | + String requestBody = MjClient.setParams(requestTemplate, requestParams); | |
| 75 | + // 发送请求 | |
| 76 | + String res = MjClient.post(url, midjourneyConfig.getToken(), requestBody); | |
| 77 | + // 这个 res 只要不返回值,就是成功! | |
| 78 | + boolean isSuccess = StrUtil.isBlank(res); | |
| 79 | + if (isSuccess) { | |
| 80 | + return true; | |
| 81 | + } | |
| 82 | + log.error("请求失败! 请求参数:{} 返回结果! {}", requestBody, res); | |
| 83 | + return isSuccess; | |
| 84 | + } | |
| 58 | 85 | } | ... | ... |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/vo/ReRoll.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.ai.midjourney.vo; | |
| 2 | + | |
| 3 | +import lombok.Data; | |
| 4 | +import lombok.experimental.Accessors; | |
| 5 | + | |
| 6 | +/** | |
| 7 | + * author: fansili | |
| 8 | + * time: 2024/4/6 21:33 | |
| 9 | + */ | |
| 10 | +@Data | |
| 11 | +@Accessors(chain = true) | |
| 12 | +public class ReRoll { | |
| 13 | + | |
| 14 | + private String messageId; | |
| 15 | + private String customId; | |
| 16 | +} | ... | ... |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/resources/http-body/reroll.json
0 → 100644
| 1 | +{ | |
| 2 | + "type": 3, | |
| 3 | + "guild_id": "$guild_id", | |
| 4 | + "channel_id": "$channel_id", | |
| 5 | + "message_id": "$message_id", | |
| 6 | + "application_id": "936929561302675456", | |
| 7 | + "session_id": "$session_id", | |
| 8 | + "nonce": "$nonce", | |
| 9 | + "message_flags": 0, | |
| 10 | + "data": { | |
| 11 | + "component_type": 2, | |
| 12 | + "custom_id": "$custom_id" | |
| 13 | + } | |
| 14 | +} | |
| 0 | 15 | \ No newline at end of file | ... | ... |