Commit f300b8a1ae7097fed00520edf4315e5dca420920

Authored by cherishsince
1 parent 246c2332

【修改todo】增加 sd 各种参数

yudao-module-ai/yudao-module-ai-api/src/main/java/cn/iocoder/yudao/module/ai/enums/ErrorCodeConstants.java
... ... @@ -37,6 +37,7 @@ public interface ErrorCodeConstants {
37 37 ErrorCode IMAGE_NOT_EXISTS = new ErrorCode(1_022_005_000, "图片不存在!");
38 38 ErrorCode IMAGE_MIDJOURNEY_SUBMIT_FAIL = new ErrorCode(1_022_005_001, "Midjourney 提交失败!原因:{}");
39 39 ErrorCode IMAGE_CUSTOM_ID_NOT_EXISTS = new ErrorCode(1_022_005_002, "Midjourney 按钮 customId 不存在! {}");
  40 + ErrorCode IMAGE_FAIL = new ErrorCode(1_022_005_002, "图片绘画失败! {}");
40 41  
41 42 // ========== API 音乐 1-040-006-000 ==========
42 43 ErrorCode MUSIC_NOT_EXISTS = new ErrorCode(1_022_006_000, "音乐不存在!");
... ...
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/image/AiImageServiceImpl.java
... ... @@ -123,9 +123,16 @@ public class AiImageServiceImpl implements AiImageService {
123 123 .withResponseFormat("b64_json")
124 124 .build();
125 125 } else if (ObjUtil.equal(draw.getPlatform(), AiPlatformEnum.STABLE_DIFFUSION.getPlatform())) {
  126 + // https://platform.stability.ai/docs/api-reference#tag/SDXL-and-SD1.6/operation/textToImage
126 127 // https://platform.stability.ai/docs/api-reference#tag/Text-to-Image/operation/textToImage
127 128 return StabilityAiImageOptions.builder().withModel(draw.getModel())
128   - .withHeight(draw.getHeight()).withWidth(draw.getWidth()) // TODO @范:各种参数的接入
  129 + .withHeight(draw.getHeight()).withWidth(draw.getWidth())
  130 + .withSeed(Long.valueOf(draw.getOptions().get("seed")))
  131 + .withCfgScale(Float.valueOf(draw.getOptions().get("scale")))
  132 + .withSteps(Integer.valueOf(draw.getOptions().get("steps")))
  133 + .withSampler(String.valueOf(draw.getOptions().get("sampler")))
  134 + .withStylePreset(String.valueOf(draw.getOptions().get("stylePreset")))
  135 + .withClipGuidancePreset(String.valueOf(draw.getOptions().get("clipGuidancePreset")))
129 136 .build();
130 137 }
131 138 throw new IllegalArgumentException("不支持的 AI 平台:" + draw.getPlatform());
... ...
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;
2   -
3   -import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenChatClient;
4   -import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenChatModal;
5   -import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenOptions;
6   -import cn.iocoder.yudao.framework.ai.core.model.tongyi.api.QianWenApi;
7   -import com.alibaba.dashscope.aigc.generation.GenerationResult;
8   -import com.alibaba.dashscope.aigc.generation.models.QwenParam;
9   -import com.alibaba.dashscope.common.Message;
10   -import com.alibaba.dashscope.common.MessageManager;
11   -import com.alibaba.dashscope.common.Role;
12   -import com.alibaba.dashscope.exception.InputRequiredException;
13   -import com.alibaba.dashscope.exception.NoApiKeyException;
14   -import org.junit.Before;
15   -import org.junit.Test;
16   -import org.springframework.ai.chat.messages.SystemMessage;
17   -import org.springframework.ai.chat.messages.UserMessage;
18   -import org.springframework.ai.chat.model.ChatResponse;
19   -import org.springframework.ai.chat.prompt.Prompt;
20   -import reactor.core.publisher.Flux;
21   -
22   -import java.util.ArrayList;
23   -import java.util.List;
24   -import java.util.Scanner;
25   -import java.util.function.Consumer;
26   -
27   -// TODO 芋艿:整理单测
28   -/**
29   - * author: fansili
30   - * time: 2024/3/13 21:37
31   - */
32   -public class QianWenChatClientTests {
33   -
34   - private QianWenChatClient qianWenChatClient;
35   -
36   - @Before
37   - public void setup() {
38   - QianWenApi qianWenApi = new QianWenApi("sk-Zsd81gZYg7", QianWenChatModal.QWEN_72B_CHAT);
39   - QianWenOptions qianWenOptions = new QianWenOptions();
40   - qianWenOptions.setTopP(0.8F);
41   -// qianWenOptions.setTopK(3); TODO 芋艿:临时处理
42   -// qianWenOptions.setTemperature(0.6F); TODO 芋艿:临时处理
43   - qianWenChatClient = new QianWenChatClient(
44   - qianWenApi,
45   - qianWenOptions
46   - );
47   - }
48   -
49   - @Test
50   - public void callTest() {
51   - List<org.springframework.ai.chat.messages.Message> messages = new ArrayList<>();
52   - messages.add(new SystemMessage("你是一个优质的小红书文艺作者,抒写着各城市的美好文化和风景。"));
53   - messages.add(new UserMessage("长沙怎么样?"));
54   -
55   - ChatResponse call = qianWenChatClient.call(new Prompt(messages));
56   - System.err.println(call.getResult());
57   - }
58   -
59   - @Test
60   - public void streamTest() {
61   - List<org.springframework.ai.chat.messages.Message> messages = new ArrayList<>();
62   - messages.add(new SystemMessage("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。"));
63   - messages.add(new UserMessage("长沙怎么样?"));
64   -
65   - Flux<ChatResponse> flux = qianWenChatClient.stream(new Prompt(messages));
66   - flux.subscribe(new Consumer<ChatResponse>() {
67   - @Override
68   - public void accept(ChatResponse chatResponse) {
69   - System.err.print(chatResponse.getResult().getOutput().getContent());
70   - }
71   - });
72   -
73   - // 阻止退出
74   - Scanner scanner = new Scanner(System.in);
75   - scanner.nextLine();
76   - }
77   -
78   - @Test
79   - public void qianwenDemoTest() throws NoApiKeyException, InputRequiredException {
80   - com.alibaba.dashscope.aigc.generation.Generation gen = new com.alibaba.dashscope.aigc.generation.Generation();
81   - MessageManager msgManager = new MessageManager(10);
82   - Message systemMsg =
83   - Message.builder().role(Role.SYSTEM.getValue()).content("You are a helpful assistant.").build();
84   - Message userMsg = Message.builder().role(Role.USER.getValue()).content("就当前的海洋污染的情况,写一份限塑的倡议书提纲,需要有理有据地号召大家克制地使用塑料制品").build();
85   - msgManager.add(systemMsg);
86   - msgManager.add(userMsg);
87   - QwenParam param =
88   - QwenParam.builder().model("qwen-72b-chat").messages(msgManager.get())
89   - .resultFormat(QwenParam.ResultFormat.MESSAGE)
90   - .topP(0.8)
91   - /* set the random seed, optional, default to 1234 if not set */
92   - .seed(100)
93   - .apiKey("sk-Zsd81gZYg7")
94   - .build();
95   - GenerationResult result = gen.call(param);
96   - System.out.println(result);
97   - System.out.println("-----------------");
98   - System.out.println("-----------------");
99   - msgManager.add(result);
100   - param.setPrompt("能否缩短一些,只讲三点");
101   - param.setMessages(msgManager.get());
102   - result = gen.call(param);
103   - System.out.println(result);
104   - }
105   -}
  1 +//package cn.iocoder.yudao.framework.ai.chat;
  2 +//
  3 +//import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenChatClient;
  4 +//import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenChatModal;
  5 +//import cn.iocoder.yudao.framework.ai.core.model.tongyi.QianWenOptions;
  6 +//import cn.iocoder.yudao.framework.ai.core.model.tongyi.api.QianWenApi;
  7 +//import com.alibaba.dashscope.aigc.generation.GenerationResult;
  8 +//import com.alibaba.dashscope.aigc.generation.models.QwenParam;
  9 +//import com.alibaba.dashscope.common.Message;
  10 +//import com.alibaba.dashscope.common.MessageManager;
  11 +//import com.alibaba.dashscope.common.Role;
  12 +//import com.alibaba.dashscope.exception.InputRequiredException;
  13 +//import com.alibaba.dashscope.exception.NoApiKeyException;
  14 +//import org.junit.Before;
  15 +//import org.junit.Test;
  16 +//import org.springframework.ai.chat.messages.SystemMessage;
  17 +//import org.springframework.ai.chat.messages.UserMessage;
  18 +//import org.springframework.ai.chat.model.ChatResponse;
  19 +//import org.springframework.ai.chat.prompt.Prompt;
  20 +//import reactor.core.publisher.Flux;
  21 +//
  22 +//import java.util.ArrayList;
  23 +//import java.util.List;
  24 +//import java.util.Scanner;
  25 +//import java.util.function.Consumer;
  26 +//
  27 +//// TODO 芋艿:整理单测
  28 +///**
  29 +// * author: fansili
  30 +// * time: 2024/3/13 21:37
  31 +// */
  32 +//public class QianWenChatClientTests {
  33 +//
  34 +// private QianWenChatClient qianWenChatClient;
  35 +//
  36 +// @Before
  37 +// public void setup() {
  38 +// QianWenApi qianWenApi = new QianWenApi("sk-Zsd81gZYg7", QianWenChatModal.QWEN_72B_CHAT);
  39 +// QianWenOptions qianWenOptions = new QianWenOptions();
  40 +// qianWenOptions.setTopP(0.8F);
  41 +//// qianWenOptions.setTopK(3); TODO 芋艿:临时处理
  42 +//// qianWenOptions.setTemperature(0.6F); TODO 芋艿:临时处理
  43 +// qianWenChatClient = new QianWenChatClient(
  44 +// qianWenApi,
  45 +// qianWenOptions
  46 +// );
  47 +// }
  48 +//
  49 +// @Test
  50 +// public void callTest() {
  51 +// List<org.springframework.ai.chat.messages.Message> messages = new ArrayList<>();
  52 +// messages.add(new SystemMessage("你是一个优质的小红书文艺作者,抒写着各城市的美好文化和风景。"));
  53 +// messages.add(new UserMessage("长沙怎么样?"));
  54 +//
  55 +// ChatResponse call = qianWenChatClient.call(new Prompt(messages));
  56 +// System.err.println(call.getResult());
  57 +// }
  58 +//
  59 +// @Test
  60 +// public void streamTest() {
  61 +// List<org.springframework.ai.chat.messages.Message> messages = new ArrayList<>();
  62 +// messages.add(new SystemMessage("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。"));
  63 +// messages.add(new UserMessage("长沙怎么样?"));
  64 +//
  65 +// Flux<ChatResponse> flux = qianWenChatClient.stream(new Prompt(messages));
  66 +// flux.subscribe(new Consumer<ChatResponse>() {
  67 +// @Override
  68 +// public void accept(ChatResponse chatResponse) {
  69 +// System.err.print(chatResponse.getResult().getOutput().getContent());
  70 +// }
  71 +// });
  72 +//
  73 +// // 阻止退出
  74 +// Scanner scanner = new Scanner(System.in);
  75 +// scanner.nextLine();
  76 +// }
  77 +//
  78 +// @Test
  79 +// public void qianwenDemoTest() throws NoApiKeyException, InputRequiredException {
  80 +// com.alibaba.dashscope.aigc.generation.Generation gen = new com.alibaba.dashscope.aigc.generation.Generation();
  81 +// MessageManager msgManager = new MessageManager(10);
  82 +// Message systemMsg =
  83 +// Message.builder().role(Role.SYSTEM.getValue()).content("You are a helpful assistant.").build();
  84 +// Message userMsg = Message.builder().role(Role.USER.getValue()).content("就当前的海洋污染的情况,写一份限塑的倡议书提纲,需要有理有据地号召大家克制地使用塑料制品").build();
  85 +// msgManager.add(systemMsg);
  86 +// msgManager.add(userMsg);
  87 +// QwenParam param =
  88 +// QwenParam.builder().model("qwen-72b-chat").messages(msgManager.get())
  89 +// .resultFormat(QwenParam.ResultFormat.MESSAGE)
  90 +// .topP(0.8)
  91 +// /* set the random seed, optional, default to 1234 if not set */
  92 +// .seed(100)
  93 +// .apiKey("sk-Zsd81gZYg7")
  94 +// .build();
  95 +// GenerationResult result = gen.call(param);
  96 +// System.out.println(result);
  97 +// System.out.println("-----------------");
  98 +// System.out.println("-----------------");
  99 +// msgManager.add(result);
  100 +// param.setPrompt("能否缩短一些,只讲三点");
  101 +// param.setMessages(msgManager.get());
  102 +// result = gen.call(param);
  103 +// System.out.println(result);
  104 +// }
  105 +//}
... ...