Commit 337ae045519b8c83cb4ff53aa57cf9426b5fd5ff
1 parent
a44628e4
AI:code review chat 部分的代码(mj)
Showing
6 changed files
with
17 additions
and
6 deletions
yudao-dependencies/pom.xml
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/constants/MjGennerateStatusEnum.java
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 | 24 | import java.io.IOException; |
| 25 | 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 | 34 | @Slf4j |
| 34 | 35 | public class MjInteractions { |
| 35 | 36 | |
| 36 | - | |
| 37 | 37 | private final String url; |
| 38 | 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 | 41 | private static final String HEADER_REFERER = "https://discord.com/channels/%s/%s"; |
| 41 | 42 | |
| 42 | - | |
| 43 | 43 | public MjInteractions(MidjourneyConfig midjourneyConfig) { |
| 44 | 44 | this.midjourneyConfig = midjourneyConfig; |
| 45 | 45 | this.url = midjourneyConfig.getServerUrl().concat(midjourneyConfig.getApiInteractions()); |
| ... | ... | @@ -50,10 +50,11 @@ public class MjInteractions { |
| 50 | 50 | String requestTemplate = midjourneyConfig.getRequestTemplates().get("imagine"); |
| 51 | 51 | // 设置参数 |
| 52 | 52 | HashMap<String, String> requestParams = Maps.newHashMap(); |
| 53 | + // TODO @fansili:感觉参数的组装,可以搞成一个公用的方法;就是 config + 入参的感觉; | |
| 53 | 54 | requestParams.put("guild_id", midjourneyConfig.getGuildId()); |
| 54 | 55 | requestParams.put("channel_id", midjourneyConfig.getChannelId()); |
| 55 | 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 | 58 | requestParams.put("prompt", prompt); |
| 58 | 59 | // 解析 template 参数占位符 |
| 59 | 60 | String requestBody = MjUtil.parseTemplate(requestTemplate, requestParams); |
| ... | ... | @@ -63,6 +64,7 @@ public class MjInteractions { |
| 63 | 64 | HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, httpHeaders); |
| 64 | 65 | String res = restTemplate.postForObject(url, requestEntity, String.class); |
| 65 | 66 | // 这个 res 只要不返回值,就是成功! |
| 67 | + // TODO @fansili:可以直接 if (StrUtil.isBlank(res)) | |
| 66 | 68 | boolean isSuccess = StrUtil.isBlank(res); |
| 67 | 69 | if (isSuccess) { |
| 68 | 70 | return true; |
| ... | ... | @@ -70,7 +72,7 @@ public class MjInteractions { |
| 70 | 72 | log.error("请求失败! 请求参数:{} 返回结果! {}", requestBody, res); |
| 71 | 73 | return isSuccess; |
| 72 | 74 | } |
| 73 | - | |
| 75 | + // TODO @fansili:方法和方法之间,空一行哈; | |
| 74 | 76 | |
| 75 | 77 | |
| 76 | 78 | public Boolean reRoll(ReRoll reRoll) { |
| ... | ... | @@ -100,12 +102,13 @@ public class MjInteractions { |
| 100 | 102 | return isSuccess; |
| 101 | 103 | } |
| 102 | 104 | |
| 103 | - | |
| 105 | + // TODO @fansili:搞成私有方法,可能会好点; | |
| 104 | 106 | public UploadAttachmentsRes uploadAttachments(Attachments attachments) { |
| 105 | 107 | // file |
| 106 | 108 | JSONObject fileObj = new JSONObject(); |
| 107 | 109 | fileObj.put("id", "0"); |
| 108 | 110 | fileObj.put("filename", attachments.getFileSystemResource().getFilename()); |
| 111 | + // TODO @fansili:这块用 lombok 哪个异常处理,简化下代码; | |
| 109 | 112 | try { |
| 110 | 113 | fileObj.put("file_size", attachments.getFileSystemResource().contentLength()); |
| 111 | 114 | } catch (IOException e) { |
| ... | ... | @@ -116,6 +119,7 @@ public class MjInteractions { |
| 116 | 119 | multipartRequest.put("files", Lists.newArrayList(fileObj)); |
| 117 | 120 | // 设置header值 |
| 118 | 121 | HttpHeaders httpHeaders = new HttpHeaders(); |
| 122 | + // TODO @fansili:通用的 header 构建,抽一个方法哈; | |
| 119 | 123 | httpHeaders.setContentType(MediaType.APPLICATION_JSON); |
| 120 | 124 | httpHeaders.set("Authorization", midjourneyConfig.getToken()); |
| 121 | 125 | httpHeaders.set("User-Agent", midjourneyConfig.getUserAage()); |
| ... | ... | @@ -185,4 +189,5 @@ public class MjInteractions { |
| 185 | 189 | httpHeaders.set("Referer", String.format(HEADER_REFERER, midjourneyConfig.getGuildId(), midjourneyConfig.getChannelId())); |
| 186 | 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 | 22 | import java.net.URI; |
| 23 | 23 | import java.util.concurrent.TimeoutException; |
| 24 | 24 | |
| 25 | +// TODO @fansili:mj 这块 websocket 有点小复杂,虽然代码量 400 多行;感觉可以考虑,有没第三方 sdk,通过它透明接入 mj | |
| 25 | 26 | @Slf4j |
| 26 | 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
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/util/JacksonUtil.java