Commit 98b4fb7ff0842f9292bc9e05b951f7d590722c3a
1 parent
2f6b9b2e
增加 imagine 命令发送
Showing
8 changed files
with
214 additions
and
8 deletions
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/MidjourneyClient.java
0 → 100644
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/MidjourneyConfig.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.ai.midjourney; | |
| 2 | + | |
| 3 | +import lombok.Data; | |
| 4 | +import lombok.experimental.Accessors; | |
| 5 | + | |
| 6 | +import java.util.Map; | |
| 7 | + | |
| 8 | +/** | |
| 9 | + * Midjourney 配置 | |
| 10 | + * | |
| 11 | + * author: fansili | |
| 12 | + * time: 2024/4/3 17:10 | |
| 13 | + */ | |
| 14 | +@Data | |
| 15 | +@Accessors(chain = true) | |
| 16 | +public class MidjourneyConfig { | |
| 17 | + | |
| 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 | + /** | |
| 29 | + * token信息 | |
| 30 | + * | |
| 31 | + * tip: 登录discard F12找 messages 接口 | |
| 32 | + */ | |
| 33 | + private String token; | |
| 34 | + /** | |
| 35 | + * 服务器id | |
| 36 | + */ | |
| 37 | + private String guildId; | |
| 38 | + /** | |
| 39 | + * 频道id | |
| 40 | + */ | |
| 41 | + private String channelId; | |
| 42 | + | |
| 43 | + // | |
| 44 | + // api 接口 | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * 服务地址 | |
| 48 | + */ | |
| 49 | + private String serverUrl = "https://discord.com/"; | |
| 50 | + /** | |
| 51 | + * 发送命令 | |
| 52 | + */ | |
| 53 | + private String apiInteractions = "api/v9/interactions"; | |
| 54 | + | |
| 55 | + | |
| 56 | + // | |
| 57 | + // 浏览器配置 | |
| 58 | + | |
| 59 | + private String userAage = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"; | |
| 60 | + | |
| 61 | + | |
| 62 | + // | |
| 63 | + // 请求 json 文件 | |
| 64 | + | |
| 65 | + | |
| 66 | + private Map<String, String> requestTemplates; | |
| 67 | +} | ... | ... |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/demo/MjCommandEnum.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/MidjourneyInteractionsEnum.java
| 1 | -package cn.iocoder.yudao.framework.ai.midjourney.demo; | |
| 1 | +package cn.iocoder.yudao.framework.ai.midjourney; | |
| 2 | 2 | |
| 3 | 3 | import lombok.Getter; |
| 4 | 4 | |
| ... | ... | @@ -6,7 +6,7 @@ import lombok.Getter; |
| 6 | 6 | * MJ 命令 |
| 7 | 7 | */ |
| 8 | 8 | @Getter |
| 9 | -public enum MjCommandEnum { | |
| 9 | +public enum MidjourneyInteractionsEnum { | |
| 10 | 10 | |
| 11 | 11 | IMAGINE("imagine", "生成图片"), |
| 12 | 12 | DESCRIBE("describe", "生成描述"), |
| ... | ... | @@ -17,7 +17,7 @@ public enum MjCommandEnum { |
| 17 | 17 | |
| 18 | 18 | ; |
| 19 | 19 | |
| 20 | - MjCommandEnum(String value, String message) { | |
| 20 | + MidjourneyInteractionsEnum(String value, String message) { | |
| 21 | 21 | this.value =value; |
| 22 | 22 | this.message =message; |
| 23 | 23 | } | ... | ... |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/demo/MjExecute.java
| 1 | 1 | package cn.iocoder.yudao.framework.ai.midjourney.demo; |
| 2 | 2 | |
| 3 | +import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyInteractionsEnum; | |
| 4 | + | |
| 3 | 5 | /** |
| 4 | 6 | * mj 命令 |
| 5 | 7 | */ |
| ... | ... | @@ -12,6 +14,6 @@ public interface MjExecute { |
| 12 | 14 | * @param prompt |
| 13 | 15 | * @return |
| 14 | 16 | */ |
| 15 | - boolean execute(MjCommandEnum mjCommand, String prompt); | |
| 17 | + boolean execute(MidjourneyInteractionsEnum mjCommand, String prompt); | |
| 16 | 18 | |
| 17 | 19 | } | ... | ... |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/demo/MjHttpExecute.java
| 1 | 1 | package cn.iocoder.yudao.framework.ai.midjourney.demo; |
| 2 | 2 | |
| 3 | 3 | import cn.hutool.core.io.FileUtil; |
| 4 | +import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyInteractionsEnum; | |
| 4 | 5 | import org.springframework.http.HttpEntity; |
| 5 | 6 | import org.springframework.http.HttpHeaders; |
| 6 | 7 | import org.springframework.http.MediaType; |
| ... | ... | @@ -13,7 +14,7 @@ public class MjHttpExecute implements MjExecute { |
| 13 | 14 | private static final String URL = "https://discord.com/"; |
| 14 | 15 | |
| 15 | 16 | @Override |
| 16 | - public boolean execute(MjCommandEnum mjCommand, String prompt) { | |
| 17 | + public boolean execute(MidjourneyInteractionsEnum mjCommand, String prompt) { | |
| 17 | 18 | // 发送的 uri |
| 18 | 19 | String uri = "api/v9/interactions"; |
| 19 | 20 | // restTemplate 发送post请求 |
| ... | ... | @@ -44,9 +45,6 @@ public class MjHttpExecute implements MjExecute { |
| 44 | 45 | String res = restTemplate.postForObject(URL + uri, requestEntity, String.class); |
| 45 | 46 | System.err.println("11"); |
| 46 | 47 | |
| 47 | - // | |
| 48 | -// MjHttpExecute mjHttpExecute = new MjHttpExecute(); | |
| 49 | -// mjHttpExecute.execute(null, "童话世界应该是什么样?"); | |
| 50 | 48 | |
| 51 | 49 | } |
| 52 | 50 | } | ... | ... |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/interactions/MjClient.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.ai.midjourney.interactions; | |
| 2 | + | |
| 3 | +import org.springframework.http.HttpEntity; | |
| 4 | +import org.springframework.http.HttpHeaders; | |
| 5 | +import org.springframework.http.MediaType; | |
| 6 | +import org.springframework.web.client.RestTemplate; | |
| 7 | + | |
| 8 | +import java.util.Map; | |
| 9 | + | |
| 10 | +/** | |
| 11 | + * mj client | |
| 12 | + * <p> | |
| 13 | + * author: fansili | |
| 14 | + * time: 2024/4/3 17:37 | |
| 15 | + */ | |
| 16 | +public class MjClient { | |
| 17 | + | |
| 18 | + private static RestTemplate restTemplate = new RestTemplate(); | |
| 19 | + private static HttpHeaders headers = new HttpHeaders(); | |
| 20 | + | |
| 21 | + static { | |
| 22 | + headers.setContentType(MediaType.APPLICATION_JSON); // 设置内容类型为JSON | |
| 23 | +// headers.set("Authorization", token); // 如果需要,设置认证信息(例如JWT令牌) | |
| 24 | + headers.set("Referer", "https://discord.com/channels/1221445697157468200/1221445862962630706"); // 如果需要,设置认证信息(例如JWT令牌) | |
| 25 | + headers.set("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36"); // 如果需要,设置认证信息(例如JWT令牌) | |
| 26 | + headers.set("Cookie", "__dcfduid=6ca536c0e3fa11eeb7cbe34c31b49caf; __sdcfduid=6ca536c1e3fa11eeb7cbe34c31b49caf52cce5ffd8983d2a052cf6aba75fe5fe566f2c265902e283ce30dbf98b8c9c93; _gcl_au=1.1.245923998.1710853617; _ga=GA1.1.111061823.1710853617; __cfruid=6385bb3f48345a006b25992db7dcf984e395736d-1712124666; _cfuvid=O09la5ms0ypNptiG0iD8A6BKWlTxz1LG0WR7qRStD7o-1712124666575-0.0.1.1-604800000; locale=zh-CN; cf_clearance=l_YGod1_SUtYxpDVeZXiX7DLLPl1DYrquZe8WVltvYs-1712124668-1.0.1.1-Hl2.fToel23EpF2HCu9J20rB4D7OhhCzoajPSdo.9Up.wPxhvq22DP9RHzEBKuIUlKyH.kJLxXJfAt2N.LD5WQ; OptanonConsent=isIABGlobal=false&datestamp=Wed+Apr+03+2024+14%3A11%3A15+GMT%2B0800+(%E4%B8%AD%E5%9B%BD%E6%A0%87%E5%87%86%E6%97%B6%E9%97%B4)&version=6.33.0&hosts=&landingPath=https%3A%2F%2Fdiscord.com%2F&groups=C0001%3A1%2CC0002%3A1%2CC0003%3A1; _ga_Q149DFWHT7=GS1.1.1712124668.4.1.1712124679.0.0.0"); // 如果需要,设置认证信息(例如JWT令牌) | |
| 27 | + } | |
| 28 | + | |
| 29 | + public static String post(String url, String token, String body) { | |
| 30 | + // 设置token | |
| 31 | + headers.set("Authorization", token); | |
| 32 | + // 封装请求体和头部信息 | |
| 33 | + HttpEntity<String> requestEntity = new HttpEntity<>(body, headers); | |
| 34 | + // 发送请求 | |
| 35 | + String result = restTemplate.postForObject(url, requestEntity, String.class); | |
| 36 | + return result; | |
| 37 | + } | |
| 38 | + | |
| 39 | + | |
| 40 | + public static String setParams(String requestTemplate, Map<String, String> requestParams) { | |
| 41 | + for (Map.Entry<String, String> entry : requestParams.entrySet()) { | |
| 42 | + requestTemplate = requestTemplate.replace("$".concat(entry.getKey()), entry.getValue()); | |
| 43 | + } | |
| 44 | + return requestTemplate; | |
| 45 | + } | |
| 46 | +} | ... | ... |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/interactions/MjImagineInteractions.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.ai.midjourney.interactions; | |
| 2 | + | |
| 3 | +import cn.hutool.core.util.IdUtil; | |
| 4 | +import cn.hutool.core.util.StrUtil; | |
| 5 | +import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyConfig; | |
| 6 | +import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyInteractionsEnum; | |
| 7 | +import com.google.common.collect.ImmutableList; | |
| 8 | +import com.google.common.collect.Maps; | |
| 9 | +import lombok.extern.slf4j.Slf4j; | |
| 10 | +import org.checkerframework.checker.nullness.qual.Nullable; | |
| 11 | + | |
| 12 | +import java.util.HashMap; | |
| 13 | +import java.util.List; | |
| 14 | +import java.util.UUID; | |
| 15 | + | |
| 16 | +/** | |
| 17 | + * | |
| 18 | + * | |
| 19 | + * author: fansili | |
| 20 | + * time: 2024/4/3 17:36 | |
| 21 | + */ | |
| 22 | +@Slf4j | |
| 23 | +public class MjImagineInteractions implements MjInteractions { | |
| 24 | + | |
| 25 | + private MidjourneyConfig midjourneyConfig; | |
| 26 | + | |
| 27 | + public MjImagineInteractions(MidjourneyConfig midjourneyConfig) { | |
| 28 | + this.midjourneyConfig = midjourneyConfig; | |
| 29 | + } | |
| 30 | + | |
| 31 | + @Override | |
| 32 | + public List<MidjourneyInteractionsEnum> supperInteractions() { | |
| 33 | + return null; | |
| 34 | + } | |
| 35 | + | |
| 36 | + @Override | |
| 37 | + public Boolean execute(String prompt) { | |
| 38 | + String url = midjourneyConfig.getServerUrl().concat(midjourneyConfig.getApiInteractions()); | |
| 39 | + // 获取请求模板 | |
| 40 | + String requestTemplate = midjourneyConfig.getRequestTemplates().get("imagine"); | |
| 41 | + // 设置参数 | |
| 42 | + HashMap<String, String> requestParams = Maps.newHashMap(); | |
| 43 | + requestParams.put("guild_id", midjourneyConfig.getGuildId()); | |
| 44 | + requestParams.put("channel_id", midjourneyConfig.getChannelId()); | |
| 45 | + requestParams.put("session_id", UUID.randomUUID().toString().replaceAll("-", "")); | |
| 46 | + requestParams.put("nonce", String.valueOf(IdUtil.getSnowflakeNextId())); | |
| 47 | + requestParams.put("prompt", prompt); | |
| 48 | + // 设置参数 | |
| 49 | + String requestBody = MjClient.setParams(requestTemplate, requestParams); | |
| 50 | + // 发送请求 | |
| 51 | + String res = MjClient.post(url, midjourneyConfig.getToken(), requestBody); | |
| 52 | + // | |
| 53 | + System.err.println(res); | |
| 54 | + log.info(res); | |
| 55 | + // 这个 res 只要不返回值,就是成功! | |
| 56 | + return StrUtil.isBlank(res); | |
| 57 | + } | |
| 58 | +} | ... | ... |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/interactions/MjInteractions.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.ai.midjourney.interactions; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyInteractionsEnum; | |
| 4 | + | |
| 5 | +import java.util.List; | |
| 6 | + | |
| 7 | +/** | |
| 8 | + * mj Interactions | |
| 9 | + * | |
| 10 | + * author: fansili | |
| 11 | + * time: 2024/4/3 17:22 | |
| 12 | + */ | |
| 13 | +public interface MjInteractions { | |
| 14 | + | |
| 15 | + List<MidjourneyInteractionsEnum> supperInteractions(); | |
| 16 | + | |
| 17 | + Boolean execute(String prompt); | |
| 18 | +} | ... | ... |