Browse Source

【代码优化】AI:完善 StabilityAiImageModelTests 单测,方便大家快速体验

YunaiV 8 months ago
parent
commit
c5ed07a091

+ 1 - 1
yudao-module-ai/pom.xml

@@ -20,7 +20,7 @@
     <description>
         ai 模块下,接入 LLM 大模型,支持聊天、绘图、音乐、写作、思维脑图等功能。
         目前已接入各种模型,不限于:
-          国内:通义千问、文心一言、讯飞星火
+          国内:通义千问、文心一言、讯飞星火、智谱 GLM、DeepSeek
           国外:OpenAI、Ollama、Midjourney、StableDiffusion、Suno
     </description>
 

+ 1 - 1
yudao-module-ai/yudao-module-ai-biz/pom.xml

@@ -14,7 +14,7 @@
     <description>
         ai 模块下,接入 LLM 大模型,支持聊天、绘图、音乐、写作、思维脑图等功能。
         目前已接入各种模型,不限于:
-        国内:通义千问、文心一言、讯飞星火
+        国内:通义千问、文心一言、讯飞星火、智谱 GLM、DeepSeek
         国外:OpenAI、Ollama、Midjourney、StableDiffusion、Suno
     </description>
 

+ 6 - 9
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/package-info.java

@@ -1,13 +1,10 @@
 /**
- * 从 https://github.com/spring-projects/spring-ai 拷贝。
+ * AI 大模型组件,基于 Spring AI 拓展
  *
- * 最大目的:适配 JDK8 兼容性
- *
- * 包路径:
- * 1. chat、parser、model、parser 包:https://github.com/spring-projects/spring-ai/tree/main/spring-ai-core 拷贝
- * 2. models 包:对标 https://github.com/spring-projects/spring-ai/tree/main/models 拷贝
- *  2.1 xinghuo 包:【讯飞】星火,自己实现
- *  2.2 midjourney 包:Midjourney API,对接 https://github.com/novicezk/midjourney-proxy 实现
- *  2.3 suno 包:Suno API,对接 https://github.com/gcui-art/suno-api 实现
+ * models 包路径:
+ *  1. xinghuo 包:【讯飞】星火,自己实现
+ *  2. deepseek 包:【深度求索】DeepSeek,自己实现
+ *  3. midjourney 包:Midjourney API,对接 https://github.com/novicezk/midjourney-proxy 实现
+ *  4. suno 包:Suno API,对接 https://github.com/gcui-art/suno-api 实现
  */
 package cn.iocoder.yudao.framework.ai;

+ 1 - 0
yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/image/OpenAiImageModelTests.java

@@ -35,6 +35,7 @@ public class OpenAiImageModelTests {
 
         // 方法调用
         ImageResponse response = imageClient.call(prompt);
+        // 打印结果
         System.out.println(response);
     }
 

+ 65 - 0
yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/image/StabilityAiImageModelTests.java

@@ -0,0 +1,65 @@
+package cn.iocoder.yudao.framework.ai.image;
+
+import cn.hutool.core.codec.Base64;
+import cn.hutool.core.thread.ThreadUtil;
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.api.Test;
+import org.springframework.ai.image.ImageOptions;
+import org.springframework.ai.image.ImagePrompt;
+import org.springframework.ai.image.ImageResponse;
+import org.springframework.ai.openai.OpenAiImageOptions;
+import org.springframework.ai.stabilityai.StabilityAiImageModel;
+import org.springframework.ai.stabilityai.api.StabilityAiApi;
+
+import javax.swing.*;
+import java.awt.*;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * {@link StabilityAiImageModel} 集成测试类
+ *
+ * @author fansili
+ */
+public class StabilityAiImageModelTests {
+
+    private final StabilityAiApi imageApi = new StabilityAiApi(
+            "sk-e53UqbboF8QJCscYvzJscJxJXoFcFg4iJjl1oqgE7baJETmx");
+    private final StabilityAiImageModel imageClient = new StabilityAiImageModel(imageApi);
+
+    @Test
+    @Disabled
+    public void testCall() {
+        // 准备参数
+        ImageOptions options = OpenAiImageOptions.builder()
+                .withModel("stable-diffusion-v1-6")
+                .withHeight(256).withWidth(256)
+                .build();
+        ImagePrompt prompt = new ImagePrompt("great wall", options);
+
+        // 方法调用
+        ImageResponse response = imageClient.call(prompt);
+        // 打印结果
+        String b64Json = response.getResult().getOutput().getB64Json();
+        System.out.println(response);
+        viewImage(b64Json);
+    }
+
+    public static void viewImage(String b64Json) {
+        // 创建一个 JFrame
+        JFrame frame = new JFrame("Byte Image Display");
+        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+        frame.setSize(800, 600);
+
+        // 创建一个 JLabel 来显示图片
+        byte[] imageBytes = Base64.decode(b64Json);
+        JLabel label = new JLabel(new ImageIcon(imageBytes));
+
+        // 将 JLabel 添加到 JFrame
+        frame.getContentPane().add(label, BorderLayout.CENTER);
+
+        // 显示 JFrame
+        frame.setVisible(true);
+        ThreadUtil.sleep(1, TimeUnit.HOURS);
+    }
+
+}