mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-13 18:45:06 +08:00
【代码优化】AI:MJ 配置类的简化
This commit is contained in:
@ -2,6 +2,7 @@ package cn.iocoder.yudao.framework.ai.config;
|
||||
|
||||
import cn.iocoder.yudao.framework.ai.core.factory.AiClientFactory;
|
||||
import cn.iocoder.yudao.framework.ai.core.factory.AiClientFactoryImpl;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.midjourney.api.MidjourneyApi;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.suno.api.SunoApi;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenChatClient;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenChatModal;
|
||||
@ -96,6 +97,13 @@ public class YudaoAiAutoConfiguration {
|
||||
);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(value = "yudao.ai.midjourney.enable", havingValue = "true")
|
||||
public MidjourneyApi midjourneyApi(YudaoAiProperties yudaoAiProperties) {
|
||||
YudaoAiProperties.MidjourneyProperties config = yudaoAiProperties.getMidjourney();
|
||||
return new MidjourneyApi(config.getBaseUrl(), config.getApiKey(), config.getNotifyUrl());
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnProperty(value = "yudao.ai.suno.enable", havingValue = "true")
|
||||
public SunoApi sunoApi(YudaoAiProperties yudaoAiProperties) {
|
||||
|
@ -64,15 +64,18 @@ public class YudaoAiProperties {
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public static class XingHuoProperties extends ChatProperties {
|
||||
|
||||
private String appId;
|
||||
private String appKey;
|
||||
private String secretKey;
|
||||
private XingHuoChatModel model;
|
||||
|
||||
}
|
||||
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public static class YiYanProperties extends ChatProperties {
|
||||
|
||||
/**
|
||||
* appKey
|
||||
*/
|
||||
@ -92,26 +95,13 @@ public class YudaoAiProperties {
|
||||
}
|
||||
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
public static class MidjourneyProperties {
|
||||
private boolean enable = false;
|
||||
|
||||
/**
|
||||
* socket 链接地址
|
||||
*/
|
||||
private String wssUrl = "wss://gateway.discord.gg";
|
||||
/**
|
||||
* token
|
||||
*/
|
||||
private String token;
|
||||
/**
|
||||
* 服务id
|
||||
*/
|
||||
private String guildId;
|
||||
/**
|
||||
* 频道id
|
||||
*/
|
||||
private String channelId;
|
||||
private String enable;
|
||||
private String apiKey;
|
||||
private String baseUrl;
|
||||
private String notifyUrl;
|
||||
|
||||
}
|
||||
|
||||
@Data
|
||||
|
@ -1,10 +1,11 @@
|
||||
package cn.iocoder.yudao.framework.ai.core.model.midjourney.api;
|
||||
|
||||
import cn.iocoder.yudao.framework.ai.core.model.midjourney.MidjourneyConfig;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import com.google.common.collect.Lists;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.Getter;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.ai.openai.api.ApiUtils;
|
||||
@ -26,11 +27,17 @@ public class MidjourneyApi {
|
||||
|
||||
private final WebClient webClient;
|
||||
|
||||
public MidjourneyApi(MidjourneyConfig midjourneyConfig) {
|
||||
/**
|
||||
* 回调地址
|
||||
*/
|
||||
private final String notifyUrl;
|
||||
|
||||
public MidjourneyApi(String baseUrl, String apiKey, String notifyUrl) {
|
||||
this.webClient = WebClient.builder()
|
||||
.baseUrl(midjourneyConfig.getUrl())
|
||||
.defaultHeaders(ApiUtils.getJsonContentHeaders(midjourneyConfig.getKey()))
|
||||
.baseUrl(baseUrl)
|
||||
.defaultHeaders(ApiUtils.getJsonContentHeaders(apiKey))
|
||||
.build();
|
||||
this.notifyUrl = notifyUrl;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -40,6 +47,9 @@ public class MidjourneyApi {
|
||||
* @return 提交结果
|
||||
*/
|
||||
public SubmitResponse imagine(ImagineRequest request) {
|
||||
if (StrUtil.isEmpty(request.getNotifyHook())) {
|
||||
request.setNotifyHook(notifyUrl);
|
||||
}
|
||||
String response = post("/submit/imagine", request);
|
||||
return JsonUtils.parseObject(response, SubmitResponse.class);
|
||||
}
|
||||
@ -51,8 +61,11 @@ public class MidjourneyApi {
|
||||
* @return 提交结果
|
||||
*/
|
||||
public SubmitResponse action(ActionRequest request) {
|
||||
String res = post("/submit/action", request);
|
||||
return JsonUtils.parseObject(res, SubmitResponse.class);
|
||||
if (StrUtil.isEmpty(request.getNotifyHook())) {
|
||||
request.setNotifyHook(notifyUrl);
|
||||
}
|
||||
String response = post("/submit/action", request);
|
||||
return JsonUtils.parseObject(response, SubmitResponse.class);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -86,23 +99,40 @@ public class MidjourneyApi {
|
||||
|
||||
/**
|
||||
* Imagine 请求(生成图片)
|
||||
*
|
||||
* @param base64Array 垫图(参考图) base64数 组
|
||||
* @param notifyHook 通知地址
|
||||
* @param prompt 提示词
|
||||
* @param state 自定义参数
|
||||
*/
|
||||
public record ImagineRequest(List<String> base64Array,
|
||||
String notifyHook,
|
||||
String prompt,
|
||||
String state) {
|
||||
@Data
|
||||
public static final class ImagineRequest {
|
||||
|
||||
/**
|
||||
* 垫图(参考图) base64 数组
|
||||
*/
|
||||
private List<String> base64Array;
|
||||
/**
|
||||
* 提示词
|
||||
*/
|
||||
private String prompt;
|
||||
/**
|
||||
* 通知地址
|
||||
*/
|
||||
private String notifyHook;
|
||||
/**
|
||||
* 自定义参数
|
||||
*/
|
||||
private String state;
|
||||
|
||||
public ImagineRequest(List<String> base64Array, String prompt, String notifyHook, String state) {
|
||||
this.base64Array = base64Array;
|
||||
this.prompt = prompt;
|
||||
this.notifyHook = notifyHook;
|
||||
this.state = state;
|
||||
}
|
||||
|
||||
public static String buildState(Integer width, Integer height, String version, String model) {
|
||||
StringBuilder params = new StringBuilder();
|
||||
// --ar 来设置尺寸
|
||||
params.append(String.format(" --ar %s:%s ", width, height));
|
||||
// --niji 模型
|
||||
if (MidjourneyApi.ModelEnum.NIJI.getModel().equals(model)) {
|
||||
if (ModelEnum.NIJI.getModel().equals(model)) {
|
||||
params.append(String.format(" --niji %s ", version));
|
||||
} else {
|
||||
params.append(String.format(" --v %s ", version));
|
||||
@ -114,15 +144,20 @@ public class MidjourneyApi {
|
||||
|
||||
/**
|
||||
* Action 请求
|
||||
*
|
||||
* @param customId 操作按钮id
|
||||
* @param taskId 操作按钮id
|
||||
* @param notifyHook 通知地址
|
||||
*/
|
||||
public record ActionRequest(String customId,
|
||||
String taskId,
|
||||
String notifyHook
|
||||
) {
|
||||
@Data
|
||||
public static final class ActionRequest {
|
||||
|
||||
private String customId;
|
||||
private String taskId;
|
||||
private String notifyHook;
|
||||
|
||||
public ActionRequest(String taskId, String customId, String notifyHook) {
|
||||
this.customId = customId;
|
||||
this.taskId = taskId;
|
||||
this.notifyHook = notifyHook;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user