Commit 337ae045519b8c83cb4ff53aa57cf9426b5fd5ff
1 parent
a44628e4
AI:code review chat 部分的代码(mj)
Showing
6 changed files
with
17 additions
and
6 deletions
yudao-dependencies/pom.xml
| @@ -629,6 +629,7 @@ | @@ -629,6 +629,7 @@ | ||
| 629 | <version>${ureport2.version}</version> | 629 | <version>${ureport2.version}</version> |
| 630 | </dependency> | 630 | </dependency> |
| 631 | 631 | ||
| 632 | + <!-- TODO @fansili:看看怎么可以不依赖这个 bom 文件 --> | ||
| 632 | <!-- 添加ai模块 --> | 633 | <!-- 添加ai模块 --> |
| 633 | <dependency> | 634 | <dependency> |
| 634 | <groupId>org.springframework.ai</groupId> | 635 | <groupId>org.springframework.ai</groupId> |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/constants/MjGennerateStatusEnum.java
| @@ -2,6 +2,7 @@ package cn.iocoder.yudao.framework.ai.midjourney.constants; | @@ -2,6 +2,7 @@ package cn.iocoder.yudao.framework.ai.midjourney.constants; | ||
| 2 | 2 | ||
| 3 | import lombok.Getter; | 3 | import lombok.Getter; |
| 4 | 4 | ||
| 5 | +// TODO @fansili:1)Mj 缩写,还是搞成全称。。虽然长一点,但是感觉会相对清晰一些哈;2)lombok 相关的注解,可以用用哈;3)value 改 status; | ||
| 5 | /** | 6 | /** |
| 6 | * mj 生成状态 | 7 | * mj 生成状态 |
| 7 | * | 8 | * |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/interactions/MjInteractions.java
| @@ -24,6 +24,7 @@ import org.springframework.web.client.RestTemplate; | @@ -24,6 +24,7 @@ import org.springframework.web.client.RestTemplate; | ||
| 24 | import java.io.IOException; | 24 | import java.io.IOException; |
| 25 | import java.util.HashMap; | 25 | import java.util.HashMap; |
| 26 | 26 | ||
| 27 | +// TODO @fansili:按照 spring ai 的封装习惯,这个类是不是 MidjourneyApi | ||
| 27 | /** | 28 | /** |
| 28 | * 图片生成 | 29 | * 图片生成 |
| 29 | * | 30 | * |
| @@ -33,13 +34,12 @@ import java.util.HashMap; | @@ -33,13 +34,12 @@ import java.util.HashMap; | ||
| 33 | @Slf4j | 34 | @Slf4j |
| 34 | public class MjInteractions { | 35 | public class MjInteractions { |
| 35 | 36 | ||
| 36 | - | ||
| 37 | private final String url; | 37 | private final String url; |
| 38 | private final MidjourneyConfig midjourneyConfig; | 38 | private final MidjourneyConfig midjourneyConfig; |
| 39 | - private final RestTemplate restTemplate = new RestTemplate(); | 39 | + private final RestTemplate restTemplate = new RestTemplate(); // TODO @fansili:优先级低:后续搞到统一的管理 |
| 40 | + // TODO @fansili:静态变量,放在最前面哈; | ||
| 40 | private static final String HEADER_REFERER = "https://discord.com/channels/%s/%s"; | 41 | private static final String HEADER_REFERER = "https://discord.com/channels/%s/%s"; |
| 41 | 42 | ||
| 42 | - | ||
| 43 | public MjInteractions(MidjourneyConfig midjourneyConfig) { | 43 | public MjInteractions(MidjourneyConfig midjourneyConfig) { |
| 44 | this.midjourneyConfig = midjourneyConfig; | 44 | this.midjourneyConfig = midjourneyConfig; |
| 45 | this.url = midjourneyConfig.getServerUrl().concat(midjourneyConfig.getApiInteractions()); | 45 | this.url = midjourneyConfig.getServerUrl().concat(midjourneyConfig.getApiInteractions()); |
| @@ -50,10 +50,11 @@ public class MjInteractions { | @@ -50,10 +50,11 @@ public class MjInteractions { | ||
| 50 | String requestTemplate = midjourneyConfig.getRequestTemplates().get("imagine"); | 50 | String requestTemplate = midjourneyConfig.getRequestTemplates().get("imagine"); |
| 51 | // 设置参数 | 51 | // 设置参数 |
| 52 | HashMap<String, String> requestParams = Maps.newHashMap(); | 52 | HashMap<String, String> requestParams = Maps.newHashMap(); |
| 53 | + // TODO @fansili:感觉参数的组装,可以搞成一个公用的方法;就是 config + 入参的感觉; | ||
| 53 | requestParams.put("guild_id", midjourneyConfig.getGuildId()); | 54 | requestParams.put("guild_id", midjourneyConfig.getGuildId()); |
| 54 | requestParams.put("channel_id", midjourneyConfig.getChannelId()); | 55 | requestParams.put("channel_id", midjourneyConfig.getChannelId()); |
| 55 | requestParams.put("session_id", midjourneyConfig.getSessionId()); | 56 | requestParams.put("session_id", midjourneyConfig.getSessionId()); |
| 56 | - requestParams.put("nonce", String.valueOf(IdUtil.getSnowflakeNextId())); | 57 | + requestParams.put("nonce", String.valueOf(IdUtil.getSnowflakeNextId())); // TODO @fansili:建议用 uuid 之类的;nextId 跨进程未必合适哈; |
| 57 | requestParams.put("prompt", prompt); | 58 | requestParams.put("prompt", prompt); |
| 58 | // 解析 template 参数占位符 | 59 | // 解析 template 参数占位符 |
| 59 | String requestBody = MjUtil.parseTemplate(requestTemplate, requestParams); | 60 | String requestBody = MjUtil.parseTemplate(requestTemplate, requestParams); |
| @@ -63,6 +64,7 @@ public class MjInteractions { | @@ -63,6 +64,7 @@ public class MjInteractions { | ||
| 63 | HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, httpHeaders); | 64 | HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, httpHeaders); |
| 64 | String res = restTemplate.postForObject(url, requestEntity, String.class); | 65 | String res = restTemplate.postForObject(url, requestEntity, String.class); |
| 65 | // 这个 res 只要不返回值,就是成功! | 66 | // 这个 res 只要不返回值,就是成功! |
| 67 | + // TODO @fansili:可以直接 if (StrUtil.isBlank(res)) | ||
| 66 | boolean isSuccess = StrUtil.isBlank(res); | 68 | boolean isSuccess = StrUtil.isBlank(res); |
| 67 | if (isSuccess) { | 69 | if (isSuccess) { |
| 68 | return true; | 70 | return true; |
| @@ -70,7 +72,7 @@ public class MjInteractions { | @@ -70,7 +72,7 @@ public class MjInteractions { | ||
| 70 | log.error("请求失败! 请求参数:{} 返回结果! {}", requestBody, res); | 72 | log.error("请求失败! 请求参数:{} 返回结果! {}", requestBody, res); |
| 71 | return isSuccess; | 73 | return isSuccess; |
| 72 | } | 74 | } |
| 73 | - | 75 | + // TODO @fansili:方法和方法之间,空一行哈; |
| 74 | 76 | ||
| 75 | 77 | ||
| 76 | public Boolean reRoll(ReRoll reRoll) { | 78 | public Boolean reRoll(ReRoll reRoll) { |
| @@ -100,12 +102,13 @@ public class MjInteractions { | @@ -100,12 +102,13 @@ public class MjInteractions { | ||
| 100 | return isSuccess; | 102 | return isSuccess; |
| 101 | } | 103 | } |
| 102 | 104 | ||
| 103 | - | 105 | + // TODO @fansili:搞成私有方法,可能会好点; |
| 104 | public UploadAttachmentsRes uploadAttachments(Attachments attachments) { | 106 | public UploadAttachmentsRes uploadAttachments(Attachments attachments) { |
| 105 | // file | 107 | // file |
| 106 | JSONObject fileObj = new JSONObject(); | 108 | JSONObject fileObj = new JSONObject(); |
| 107 | fileObj.put("id", "0"); | 109 | fileObj.put("id", "0"); |
| 108 | fileObj.put("filename", attachments.getFileSystemResource().getFilename()); | 110 | fileObj.put("filename", attachments.getFileSystemResource().getFilename()); |
| 111 | + // TODO @fansili:这块用 lombok 哪个异常处理,简化下代码; | ||
| 109 | try { | 112 | try { |
| 110 | fileObj.put("file_size", attachments.getFileSystemResource().contentLength()); | 113 | fileObj.put("file_size", attachments.getFileSystemResource().contentLength()); |
| 111 | } catch (IOException e) { | 114 | } catch (IOException e) { |
| @@ -116,6 +119,7 @@ public class MjInteractions { | @@ -116,6 +119,7 @@ public class MjInteractions { | ||
| 116 | multipartRequest.put("files", Lists.newArrayList(fileObj)); | 119 | multipartRequest.put("files", Lists.newArrayList(fileObj)); |
| 117 | // 设置header值 | 120 | // 设置header值 |
| 118 | HttpHeaders httpHeaders = new HttpHeaders(); | 121 | HttpHeaders httpHeaders = new HttpHeaders(); |
| 122 | + // TODO @fansili:通用的 header 构建,抽一个方法哈; | ||
| 119 | httpHeaders.setContentType(MediaType.APPLICATION_JSON); | 123 | httpHeaders.setContentType(MediaType.APPLICATION_JSON); |
| 120 | httpHeaders.set("Authorization", midjourneyConfig.getToken()); | 124 | httpHeaders.set("Authorization", midjourneyConfig.getToken()); |
| 121 | httpHeaders.set("User-Agent", midjourneyConfig.getUserAage()); | 125 | httpHeaders.set("User-Agent", midjourneyConfig.getUserAage()); |
| @@ -185,4 +189,5 @@ public class MjInteractions { | @@ -185,4 +189,5 @@ public class MjInteractions { | ||
| 185 | httpHeaders.set("Referer", String.format(HEADER_REFERER, midjourneyConfig.getGuildId(), midjourneyConfig.getChannelId())); | 189 | httpHeaders.set("Referer", String.format(HEADER_REFERER, midjourneyConfig.getGuildId(), midjourneyConfig.getChannelId())); |
| 186 | return httpHeaders; | 190 | return httpHeaders; |
| 187 | } | 191 | } |
| 192 | + | ||
| 188 | } | 193 | } |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/webSocket/MjWebSocketStarter.java
| @@ -22,6 +22,7 @@ import java.io.IOException; | @@ -22,6 +22,7 @@ import java.io.IOException; | ||
| 22 | import java.net.URI; | 22 | import java.net.URI; |
| 23 | import java.util.concurrent.TimeoutException; | 23 | import java.util.concurrent.TimeoutException; |
| 24 | 24 | ||
| 25 | +// TODO @fansili:mj 这块 websocket 有点小复杂,虽然代码量 400 多行;感觉可以考虑,有没第三方 sdk,通过它透明接入 mj | ||
| 25 | @Slf4j | 26 | @Slf4j |
| 26 | public class MjWebSocketStarter implements WebSocketStarter { | 27 | public class MjWebSocketStarter implements WebSocketStarter { |
| 27 | /** | 28 | /** |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/package-info.java
| @@ -2,6 +2,8 @@ | @@ -2,6 +2,8 @@ | ||
| 2 | * author: fansili | 2 | * author: fansili |
| 3 | * time: 2024/3/12 20:29 | 3 | * time: 2024/3/12 20:29 |
| 4 | * | 4 | * |
| 5 | + * TODO @fansili:包的想法,需要重点看看 | ||
| 6 | + * | ||
| 5 | * 1. org.springframework.ai:包括 chat、image、model、parser、util 部分 | 7 | * 1. org.springframework.ai:包括 chat、image、model、parser、util 部分 |
| 6 | * | 8 | * |
| 7 | * 2. yudao.framework.models | 9 | * 2. yudao.framework.models |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/util/JacksonUtil.java
| @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; | @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; | ||
| 9 | 9 | ||
| 10 | import java.io.IOException; | 10 | import java.io.IOException; |
| 11 | 11 | ||
| 12 | +// TODO @fansili:看看能不能用 JsonUtils | ||
| 12 | /** | 13 | /** |
| 13 | * Jackson工具类 | 14 | * Jackson工具类 |
| 14 | * | 15 | * |