Commit 8b980d1e11fc9c3c23f9502158fb56db444fe6ab

Authored by cherishsince
1 parent 21b6e9f9

增加 reRoll,图片选择、图片放大

yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/interactions/MjClient.java
... ... @@ -20,10 +20,9 @@ public class MjClient {
20 20  
21 21 static {
22 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令牌)
  23 + headers.set("Referer", "https://discord.com/channels/1221445697157468200/1221445862962630706");
  24 + 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");
  25 + 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");
27 26 }
28 27  
29 28 public static String post(String url, String token, String body) {
... ...
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/interactions/MjImagineInteractions.java deleted
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.constants.MjInteractionsEnum;
7   -import cn.iocoder.yudao.framework.ai.midjourney.vo.ReRoll;
8   -import com.google.common.collect.Lists;
9   -import com.google.common.collect.Maps;
10   -import lombok.extern.slf4j.Slf4j;
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<MjInteractionsEnum> supperInteractions() {
33   - return Lists.newArrayList(MjInteractionsEnum.IMAGINE);
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", midjourneyConfig.getSessionId());
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   - // 这个 res 只要不返回值,就是成功!
53   - boolean isSuccess = StrUtil.isBlank(res);
54   - if (isSuccess) {
55   - return true;
56   - }
57   - log.error("请求失败! 请求参数:{} 返回结果! {}", requestBody, res);
58   - return isSuccess;
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   - }
85   -}
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/interactions/MjInteractions.java
1 1 package cn.iocoder.yudao.framework.ai.midjourney.interactions;
2 2  
3   -import cn.iocoder.yudao.framework.ai.midjourney.constants.MjInteractionsEnum;
  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.vo.ReRoll;
  7 +import com.google.common.collect.Maps;
  8 +import lombok.extern.slf4j.Slf4j;
4 9  
5   -import java.util.List;
  10 +import java.util.HashMap;
6 11  
7 12 /**
8   - * mj Interactions
  13 + * 图片生成
9 14 *
10 15 * author: fansili
11   - * time: 2024/4/3 17:22
  16 + * time: 2024/4/3 17:36
12 17 */
13   -public interface MjInteractions {
  18 +@Slf4j
  19 +public class MjInteractions {
14 20  
15   - List<MjInteractionsEnum> supperInteractions();
  21 + private MidjourneyConfig midjourneyConfig;
16 22  
17   - Boolean execute(String prompt);
  23 + private String url;
  24 +
  25 + public MjInteractions(MidjourneyConfig midjourneyConfig) {
  26 + this.midjourneyConfig = midjourneyConfig;
  27 + this.url = midjourneyConfig.getServerUrl().concat(midjourneyConfig.getApiInteractions());
  28 + }
  29 +
  30 + public Boolean imagine(String prompt) {
  31 + // 获取请求模板
  32 + String requestTemplate = midjourneyConfig.getRequestTemplates().get("imagine");
  33 + // 设置参数
  34 + HashMap<String, String> requestParams = Maps.newHashMap();
  35 + requestParams.put("guild_id", midjourneyConfig.getGuildId());
  36 + requestParams.put("channel_id", midjourneyConfig.getChannelId());
  37 + requestParams.put("session_id", midjourneyConfig.getSessionId());
  38 + requestParams.put("nonce", String.valueOf(IdUtil.getSnowflakeNextId()));
  39 + requestParams.put("prompt", prompt);
  40 + // 设置参数
  41 + String requestBody = MjClient.setParams(requestTemplate, requestParams);
  42 + // 发送请求
  43 + String res = MjClient.post(url, midjourneyConfig.getToken(), requestBody);
  44 + // 这个 res 只要不返回值,就是成功!
  45 + boolean isSuccess = StrUtil.isBlank(res);
  46 + if (isSuccess) {
  47 + return true;
  48 + }
  49 + log.error("请求失败! 请求参数:{} 返回结果! {}", requestBody, res);
  50 + return isSuccess;
  51 + }
  52 +
  53 + public Boolean reRoll(ReRoll reRoll) {
  54 + // 获取请求模板
  55 + String requestTemplate = midjourneyConfig.getRequestTemplates().get("reroll");
  56 + // 设置参数
  57 + HashMap<String, String> requestParams = Maps.newHashMap();
  58 + requestParams.put("guild_id", midjourneyConfig.getGuildId());
  59 + requestParams.put("channel_id", midjourneyConfig.getChannelId());
  60 + requestParams.put("session_id", midjourneyConfig.getSessionId());
  61 + requestParams.put("nonce", String.valueOf(IdUtil.getSnowflakeNextId()));
  62 + requestParams.put("custom_id", reRoll.getCustomId());
  63 + requestParams.put("message_id", reRoll.getMessageId());
  64 + // 设置参数
  65 + String requestBody = MjClient.setParams(requestTemplate, requestParams);
  66 + // 发送请求
  67 + String res = MjClient.post(url, midjourneyConfig.getToken(), requestBody);
  68 + // 这个 res 只要不返回值,就是成功!
  69 + boolean isSuccess = StrUtil.isBlank(res);
  70 + if (isSuccess) {
  71 + return true;
  72 + }
  73 + log.error("请求失败! 请求参数:{} 返回结果! {}", requestBody, res);
  74 + return isSuccess;
  75 + }
18 76 }
... ...