增加图片选择,和图片放大相关操作

This commit is contained in:
cherishsince 2024-04-06 21:53:22 +08:00
parent f2b9c14819
commit b1158fb1a7
6 changed files with 82 additions and 15 deletions

View File

@ -4,6 +4,7 @@ import lombok.Data;
import lombok.experimental.Accessors; import lombok.experimental.Accessors;
import java.util.Map; import java.util.Map;
import java.util.UUID;
/** /**
* Midjourney 配置 * Midjourney 配置
@ -15,16 +16,6 @@ import java.util.Map;
@Accessors(chain = true) @Accessors(chain = true)
public class MidjourneyConfig { 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信息 * token信息
* *
@ -64,4 +55,23 @@ public class MidjourneyConfig {
private Map<String, String> requestTemplates; private Map<String, String> requestTemplates;
//
//
private String sessionId;
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;
// 生成 session id
sessionId = UUID.randomUUID().toString().replaceAll("-", "");
}
} }

View File

@ -17,6 +17,7 @@ public class MjMessage {
* 现在已知 * 现在已知
* 0我们发送的消息和指令 * 0我们发送的消息和指令
* 20: mj生成图片发送过程中 * 20: mj生成图片发送过程中
* 19: 选择了某一张图片后的通知
*/ */
private Integer type; private Integer type;
/** /**

View File

@ -32,8 +32,7 @@ public class MjClient {
// 封装请求体和头部信息 // 封装请求体和头部信息
HttpEntity<String> requestEntity = new HttpEntity<>(body, headers); HttpEntity<String> requestEntity = new HttpEntity<>(body, headers);
// 发送请求 // 发送请求
String result = restTemplate.postForObject(url, requestEntity, String.class); return restTemplate.postForObject(url, requestEntity, String.class);
return result;
} }

View File

@ -4,6 +4,8 @@ import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyConfig; import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyConfig;
import cn.iocoder.yudao.framework.ai.midjourney.constants.MjInteractionsEnum; import cn.iocoder.yudao.framework.ai.midjourney.constants.MjInteractionsEnum;
import cn.iocoder.yudao.framework.ai.midjourney.vo.ReRoll;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -12,7 +14,7 @@ import java.util.List;
import java.util.UUID; import java.util.UUID;
/** /**
* * 图片生成
* *
* author: fansili * author: fansili
* time: 2024/4/3 17:36 * time: 2024/4/3 17:36
@ -28,7 +30,7 @@ public class MjImagineInteractions implements MjInteractions {
@Override @Override
public List<MjInteractionsEnum> supperInteractions() { public List<MjInteractionsEnum> supperInteractions() {
return null; return Lists.newArrayList(MjInteractionsEnum.IMAGINE);
} }
@Override @Override
@ -40,7 +42,7 @@ public class MjImagineInteractions implements MjInteractions {
HashMap<String, String> requestParams = Maps.newHashMap(); HashMap<String, String> requestParams = Maps.newHashMap();
requestParams.put("guild_id", midjourneyConfig.getGuildId()); requestParams.put("guild_id", midjourneyConfig.getGuildId());
requestParams.put("channel_id", midjourneyConfig.getChannelId()); requestParams.put("channel_id", midjourneyConfig.getChannelId());
requestParams.put("session_id", UUID.randomUUID().toString().replaceAll("-", "")); requestParams.put("session_id", midjourneyConfig.getSessionId());
requestParams.put("nonce", String.valueOf(IdUtil.getSnowflakeNextId())); requestParams.put("nonce", String.valueOf(IdUtil.getSnowflakeNextId()));
requestParams.put("prompt", prompt); requestParams.put("prompt", prompt);
// 设置参数 // 设置参数
@ -55,4 +57,29 @@ public class MjImagineInteractions implements MjInteractions {
log.error("请求失败! 请求参数:{} 返回结果! {}", requestBody, res); log.error("请求失败! 请求参数:{} 返回结果! {}", requestBody, res);
return isSuccess; return isSuccess;
} }
public Boolean reRoll(ReRoll reRoll) {
String url = midjourneyConfig.getServerUrl().concat(midjourneyConfig.getApiInteractions());
// 获取请求模板
String requestTemplate = midjourneyConfig.getRequestTemplates().get("reroll");
// 设置参数
HashMap<String, String> requestParams = Maps.newHashMap();
requestParams.put("guild_id", midjourneyConfig.getGuildId());
requestParams.put("channel_id", midjourneyConfig.getChannelId());
requestParams.put("session_id", midjourneyConfig.getSessionId());
requestParams.put("nonce", String.valueOf(IdUtil.getSnowflakeNextId()));
requestParams.put("custom_id", reRoll.getCustomId());
requestParams.put("message_id", reRoll.getMessageId());
// 设置参数
String requestBody = MjClient.setParams(requestTemplate, requestParams);
// 发送请求
String res = MjClient.post(url, midjourneyConfig.getToken(), requestBody);
// 这个 res 只要不返回值就是成功!
boolean isSuccess = StrUtil.isBlank(res);
if (isSuccess) {
return true;
}
log.error("请求失败! 请求参数:{} 返回结果! {}", requestBody, res);
return isSuccess;
}
} }

View File

@ -0,0 +1,16 @@
package cn.iocoder.yudao.framework.ai.midjourney.vo;
import lombok.Data;
import lombok.experimental.Accessors;
/**
* author: fansili
* time: 2024/4/6 21:33
*/
@Data
@Accessors(chain = true)
public class ReRoll {
private String messageId;
private String customId;
}

View File

@ -0,0 +1,14 @@
{
"type": 3,
"guild_id": "$guild_id",
"channel_id": "$channel_id",
"message_id": "$message_id",
"application_id": "936929561302675456",
"session_id": "$session_id",
"nonce": "$nonce",
"message_flags": 0,
"data": {
"component_type": 2,
"custom_id": "$custom_id"
}
}