AI:code review chat 部分的代码(mj)

This commit is contained in:
YunaiV 2024-04-09 09:31:56 +08:00
parent a44628e436
commit 337ae04551
6 changed files with 17 additions and 6 deletions

View File

@ -629,6 +629,7 @@
<version>${ureport2.version}</version>
</dependency>
<!-- TODO @fansili:看看怎么可以不依赖这个 bom 文件 -->
<!-- 添加ai模块 -->
<dependency>
<groupId>org.springframework.ai</groupId>

View File

@ -2,6 +2,7 @@ package cn.iocoder.yudao.framework.ai.midjourney.constants;
import lombok.Getter;
// TODO @fansili1Mj 缩写还是搞成全称虽然长一点但是感觉会相对清晰一些哈2lombok 相关的注解可以用用哈3value status
/**
* mj 生成状态
*

View File

@ -24,6 +24,7 @@ import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.util.HashMap;
// TODO @fansili按照 spring ai 的封装习惯这个类是不是 MidjourneyApi
/**
* 图片生成
*
@ -33,13 +34,12 @@ import java.util.HashMap;
@Slf4j
public class MjInteractions {
private final String url;
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";
public MjInteractions(MidjourneyConfig midjourneyConfig) {
this.midjourneyConfig = midjourneyConfig;
this.url = midjourneyConfig.getServerUrl().concat(midjourneyConfig.getApiInteractions());
@ -50,10 +50,11 @@ public class MjInteractions {
String requestTemplate = midjourneyConfig.getRequestTemplates().get("imagine");
// 设置参数
HashMap<String, String> requestParams = Maps.newHashMap();
// TODO @fansili感觉参数的组装可以搞成一个公用的方法就是 config + 入参的感觉
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("nonce", String.valueOf(IdUtil.getSnowflakeNextId())); // TODO @fansili建议用 uuid 之类的nextId 跨进程未必合适哈
requestParams.put("prompt", prompt);
// 解析 template 参数占位符
String requestBody = MjUtil.parseTemplate(requestTemplate, requestParams);
@ -63,6 +64,7 @@ public class MjInteractions {
HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, httpHeaders);
String res = restTemplate.postForObject(url, requestEntity, String.class);
// 这个 res 只要不返回值就是成功!
// TODO @fansili可以直接 if (StrUtil.isBlank(res))
boolean isSuccess = StrUtil.isBlank(res);
if (isSuccess) {
return true;
@ -70,7 +72,7 @@ public class MjInteractions {
log.error("请求失败! 请求参数:{} 返回结果! {}", requestBody, res);
return isSuccess;
}
// TODO @fansili方法和方法之间空一行哈
public Boolean reRoll(ReRoll reRoll) {
@ -100,12 +102,13 @@ public class MjInteractions {
return isSuccess;
}
// TODO @fansili搞成私有方法可能会好点
public UploadAttachmentsRes uploadAttachments(Attachments attachments) {
// file
JSONObject fileObj = new JSONObject();
fileObj.put("id", "0");
fileObj.put("filename", attachments.getFileSystemResource().getFilename());
// TODO @fansili这块用 lombok 哪个异常处理简化下代码
try {
fileObj.put("file_size", attachments.getFileSystemResource().contentLength());
} catch (IOException e) {
@ -116,6 +119,7 @@ public class MjInteractions {
multipartRequest.put("files", Lists.newArrayList(fileObj));
// 设置header值
HttpHeaders httpHeaders = new HttpHeaders();
// TODO @fansili通用的 header 构建抽一个方法哈
httpHeaders.setContentType(MediaType.APPLICATION_JSON);
httpHeaders.set("Authorization", midjourneyConfig.getToken());
httpHeaders.set("User-Agent", midjourneyConfig.getUserAage());
@ -185,4 +189,5 @@ public class MjInteractions {
httpHeaders.set("Referer", String.format(HEADER_REFERER, midjourneyConfig.getGuildId(), midjourneyConfig.getChannelId()));
return httpHeaders;
}
}

View File

@ -22,6 +22,7 @@ import java.io.IOException;
import java.net.URI;
import java.util.concurrent.TimeoutException;
// TODO @fansilimj 这块 websocket 有点小复杂虽然代码量 400 多行感觉可以考虑有没第三方 sdk通过它透明接入 mj
@Slf4j
public class MjWebSocketStarter implements WebSocketStarter {
/**

View File

@ -2,6 +2,8 @@
* author: fansili
* time: 2024/3/12 20:29
*
* TODO @fansili包的想法需要重点看看
*
* 1. org.springframework.ai包括 chatimagemodelparserutil 部分
*
* 2. yudao.framework.models

View File

@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import java.io.IOException;
// TODO @fansili看看能不能用 JsonUtils
/**
* Jackson工具类
*