Commit d05a7bd59a83ab5a0102d7380022c6f0eeedff85

Authored by YunaiV
1 parent 5655ae92

【代码优化】AI:适配 Spring AI 1.0.6 对 Ollama 的逻辑

yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/image/AiImageServiceImpl.java
@@ -20,6 +20,7 @@ import cn.iocoder.yudao.module.ai.dal.mysql.image.AiImageMapper; @@ -20,6 +20,7 @@ import cn.iocoder.yudao.module.ai.dal.mysql.image.AiImageMapper;
20 import cn.iocoder.yudao.module.ai.enums.image.AiImageStatusEnum; 20 import cn.iocoder.yudao.module.ai.enums.image.AiImageStatusEnum;
21 import cn.iocoder.yudao.module.ai.service.model.AiApiKeyService; 21 import cn.iocoder.yudao.module.ai.service.model.AiApiKeyService;
22 import cn.iocoder.yudao.module.infra.api.file.FileApi; 22 import cn.iocoder.yudao.module.infra.api.file.FileApi;
  23 +import com.alibaba.cloud.ai.dashscope.image.DashScopeImageOptions;
23 import jakarta.annotation.Resource; 24 import jakarta.annotation.Resource;
24 import lombok.extern.slf4j.Slf4j; 25 import lombok.extern.slf4j.Slf4j;
25 import org.springframework.ai.image.ImageModel; 26 import org.springframework.ai.image.ImageModel;
@@ -133,14 +134,14 @@ public class AiImageServiceImpl implements AiImageService { @@ -133,14 +134,14 @@ public class AiImageServiceImpl implements AiImageService {
133 } else if (ObjUtil.equal(draw.getPlatform(), AiPlatformEnum.STABLE_DIFFUSION.getPlatform())) { 134 } else if (ObjUtil.equal(draw.getPlatform(), AiPlatformEnum.STABLE_DIFFUSION.getPlatform())) {
134 // https://platform.stability.ai/docs/api-reference#tag/SDXL-and-SD1.6/operation/textToImage 135 // https://platform.stability.ai/docs/api-reference#tag/SDXL-and-SD1.6/operation/textToImage
135 // https://platform.stability.ai/docs/api-reference#tag/Text-to-Image/operation/textToImage 136 // https://platform.stability.ai/docs/api-reference#tag/Text-to-Image/operation/textToImage
136 - return StabilityAiImageOptions.builder().withModel(draw.getModel())  
137 - .withHeight(draw.getHeight()).withWidth(draw.getWidth())  
138 - .withSeed(Long.valueOf(draw.getOptions().get("seed")))  
139 - .withCfgScale(Float.valueOf(draw.getOptions().get("scale")))  
140 - .withSteps(Integer.valueOf(draw.getOptions().get("steps")))  
141 - .withSampler(String.valueOf(draw.getOptions().get("sampler")))  
142 - .withStylePreset(String.valueOf(draw.getOptions().get("stylePreset")))  
143 - .withClipGuidancePreset(String.valueOf(draw.getOptions().get("clipGuidancePreset"))) 137 + return StabilityAiImageOptions.builder().model(draw.getModel())
  138 + .height(draw.getHeight()).width(draw.getWidth())
  139 + .seed(Long.valueOf(draw.getOptions().get("seed")))
  140 + .cfgScale(Float.valueOf(draw.getOptions().get("scale")))
  141 + .steps(Integer.valueOf(draw.getOptions().get("steps")))
  142 + .sampler(String.valueOf(draw.getOptions().get("sampler")))
  143 + .stylePreset(String.valueOf(draw.getOptions().get("stylePreset")))
  144 + .clipGuidancePreset(String.valueOf(draw.getOptions().get("clipGuidancePreset")))
144 .build(); 145 .build();
145 } else if (ObjUtil.equal(draw.getPlatform(), AiPlatformEnum.TONG_YI.getPlatform())) { 146 } else if (ObjUtil.equal(draw.getPlatform(), AiPlatformEnum.TONG_YI.getPlatform())) {
146 return DashScopeImageOptions.builder() 147 return DashScopeImageOptions.builder()
@@ -149,12 +150,12 @@ public class AiImageServiceImpl implements AiImageService { @@ -149,12 +150,12 @@ public class AiImageServiceImpl implements AiImageService {
149 .build(); 150 .build();
150 } else if (ObjUtil.equal(draw.getPlatform(), AiPlatformEnum.YI_YAN.getPlatform())) { 151 } else if (ObjUtil.equal(draw.getPlatform(), AiPlatformEnum.YI_YAN.getPlatform())) {
151 return QianFanImageOptions.builder() 152 return QianFanImageOptions.builder()
152 - .withModel(draw.getModel()).withN(1)  
153 - .withHeight(draw.getHeight()).withWidth(draw.getWidth()) 153 + .model(draw.getModel()).N(1)
  154 + .height(draw.getHeight()).width(draw.getWidth())
154 .build(); 155 .build();
155 } else if (ObjUtil.equal(draw.getPlatform(), AiPlatformEnum.ZHI_PU.getPlatform())) { 156 } else if (ObjUtil.equal(draw.getPlatform(), AiPlatformEnum.ZHI_PU.getPlatform())) {
156 return ZhiPuAiImageOptions.builder() 157 return ZhiPuAiImageOptions.builder()
157 - .withModel(draw.getModel()) 158 + .model(draw.getModel())
158 .build(); 159 .build();
159 } 160 }
160 throw new IllegalArgumentException("不支持的 AI 平台:" + draw.getPlatform()); 161 throw new IllegalArgumentException("不支持的 AI 平台:" + draw.getPlatform());
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/config/YudaoAiAutoConfiguration.java
@@ -35,7 +35,6 @@ public class YudaoAiAutoConfiguration { @@ -35,7 +35,6 @@ public class YudaoAiAutoConfiguration {
35 return new AiModelFactoryImpl(); 35 return new AiModelFactoryImpl();
36 } 36 }
37 37
38 -  
39 // ========== 各种 AI Client 创建 ========== 38 // ========== 各种 AI Client 创建 ==========
40 39
41 @Bean 40 @Bean
yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/chat/LlamaChatModelTests.java
1 package cn.iocoder.yudao.framework.ai.chat; 1 package cn.iocoder.yudao.framework.ai.chat;
2 2
  3 +import org.junit.jupiter.api.Disabled;
  4 +import org.junit.jupiter.api.Test;
  5 +import org.springframework.ai.chat.messages.Message;
  6 +import org.springframework.ai.chat.messages.SystemMessage;
  7 +import org.springframework.ai.chat.messages.UserMessage;
  8 +import org.springframework.ai.chat.model.ChatResponse;
  9 +import org.springframework.ai.chat.prompt.Prompt;
3 import org.springframework.ai.ollama.OllamaChatModel; 10 import org.springframework.ai.ollama.OllamaChatModel;
  11 +import org.springframework.ai.ollama.api.OllamaApi;
  12 +import org.springframework.ai.ollama.api.OllamaModel;
  13 +import org.springframework.ai.ollama.api.OllamaOptions;
  14 +import reactor.core.publisher.Flux;
  15 +
  16 +import java.util.ArrayList;
  17 +import java.util.List;
4 18
5 /** 19 /**
6 * {@link OllamaChatModel} 集成测试 20 * {@link OllamaChatModel} 集成测试
@@ -9,41 +23,43 @@ import org.springframework.ai.ollama.OllamaChatModel; @@ -9,41 +23,43 @@ import org.springframework.ai.ollama.OllamaChatModel;
9 */ 23 */
10 public class LlamaChatModelTests { 24 public class LlamaChatModelTests {
11 25
12 -// private final OllamaApi ollamaApi = new OllamaApi(  
13 -// "http://127.0.0.1:11434");  
14 -// private final OllamaChatModel chatModel = new OllamaChatModel(ollamaApi,  
15 -// OllamaOptions.create().withModel(OllamaModel.LLAMA3.getModelName()));  
16 -//  
17 -// @Test  
18 -// @Disabled  
19 -// public void testCall() {  
20 -// // 准备参数  
21 -// List<Message> messages = new ArrayList<>();  
22 -// messages.add(new SystemMessage("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。"));  
23 -// messages.add(new UserMessage("1 + 1 = ?"));  
24 -//  
25 -// // 调用  
26 -// ChatResponse response = chatModel.call(new Prompt(messages));  
27 -// // 打印结果  
28 -// System.out.println(response);  
29 -// System.out.println(response.getResult().getOutput());  
30 -// }  
31 -//  
32 -// @Test  
33 -// @Disabled  
34 -// public void testStream() {  
35 -// // 准备参数  
36 -// List<Message> messages = new ArrayList<>();  
37 -// messages.add(new SystemMessage("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。"));  
38 -// messages.add(new UserMessage("1 + 1 = ?"));  
39 -//  
40 -// // 调用  
41 -// Flux<ChatResponse> flux = chatModel.stream(new Prompt(messages));  
42 -// // 打印结果  
43 -// flux.doOnNext(response -> {  
44 -//// System.out.println(response);  
45 -// System.out.println(response.getResult().getOutput());  
46 -// }).then().block();  
47 -// } 26 + private final OllamaChatModel chatModel = OllamaChatModel.builder()
  27 + .ollamaApi(new OllamaApi("http://127.0.0.1:11434")) // Ollama 服务地址
  28 + .defaultOptions(OllamaOptions.builder()
  29 + .model(OllamaModel.LLAMA3.getName()) // 模型
  30 + .build())
  31 + .build();
  32 +
  33 + @Test
  34 + @Disabled
  35 + public void testCall() {
  36 + // 准备参数
  37 + List<Message> messages = new ArrayList<>();
  38 + messages.add(new SystemMessage("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。"));
  39 + messages.add(new UserMessage("1 + 1 = ?"));
  40 +
  41 + // 调用
  42 + ChatResponse response = chatModel.call(new Prompt(messages));
  43 + // 打印结果
  44 + System.out.println(response);
  45 + System.out.println(response.getResult().getOutput());
  46 + }
  47 +
  48 + @Test
  49 + @Disabled
  50 + public void testStream() {
  51 + // 准备参数
  52 + List<Message> messages = new ArrayList<>();
  53 + messages.add(new SystemMessage("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。"));
  54 + messages.add(new UserMessage("1 + 1 = ?"));
  55 +
  56 + // 调用
  57 + Flux<ChatResponse> flux = chatModel.stream(new Prompt(messages));
  58 + // 打印结果
  59 + flux.doOnNext(response -> {
  60 +// System.out.println(response);
  61 + System.out.println(response.getResult().getOutput());
  62 + }).then().block();
  63 + }
48 64
49 } 65 }