mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2025-01-31 17:40:05 +08:00
AI:code review chat 部分的代码(mj)
This commit is contained in:
parent
a44628e436
commit
337ae04551
@ -629,6 +629,7 @@
|
|||||||
<version>${ureport2.version}</version>
|
<version>${ureport2.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!-- TODO @fansili:看看怎么可以不依赖这个 bom 文件 -->
|
||||||
<!-- 添加ai模块 -->
|
<!-- 添加ai模块 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.ai</groupId>
|
<groupId>org.springframework.ai</groupId>
|
||||||
|
@ -2,6 +2,7 @@ package cn.iocoder.yudao.framework.ai.midjourney.constants;
|
|||||||
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
|
|
||||||
|
// TODO @fansili:1)Mj 缩写,还是搞成全称。。虽然长一点,但是感觉会相对清晰一些哈;2)lombok 相关的注解,可以用用哈;3)value 改 status;
|
||||||
/**
|
/**
|
||||||
* mj 生成状态
|
* mj 生成状态
|
||||||
*
|
*
|
||||||
|
@ -24,6 +24,7 @@ import org.springframework.web.client.RestTemplate;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
// TODO @fansili:按照 spring ai 的封装习惯,这个类是不是 MidjourneyApi
|
||||||
/**
|
/**
|
||||||
* 图片生成
|
* 图片生成
|
||||||
*
|
*
|
||||||
@ -33,13 +34,12 @@ import java.util.HashMap;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class MjInteractions {
|
public class MjInteractions {
|
||||||
|
|
||||||
|
|
||||||
private final String url;
|
private final String url;
|
||||||
private final MidjourneyConfig midjourneyConfig;
|
private final MidjourneyConfig midjourneyConfig;
|
||||||
private final RestTemplate restTemplate = new RestTemplate();
|
private final RestTemplate restTemplate = new RestTemplate(); // TODO @fansili:优先级低:后续搞到统一的管理
|
||||||
|
// TODO @fansili:静态变量,放在最前面哈;
|
||||||
private static final String HEADER_REFERER = "https://discord.com/channels/%s/%s";
|
private static final String HEADER_REFERER = "https://discord.com/channels/%s/%s";
|
||||||
|
|
||||||
|
|
||||||
public MjInteractions(MidjourneyConfig midjourneyConfig) {
|
public MjInteractions(MidjourneyConfig midjourneyConfig) {
|
||||||
this.midjourneyConfig = midjourneyConfig;
|
this.midjourneyConfig = midjourneyConfig;
|
||||||
this.url = midjourneyConfig.getServerUrl().concat(midjourneyConfig.getApiInteractions());
|
this.url = midjourneyConfig.getServerUrl().concat(midjourneyConfig.getApiInteractions());
|
||||||
@ -50,10 +50,11 @@ public class MjInteractions {
|
|||||||
String requestTemplate = midjourneyConfig.getRequestTemplates().get("imagine");
|
String requestTemplate = midjourneyConfig.getRequestTemplates().get("imagine");
|
||||||
// 设置参数
|
// 设置参数
|
||||||
HashMap<String, String> requestParams = Maps.newHashMap();
|
HashMap<String, String> requestParams = Maps.newHashMap();
|
||||||
|
// TODO @fansili:感觉参数的组装,可以搞成一个公用的方法;就是 config + 入参的感觉;
|
||||||
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", midjourneyConfig.getSessionId());
|
requestParams.put("session_id", midjourneyConfig.getSessionId());
|
||||||
requestParams.put("nonce", String.valueOf(IdUtil.getSnowflakeNextId()));
|
requestParams.put("nonce", String.valueOf(IdUtil.getSnowflakeNextId())); // TODO @fansili:建议用 uuid 之类的;nextId 跨进程未必合适哈;
|
||||||
requestParams.put("prompt", prompt);
|
requestParams.put("prompt", prompt);
|
||||||
// 解析 template 参数占位符
|
// 解析 template 参数占位符
|
||||||
String requestBody = MjUtil.parseTemplate(requestTemplate, requestParams);
|
String requestBody = MjUtil.parseTemplate(requestTemplate, requestParams);
|
||||||
@ -63,6 +64,7 @@ public class MjInteractions {
|
|||||||
HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, httpHeaders);
|
HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, httpHeaders);
|
||||||
String res = restTemplate.postForObject(url, requestEntity, String.class);
|
String res = restTemplate.postForObject(url, requestEntity, String.class);
|
||||||
// 这个 res 只要不返回值,就是成功!
|
// 这个 res 只要不返回值,就是成功!
|
||||||
|
// TODO @fansili:可以直接 if (StrUtil.isBlank(res))
|
||||||
boolean isSuccess = StrUtil.isBlank(res);
|
boolean isSuccess = StrUtil.isBlank(res);
|
||||||
if (isSuccess) {
|
if (isSuccess) {
|
||||||
return true;
|
return true;
|
||||||
@ -70,7 +72,7 @@ public class MjInteractions {
|
|||||||
log.error("请求失败! 请求参数:{} 返回结果! {}", requestBody, res);
|
log.error("请求失败! 请求参数:{} 返回结果! {}", requestBody, res);
|
||||||
return isSuccess;
|
return isSuccess;
|
||||||
}
|
}
|
||||||
|
// TODO @fansili:方法和方法之间,空一行哈;
|
||||||
|
|
||||||
|
|
||||||
public Boolean reRoll(ReRoll reRoll) {
|
public Boolean reRoll(ReRoll reRoll) {
|
||||||
@ -100,12 +102,13 @@ public class MjInteractions {
|
|||||||
return isSuccess;
|
return isSuccess;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO @fansili:搞成私有方法,可能会好点;
|
||||||
public UploadAttachmentsRes uploadAttachments(Attachments attachments) {
|
public UploadAttachmentsRes uploadAttachments(Attachments attachments) {
|
||||||
// file
|
// file
|
||||||
JSONObject fileObj = new JSONObject();
|
JSONObject fileObj = new JSONObject();
|
||||||
fileObj.put("id", "0");
|
fileObj.put("id", "0");
|
||||||
fileObj.put("filename", attachments.getFileSystemResource().getFilename());
|
fileObj.put("filename", attachments.getFileSystemResource().getFilename());
|
||||||
|
// TODO @fansili:这块用 lombok 哪个异常处理,简化下代码;
|
||||||
try {
|
try {
|
||||||
fileObj.put("file_size", attachments.getFileSystemResource().contentLength());
|
fileObj.put("file_size", attachments.getFileSystemResource().contentLength());
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
@ -116,6 +119,7 @@ public class MjInteractions {
|
|||||||
multipartRequest.put("files", Lists.newArrayList(fileObj));
|
multipartRequest.put("files", Lists.newArrayList(fileObj));
|
||||||
// 设置header值
|
// 设置header值
|
||||||
HttpHeaders httpHeaders = new HttpHeaders();
|
HttpHeaders httpHeaders = new HttpHeaders();
|
||||||
|
// TODO @fansili:通用的 header 构建,抽一个方法哈;
|
||||||
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
|
||||||
httpHeaders.set("Authorization", midjourneyConfig.getToken());
|
httpHeaders.set("Authorization", midjourneyConfig.getToken());
|
||||||
httpHeaders.set("User-Agent", midjourneyConfig.getUserAage());
|
httpHeaders.set("User-Agent", midjourneyConfig.getUserAage());
|
||||||
@ -185,4 +189,5 @@ public class MjInteractions {
|
|||||||
httpHeaders.set("Referer", String.format(HEADER_REFERER, midjourneyConfig.getGuildId(), midjourneyConfig.getChannelId()));
|
httpHeaders.set("Referer", String.format(HEADER_REFERER, midjourneyConfig.getGuildId(), midjourneyConfig.getChannelId()));
|
||||||
return httpHeaders;
|
return httpHeaders;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,7 @@ import java.io.IOException;
|
|||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
|
|
||||||
|
// TODO @fansili:mj 这块 websocket 有点小复杂,虽然代码量 400 多行;感觉可以考虑,有没第三方 sdk,通过它透明接入 mj
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class MjWebSocketStarter implements WebSocketStarter {
|
public class MjWebSocketStarter implements WebSocketStarter {
|
||||||
/**
|
/**
|
||||||
|
@ -2,6 +2,8 @@
|
|||||||
* author: fansili
|
* author: fansili
|
||||||
* time: 2024/3/12 20:29
|
* time: 2024/3/12 20:29
|
||||||
*
|
*
|
||||||
|
* TODO @fansili:包的想法,需要重点看看
|
||||||
|
*
|
||||||
* 1. org.springframework.ai:包括 chat、image、model、parser、util 部分
|
* 1. org.springframework.ai:包括 chat、image、model、parser、util 部分
|
||||||
*
|
*
|
||||||
* 2. yudao.framework.models
|
* 2. yudao.framework.models
|
||||||
|
@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
|
// TODO @fansili:看看能不能用 JsonUtils
|
||||||
/**
|
/**
|
||||||
* Jackson工具类
|
* Jackson工具类
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user