Commit 32bc632947783ac18dd8618d57e216ac6174892c
1 parent
337ae045
处理todo
Showing
24 changed files
with
165 additions
and
268 deletions
yudao-module-ai/yudao-module-ai-api/src/main/java/cn/iocoder/yudao/module/ai/enums/AiModelEnum.java deleted
| 1 | -package cn.iocoder.yudao.module.ai.enums; | ||
| 2 | - | ||
| 3 | -import lombok.Getter; | ||
| 4 | - | ||
| 5 | -// TODO @fansili:1)类注释要加下;2)author 和 time 用 javadoc,@author 和 @since;3)@AllArgsConstructor 使用这个注解,去掉构造方法;4)value 改成 model 字段,然后注释都写下哈;5)message 改成 name,然后注释都写下哈 | ||
| 6 | -/** | ||
| 7 | - * author: fansili | ||
| 8 | - * time: 2024/3/4 12:36 | ||
| 9 | - */ | ||
| 10 | -@Getter | ||
| 11 | -public enum AiModelEnum { | ||
| 12 | - | ||
| 13 | - OPEN_AI_GPT_3_5("gpt-3.5-turbo", "GPT3.5"), | ||
| 14 | - OPEN_AI_GPT_4("gpt-4-turbo", "GPT4") | ||
| 15 | - | ||
| 16 | - ; | ||
| 17 | - | ||
| 18 | - AiModelEnum(String value, String message) { | ||
| 19 | - this.value = value; | ||
| 20 | - this.message = message; | ||
| 21 | - } | ||
| 22 | - | ||
| 23 | - // TODO @fan | ||
| 24 | - private String value; | ||
| 25 | - | ||
| 26 | - private String message; | ||
| 27 | - | ||
| 28 | -} |
yudao-module-ai/yudao-module-ai-api/src/main/java/cn/iocoder/yudao/module/ai/enums/OpenAiModelEnum.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.ai.enums; | ||
| 2 | + | ||
| 3 | +import lombok.AllArgsConstructor; | ||
| 4 | +import lombok.Getter; | ||
| 5 | + | ||
| 6 | +// TODO done @fansili:1)类注释要加下;2)author 和 time 用 javadoc,@author 和 @since;3)@AllArgsConstructor 使用这个注解,去掉构造方法;4)value 改成 model 字段,然后注释都写下哈;5)message 改成 name,然后注释都写下哈 | ||
| 7 | +/** | ||
| 8 | + * @author: fansili | ||
| 9 | + * @time: 2024/3/4 12:36 | ||
| 10 | + */ | ||
| 11 | +@Getter | ||
| 12 | +@AllArgsConstructor | ||
| 13 | +public enum OpenAiModelEnum { | ||
| 14 | + | ||
| 15 | + /** | ||
| 16 | + * open ai 3.5模型 | ||
| 17 | + */ | ||
| 18 | + OPEN_AI_GPT_3_5("gpt-3.5-turbo", "GPT3.5"), | ||
| 19 | + /** | ||
| 20 | + * open ai 4.0 收费模型 | ||
| 21 | + */ | ||
| 22 | + OPEN_AI_GPT_4("gpt-4-turbo", "GPT4") | ||
| 23 | + | ||
| 24 | + ; | ||
| 25 | + | ||
| 26 | + /** | ||
| 27 | + * 模型 - 用于参数传递 | ||
| 28 | + */ | ||
| 29 | + private String model; | ||
| 30 | + /** | ||
| 31 | + * 模型名字 - 用于展示 | ||
| 32 | + */ | ||
| 33 | + private String name; | ||
| 34 | + | ||
| 35 | +} |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/ChatController.java
| @@ -4,7 +4,7 @@ import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; | @@ -4,7 +4,7 @@ import cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil; | ||
| 4 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; | 4 | import cn.iocoder.yudao.framework.common.pojo.CommonResult; |
| 5 | import cn.iocoder.yudao.module.ai.ErrorCodeConstants; | 5 | import cn.iocoder.yudao.module.ai.ErrorCodeConstants; |
| 6 | import cn.iocoder.yudao.module.ai.controller.admin.vo.AiChatReqVO; | 6 | import cn.iocoder.yudao.module.ai.controller.admin.vo.AiChatReqVO; |
| 7 | -import cn.iocoder.yudao.module.ai.enums.AiModelEnum; | 7 | +import cn.iocoder.yudao.module.ai.enums.OpenAiModelEnum; |
| 8 | import io.swagger.v3.oas.annotations.Operation; | 8 | import io.swagger.v3.oas.annotations.Operation; |
| 9 | import io.swagger.v3.oas.annotations.tags.Tag; | 9 | import io.swagger.v3.oas.annotations.tags.Tag; |
| 10 | import jakarta.servlet.http.HttpServletResponse; | 10 | import jakarta.servlet.http.HttpServletResponse; |
| @@ -13,7 +13,6 @@ import org.springframework.ai.chat.ChatClient; | @@ -13,7 +13,6 @@ import org.springframework.ai.chat.ChatClient; | ||
| 13 | import org.springframework.ai.chat.ChatResponse; | 13 | import org.springframework.ai.chat.ChatResponse; |
| 14 | import org.springframework.ai.chat.prompt.Prompt; | 14 | import org.springframework.ai.chat.prompt.Prompt; |
| 15 | import org.springframework.ai.openai.OpenAiChatClient; | 15 | import org.springframework.ai.openai.OpenAiChatClient; |
| 16 | -import org.springframework.ai.openai.api.OpenAiApi; | ||
| 17 | import org.springframework.beans.factory.annotation.Autowired; | 16 | import org.springframework.beans.factory.annotation.Autowired; |
| 18 | import org.springframework.context.ApplicationContext; | 17 | import org.springframework.context.ApplicationContext; |
| 19 | import org.springframework.validation.annotation.Validated; | 18 | import org.springframework.validation.annotation.Validated; |
| @@ -23,16 +22,9 @@ import org.springframework.web.bind.annotation.RequestMapping; | @@ -23,16 +22,9 @@ import org.springframework.web.bind.annotation.RequestMapping; | ||
| 23 | import org.springframework.web.bind.annotation.RestController; | 22 | import org.springframework.web.bind.annotation.RestController; |
| 24 | import reactor.core.publisher.Flux; | 23 | import reactor.core.publisher.Flux; |
| 25 | 24 | ||
| 26 | -import java.util.Scanner; | ||
| 27 | import java.util.function.Consumer; | 25 | import java.util.function.Consumer; |
| 28 | 26 | ||
| 29 | -// TODO @fansili:有了 swagger 注释,就不用类注释了 | ||
| 30 | -/** | ||
| 31 | - * AI模块 | ||
| 32 | - * | ||
| 33 | - * author: fansili | ||
| 34 | - * time: 2024/3/3 20:28 | ||
| 35 | - */ | 27 | +// TODO done @fansili:有了 swagger 注释,就不用类注释了 |
| 36 | @Tag(name = "AI模块") | 28 | @Tag(name = "AI模块") |
| 37 | @RestController | 29 | @RestController |
| 38 | @RequestMapping("/ai-api") | 30 | @RequestMapping("/ai-api") |
| @@ -48,7 +40,7 @@ public class ChatController { | @@ -48,7 +40,7 @@ public class ChatController { | ||
| 48 | ChatClient chatClient = getChatClient(reqVO.getAiModel()); | 40 | ChatClient chatClient = getChatClient(reqVO.getAiModel()); |
| 49 | String res; | 41 | String res; |
| 50 | try { | 42 | try { |
| 51 | - res = chatClient.call(reqVO.getInputText()); | 43 | + res = chatClient.call(reqVO.getPrompt()); |
| 52 | } catch (Exception e) { | 44 | } catch (Exception e) { |
| 53 | res = e.getMessage(); | 45 | res = e.getMessage(); |
| 54 | } | 46 | } |
| @@ -59,33 +51,14 @@ public class ChatController { | @@ -59,33 +51,14 @@ public class ChatController { | ||
| 59 | @Operation(summary = "对话聊天chatStream", description = "简单的ai聊天") | 51 | @Operation(summary = "对话聊天chatStream", description = "简单的ai聊天") |
| 60 | public CommonResult chatStream(HttpServletResponse response, @RequestBody @Validated AiChatReqVO reqVO) throws InterruptedException { | 52 | public CommonResult chatStream(HttpServletResponse response, @RequestBody @Validated AiChatReqVO reqVO) throws InterruptedException { |
| 61 | OpenAiChatClient chatClient = applicationContext.getBean(OpenAiChatClient.class); | 53 | OpenAiChatClient chatClient = applicationContext.getBean(OpenAiChatClient.class); |
| 62 | - Flux<ChatResponse> chatResponse = chatClient.stream(new Prompt(reqVO.getInputText())); | 54 | + Flux<ChatResponse> chatResponse = chatClient.stream(new Prompt(reqVO.getPrompt())); |
| 63 | chatResponse.subscribe(new Consumer<ChatResponse>() { | 55 | chatResponse.subscribe(new Consumer<ChatResponse>() { |
| 64 | @Override | 56 | @Override |
| 65 | public void accept(ChatResponse chatResponse) { | 57 | public void accept(ChatResponse chatResponse) { |
| 66 | System.err.println(chatResponse.getResults().get(0).getOutput().getContent()); | 58 | System.err.println(chatResponse.getResults().get(0).getOutput().getContent()); |
| 67 | } | 59 | } |
| 68 | }); | 60 | }); |
| 69 | - return CommonResult.success("1"); | ||
| 70 | - } | ||
| 71 | - | ||
| 72 | - public static void main(String[] args) { | ||
| 73 | - OpenAiChatClient openAiChatClient = new OpenAiChatClient(new OpenAiApi("openkey")); | ||
| 74 | - Flux<ChatResponse> responseFlux = openAiChatClient.stream(new Prompt("最好的编程语言!")); | ||
| 75 | - long now = System.currentTimeMillis(); | ||
| 76 | - responseFlux.subscribe(new Consumer<ChatResponse>() { | ||
| 77 | - @Override | ||
| 78 | - public void accept(ChatResponse chatResponse) { | ||
| 79 | - if (chatResponse.getResults().get(0).getOutput() == null) { | ||
| 80 | - return; | ||
| 81 | - } | ||
| 82 | - System.err.println(chatResponse.getResults().get(0).getOutput().getContent()); | ||
| 83 | - } | ||
| 84 | - }); | ||
| 85 | - | ||
| 86 | - // 阻止退出 | ||
| 87 | - Scanner scanner = new Scanner(System.in); | ||
| 88 | - scanner.nextLine(); | 61 | + return CommonResult.success(null); |
| 89 | } | 62 | } |
| 90 | 63 | ||
| 91 | /** | 64 | /** |
| @@ -94,8 +67,8 @@ public class ChatController { | @@ -94,8 +67,8 @@ public class ChatController { | ||
| 94 | * @param aiModelEnum | 67 | * @param aiModelEnum |
| 95 | * @return | 68 | * @return |
| 96 | */ | 69 | */ |
| 97 | - private ChatClient getChatClient(AiModelEnum aiModelEnum) { | ||
| 98 | - if (AiModelEnum.OPEN_AI_GPT_3_5 == aiModelEnum) { | 70 | + private ChatClient getChatClient(OpenAiModelEnum aiModelEnum) { |
| 71 | + if (OpenAiModelEnum.OPEN_AI_GPT_3_5 == aiModelEnum) { | ||
| 99 | return applicationContext.getBean(OpenAiChatClient.class); | 72 | return applicationContext.getBean(OpenAiChatClient.class); |
| 100 | } | 73 | } |
| 101 | // AI模型暂不支持 | 74 | // AI模型暂不支持 |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/vo/AiChatReqVO.java
| 1 | package cn.iocoder.yudao.module.ai.controller.admin.vo; | 1 | package cn.iocoder.yudao.module.ai.controller.admin.vo; |
| 2 | 2 | ||
| 3 | -import cn.iocoder.yudao.module.ai.enums.AiModelEnum; | 3 | +import cn.iocoder.yudao.module.ai.enums.OpenAiModelEnum; |
| 4 | import io.swagger.v3.oas.annotations.media.Schema; | 4 | import io.swagger.v3.oas.annotations.media.Schema; |
| 5 | import jakarta.validation.constraints.NotNull; | 5 | import jakarta.validation.constraints.NotNull; |
| 6 | import lombok.Data; | 6 | import lombok.Data; |
| 7 | 7 | ||
| 8 | -// TODO @fansili 1)swagger 注释不太对;2)有了 swagger 注释,就不用类注释了 | ||
| 9 | -/** | ||
| 10 | - * ai 聊天 req | ||
| 11 | - * | ||
| 12 | - * author: fansili | ||
| 13 | - * time: 2024/3/4 12:33 | ||
| 14 | - */ | ||
| 15 | -@Schema(description = "用户 App - 上传文件 Request VO") | 8 | +// TODO done @fansili 1)swagger 注释不太对;2)有了 swagger 注释,就不用类注释了 |
| 16 | @Data | 9 | @Data |
| 10 | +@Schema(description = "用户 App - 上传文件 Request VO") | ||
| 17 | public class AiChatReqVO { | 11 | public class AiChatReqVO { |
| 18 | 12 | ||
| 19 | - @Schema(description = "输入内容", requiredMode = Schema.RequiredMode.REQUIRED) | ||
| 20 | - @NotNull(message = "输入内容不能为空") | ||
| 21 | - private String inputText; | 13 | + @Schema(description = "提示词", requiredMode = Schema.RequiredMode.REQUIRED) |
| 14 | + @NotNull(message = "提示词不能为空!") | ||
| 15 | + private String prompt; | ||
| 22 | 16 | ||
| 23 | @Schema(description = "AI模型", requiredMode = Schema.RequiredMode.REQUIRED) | 17 | @Schema(description = "AI模型", requiredMode = Schema.RequiredMode.REQUIRED) |
| 24 | @NotNull(message = "AI模型不能为空") | 18 | @NotNull(message = "AI模型不能为空") |
| 25 | - private AiModelEnum aiModel; | 19 | + private OpenAiModelEnum aiModel; |
| 26 | 20 | ||
| 27 | } | 21 | } |
yudao-module-ai/yudao-spring-boot-starter-ai/pom.xml
| @@ -149,6 +149,10 @@ | @@ -149,6 +149,10 @@ | ||
| 149 | <!-- </exclusion>--> | 149 | <!-- </exclusion>--> |
| 150 | <!-- </exclusions>--> | 150 | <!-- </exclusions>--> |
| 151 | </dependency> | 151 | </dependency> |
| 152 | + <dependency> | ||
| 153 | + <groupId>cn.iocoder.boot</groupId> | ||
| 154 | + <artifactId>yudao-common</artifactId> | ||
| 155 | + </dependency> | ||
| 152 | </dependencies> | 156 | </dependencies> |
| 153 | 157 | ||
| 154 | </project> | 158 | </project> |
| 155 | \ No newline at end of file | 159 | \ No newline at end of file |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatqianwen/QianWenChatClient.java
| @@ -6,6 +6,7 @@ import cn.iocoder.yudao.framework.ai.chat.*; | @@ -6,6 +6,7 @@ import cn.iocoder.yudao.framework.ai.chat.*; | ||
| 6 | import cn.iocoder.yudao.framework.ai.chat.messages.MessageType; | 6 | import cn.iocoder.yudao.framework.ai.chat.messages.MessageType; |
| 7 | import cn.iocoder.yudao.framework.ai.chat.prompt.ChatOptions; | 7 | import cn.iocoder.yudao.framework.ai.chat.prompt.ChatOptions; |
| 8 | import cn.iocoder.yudao.framework.ai.chat.prompt.Prompt; | 8 | import cn.iocoder.yudao.framework.ai.chat.prompt.Prompt; |
| 9 | +import cn.iocoder.yudao.framework.ai.chatqianwen.api.QianWenApi; | ||
| 9 | import cn.iocoder.yudao.framework.ai.chatyiyan.exception.YiYanApiException; | 10 | import cn.iocoder.yudao.framework.ai.chatyiyan.exception.YiYanApiException; |
| 10 | import com.aliyun.broadscope.bailian.sdk.models.*; | 11 | import com.aliyun.broadscope.bailian.sdk.models.*; |
| 11 | import lombok.extern.slf4j.Slf4j; | 12 | import lombok.extern.slf4j.Slf4j; |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatqianwen/QianWenApi.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/chatqianwen/api/QianWenApi.java
| 1 | -package cn.iocoder.yudao.framework.ai.chatqianwen; | 1 | +package cn.iocoder.yudao.framework.ai.chatqianwen.api; |
| 2 | 2 | ||
| 3 | import com.aliyun.broadscope.bailian.sdk.AccessTokenClient; | 3 | import com.aliyun.broadscope.bailian.sdk.AccessTokenClient; |
| 4 | import com.aliyun.broadscope.bailian.sdk.ApplicationClient; | 4 | import com.aliyun.broadscope.bailian.sdk.ApplicationClient; |
| @@ -9,7 +9,7 @@ import org.springframework.http.HttpStatusCode; | @@ -9,7 +9,7 @@ import org.springframework.http.HttpStatusCode; | ||
| 9 | import org.springframework.http.ResponseEntity; | 9 | import org.springframework.http.ResponseEntity; |
| 10 | import reactor.core.publisher.Flux; | 10 | import reactor.core.publisher.Flux; |
| 11 | 11 | ||
| 12 | -// TODO @fansili:是不是挪到 api 包里?按照 spring ai 的结构;根目录只放 client 和 options | 12 | +// TODO done @fansili:是不是挪到 api 包里?按照 spring ai 的结构;根目录只放 client 和 options |
| 13 | /** | 13 | /** |
| 14 | * 阿里 通义千问 | 14 | * 阿里 通义千问 |
| 15 | * | 15 | * |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/imageopenai/OpenAiImageApi.java
| @@ -4,6 +4,7 @@ import cn.hutool.json.JSONUtil; | @@ -4,6 +4,7 @@ import cn.hutool.json.JSONUtil; | ||
| 4 | import cn.iocoder.yudao.framework.ai.imageopenai.api.OpenAiImageRequest; | 4 | import cn.iocoder.yudao.framework.ai.imageopenai.api.OpenAiImageRequest; |
| 5 | import cn.iocoder.yudao.framework.ai.imageopenai.api.OpenAiImageResponse; | 5 | import cn.iocoder.yudao.framework.ai.imageopenai.api.OpenAiImageResponse; |
| 6 | import cn.iocoder.yudao.framework.ai.util.JacksonUtil; | 6 | import cn.iocoder.yudao.framework.ai.util.JacksonUtil; |
| 7 | +import cn.iocoder.yudao.framework.common.util.json.JsonUtils; | ||
| 7 | import io.netty.channel.ChannelOption; | 8 | import io.netty.channel.ChannelOption; |
| 8 | import lombok.extern.slf4j.Slf4j; | 9 | import lombok.extern.slf4j.Slf4j; |
| 9 | import org.apache.http.HttpEntity; | 10 | import org.apache.http.HttpEntity; |
| @@ -55,7 +56,7 @@ public class OpenAiImageApi { | @@ -55,7 +56,7 @@ public class OpenAiImageApi { | ||
| 55 | httpPost.setURI(URI.create(DEFAULT_BASE_URL.concat("/v1/images/generations"))); | 56 | httpPost.setURI(URI.create(DEFAULT_BASE_URL.concat("/v1/images/generations"))); |
| 56 | httpPost.setHeader("Content-Type", "application/json"); | 57 | httpPost.setHeader("Content-Type", "application/json"); |
| 57 | httpPost.setHeader("Authorization", "Bearer " + apiKey); | 58 | httpPost.setHeader("Authorization", "Bearer " + apiKey); |
| 58 | - httpPost.setEntity(new StringEntity(JacksonUtil.toJson(request), "UTF-8")); | 59 | + httpPost.setEntity(new StringEntity(JsonUtils.toJsonString(request), "UTF-8")); |
| 59 | 60 | ||
| 60 | CloseableHttpResponse response= null; | 61 | CloseableHttpResponse response= null; |
| 61 | try { | 62 | try { |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/MjMessage.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/MidjourneyMessage.java
| 1 | package cn.iocoder.yudao.framework.ai.midjourney; | 1 | package cn.iocoder.yudao.framework.ai.midjourney; |
| 2 | 2 | ||
| 3 | +import cn.iocoder.yudao.framework.ai.midjourney.constants.MidjourneyGennerateStatusEnum; | ||
| 3 | import lombok.Data; | 4 | import lombok.Data; |
| 4 | import lombok.experimental.Accessors; | 5 | import lombok.experimental.Accessors; |
| 5 | 6 | ||
| @@ -7,7 +8,7 @@ import java.util.List; | @@ -7,7 +8,7 @@ import java.util.List; | ||
| 7 | 8 | ||
| 8 | @Data | 9 | @Data |
| 9 | @Accessors(chain = true) | 10 | @Accessors(chain = true) |
| 10 | -public class MjMessage { | 11 | +public class MidjourneyMessage { |
| 11 | 12 | ||
| 12 | /** | 13 | /** |
| 13 | * id是一个重要的字段,在同时生成多个的时候,可以区分生成信息 | 14 | * id是一个重要的字段,在同时生成多个的时候,可以区分生成信息 |
| @@ -41,7 +42,7 @@ public class MjMessage { | @@ -41,7 +42,7 @@ public class MjMessage { | ||
| 41 | * 1、等待 | 42 | * 1、等待 |
| 42 | * 2、进行中 | 43 | * 2、进行中 |
| 43 | * 3、完成 | 44 | * 3、完成 |
| 44 | - * {@link cn.iocoder.yudao.framework.ai.midjourney.constants.MjGennerateStatusEnum} | 45 | + * {@link MidjourneyGennerateStatusEnum} |
| 45 | */ | 46 | */ |
| 46 | private String generateStatus; | 47 | private String generateStatus; |
| 47 | 48 |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/constants/MjConstants.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/constants/MidjourneyConstants.java
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/constants/MjGennerateStatusEnum.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/constants/MidjourneyGennerateStatusEnum.java
| 1 | package cn.iocoder.yudao.framework.ai.midjourney.constants; | 1 | package cn.iocoder.yudao.framework.ai.midjourney.constants; |
| 2 | 2 | ||
| 3 | +import lombok.AllArgsConstructor; | ||
| 3 | import lombok.Getter; | 4 | import lombok.Getter; |
| 4 | 5 | ||
| 5 | -// TODO @fansili:1)Mj 缩写,还是搞成全称。。虽然长一点,但是感觉会相对清晰一些哈;2)lombok 相关的注解,可以用用哈;3)value 改 status; | 6 | +// TODO done @fansili:1)Mj 缩写,还是搞成全称。。虽然长一点,但是感觉会相对清晰一些哈;2)lombok 相关的注解,可以用用哈;3)value 改 status; |
| 6 | /** | 7 | /** |
| 7 | * mj 生成状态 | 8 | * mj 生成状态 |
| 8 | * | 9 | * |
| @@ -10,8 +11,8 @@ import lombok.Getter; | @@ -10,8 +11,8 @@ import lombok.Getter; | ||
| 10 | * time: 2024/4/6 21:07 | 11 | * time: 2024/4/6 21:07 |
| 11 | */ | 12 | */ |
| 12 | @Getter | 13 | @Getter |
| 13 | -public enum MjGennerateStatusEnum { | ||
| 14 | - | 14 | +@AllArgsConstructor |
| 15 | +public enum MidjourneyGennerateStatusEnum { | ||
| 15 | 16 | ||
| 16 | WAITING("waiting", "等待..."), | 17 | WAITING("waiting", "等待..."), |
| 17 | IN_PROGRESS("in_progress", "进行中"), | 18 | IN_PROGRESS("in_progress", "进行中"), |
| @@ -19,12 +20,12 @@ public enum MjGennerateStatusEnum { | @@ -19,12 +20,12 @@ public enum MjGennerateStatusEnum { | ||
| 19 | 20 | ||
| 20 | ; | 21 | ; |
| 21 | 22 | ||
| 22 | - MjGennerateStatusEnum(String value, String message) { | ||
| 23 | - this.value = value; | ||
| 24 | - this.message = message; | ||
| 25 | - } | ||
| 26 | - | ||
| 27 | - private String value; | ||
| 28 | - | 23 | + /** |
| 24 | + * 状态 | ||
| 25 | + */ | ||
| 26 | + private String status; | ||
| 27 | + /** | ||
| 28 | + * 状态信息 | ||
| 29 | + */ | ||
| 29 | private String message; | 30 | private String message; |
| 30 | } | 31 | } |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/constants/MjInteractionsEnum.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/constants/MidjourneyInteractionsEnum.java
| @@ -6,7 +6,7 @@ import lombok.Getter; | @@ -6,7 +6,7 @@ import lombok.Getter; | ||
| 6 | * MJ 命令 | 6 | * MJ 命令 |
| 7 | */ | 7 | */ |
| 8 | @Getter | 8 | @Getter |
| 9 | -public enum MjInteractionsEnum { | 9 | +public enum MidjourneyInteractionsEnum { |
| 10 | 10 | ||
| 11 | IMAGINE("imagine", "生成图片"), | 11 | IMAGINE("imagine", "生成图片"), |
| 12 | DESCRIBE("describe", "生成描述"), | 12 | DESCRIBE("describe", "生成描述"), |
| @@ -17,7 +17,7 @@ public enum MjInteractionsEnum { | @@ -17,7 +17,7 @@ public enum MjInteractionsEnum { | ||
| 17 | 17 | ||
| 18 | ; | 18 | ; |
| 19 | 19 | ||
| 20 | - MjInteractionsEnum(String value, String message) { | 20 | + MidjourneyInteractionsEnum(String value, String message) { |
| 21 | this.value =value; | 21 | this.value =value; |
| 22 | this.message =message; | 22 | this.message =message; |
| 23 | } | 23 | } |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/constants/MjMessageTypeEnum.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/constants/MidjourneyMessageTypeEnum.java
| 1 | package cn.iocoder.yudao.framework.ai.midjourney.constants; | 1 | package cn.iocoder.yudao.framework.ai.midjourney.constants; |
| 2 | 2 | ||
| 3 | 3 | ||
| 4 | -public enum MjMessageTypeEnum { | 4 | +public enum MidjourneyMessageTypeEnum { |
| 5 | /** | 5 | /** |
| 6 | * 创建. | 6 | * 创建. |
| 7 | */ | 7 | */ |
| @@ -15,7 +15,7 @@ public enum MjMessageTypeEnum { | @@ -15,7 +15,7 @@ public enum MjMessageTypeEnum { | ||
| 15 | */ | 15 | */ |
| 16 | DELETE; | 16 | DELETE; |
| 17 | 17 | ||
| 18 | - public static MjMessageTypeEnum of(String type) { | 18 | + public static MidjourneyMessageTypeEnum of(String type) { |
| 19 | return switch (type) { | 19 | return switch (type) { |
| 20 | case "MESSAGE_CREATE" -> CREATE; | 20 | case "MESSAGE_CREATE" -> CREATE; |
| 21 | case "MESSAGE_UPDATE" -> UPDATE; | 21 | case "MESSAGE_UPDATE" -> UPDATE; |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/constants/MjNotifyCode.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/constants/MidjourneyNotifyCode.java
| @@ -3,7 +3,7 @@ package cn.iocoder.yudao.framework.ai.midjourney.constants; | @@ -3,7 +3,7 @@ package cn.iocoder.yudao.framework.ai.midjourney.constants; | ||
| 3 | import lombok.experimental.UtilityClass; | 3 | import lombok.experimental.UtilityClass; |
| 4 | 4 | ||
| 5 | @UtilityClass | 5 | @UtilityClass |
| 6 | -public final class MjNotifyCode { | 6 | +public final class MidjourneyNotifyCode { |
| 7 | /** | 7 | /** |
| 8 | * 成功. | 8 | * 成功. |
| 9 | */ | 9 | */ |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/interactions/MjInteractions.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/interactions/MidjourneyInteractions.java
| @@ -3,8 +3,8 @@ package cn.iocoder.yudao.framework.ai.midjourney.interactions; | @@ -3,8 +3,8 @@ package cn.iocoder.yudao.framework.ai.midjourney.interactions; | ||
| 3 | import cn.hutool.core.util.IdUtil; | 3 | import cn.hutool.core.util.IdUtil; |
| 4 | import cn.hutool.core.util.StrUtil; | 4 | import cn.hutool.core.util.StrUtil; |
| 5 | import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyConfig; | 5 | import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyConfig; |
| 6 | -import cn.iocoder.yudao.framework.ai.midjourney.constants.MjConstants; | ||
| 7 | -import cn.iocoder.yudao.framework.ai.midjourney.util.MjUtil; | 6 | +import cn.iocoder.yudao.framework.ai.midjourney.constants.MidjourneyConstants; |
| 7 | +import cn.iocoder.yudao.framework.ai.midjourney.util.MidjourneyUtil; | ||
| 8 | import cn.iocoder.yudao.framework.ai.midjourney.vo.Attachments; | 8 | import cn.iocoder.yudao.framework.ai.midjourney.vo.Attachments; |
| 9 | import cn.iocoder.yudao.framework.ai.midjourney.vo.Describe; | 9 | import cn.iocoder.yudao.framework.ai.midjourney.vo.Describe; |
| 10 | import cn.iocoder.yudao.framework.ai.midjourney.vo.ReRoll; | 10 | import cn.iocoder.yudao.framework.ai.midjourney.vo.ReRoll; |
| @@ -32,15 +32,17 @@ import java.util.HashMap; | @@ -32,15 +32,17 @@ import java.util.HashMap; | ||
| 32 | * time: 2024/4/3 17:36 | 32 | * time: 2024/4/3 17:36 |
| 33 | */ | 33 | */ |
| 34 | @Slf4j | 34 | @Slf4j |
| 35 | -public class MjInteractions { | 35 | +public class MidjourneyInteractions { |
| 36 | + | ||
| 37 | + // TODO done @fansili:静态变量,放在最前面哈; | ||
| 38 | + private static final String HEADER_REFERER = "https://discord.com/channels/%s/%s"; | ||
| 36 | 39 | ||
| 37 | private final String url; | 40 | private final String url; |
| 38 | private final MidjourneyConfig midjourneyConfig; | 41 | private final MidjourneyConfig midjourneyConfig; |
| 39 | private final RestTemplate restTemplate = new RestTemplate(); // TODO @fansili:优先级低:后续搞到统一的管理 | 42 | private final RestTemplate restTemplate = new RestTemplate(); // TODO @fansili:优先级低:后续搞到统一的管理 |
| 40 | - // TODO @fansili:静态变量,放在最前面哈; | ||
| 41 | - private static final String HEADER_REFERER = "https://discord.com/channels/%s/%s"; | ||
| 42 | 43 | ||
| 43 | - public MjInteractions(MidjourneyConfig midjourneyConfig) { | 44 | + |
| 45 | + public MidjourneyInteractions(MidjourneyConfig midjourneyConfig) { | ||
| 44 | this.midjourneyConfig = midjourneyConfig; | 46 | this.midjourneyConfig = midjourneyConfig; |
| 45 | this.url = midjourneyConfig.getServerUrl().concat(midjourneyConfig.getApiInteractions()); | 47 | this.url = midjourneyConfig.getServerUrl().concat(midjourneyConfig.getApiInteractions()); |
| 46 | } | 48 | } |
| @@ -57,7 +59,7 @@ public class MjInteractions { | @@ -57,7 +59,7 @@ public class MjInteractions { | ||
| 57 | requestParams.put("nonce", String.valueOf(IdUtil.getSnowflakeNextId())); // TODO @fansili:建议用 uuid 之类的;nextId 跨进程未必合适哈; | 59 | requestParams.put("nonce", String.valueOf(IdUtil.getSnowflakeNextId())); // TODO @fansili:建议用 uuid 之类的;nextId 跨进程未必合适哈; |
| 58 | requestParams.put("prompt", prompt); | 60 | requestParams.put("prompt", prompt); |
| 59 | // 解析 template 参数占位符 | 61 | // 解析 template 参数占位符 |
| 60 | - String requestBody = MjUtil.parseTemplate(requestTemplate, requestParams); | 62 | + String requestBody = MidjourneyUtil.parseTemplate(requestTemplate, requestParams); |
| 61 | // 获取 header | 63 | // 获取 header |
| 62 | HttpHeaders httpHeaders = getHttpHeaders(); | 64 | HttpHeaders httpHeaders = getHttpHeaders(); |
| 63 | // 发送请求 | 65 | // 发送请求 |
| @@ -65,14 +67,14 @@ public class MjInteractions { | @@ -65,14 +67,14 @@ public class MjInteractions { | ||
| 65 | String res = restTemplate.postForObject(url, requestEntity, String.class); | 67 | String res = restTemplate.postForObject(url, requestEntity, String.class); |
| 66 | // 这个 res 只要不返回值,就是成功! | 68 | // 这个 res 只要不返回值,就是成功! |
| 67 | // TODO @fansili:可以直接 if (StrUtil.isBlank(res)) | 69 | // TODO @fansili:可以直接 if (StrUtil.isBlank(res)) |
| 68 | - boolean isSuccess = StrUtil.isBlank(res); | ||
| 69 | - if (isSuccess) { | 70 | + if (StrUtil.isBlank(res)) { |
| 70 | return true; | 71 | return true; |
| 72 | + } else { | ||
| 73 | + log.error("请求失败! 请求参数:{} 返回结果! {}", requestBody, res); | ||
| 74 | + return false; | ||
| 71 | } | 75 | } |
| 72 | - log.error("请求失败! 请求参数:{} 返回结果! {}", requestBody, res); | ||
| 73 | - return isSuccess; | ||
| 74 | } | 76 | } |
| 75 | - // TODO @fansili:方法和方法之间,空一行哈; | 77 | + // TODO done @fansili:方法和方法之间,空一行哈; |
| 76 | 78 | ||
| 77 | 79 | ||
| 78 | public Boolean reRoll(ReRoll reRoll) { | 80 | public Boolean reRoll(ReRoll reRoll) { |
| @@ -89,7 +91,7 @@ public class MjInteractions { | @@ -89,7 +91,7 @@ public class MjInteractions { | ||
| 89 | // 获取 header | 91 | // 获取 header |
| 90 | HttpHeaders httpHeaders = getHttpHeaders(); | 92 | HttpHeaders httpHeaders = getHttpHeaders(); |
| 91 | // 设置参数 | 93 | // 设置参数 |
| 92 | - String requestBody = MjUtil.parseTemplate(requestTemplate, requestParams); | 94 | + String requestBody = MidjourneyUtil.parseTemplate(requestTemplate, requestParams); |
| 93 | // 发送请求 | 95 | // 发送请求 |
| 94 | HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, httpHeaders); | 96 | HttpEntity<String> requestEntity = new HttpEntity<>(requestBody, httpHeaders); |
| 95 | String res = restTemplate.postForObject(url, requestEntity, String.class); | 97 | String res = restTemplate.postForObject(url, requestEntity, String.class); |
| @@ -123,7 +125,7 @@ public class MjInteractions { | @@ -123,7 +125,7 @@ public class MjInteractions { | ||
| 123 | httpHeaders.setContentType(MediaType.APPLICATION_JSON); | 125 | httpHeaders.setContentType(MediaType.APPLICATION_JSON); |
| 124 | httpHeaders.set("Authorization", midjourneyConfig.getToken()); | 126 | httpHeaders.set("Authorization", midjourneyConfig.getToken()); |
| 125 | httpHeaders.set("User-Agent", midjourneyConfig.getUserAage()); | 127 | httpHeaders.set("User-Agent", midjourneyConfig.getUserAage()); |
| 126 | - httpHeaders.set("Cookie", MjConstants.HTTP_COOKIE); | 128 | + httpHeaders.set("Cookie", MidjourneyConstants.HTTP_COOKIE); |
| 127 | httpHeaders.set("Referer", String.format(HEADER_REFERER, midjourneyConfig.getGuildId(), midjourneyConfig.getChannelId())); | 129 | httpHeaders.set("Referer", String.format(HEADER_REFERER, midjourneyConfig.getGuildId(), midjourneyConfig.getChannelId())); |
| 128 | // 创建HttpEntity对象,包含表单数据和头部信息 | 130 | // 创建HttpEntity对象,包含表单数据和头部信息 |
| 129 | HttpEntity<MultiValueMap<String, Object>> multiValueMapHttpEntity = new HttpEntity<>(multipartRequest, httpHeaders); | 131 | HttpEntity<MultiValueMap<String, Object>> multiValueMapHttpEntity = new HttpEntity<>(multipartRequest, httpHeaders); |
| @@ -132,16 +134,13 @@ public class MjInteractions { | @@ -132,16 +134,13 @@ public class MjInteractions { | ||
| 132 | String response = restTemplate.postForObject(midjourneyConfig.getServerUrl().concat(uri), multiValueMapHttpEntity, String.class); | 134 | String response = restTemplate.postForObject(midjourneyConfig.getServerUrl().concat(uri), multiValueMapHttpEntity, String.class); |
| 133 | UploadAttachmentsRes uploadAttachmentsRes = JSON.parseObject(response, UploadAttachmentsRes.class); | 135 | UploadAttachmentsRes uploadAttachmentsRes = JSON.parseObject(response, UploadAttachmentsRes.class); |
| 134 | 136 | ||
| 135 | - | ||
| 136 | // | 137 | // |
| 137 | // 上传文件 | 138 | // 上传文件 |
| 138 | String uploadUrl = uploadAttachmentsRes.getAttachments().getFirst().getUploadUrl(); | 139 | String uploadUrl = uploadAttachmentsRes.getAttachments().getFirst().getUploadUrl(); |
| 139 | - String uploadAttachmentsUrl = midjourneyConfig.getApiAttachmentsUpload().concat(uploadUrl); | ||
| 140 | httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); | 140 | httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); |
| 141 | HttpEntity<FileSystemResource> fileSystemResourceHttpEntity = new HttpEntity<>(attachments.getFileSystemResource(), httpHeaders); | 141 | HttpEntity<FileSystemResource> fileSystemResourceHttpEntity = new HttpEntity<>(attachments.getFileSystemResource(), httpHeaders); |
| 142 | ResponseEntity<String> exchange = restTemplate.exchange(uploadUrl, HttpMethod.PUT, fileSystemResourceHttpEntity, String.class); | 142 | ResponseEntity<String> exchange = restTemplate.exchange(uploadUrl, HttpMethod.PUT, fileSystemResourceHttpEntity, String.class); |
| 143 | String uploadRes = exchange.getBody(); | 143 | String uploadRes = exchange.getBody(); |
| 144 | - | ||
| 145 | return uploadAttachmentsRes; | 144 | return uploadAttachmentsRes; |
| 146 | } | 145 | } |
| 147 | 146 | ||
| @@ -161,9 +160,9 @@ public class MjInteractions { | @@ -161,9 +160,9 @@ public class MjInteractions { | ||
| 161 | httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); // 设置内容类型为JSON | 160 | httpHeaders.setContentType(MediaType.MULTIPART_FORM_DATA); // 设置内容类型为JSON |
| 162 | httpHeaders.set("Authorization", midjourneyConfig.getToken()); | 161 | httpHeaders.set("Authorization", midjourneyConfig.getToken()); |
| 163 | httpHeaders.set("User-Agent", midjourneyConfig.getUserAage()); | 162 | httpHeaders.set("User-Agent", midjourneyConfig.getUserAage()); |
| 164 | - httpHeaders.set("Cookie", MjConstants.HTTP_COOKIE); | 163 | + httpHeaders.set("Cookie", MidjourneyConstants.HTTP_COOKIE); |
| 165 | httpHeaders.set("Referer", String.format(HEADER_REFERER, midjourneyConfig.getGuildId(), midjourneyConfig.getChannelId())); | 164 | httpHeaders.set("Referer", String.format(HEADER_REFERER, midjourneyConfig.getGuildId(), midjourneyConfig.getChannelId())); |
| 166 | - String requestBody = MjUtil.parseTemplate(requestTemplate, requestParams); | 165 | + String requestBody = MidjourneyUtil.parseTemplate(requestTemplate, requestParams); |
| 167 | // 创建表单数据 | 166 | // 创建表单数据 |
| 168 | MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(); | 167 | MultiValueMap<String, String> formData = new LinkedMultiValueMap<>(); |
| 169 | formData.add("payload_json", requestBody); | 168 | formData.add("payload_json", requestBody); |
| @@ -185,7 +184,7 @@ public class MjInteractions { | @@ -185,7 +184,7 @@ public class MjInteractions { | ||
| 185 | httpHeaders.setContentType(MediaType.APPLICATION_JSON); // 设置内容类型为JSON | 184 | httpHeaders.setContentType(MediaType.APPLICATION_JSON); // 设置内容类型为JSON |
| 186 | httpHeaders.set("Authorization", midjourneyConfig.getToken()); | 185 | httpHeaders.set("Authorization", midjourneyConfig.getToken()); |
| 187 | httpHeaders.set("User-Agent", midjourneyConfig.getUserAage()); | 186 | httpHeaders.set("User-Agent", midjourneyConfig.getUserAage()); |
| 188 | - httpHeaders.set("Cookie", MjConstants.HTTP_COOKIE); | 187 | + httpHeaders.set("Cookie", MidjourneyConstants.HTTP_COOKIE); |
| 189 | httpHeaders.set("Referer", String.format(HEADER_REFERER, midjourneyConfig.getGuildId(), midjourneyConfig.getChannelId())); | 188 | httpHeaders.set("Referer", String.format(HEADER_REFERER, midjourneyConfig.getGuildId(), midjourneyConfig.getChannelId())); |
| 190 | return httpHeaders; | 189 | return httpHeaders; |
| 191 | } | 190 | } |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/util/MjUtil.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/util/MidjourneyUtil.java
| 1 | package cn.iocoder.yudao.framework.ai.midjourney.util; | 1 | package cn.iocoder.yudao.framework.ai.midjourney.util; |
| 2 | 2 | ||
| 3 | import cn.hutool.core.text.CharSequenceUtil; | 3 | import cn.hutool.core.text.CharSequenceUtil; |
| 4 | -import cn.iocoder.yudao.framework.ai.midjourney.MjMessage; | 4 | +import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyMessage; |
| 5 | 5 | ||
| 6 | import java.util.Map; | 6 | import java.util.Map; |
| 7 | import java.util.regex.Matcher; | 7 | import java.util.regex.Matcher; |
| @@ -13,7 +13,7 @@ import java.util.regex.Pattern; | @@ -13,7 +13,7 @@ import java.util.regex.Pattern; | ||
| 13 | * author: fansili | 13 | * author: fansili |
| 14 | * time: 2024/4/6 19:00 | 14 | * time: 2024/4/6 19:00 |
| 15 | */ | 15 | */ |
| 16 | -public class MjUtil { | 16 | +public class MidjourneyUtil { |
| 17 | /** | 17 | /** |
| 18 | * content正则匹配prompt和进度. | 18 | * content正则匹配prompt和进度. |
| 19 | */ | 19 | */ |
| @@ -26,12 +26,12 @@ public class MjUtil { | @@ -26,12 +26,12 @@ public class MjUtil { | ||
| 26 | * @param content | 26 | * @param content |
| 27 | * @return | 27 | * @return |
| 28 | */ | 28 | */ |
| 29 | - public static MjMessage.Content parseContent(String content) { | 29 | + public static MidjourneyMessage.Content parseContent(String content) { |
| 30 | // 有三种格式。 | 30 | // 有三种格式。 |
| 31 | // 南极应该是什么样子? | 31 | // 南极应该是什么样子? |
| 32 | // "**南极应该是什么样子? --v 6.0 --style raw** - <@972721304891453450> (32%) (fast, stealth)", | 32 | // "**南极应该是什么样子? --v 6.0 --style raw** - <@972721304891453450> (32%) (fast, stealth)", |
| 33 | // "**南极应该是什么样子? --v 6.0 --style raw** - <@972721304891453450> (fast, stealth)" | 33 | // "**南极应该是什么样子? --v 6.0 --style raw** - <@972721304891453450> (fast, stealth)" |
| 34 | - MjMessage.Content mjContent = new MjMessage.Content(); | 34 | + MidjourneyMessage.Content mjContent = new MidjourneyMessage.Content(); |
| 35 | if (CharSequenceUtil.isBlank(content)) { | 35 | if (CharSequenceUtil.isBlank(content)) { |
| 36 | return null; | 36 | return null; |
| 37 | } | 37 | } |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/webSocket/MjWebSocketStarter.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/webSocket/MidjourneyWebSocketStarter.java
| @@ -4,9 +4,9 @@ package cn.iocoder.yudao.framework.ai.midjourney.webSocket; | @@ -4,9 +4,9 @@ package cn.iocoder.yudao.framework.ai.midjourney.webSocket; | ||
| 4 | import cn.hutool.core.text.CharSequenceUtil; | 4 | import cn.hutool.core.text.CharSequenceUtil; |
| 5 | import cn.hutool.core.thread.ThreadUtil; | 5 | import cn.hutool.core.thread.ThreadUtil; |
| 6 | import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyConfig; | 6 | import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyConfig; |
| 7 | -import cn.iocoder.yudao.framework.ai.midjourney.constants.MjNotifyCode; | ||
| 8 | -import cn.iocoder.yudao.framework.ai.midjourney.webSocket.handler.MjWebSocketHandler; | ||
| 9 | -import cn.iocoder.yudao.framework.ai.midjourney.webSocket.listener.MjMessageListener; | 7 | +import cn.iocoder.yudao.framework.ai.midjourney.constants.MidjourneyNotifyCode; |
| 8 | +import cn.iocoder.yudao.framework.ai.midjourney.webSocket.handler.MidjourneyWebSocketHandler; | ||
| 9 | +import cn.iocoder.yudao.framework.ai.midjourney.webSocket.listener.MidjourneyMessageListener; | ||
| 10 | import lombok.Getter; | 10 | import lombok.Getter; |
| 11 | import lombok.extern.slf4j.Slf4j; | 11 | import lombok.extern.slf4j.Slf4j; |
| 12 | import org.apache.tomcat.websocket.Constants; | 12 | import org.apache.tomcat.websocket.Constants; |
| @@ -20,11 +20,10 @@ import org.springframework.web.socket.client.standard.StandardWebSocketClient; | @@ -20,11 +20,10 @@ import org.springframework.web.socket.client.standard.StandardWebSocketClient; | ||
| 20 | 20 | ||
| 21 | import java.io.IOException; | 21 | import java.io.IOException; |
| 22 | import java.net.URI; | 22 | import java.net.URI; |
| 23 | -import java.util.concurrent.TimeoutException; | ||
| 24 | 23 | ||
| 25 | // TODO @fansili:mj 这块 websocket 有点小复杂,虽然代码量 400 多行;感觉可以考虑,有没第三方 sdk,通过它透明接入 mj | 24 | // TODO @fansili:mj 这块 websocket 有点小复杂,虽然代码量 400 多行;感觉可以考虑,有没第三方 sdk,通过它透明接入 mj |
| 26 | @Slf4j | 25 | @Slf4j |
| 27 | -public class MjWebSocketStarter implements WebSocketStarter { | 26 | +public class MidjourneyWebSocketStarter implements WebSocketStarter { |
| 28 | /** | 27 | /** |
| 29 | * 链接重试次数 | 28 | * 链接重试次数 |
| 30 | */ | 29 | */ |
| @@ -36,7 +35,7 @@ public class MjWebSocketStarter implements WebSocketStarter { | @@ -36,7 +35,7 @@ public class MjWebSocketStarter implements WebSocketStarter { | ||
| 36 | /** | 35 | /** |
| 37 | * mj 监听(所有message 都会 callback到这里) | 36 | * mj 监听(所有message 都会 callback到这里) |
| 38 | */ | 37 | */ |
| 39 | - private final MjMessageListener userMessageListener; | 38 | + private final MidjourneyMessageListener userMessageListener; |
| 40 | /** | 39 | /** |
| 41 | * wss 服务器 | 40 | * wss 服务器 |
| 42 | */ | 41 | */ |
| @@ -58,10 +57,10 @@ public class MjWebSocketStarter implements WebSocketStarter { | @@ -58,10 +57,10 @@ public class MjWebSocketStarter implements WebSocketStarter { | ||
| 58 | */ | 57 | */ |
| 59 | private WebSocketSession webSocketSession = null; | 58 | private WebSocketSession webSocketSession = null; |
| 60 | 59 | ||
| 61 | - public MjWebSocketStarter(String wssServer, | ||
| 62 | - String resumeWss, | ||
| 63 | - MidjourneyConfig midjourneyConfig, | ||
| 64 | - MjMessageListener userMessageListener) { | 60 | + public MidjourneyWebSocketStarter(String wssServer, |
| 61 | + String resumeWss, | ||
| 62 | + MidjourneyConfig midjourneyConfig, | ||
| 63 | + MidjourneyMessageListener userMessageListener) { | ||
| 65 | this.wssServer = wssServer; | 64 | this.wssServer = wssServer; |
| 66 | this.resumeWss = resumeWss; | 65 | this.resumeWss = resumeWss; |
| 67 | this.midjourneyConfig = midjourneyConfig; | 66 | this.midjourneyConfig = midjourneyConfig; |
| @@ -83,7 +82,7 @@ public class MjWebSocketStarter implements WebSocketStarter { | @@ -83,7 +82,7 @@ public class MjWebSocketStarter implements WebSocketStarter { | ||
| 83 | headers.add("Sec-Websocket-Extensions", "permessage-deflate; client_max_window_bits"); | 82 | headers.add("Sec-Websocket-Extensions", "permessage-deflate; client_max_window_bits"); |
| 84 | headers.add("User-Agent", this.midjourneyConfig.getUserAage()); | 83 | headers.add("User-Agent", this.midjourneyConfig.getUserAage()); |
| 85 | // 创建 mjHeader | 84 | // 创建 mjHeader |
| 86 | - MjWebSocketHandler mjWebSocketHandler = new MjWebSocketHandler( | 85 | + MidjourneyWebSocketHandler mjWebSocketHandler = new MidjourneyWebSocketHandler( |
| 87 | this.midjourneyConfig, this.userMessageListener, this::onSocketSuccess, this::onSocketFailure); | 86 | this.midjourneyConfig, this.userMessageListener, this::onSocketSuccess, this::onSocketFailure); |
| 88 | // | 87 | // |
| 89 | String gatewayUrl; | 88 | String gatewayUrl; |
| @@ -105,12 +104,12 @@ public class MjWebSocketStarter implements WebSocketStarter { | @@ -105,12 +104,12 @@ public class MjWebSocketStarter implements WebSocketStarter { | ||
| 105 | socketSessionFuture.addCallback(new ListenableFutureCallback<>() { | 104 | socketSessionFuture.addCallback(new ListenableFutureCallback<>() { |
| 106 | @Override | 105 | @Override |
| 107 | public void onFailure(@NotNull Throwable e) { | 106 | public void onFailure(@NotNull Throwable e) { |
| 108 | - onSocketFailure(MjWebSocketHandler.CLOSE_CODE_EXCEPTION, e.getMessage()); | 107 | + onSocketFailure(MidjourneyWebSocketHandler.CLOSE_CODE_EXCEPTION, e.getMessage()); |
| 109 | } | 108 | } |
| 110 | 109 | ||
| 111 | @Override | 110 | @Override |
| 112 | public void onSuccess(WebSocketSession session) { | 111 | public void onSuccess(WebSocketSession session) { |
| 113 | - MjWebSocketStarter.this.webSocketSession = session; | 112 | + MidjourneyWebSocketStarter.this.webSocketSession = session; |
| 114 | } | 113 | } |
| 115 | }); | 114 | }); |
| 116 | } | 115 | } |
| @@ -118,7 +117,7 @@ public class MjWebSocketStarter implements WebSocketStarter { | @@ -118,7 +117,7 @@ public class MjWebSocketStarter implements WebSocketStarter { | ||
| 118 | private void onSocketSuccess(String sessionId, Object sequence, String resumeGatewayUrl) { | 117 | private void onSocketSuccess(String sessionId, Object sequence, String resumeGatewayUrl) { |
| 119 | this.resumeData = new ResumeData(sessionId, sequence, resumeGatewayUrl); | 118 | this.resumeData = new ResumeData(sessionId, sequence, resumeGatewayUrl); |
| 120 | this.running = true; | 119 | this.running = true; |
| 121 | - notifyWssLock(MjNotifyCode.SUCCESS, ""); | 120 | + notifyWssLock(MidjourneyNotifyCode.SUCCESS, ""); |
| 122 | } | 121 | } |
| 123 | 122 | ||
| 124 | private void onSocketFailure(int code, String reason) { | 123 | private void onSocketFailure(int code, String reason) { |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/webSocket/handler/MjWebSocketHandler.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/webSocket/handler/MidjourneyWebSocketHandler.java
| @@ -8,7 +8,7 @@ import cn.hutool.http.useragent.UserAgentUtil; | @@ -8,7 +8,7 @@ import cn.hutool.http.useragent.UserAgentUtil; | ||
| 8 | import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyConfig; | 8 | import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyConfig; |
| 9 | import cn.iocoder.yudao.framework.ai.midjourney.webSocket.FailureCallback; | 9 | import cn.iocoder.yudao.framework.ai.midjourney.webSocket.FailureCallback; |
| 10 | import cn.iocoder.yudao.framework.ai.midjourney.webSocket.SuccessCallback; | 10 | import cn.iocoder.yudao.framework.ai.midjourney.webSocket.SuccessCallback; |
| 11 | -import cn.iocoder.yudao.framework.ai.midjourney.webSocket.listener.MjMessageListener; | 11 | +import cn.iocoder.yudao.framework.ai.midjourney.webSocket.listener.MidjourneyMessageListener; |
| 12 | import lombok.Setter; | 12 | import lombok.Setter; |
| 13 | import lombok.extern.slf4j.Slf4j; | 13 | import lombok.extern.slf4j.Slf4j; |
| 14 | import net.dv8tion.jda.api.utils.data.DataArray; | 14 | import net.dv8tion.jda.api.utils.data.DataArray; |
| @@ -29,7 +29,7 @@ import java.util.concurrent.ScheduledExecutorService; | @@ -29,7 +29,7 @@ import java.util.concurrent.ScheduledExecutorService; | ||
| 29 | import java.util.concurrent.TimeUnit; | 29 | import java.util.concurrent.TimeUnit; |
| 30 | 30 | ||
| 31 | @Slf4j | 31 | @Slf4j |
| 32 | -public class MjWebSocketHandler implements WebSocketHandler { | 32 | +public class MidjourneyWebSocketHandler implements WebSocketHandler { |
| 33 | /** | 33 | /** |
| 34 | * close 错误码:重连 | 34 | * close 错误码:重连 |
| 35 | */ | 35 | */ |
| @@ -49,7 +49,7 @@ public class MjWebSocketHandler implements WebSocketHandler { | @@ -49,7 +49,7 @@ public class MjWebSocketHandler implements WebSocketHandler { | ||
| 49 | /** | 49 | /** |
| 50 | * mj 消息监听 | 50 | * mj 消息监听 |
| 51 | */ | 51 | */ |
| 52 | - private final MjMessageListener userMessageListener; | 52 | + private final MidjourneyMessageListener userMessageListener; |
| 53 | /** | 53 | /** |
| 54 | * 成功回调 | 54 | * 成功回调 |
| 55 | */ | 55 | */ |
| @@ -85,10 +85,10 @@ public class MjWebSocketHandler implements WebSocketHandler { | @@ -85,10 +85,10 @@ public class MjWebSocketHandler implements WebSocketHandler { | ||
| 85 | */ | 85 | */ |
| 86 | private final Decompressor decompressor = new ZlibDecompressor(2048); | 86 | private final Decompressor decompressor = new ZlibDecompressor(2048); |
| 87 | 87 | ||
| 88 | - public MjWebSocketHandler(MidjourneyConfig account, | ||
| 89 | - MjMessageListener userMessageListener, | ||
| 90 | - SuccessCallback successCallback, | ||
| 91 | - FailureCallback failureCallback) { | 88 | + public MidjourneyWebSocketHandler(MidjourneyConfig account, |
| 89 | + MidjourneyMessageListener userMessageListener, | ||
| 90 | + SuccessCallback successCallback, | ||
| 91 | + FailureCallback failureCallback) { | ||
| 92 | this.midjourneyConfig = account; | 92 | this.midjourneyConfig = account; |
| 93 | this.userMessageListener = userMessageListener; | 93 | this.userMessageListener = userMessageListener; |
| 94 | this.successCallback = successCallback; | 94 | this.successCallback = successCallback; |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/webSocket/listener/MjMessageListener.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/midjourney/webSocket/listener/MidjourneyMessageListener.java
| @@ -6,11 +6,11 @@ import cn.hutool.core.text.CharSequenceUtil; | @@ -6,11 +6,11 @@ import cn.hutool.core.text.CharSequenceUtil; | ||
| 6 | import cn.hutool.core.util.StrUtil; | 6 | import cn.hutool.core.util.StrUtil; |
| 7 | import cn.hutool.json.JSONUtil; | 7 | import cn.hutool.json.JSONUtil; |
| 8 | import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyConfig; | 8 | import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyConfig; |
| 9 | -import cn.iocoder.yudao.framework.ai.midjourney.MjMessage; | ||
| 10 | -import cn.iocoder.yudao.framework.ai.midjourney.constants.MjConstants; | ||
| 11 | -import cn.iocoder.yudao.framework.ai.midjourney.constants.MjGennerateStatusEnum; | ||
| 12 | -import cn.iocoder.yudao.framework.ai.midjourney.constants.MjMessageTypeEnum; | ||
| 13 | -import cn.iocoder.yudao.framework.ai.midjourney.util.MjUtil; | 9 | +import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyMessage; |
| 10 | +import cn.iocoder.yudao.framework.ai.midjourney.constants.MidjourneyConstants; | ||
| 11 | +import cn.iocoder.yudao.framework.ai.midjourney.constants.MidjourneyGennerateStatusEnum; | ||
| 12 | +import cn.iocoder.yudao.framework.ai.midjourney.constants.MidjourneyMessageTypeEnum; | ||
| 13 | +import cn.iocoder.yudao.framework.ai.midjourney.util.MidjourneyUtil; | ||
| 14 | import com.alibaba.fastjson.JSON; | 14 | import com.alibaba.fastjson.JSON; |
| 15 | import lombok.extern.slf4j.Slf4j; | 15 | import lombok.extern.slf4j.Slf4j; |
| 16 | import net.dv8tion.jda.api.utils.data.DataObject; | 16 | import net.dv8tion.jda.api.utils.data.DataObject; |
| @@ -18,17 +18,17 @@ import net.dv8tion.jda.api.utils.data.DataObject; | @@ -18,17 +18,17 @@ import net.dv8tion.jda.api.utils.data.DataObject; | ||
| 18 | import java.util.List; | 18 | import java.util.List; |
| 19 | 19 | ||
| 20 | @Slf4j | 20 | @Slf4j |
| 21 | -public class MjMessageListener { | 21 | +public class MidjourneyMessageListener { |
| 22 | 22 | ||
| 23 | private MidjourneyConfig midjourneyConfig; | 23 | private MidjourneyConfig midjourneyConfig; |
| 24 | 24 | ||
| 25 | - public MjMessageListener(MidjourneyConfig midjourneyConfig) { | 25 | + public MidjourneyMessageListener(MidjourneyConfig midjourneyConfig) { |
| 26 | this.midjourneyConfig = midjourneyConfig; | 26 | this.midjourneyConfig = midjourneyConfig; |
| 27 | } | 27 | } |
| 28 | 28 | ||
| 29 | public void onMessage(DataObject raw) { | 29 | public void onMessage(DataObject raw) { |
| 30 | - MjMessageTypeEnum messageType = MjMessageTypeEnum.of(raw.getString("t")); | ||
| 31 | - if (messageType == null || MjMessageTypeEnum.DELETE == messageType) { | 30 | + MidjourneyMessageTypeEnum messageType = MidjourneyMessageTypeEnum.of(raw.getString("t")); |
| 31 | + if (messageType == null || MidjourneyMessageTypeEnum.DELETE == messageType) { | ||
| 32 | return; | 32 | return; |
| 33 | } | 33 | } |
| 34 | DataObject data = raw.getObject("d"); | 34 | DataObject data = raw.getObject("d"); |
| @@ -37,21 +37,21 @@ public class MjMessageListener { | @@ -37,21 +37,21 @@ public class MjMessageListener { | ||
| 37 | } | 37 | } |
| 38 | 38 | ||
| 39 | // 转换几个重要的信息 | 39 | // 转换几个重要的信息 |
| 40 | - MjMessage mjMessage = new MjMessage(); | ||
| 41 | - mjMessage.setId(data.getString(MjConstants.MSG_ID)); | ||
| 42 | - mjMessage.setType(data.getInt(MjConstants.MSG_TYPE)); | 40 | + MidjourneyMessage mjMessage = new MidjourneyMessage(); |
| 41 | + mjMessage.setId(data.getString(MidjourneyConstants.MSG_ID)); | ||
| 42 | + mjMessage.setType(data.getInt(MidjourneyConstants.MSG_TYPE)); | ||
| 43 | mjMessage.setRawData(StrUtil.str(raw.toJson(), "UTF-8")); | 43 | mjMessage.setRawData(StrUtil.str(raw.toJson(), "UTF-8")); |
| 44 | - mjMessage.setContent(MjUtil.parseContent(data.getString(MjConstants.MSG_CONTENT))); | 44 | + mjMessage.setContent(MidjourneyUtil.parseContent(data.getString(MidjourneyConstants.MSG_CONTENT))); |
| 45 | // 转换 components | 45 | // 转换 components |
| 46 | - if (!data.getArray(MjConstants.MSG_COMPONENTS).isEmpty()) { | ||
| 47 | - String componentsJson = StrUtil.str(data.getArray(MjConstants.MSG_COMPONENTS).toJson(), "UTF-8"); | ||
| 48 | - List<MjMessage.ComponentType> components = JSON.parseArray(componentsJson, MjMessage.ComponentType.class); | 46 | + if (!data.getArray(MidjourneyConstants.MSG_COMPONENTS).isEmpty()) { |
| 47 | + String componentsJson = StrUtil.str(data.getArray(MidjourneyConstants.MSG_COMPONENTS).toJson(), "UTF-8"); | ||
| 48 | + List<MidjourneyMessage.ComponentType> components = JSON.parseArray(componentsJson, MidjourneyMessage.ComponentType.class); | ||
| 49 | mjMessage.setComponents(components); | 49 | mjMessage.setComponents(components); |
| 50 | } | 50 | } |
| 51 | // 转换附件 | 51 | // 转换附件 |
| 52 | - if (!data.getArray(MjConstants.MSG_ATTACHMENTS).isEmpty()) { | ||
| 53 | - String attachmentsJson = StrUtil.str(data.getArray(MjConstants.MSG_ATTACHMENTS).toJson(), "UTF-8"); | ||
| 54 | - List<MjMessage.Attachment> attachments = JSON.parseArray(attachmentsJson, MjMessage.Attachment.class); | 52 | + if (!data.getArray(MidjourneyConstants.MSG_ATTACHMENTS).isEmpty()) { |
| 53 | + String attachmentsJson = StrUtil.str(data.getArray(MidjourneyConstants.MSG_ATTACHMENTS).toJson(), "UTF-8"); | ||
| 54 | + List<MidjourneyMessage.Attachment> attachments = JSON.parseArray(attachmentsJson, MidjourneyMessage.Attachment.class); | ||
| 55 | mjMessage.setAttachments(attachments); | 55 | mjMessage.setAttachments(attachments); |
| 56 | } | 56 | } |
| 57 | // 转换状态 | 57 | // 转换状态 |
| @@ -61,18 +61,18 @@ public class MjMessageListener { | @@ -61,18 +61,18 @@ public class MjMessageListener { | ||
| 61 | System.err.println(JSONUtil.toJsonPrettyStr(mjMessage)); | 61 | System.err.println(JSONUtil.toJsonPrettyStr(mjMessage)); |
| 62 | } | 62 | } |
| 63 | 63 | ||
| 64 | - private void convertGenerateStatus(MjMessage mjMessage) { | 64 | + private void convertGenerateStatus(MidjourneyMessage mjMessage) { |
| 65 | if (mjMessage.getType() == 20 && mjMessage.getContent().getStatus().contains("Waiting")) { | 65 | if (mjMessage.getType() == 20 && mjMessage.getContent().getStatus().contains("Waiting")) { |
| 66 | - mjMessage.setGenerateStatus(MjGennerateStatusEnum.WAITING.getValue()); | 66 | + mjMessage.setGenerateStatus(MidjourneyGennerateStatusEnum.WAITING.getValue()); |
| 67 | } else if (mjMessage.getType() == 20 && !StrUtil.isBlank(mjMessage.getContent().getProgress())) { | 67 | } else if (mjMessage.getType() == 20 && !StrUtil.isBlank(mjMessage.getContent().getProgress())) { |
| 68 | - mjMessage.setGenerateStatus(MjGennerateStatusEnum.IN_PROGRESS.getValue()); | 68 | + mjMessage.setGenerateStatus(MidjourneyGennerateStatusEnum.IN_PROGRESS.getValue()); |
| 69 | } else if (mjMessage.getType() == 0 && !CollUtil.isEmpty(mjMessage.getComponents())) { | 69 | } else if (mjMessage.getType() == 0 && !CollUtil.isEmpty(mjMessage.getComponents())) { |
| 70 | - mjMessage.setGenerateStatus(MjGennerateStatusEnum.COMPLETED.getValue()); | 70 | + mjMessage.setGenerateStatus(MidjourneyGennerateStatusEnum.COMPLETED.getValue()); |
| 71 | } | 71 | } |
| 72 | } | 72 | } |
| 73 | 73 | ||
| 74 | - private boolean ignoreAndLogMessage(DataObject data, MjMessageTypeEnum messageType) { | ||
| 75 | - String channelId = data.getString(MjConstants.MSG_CHANNEL_ID); | 74 | + private boolean ignoreAndLogMessage(DataObject data, MidjourneyMessageTypeEnum messageType) { |
| 75 | + String channelId = data.getString(MidjourneyConstants.MSG_CHANNEL_ID); | ||
| 76 | if (!CharSequenceUtil.equals(channelId, midjourneyConfig.getChannelId())) { | 76 | if (!CharSequenceUtil.equals(channelId, midjourneyConfig.getChannelId())) { |
| 77 | return true; | 77 | return true; |
| 78 | } | 78 | } |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/util/JacksonUtil.java deleted
| 1 | -package cn.iocoder.yudao.framework.ai.util; | ||
| 2 | - | ||
| 3 | -import com.fasterxml.jackson.annotation.JsonInclude; | ||
| 4 | -import com.fasterxml.jackson.core.JsonProcessingException; | ||
| 5 | -import com.fasterxml.jackson.databind.ObjectMapper; | ||
| 6 | -import com.fasterxml.jackson.databind.SerializationFeature; | ||
| 7 | -import com.fasterxml.jackson.databind.module.SimpleModule; | ||
| 8 | -import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; | ||
| 9 | - | ||
| 10 | -import java.io.IOException; | ||
| 11 | - | ||
| 12 | -// TODO @fansili:看看能不能用 JsonUtils | ||
| 13 | -/** | ||
| 14 | - * Jackson工具类 | ||
| 15 | - * | ||
| 16 | - * author: fansili | ||
| 17 | - * time: 2024/3/17 10:13 | ||
| 18 | - */ | ||
| 19 | -public class JacksonUtil { | ||
| 20 | - | ||
| 21 | - private static final ObjectMapper objectMapper = new ObjectMapper(); | ||
| 22 | - | ||
| 23 | - /** | ||
| 24 | - * 初始化 ObjectMapper 以美化输出(即格式化JSON内容) | ||
| 25 | - */ | ||
| 26 | - static { | ||
| 27 | - // 美化输出(缩进) | ||
| 28 | - objectMapper.enable(SerializationFeature.INDENT_OUTPUT); | ||
| 29 | - // 忽略值为 null 的属性 | ||
| 30 | - objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); | ||
| 31 | - // 配置一个模块来将 Long 类型转换为 String 类型 | ||
| 32 | - SimpleModule module = new SimpleModule(); | ||
| 33 | - module.addSerializer(Long.class, ToStringSerializer.instance); | ||
| 34 | - objectMapper.registerModule(module); | ||
| 35 | - } | ||
| 36 | - | ||
| 37 | - /** | ||
| 38 | - * 将对象转换为 JSON 字符串 | ||
| 39 | - * | ||
| 40 | - * @param obj 需要序列化的Java对象 | ||
| 41 | - * @return 序列化后的 JSON 字符串 | ||
| 42 | - * @throws JsonProcessingException 当 JSON 序列化过程中出现错误时抛出异常 | ||
| 43 | - */ | ||
| 44 | - public static String toJson(Object obj) { | ||
| 45 | - try { | ||
| 46 | - return objectMapper.writeValueAsString(obj); | ||
| 47 | - } catch (JsonProcessingException e) { | ||
| 48 | - throw new RuntimeException(e); | ||
| 49 | - } | ||
| 50 | - } | ||
| 51 | - | ||
| 52 | - /** | ||
| 53 | - * 将 JSON 字符串反序列化为指定类型的对象 | ||
| 54 | - * | ||
| 55 | - * @param json JSON 字符串 | ||
| 56 | - * @param clazz 目标类型 Class 对象 | ||
| 57 | - * @param <T> 泛型类型参数 | ||
| 58 | - * @return 反序列化后的 Java 对象 | ||
| 59 | - * @throws IOException 当 JSON 解析过程中出现错误时抛出异常 | ||
| 60 | - */ | ||
| 61 | - public static <T> T fromJson(String json, Class<T> clazz) { | ||
| 62 | - try { | ||
| 63 | - return objectMapper.readValue(json, clazz); | ||
| 64 | - } catch (JsonProcessingException e) { | ||
| 65 | - throw new RuntimeException(e); | ||
| 66 | - } | ||
| 67 | - } | ||
| 68 | - | ||
| 69 | - /** | ||
| 70 | - * 将对象转换为格式化的 JSON 字符串(已启用 INDENT_OUTPUT 功能,所以所有方法都会返回格式化后的 JSON) | ||
| 71 | - * | ||
| 72 | - * @param obj 需要序列化的Java对象 | ||
| 73 | - * @return 格式化后的 JSON 字符串 | ||
| 74 | - * @throws JsonProcessingException 当 JSON 序列化过程中出现错误时抛出异常 | ||
| 75 | - */ | ||
| 76 | - public static String toFormattedJson(Object obj) { | ||
| 77 | - // 已在类初始化时设置了 SerializationFeature.INDENT_OUTPUT,此处无需额外操作 | ||
| 78 | - return toJson(obj); | ||
| 79 | - } | ||
| 80 | -} |
yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/chat/QianWenChatClientTests.java
| 1 | package cn.iocoder.yudao.framework.ai.chat; | 1 | package cn.iocoder.yudao.framework.ai.chat; |
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.ai.chat.prompt.Prompt; | 3 | import cn.iocoder.yudao.framework.ai.chat.prompt.Prompt; |
| 4 | -import cn.iocoder.yudao.framework.ai.chatqianwen.QianWenApi; | 4 | +import cn.iocoder.yudao.framework.ai.chatqianwen.api.QianWenApi; |
| 5 | import cn.iocoder.yudao.framework.ai.chatqianwen.QianWenChatClient; | 5 | import cn.iocoder.yudao.framework.ai.chatqianwen.QianWenChatClient; |
| 6 | import cn.iocoder.yudao.framework.ai.chatqianwen.QianWenOptions; | 6 | import cn.iocoder.yudao.framework.ai.chatqianwen.QianWenOptions; |
| 7 | -import com.aliyun.broadscope.bailian.sdk.models.CompletionsRequest; | ||
| 8 | import org.junit.Before; | 7 | import org.junit.Before; |
| 9 | import org.junit.Test; | 8 | import org.junit.Test; |
| 10 | import reactor.core.publisher.Flux; | 9 | import reactor.core.publisher.Flux; |
yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/mj/MjInteractionsTests.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/midjourney/MidjourneyInteractionsTests.java
| 1 | -package cn.iocoder.yudao.framework.ai.mj; | 1 | +package cn.iocoder.yudao.framework.ai.midjourney; |
| 2 | 2 | ||
| 3 | import cn.hutool.core.io.FileUtil; | 3 | import cn.hutool.core.io.FileUtil; |
| 4 | -import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyConfig; | ||
| 5 | -import cn.iocoder.yudao.framework.ai.midjourney.interactions.MjInteractions; | 4 | +import cn.iocoder.yudao.framework.ai.midjourney.interactions.MidjourneyInteractions; |
| 6 | import cn.iocoder.yudao.framework.ai.midjourney.vo.Attachments; | 5 | import cn.iocoder.yudao.framework.ai.midjourney.vo.Attachments; |
| 7 | import cn.iocoder.yudao.framework.ai.midjourney.vo.Describe; | 6 | import cn.iocoder.yudao.framework.ai.midjourney.vo.Describe; |
| 8 | import cn.iocoder.yudao.framework.ai.midjourney.vo.ReRoll; | 7 | import cn.iocoder.yudao.framework.ai.midjourney.vo.ReRoll; |
| @@ -23,7 +22,7 @@ import java.util.Map; | @@ -23,7 +22,7 @@ import java.util.Map; | ||
| 23 | * author: fansili | 22 | * author: fansili |
| 24 | * time: 2024/4/4 18:59 | 23 | * time: 2024/4/4 18:59 |
| 25 | */ | 24 | */ |
| 26 | -public class MjInteractionsTests { | 25 | +public class MidjourneyInteractionsTests { |
| 27 | 26 | ||
| 28 | private MidjourneyConfig midjourneyConfig; | 27 | private MidjourneyConfig midjourneyConfig; |
| 29 | @Before | 28 | @Before |
| @@ -39,14 +38,14 @@ public class MjInteractionsTests { | @@ -39,14 +38,14 @@ public class MjInteractionsTests { | ||
| 39 | 38 | ||
| 40 | @Test | 39 | @Test |
| 41 | public void mjImageTest() { | 40 | public void mjImageTest() { |
| 42 | - MjInteractions mjImagineInteractions = new MjInteractions(midjourneyConfig); | 41 | + MidjourneyInteractions mjImagineInteractions = new MidjourneyInteractions(midjourneyConfig); |
| 43 | mjImagineInteractions.imagine("童话里应该是什么样子?"); | 42 | mjImagineInteractions.imagine("童话里应该是什么样子?"); |
| 44 | } | 43 | } |
| 45 | 44 | ||
| 46 | 45 | ||
| 47 | @Test | 46 | @Test |
| 48 | public void reRollTest() { | 47 | public void reRollTest() { |
| 49 | - MjInteractions mjImagineInteractions = new MjInteractions(midjourneyConfig); | 48 | + MidjourneyInteractions mjImagineInteractions = new MidjourneyInteractions(midjourneyConfig); |
| 50 | mjImagineInteractions.reRoll(new ReRoll() | 49 | mjImagineInteractions.reRoll(new ReRoll() |
| 51 | .setMessageId("1226165117448753243") | 50 | .setMessageId("1226165117448753243") |
| 52 | .setCustomId("MJ::JOB::upsample::3::2aeefbef-43e2-4057-bcf1-43b5f39ab6f7")); | 51 | .setCustomId("MJ::JOB::upsample::3::2aeefbef-43e2-4057-bcf1-43b5f39ab6f7")); |
| @@ -54,7 +53,7 @@ public class MjInteractionsTests { | @@ -54,7 +53,7 @@ public class MjInteractionsTests { | ||
| 54 | 53 | ||
| 55 | @Test | 54 | @Test |
| 56 | public void uploadAttachmentsTest() { | 55 | public void uploadAttachmentsTest() { |
| 57 | - MjInteractions mjImagineInteractions = new MjInteractions(midjourneyConfig); | 56 | + MidjourneyInteractions mjImagineInteractions = new MidjourneyInteractions(midjourneyConfig); |
| 58 | UploadAttachmentsRes res = mjImagineInteractions.uploadAttachments( | 57 | UploadAttachmentsRes res = mjImagineInteractions.uploadAttachments( |
| 59 | new Attachments().setFileSystemResource( | 58 | new Attachments().setFileSystemResource( |
| 60 | new FileSystemResource(new File("/Users/fansili/Downloads/DSC01402.JPG"))) | 59 | new FileSystemResource(new File("/Users/fansili/Downloads/DSC01402.JPG"))) |
| @@ -64,7 +63,7 @@ public class MjInteractionsTests { | @@ -64,7 +63,7 @@ public class MjInteractionsTests { | ||
| 64 | 63 | ||
| 65 | @Test | 64 | @Test |
| 66 | public void describeTest() { | 65 | public void describeTest() { |
| 67 | - MjInteractions mjImagineInteractions = new MjInteractions(midjourneyConfig); | 66 | + MidjourneyInteractions mjImagineInteractions = new MidjourneyInteractions(midjourneyConfig); |
| 68 | mjImagineInteractions.describe(new Describe() | 67 | mjImagineInteractions.describe(new Describe() |
| 69 | .setFileName("DSC01402.JPG") | 68 | .setFileName("DSC01402.JPG") |
| 70 | .setFinalFileName("16826931-2873-45ec-8cfb-0ad81f1a075f/DSC01402.JPG") | 69 | .setFinalFileName("16826931-2873-45ec-8cfb-0ad81f1a075f/DSC01402.JPG") |
yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/mj/MjUtilTests.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/midjourney/MidjourneyUtilTests.java
| 1 | -package cn.iocoder.yudao.framework.ai.mj; | 1 | +package cn.iocoder.yudao.framework.ai.midjourney; |
| 2 | 2 | ||
| 3 | -import cn.iocoder.yudao.framework.ai.midjourney.util.MjUtil; | 3 | +import cn.iocoder.yudao.framework.ai.midjourney.util.MidjourneyUtil; |
| 4 | import org.junit.Test; | 4 | import org.junit.Test; |
| 5 | 5 | ||
| 6 | /** | 6 | /** |
| @@ -9,14 +9,14 @@ import org.junit.Test; | @@ -9,14 +9,14 @@ import org.junit.Test; | ||
| 9 | * author: fansili | 9 | * author: fansili |
| 10 | * time: 2024/4/6 21:57 | 10 | * time: 2024/4/6 21:57 |
| 11 | */ | 11 | */ |
| 12 | -public class MjUtilTests { | 12 | +public class MidjourneyUtilTests { |
| 13 | 13 | ||
| 14 | @Test | 14 | @Test |
| 15 | public void parseContentTest() { | 15 | public void parseContentTest() { |
| 16 | String content1 = "**南极应该是什么样子? --v 6.0 --style raw** - <@972721304891453450> (32%) (fast, stealth)"; | 16 | String content1 = "**南极应该是什么样子? --v 6.0 --style raw** - <@972721304891453450> (32%) (fast, stealth)"; |
| 17 | String content2 = "**南极应该是什么样子? --v 6.0 --style raw** - <@972721304891453450> (fast, stealth)"; | 17 | String content2 = "**南极应该是什么样子? --v 6.0 --style raw** - <@972721304891453450> (fast, stealth)"; |
| 18 | 18 | ||
| 19 | - System.err.println(MjUtil.parseContent(content1)); | ||
| 20 | - System.err.println(MjUtil.parseContent(content2)); | 19 | + System.err.println(MidjourneyUtil.parseContent(content1)); |
| 20 | + System.err.println(MidjourneyUtil.parseContent(content2)); | ||
| 21 | } | 21 | } |
| 22 | } | 22 | } |
yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/mj/MjWebSocketTests.java renamed to yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/midjourney/MidjourneyWebSocketTests.java
| 1 | -package cn.iocoder.yudao.framework.ai.mj; | 1 | +package cn.iocoder.yudao.framework.ai.midjourney; |
| 2 | 2 | ||
| 3 | import cn.hutool.core.io.FileUtil; | 3 | import cn.hutool.core.io.FileUtil; |
| 4 | -import cn.iocoder.yudao.framework.ai.midjourney.MidjourneyConfig; | ||
| 5 | -import cn.iocoder.yudao.framework.ai.midjourney.webSocket.listener.MjMessageListener; | ||
| 6 | -import cn.iocoder.yudao.framework.ai.midjourney.webSocket.MjWebSocketStarter; | 4 | +import cn.iocoder.yudao.framework.ai.midjourney.webSocket.listener.MidjourneyMessageListener; |
| 5 | +import cn.iocoder.yudao.framework.ai.midjourney.webSocket.MidjourneyWebSocketStarter; | ||
| 7 | import org.junit.Before; | 6 | import org.junit.Before; |
| 8 | import org.junit.Test; | 7 | import org.junit.Test; |
| 9 | 8 | ||
| @@ -17,7 +16,7 @@ import java.util.Scanner; | @@ -17,7 +16,7 @@ import java.util.Scanner; | ||
| 17 | * author: fansili | 16 | * author: fansili |
| 18 | * time: 2024/4/3 16:40 | 17 | * time: 2024/4/3 16:40 |
| 19 | */ | 18 | */ |
| 20 | -public class MjWebSocketTests { | 19 | +public class MidjourneyWebSocketTests { |
| 21 | 20 | ||
| 22 | private MidjourneyConfig midjourneyConfig; | 21 | private MidjourneyConfig midjourneyConfig; |
| 23 | 22 | ||
| @@ -35,8 +34,8 @@ public class MjWebSocketTests { | @@ -35,8 +34,8 @@ public class MjWebSocketTests { | ||
| 35 | @Test | 34 | @Test |
| 36 | public void startSocketTest() { | 35 | public void startSocketTest() { |
| 37 | String wssUrl = "wss://gateway.discord.gg"; | 36 | String wssUrl = "wss://gateway.discord.gg"; |
| 38 | - var messageListener = new MjMessageListener(midjourneyConfig); | ||
| 39 | - var webSocketStarter = new MjWebSocketStarter(wssUrl, null, midjourneyConfig, messageListener); | 37 | + var messageListener = new MidjourneyMessageListener(midjourneyConfig); |
| 38 | + var webSocketStarter = new MidjourneyWebSocketStarter(wssUrl, null, midjourneyConfig, messageListener); | ||
| 40 | 39 | ||
| 41 | try { | 40 | try { |
| 42 | webSocketStarter.start(); | 41 | webSocketStarter.start(); |