mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-02-08 14:44:57 +08:00
增加 imagine 命令发送
This commit is contained in:
parent
2f6b9b2ef5
commit
98b4fb7ff0
@ -0,0 +1,17 @@
|
||||
package cn.iocoder.yudao.framework.ai.midjourney;
|
||||
|
||||
/**
|
||||
* midjourney
|
||||
*
|
||||
* author: fansili
|
||||
* time: 2024/4/3 17:09
|
||||
*/
|
||||
public class MidjourneyClient {
|
||||
|
||||
|
||||
public void imagine() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,67 @@
|
||||
package cn.iocoder.yudao.framework.ai.midjourney;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Midjourney 配置
|
||||
*
|
||||
* author: fansili
|
||||
* time: 2024/4/3 17:10
|
||||
*/
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public class MidjourneyConfig {
|
||||
|
||||
public MidjourneyConfig(String token, String guildId, String channelId, Map<String, String> requestTemplates) {
|
||||
this.token = token;
|
||||
this.guildId = guildId;
|
||||
this.channelId = channelId;
|
||||
this.serverUrl = serverUrl;
|
||||
this.apiInteractions = apiInteractions;
|
||||
this.userAage = userAage;
|
||||
this.requestTemplates = requestTemplates;
|
||||
}
|
||||
|
||||
/**
|
||||
* token信息
|
||||
*
|
||||
* tip: 登录discard F12找 messages 接口
|
||||
*/
|
||||
private String token;
|
||||
/**
|
||||
* 服务器id
|
||||
*/
|
||||
private String guildId;
|
||||
/**
|
||||
* 频道id
|
||||
*/
|
||||
private String channelId;
|
||||
|
||||
//
|
||||
// api 接口
|
||||
|
||||
/**
|
||||
* 服务地址
|
||||
*/
|
||||
private String serverUrl = "https://discord.com/";
|
||||
/**
|
||||
* 发送命令
|
||||
*/
|
||||
private String apiInteractions = "api/v9/interactions";
|
||||
|
||||
|
||||
//
|
||||
// 浏览器配置
|
||||
|
||||
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";
|
||||
|
||||
|
||||
//
|
||||
// 请求 json 文件
|
||||
|
||||
|
||||
private Map<String, String> requestTemplates;
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package cn.iocoder.yudao.framework.ai.midjourney.demo;
|
||||
package cn.iocoder.yudao.framework.ai.midjourney;
|
||||
|
||||
import lombok.Getter;
|
||||
|
||||
@ -6,7 +6,7 @@ import lombok.Getter;
|
||||
* MJ 命令
|
||||
*/
|
||||
@Getter
|
||||
public enum MjCommandEnum {
|
||||
public enum MidjourneyInteractionsEnum {
|
||||
|
||||
IMAGINE("imagine", "生成图片"),
|
||||
DESCRIBE("describe", "生成描述"),
|
||||
@ -17,7 +17,7 @@ public enum MjCommandEnum {
|
||||
|
||||
;
|
||||
|
||||
MjCommandEnum(String value, String message) {
|
||||
MidjourneyInteractionsEnum(String value, String message) {
|
||||
this.value =value;
|
||||
this.message =message;
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
package cn.iocoder.yudao.framework.ai.midjourney.demo;
|
||||
|
||||
import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyInteractionsEnum;
|
||||
|
||||
/**
|
||||
* mj 命令
|
||||
*/
|
||||
@ -12,6 +14,6 @@ public interface MjExecute {
|
||||
* @param prompt
|
||||
* @return
|
||||
*/
|
||||
boolean execute(MjCommandEnum mjCommand, String prompt);
|
||||
boolean execute(MidjourneyInteractionsEnum mjCommand, String prompt);
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package cn.iocoder.yudao.framework.ai.midjourney.demo;
|
||||
|
||||
import cn.hutool.core.io.FileUtil;
|
||||
import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyInteractionsEnum;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
@ -13,7 +14,7 @@ public class MjHttpExecute implements MjExecute {
|
||||
private static final String URL = "https://discord.com/";
|
||||
|
||||
@Override
|
||||
public boolean execute(MjCommandEnum mjCommand, String prompt) {
|
||||
public boolean execute(MidjourneyInteractionsEnum mjCommand, String prompt) {
|
||||
// 发送的 uri
|
||||
String uri = "api/v9/interactions";
|
||||
// restTemplate 发送post请求
|
||||
@ -44,9 +45,6 @@ public class MjHttpExecute implements MjExecute {
|
||||
String res = restTemplate.postForObject(URL + uri, requestEntity, String.class);
|
||||
System.err.println("11");
|
||||
|
||||
//
|
||||
// MjHttpExecute mjHttpExecute = new MjHttpExecute();
|
||||
// mjHttpExecute.execute(null, "童话世界应该是什么样?");
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,46 @@
|
||||
package cn.iocoder.yudao.framework.ai.midjourney.interactions;
|
||||
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* mj client
|
||||
* <p>
|
||||
* author: fansili
|
||||
* time: 2024/4/3 17:37
|
||||
*/
|
||||
public class MjClient {
|
||||
|
||||
private static RestTemplate restTemplate = new RestTemplate();
|
||||
private static HttpHeaders headers = new HttpHeaders();
|
||||
|
||||
static {
|
||||
headers.setContentType(MediaType.APPLICATION_JSON); // 设置内容类型为JSON
|
||||
// headers.set("Authorization", token); // 如果需要,设置认证信息(例如JWT令牌)
|
||||
headers.set("Referer", "https://discord.com/channels/1221445697157468200/1221445862962630706"); // 如果需要,设置认证信息(例如JWT令牌)
|
||||
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令牌)
|
||||
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令牌)
|
||||
}
|
||||
|
||||
public static String post(String url, String token, String body) {
|
||||
// 设置token
|
||||
headers.set("Authorization", token);
|
||||
// 封装请求体和头部信息
|
||||
HttpEntity<String> requestEntity = new HttpEntity<>(body, headers);
|
||||
// 发送请求
|
||||
String result = restTemplate.postForObject(url, requestEntity, String.class);
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
public static String setParams(String requestTemplate, Map<String, String> requestParams) {
|
||||
for (Map.Entry<String, String> entry : requestParams.entrySet()) {
|
||||
requestTemplate = requestTemplate.replace("$".concat(entry.getKey()), entry.getValue());
|
||||
}
|
||||
return requestTemplate;
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package cn.iocoder.yudao.framework.ai.midjourney.interactions;
|
||||
|
||||
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.MidjourneyInteractionsEnum;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Maps;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* author: fansili
|
||||
* time: 2024/4/3 17:36
|
||||
*/
|
||||
@Slf4j
|
||||
public class MjImagineInteractions implements MjInteractions {
|
||||
|
||||
private MidjourneyConfig midjourneyConfig;
|
||||
|
||||
public MjImagineInteractions(MidjourneyConfig midjourneyConfig) {
|
||||
this.midjourneyConfig = midjourneyConfig;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MidjourneyInteractionsEnum> supperInteractions() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean execute(String prompt) {
|
||||
String url = midjourneyConfig.getServerUrl().concat(midjourneyConfig.getApiInteractions());
|
||||
// 获取请求模板
|
||||
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", UUID.randomUUID().toString().replaceAll("-", ""));
|
||||
requestParams.put("nonce", String.valueOf(IdUtil.getSnowflakeNextId()));
|
||||
requestParams.put("prompt", prompt);
|
||||
// 设置参数
|
||||
String requestBody = MjClient.setParams(requestTemplate, requestParams);
|
||||
// 发送请求
|
||||
String res = MjClient.post(url, midjourneyConfig.getToken(), requestBody);
|
||||
//
|
||||
System.err.println(res);
|
||||
log.info(res);
|
||||
// 这个 res 只要不返回值,就是成功!
|
||||
return StrUtil.isBlank(res);
|
||||
}
|
||||
}
|
@ -0,0 +1,18 @@
|
||||
package cn.iocoder.yudao.framework.ai.midjourney.interactions;
|
||||
|
||||
import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyInteractionsEnum;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* mj Interactions
|
||||
*
|
||||
* author: fansili
|
||||
* time: 2024/4/3 17:22
|
||||
*/
|
||||
public interface MjInteractions {
|
||||
|
||||
List<MidjourneyInteractionsEnum> supperInteractions();
|
||||
|
||||
Boolean execute(String prompt);
|
||||
}
|
Loading…
Reference in New Issue
Block a user