mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-08-15 02:31:53 +08:00
【代码评审】AI:移除老版本的 MJ 接入
This commit is contained in:
@@ -1,123 +0,0 @@
|
||||
package cn.iocoder.yudao.module.ai.service.image.midjourneyHandler;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.iocoder.yudao.module.ai.controller.admin.image.vo.AiImageMidjourneyOperationsVO;
|
||||
import cn.iocoder.yudao.module.ai.dal.dataobject.image.AiImageDO;
|
||||
import cn.iocoder.yudao.module.ai.dal.mysql.image.AiImageMapper;
|
||||
import cn.iocoder.yudao.module.ai.enums.image.AiImageStatusEnum;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.ai.models.midjourney.MidjourneyMessage;
|
||||
import org.springframework.ai.models.midjourney.constants.MidjourneyGennerateStatusEnum;
|
||||
import org.springframework.ai.models.midjourney.webSocket.MidjourneyMessageHandler;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* yudao message handler
|
||||
*
|
||||
* @author fansili
|
||||
* @time 2024/4/29 14:34
|
||||
* @since 1.0
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
@AllArgsConstructor
|
||||
public class YuDaoMidjourneyMessageHandler implements MidjourneyMessageHandler {
|
||||
|
||||
private final AiImageMapper aiImageMapper;
|
||||
|
||||
@Override
|
||||
public void messageHandler(MidjourneyMessage midjourneyMessage) {
|
||||
log.info("yudao-midjourney-midjourney-message-handler {}", JSON.toJSONString(midjourneyMessage));
|
||||
if (midjourneyMessage.getContent() != null) {
|
||||
log.info("进度id {} 状态 {} 进度 {}",
|
||||
midjourneyMessage.getNonce(),
|
||||
midjourneyMessage.getGenerateStatus(),
|
||||
midjourneyMessage.getContent().getProgress());
|
||||
}
|
||||
//
|
||||
updateImage(midjourneyMessage);
|
||||
}
|
||||
|
||||
private void updateImage(MidjourneyMessage midjourneyMessage) {
|
||||
// Nonce 不存在不更新
|
||||
if (StrUtil.isBlank(midjourneyMessage.getNonce())) {
|
||||
return;
|
||||
}
|
||||
// 根据 Embeds 来判断是否异常
|
||||
if (CollUtil.isEmpty(midjourneyMessage.getEmbeds())) {
|
||||
successHandler(midjourneyMessage);
|
||||
} else {
|
||||
errorHandler(midjourneyMessage);
|
||||
}
|
||||
}
|
||||
|
||||
private void errorHandler(MidjourneyMessage midjourneyMessage) {
|
||||
// image 编号
|
||||
Long nonceId = Long.valueOf(midjourneyMessage.getNonce());
|
||||
// 获取 error message
|
||||
String errorMessage = getErrorMessage(midjourneyMessage);
|
||||
aiImageMapper.updateByMjNonce(nonceId,
|
||||
new AiImageDO()
|
||||
.setErrorMessage(errorMessage)
|
||||
.setStatus(AiImageStatusEnum.FAIL.getStatus())
|
||||
);
|
||||
}
|
||||
|
||||
private String getErrorMessage(MidjourneyMessage midjourneyMessage) {
|
||||
StringBuilder errorMessage = new StringBuilder();
|
||||
for (MidjourneyMessage.Embed embed : midjourneyMessage.getEmbeds()) {
|
||||
errorMessage.append(embed.getDescription());
|
||||
}
|
||||
return errorMessage.toString();
|
||||
}
|
||||
|
||||
private void successHandler(MidjourneyMessage midjourneyMessage) {
|
||||
// 获取id
|
||||
Long nonceId = Long.valueOf(midjourneyMessage.getNonce());
|
||||
// TODO @芋艿 这个地方有问题,不能根据 nonce来更新,不返回这个信息(别人获取了 image-xxx-xx 后面一段hash,由于没有mj账号测试,暂不清楚。)
|
||||
// 获取生成 url
|
||||
String imageUrl = null;
|
||||
if (CollUtil.isNotEmpty(midjourneyMessage.getAttachments())) {
|
||||
imageUrl = midjourneyMessage.getAttachments().get(0).getUrl();
|
||||
}
|
||||
// 转换状态
|
||||
AiImageStatusEnum drawingStatusEnum = null;
|
||||
String generateStatus = midjourneyMessage.getGenerateStatus();
|
||||
if (MidjourneyGennerateStatusEnum.COMPLETED.getStatus().equals(generateStatus)) {
|
||||
drawingStatusEnum = AiImageStatusEnum.SUCCESS;
|
||||
} else if (MidjourneyGennerateStatusEnum.IN_PROGRESS.getStatus().equals(generateStatus)) {
|
||||
drawingStatusEnum = AiImageStatusEnum.IN_PROGRESS;
|
||||
}
|
||||
// else if (MidjourneyGennerateStatusEnum.WAITING.getStatus().equals(generateStatus)) {
|
||||
// drawingStatusEnum = AiImageStatusEnum.WAITING;
|
||||
// }
|
||||
// 获取 midjourneyOperations
|
||||
List<AiImageMidjourneyOperationsVO> midjourneyOperations = getMidjourneyOperationsList(midjourneyMessage);
|
||||
// 更新数据库
|
||||
aiImageMapper.updateByMjNonce(nonceId,
|
||||
new AiImageDO()
|
||||
.setStatus(drawingStatusEnum == null ? null : drawingStatusEnum.getStatus())
|
||||
// .setMjNonceId(midjourneyMessage.getId())
|
||||
// .setMjOperations(JsonUtils.toJsonString(midjourneyOperations))
|
||||
);
|
||||
}
|
||||
|
||||
private List<AiImageMidjourneyOperationsVO> getMidjourneyOperationsList(MidjourneyMessage midjourneyMessage) {
|
||||
// // 为空直接返回
|
||||
// if (CollUtil.isEmpty(midjourneyMessage.getComponents())) {
|
||||
// return Collections.emptyList();
|
||||
// }
|
||||
// // 将 component 转成 AiImageMidjourneyOperationsVO
|
||||
// return midjourneyMessage.getComponents().stream()
|
||||
// .map(componentType -> componentType.getComponents().stream()
|
||||
// .map(AiImageConvert.INSTANCE::convertAiImageMidjourneyOperationsVO)
|
||||
// .collect(Collectors.toList()))
|
||||
// .toList().stream().flatMap(List::stream).toList();
|
||||
return null;
|
||||
}
|
||||
}
|
@@ -1,75 +0,0 @@
|
||||
//package cn.iocoder.yudao.module.ai.mapper.typeHandler;
|
||||
//
|
||||
//import cn.hutool.core.util.StrUtil;
|
||||
//import cn.iocoder.yudao.framework.ai.AiPlatformEnum;
|
||||
//import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
|
||||
//import cn.iocoder.yudao.module.ai.dal.dataobject.AiChatModalDO;
|
||||
//import org.apache.ibatis.type.BaseTypeHandler;
|
||||
//import org.apache.ibatis.type.JdbcType;
|
||||
//import org.apache.ibatis.type.MappedTypes;
|
||||
//
|
||||
//import java.sql.CallableStatement;
|
||||
//import java.sql.PreparedStatement;
|
||||
//import java.sql.ResultSet;
|
||||
//import java.sql.SQLException;
|
||||
//
|
||||
///**
|
||||
// * chat modal config
|
||||
// *
|
||||
// * @author fansili
|
||||
// * @time 2024/5/6 11:18
|
||||
// * @since 1.0
|
||||
// */
|
||||
//@MappedTypes(value = AiChatModalDO.Config.class)
|
||||
//public class AiChatModelConfigTypeHandler extends BaseTypeHandler<AiChatModalDO.Config> {
|
||||
//
|
||||
// @Override
|
||||
// public void setNonNullParameter(PreparedStatement ps, int i, AiChatModalDO.Config parameter, JdbcType jdbcType) throws SQLException {
|
||||
// // 将 MyCustomType 转换为数据库类型并设置到 PreparedStatement 中
|
||||
// if (parameter == null) {
|
||||
// ps.setString(i, "");
|
||||
// } else {
|
||||
// ps.setString(i, JsonUtils.toJsonString(parameter));
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public AiChatModalDO.Config getNullableResult(ResultSet rs, String columnName) throws SQLException {
|
||||
// // 从 ResultSet 中获取数据库类型并转换为 MyCustomType
|
||||
// String str = rs.getString(columnName);
|
||||
// if (StrUtil.isBlank(str)) {
|
||||
// return null;
|
||||
// }
|
||||
// AiChatModalDO.Config config = JsonUtils.parseObject(str, AiChatModalDO.Config.class);
|
||||
// // 获取平台
|
||||
// AiPlatformEnum platformEnum = AiPlatformEnum.valueOfPlatform(config.getModelPlatform());
|
||||
// if (AiPlatformEnum.CHAT_PLATFORM_LIST.contains(platformEnum)) {
|
||||
// return JsonUtils.parseObject(str, AiChatModalDO.ChatConfig.class);
|
||||
// } else if (AiPlatformEnum.OPEN_AI_DALL == platformEnum) {
|
||||
// return JsonUtils.parseObject(str, AiChatModalDO.OpenAiImageConfig.class);
|
||||
// } else if (AiPlatformEnum.MIDJOURNEY == platformEnum) {
|
||||
// return JsonUtils.parseObject(str, AiChatModalDO.MidjourneyConfig.class);
|
||||
// }
|
||||
// throw new IllegalArgumentException("ai模型中config不能转换! json: " + str);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public AiChatModalDO.Config getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
|
||||
// // 从 ResultSet 中获取数据库类型并转换为 MyCustomType
|
||||
// String str = rs.getString(columnIndex);
|
||||
// if (StrUtil.isBlank(str)) {
|
||||
// return null;
|
||||
// }
|
||||
// return JsonUtils.parseObject(str, AiChatModalDO.Config.class);
|
||||
// }
|
||||
//
|
||||
// @Override
|
||||
// public AiChatModalDO.Config getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
|
||||
// // 从 CallableStatement 中获取数据库类型并转换为 MyCustomType
|
||||
// String str = cs.getString(columnIndex);
|
||||
// if (StrUtil.isBlank(str)) {
|
||||
// return null;
|
||||
// }
|
||||
// return JsonUtils.parseObject(str, AiChatModalDO.Config.class);
|
||||
// }
|
||||
//}
|
@@ -1,27 +0,0 @@
|
||||
|
||||
### 登录 详细使用 https://www.jetbrains.com/help/idea/testing-restful-web-services.html、https://www.cnblogs.com/crazymakercircle/p/14317222.html
|
||||
|
||||
|
||||
### 对话 - list
|
||||
GET {{baseUrl}}/admin-api/ai/chat/conversation/list
|
||||
Authorization: {{token}}
|
||||
|
||||
|
||||
### 对话 - 创建对话
|
||||
POST {{baseUrl}}/admin-api/ai/chat/conversation/create
|
||||
Content-Type: application/json
|
||||
Authorization: {{token}}
|
||||
|
||||
{
|
||||
"roleId": "9"
|
||||
}
|
||||
|
||||
### 对话 - id获取
|
||||
GET {{baseUrl}}/admin-api/ai/chat/conversation/get?id=1781604279872581647
|
||||
Authorization: {{token}}
|
||||
|
||||
|
||||
### 对话 - 删除
|
||||
DELETE {{baseUrl}}/admin-api/ai/chat/conversation/delete?id=1781604279872581647
|
||||
Authorization: {{token}}
|
||||
|
@@ -1,32 +0,0 @@
|
||||
|
||||
### chat call
|
||||
GET {{baseUrl}}/admin-api/ai/chat/model/list
|
||||
Authorization: {{token}}
|
||||
|
||||
|
||||
### chat call
|
||||
PUT {{baseUrl}}/admin-api/ai/chat/model/add
|
||||
Content-Type: application/json
|
||||
Authorization: {{token}}
|
||||
|
||||
{
|
||||
"keyId": "1",
|
||||
"name": "小红书Ai写作大模型4.0",
|
||||
"model": "ERNIE 4.0",
|
||||
"platform": "yiyan",
|
||||
"sort": 100
|
||||
}
|
||||
|
||||
### chat call
|
||||
POST {{baseUrl}}/admin-api/ai/chat/model/update
|
||||
Content-Type: application/json
|
||||
Authorization: {{token}}
|
||||
|
||||
{
|
||||
"id": 9,
|
||||
"keyId": "1",
|
||||
"name": "小红书Ai写作大模型3.5 8k",
|
||||
"model": "ERNIE-3.5-8K",
|
||||
"platform": "yiyan",
|
||||
"sort": 100
|
||||
}
|
@@ -1,56 +0,0 @@
|
||||
|
||||
### chat角色 - list
|
||||
GET {{baseUrl}}/admin-api/ai/chat/role/list?pageNo=1&pageSize=20&search=
|
||||
Authorization: {{token}}
|
||||
|
||||
### chat add
|
||||
PUT {{baseUrl}}/admin-api/ai/chat/role/add
|
||||
Content-Type: application/json
|
||||
Authorization: {{token}}
|
||||
|
||||
{
|
||||
"modelId": 9,
|
||||
"name": "小红书写作v1",
|
||||
"avatar": "http://baidu.com",
|
||||
"category": "writing",
|
||||
"description": "采用gpt3.5模型,拥有小红书优质作者写作经验。",
|
||||
"systemMessage": "你是一名优秀的小红书人文、风光作者,你热爱旅游,每去往一个城市你都会用美妙的文字抒写着这座城市的大街小巷,描述着这座城市的美好。",
|
||||
"publicStatus": 0,
|
||||
"sort": 0
|
||||
}
|
||||
|
||||
|
||||
### chat update
|
||||
POST {{baseUrl}}/admin-api/ai/chat/role/update
|
||||
Content-Type: application/json
|
||||
Authorization: {{token}}
|
||||
|
||||
{
|
||||
"id": 8,
|
||||
"modelId": 9,
|
||||
"name": "小红书写作v2",
|
||||
"avatar": "http://baidu.com",
|
||||
"category": "writing",
|
||||
"description": "采用gpt3.5模型,拥有小红书优质作者写作经验。",
|
||||
"systemMessage": "你是一名优秀的小红书人文、风光作者,你热爱旅游,每去往一个城市你都会用美妙的文字抒写着这座城市的大街小巷,描述着这座城市的美好。",
|
||||
"publicStatus": 0,
|
||||
"sort": 0,
|
||||
"status": 0
|
||||
}
|
||||
|
||||
### chat update
|
||||
POST {{baseUrl}}/admin-api/ai/chat/role/update-public-status
|
||||
Content-Type: application/json
|
||||
Authorization: {{token}}
|
||||
|
||||
{
|
||||
"id": "8",
|
||||
"publicStatus": true
|
||||
}
|
||||
|
||||
|
||||
### chat update
|
||||
DELETE {{baseUrl}}/admin-api/ai/chat/role/delete?id=8
|
||||
Authorization: {{token}}
|
||||
|
||||
|
@@ -1,9 +0,0 @@
|
||||
{
|
||||
"global": {
|
||||
"token": "--"
|
||||
},
|
||||
"dev": {
|
||||
"baseUrl": "http://127.0.0.1:48080",
|
||||
"token": "Bearer 66346cc899e243b081321bd1336a257a"
|
||||
}
|
||||
}
|
@@ -1,18 +0,0 @@
|
||||
|
||||
|
||||
### 登录
|
||||
|
||||
POST {{baseUrl}}/admin-api/system/auth/login
|
||||
Content-Type: application/json
|
||||
tenant-id: 1
|
||||
|
||||
{
|
||||
"username": "admin",
|
||||
"password": "123456",
|
||||
"captchaVerification": "PfcH6mgr8tpXuMWFjvW6YVaqrswIuwmWI5dsVZSg7sGpWtDCUbHuDEXl3cFB1+VvCC/rAkSwK8Fad52FSuncVg==",
|
||||
"socialCode": "1024",
|
||||
"socialState": "9b2ffbc1-7425-4155-9894-9d5c08541d62",
|
||||
"socialCodeValid": true
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user