Commit c5ed07a091e15d72fb61bcec656c2bd3e59a5046

Authored by YunaiV
1 parent 6e64ae77

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

yudao-module-ai/pom.xml
@@ -20,7 +20,7 @@ @@ -20,7 +20,7 @@
20 <description> 20 <description>
21 ai 模块下,接入 LLM 大模型,支持聊天、绘图、音乐、写作、思维脑图等功能。 21 ai 模块下,接入 LLM 大模型,支持聊天、绘图、音乐、写作、思维脑图等功能。
22 目前已接入各种模型,不限于: 22 目前已接入各种模型,不限于:
23 - 国内:通义千问、文心一言、讯飞星火 23 + 国内:通义千问、文心一言、讯飞星火、智谱 GLM、DeepSeek
24 国外:OpenAI、Ollama、Midjourney、StableDiffusion、Suno 24 国外:OpenAI、Ollama、Midjourney、StableDiffusion、Suno
25 </description> 25 </description>
26 26
yudao-module-ai/yudao-module-ai-biz/pom.xml
@@ -14,7 +14,7 @@ @@ -14,7 +14,7 @@
14 <description> 14 <description>
15 ai 模块下,接入 LLM 大模型,支持聊天、绘图、音乐、写作、思维脑图等功能。 15 ai 模块下,接入 LLM 大模型,支持聊天、绘图、音乐、写作、思维脑图等功能。
16 目前已接入各种模型,不限于: 16 目前已接入各种模型,不限于:
17 - 国内:通义千问、文心一言、讯飞星火 17 + 国内:通义千问、文心一言、讯飞星火、智谱 GLM、DeepSeek
18 国外:OpenAI、Ollama、Midjourney、StableDiffusion、Suno 18 国外:OpenAI、Ollama、Midjourney、StableDiffusion、Suno
19 </description> 19 </description>
20 20
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/package-info.java
1 /** 1 /**
2 - * 从 https://github.com/spring-projects/spring-ai 拷贝。 2 + * AI 大模型组件,基于 Spring AI 拓展
3 * 3 *
4 - * 最大目的:适配 JDK8 兼容性  
5 - *  
6 - * 包路径:  
7 - * 1. chat、parser、model、parser 包:https://github.com/spring-projects/spring-ai/tree/main/spring-ai-core 拷贝  
8 - * 2. models 包:对标 https://github.com/spring-projects/spring-ai/tree/main/models 拷贝  
9 - * 2.1 xinghuo 包:【讯飞】星火,自己实现  
10 - * 2.2 midjourney 包:Midjourney API,对接 https://github.com/novicezk/midjourney-proxy 实现  
11 - * 2.3 suno 包:Suno API,对接 https://github.com/gcui-art/suno-api 实现 4 + * models 包路径:
  5 + * 1. xinghuo 包:【讯飞】星火,自己实现
  6 + * 2. deepseek 包:【深度求索】DeepSeek,自己实现
  7 + * 3. midjourney 包:Midjourney API,对接 https://github.com/novicezk/midjourney-proxy 实现
  8 + * 4. suno 包:Suno API,对接 https://github.com/gcui-art/suno-api 实现
12 */ 9 */
13 package cn.iocoder.yudao.framework.ai; 10 package cn.iocoder.yudao.framework.ai;
14 \ No newline at end of file 11 \ No newline at end of file
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 { @@ -35,6 +35,7 @@ public class OpenAiImageModelTests {
35 35
36 // 方法调用 36 // 方法调用
37 ImageResponse response = imageClient.call(prompt); 37 ImageResponse response = imageClient.call(prompt);
  38 + // 打印结果
38 System.out.println(response); 39 System.out.println(response);
39 } 40 }
40 41
yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/image/StabilityAiImageModelTests.java 0 → 100644
  1 +package cn.iocoder.yudao.framework.ai.image;
  2 +
  3 +import cn.hutool.core.codec.Base64;
  4 +import cn.hutool.core.thread.ThreadUtil;
  5 +import org.junit.jupiter.api.Disabled;
  6 +import org.junit.jupiter.api.Test;
  7 +import org.springframework.ai.image.ImageOptions;
  8 +import org.springframework.ai.image.ImagePrompt;
  9 +import org.springframework.ai.image.ImageResponse;
  10 +import org.springframework.ai.openai.OpenAiImageOptions;
  11 +import org.springframework.ai.stabilityai.StabilityAiImageModel;
  12 +import org.springframework.ai.stabilityai.api.StabilityAiApi;
  13 +
  14 +import javax.swing.*;
  15 +import java.awt.*;
  16 +import java.util.concurrent.TimeUnit;
  17 +
  18 +/**
  19 + * {@link StabilityAiImageModel} 集成测试类
  20 + *
  21 + * @author fansili
  22 + */
  23 +public class StabilityAiImageModelTests {
  24 +
  25 + private final StabilityAiApi imageApi = new StabilityAiApi(
  26 + "sk-e53UqbboF8QJCscYvzJscJxJXoFcFg4iJjl1oqgE7baJETmx");
  27 + private final StabilityAiImageModel imageClient = new StabilityAiImageModel(imageApi);
  28 +
  29 + @Test
  30 + @Disabled
  31 + public void testCall() {
  32 + // 准备参数
  33 + ImageOptions options = OpenAiImageOptions.builder()
  34 + .withModel("stable-diffusion-v1-6")
  35 + .withHeight(256).withWidth(256)
  36 + .build();
  37 + ImagePrompt prompt = new ImagePrompt("great wall", options);
  38 +
  39 + // 方法调用
  40 + ImageResponse response = imageClient.call(prompt);
  41 + // 打印结果
  42 + String b64Json = response.getResult().getOutput().getB64Json();
  43 + System.out.println(response);
  44 + viewImage(b64Json);
  45 + }
  46 +
  47 + public static void viewImage(String b64Json) {
  48 + // 创建一个 JFrame
  49 + JFrame frame = new JFrame("Byte Image Display");
  50 + frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  51 + frame.setSize(800, 600);
  52 +
  53 + // 创建一个 JLabel 来显示图片
  54 + byte[] imageBytes = Base64.decode(b64Json);
  55 + JLabel label = new JLabel(new ImageIcon(imageBytes));
  56 +
  57 + // 将 JLabel 添加到 JFrame
  58 + frame.getContentPane().add(label, BorderLayout.CENTER);
  59 +
  60 + // 显示 JFrame
  61 + frame.setVisible(true);
  62 + ThreadUtil.sleep(1, TimeUnit.HOURS);
  63 + }
  64 +
  65 +}