Commit 9de9e938bf6d113aac62f419e7db35518e8b4536
1 parent
7fca38ce
【新增】AI:增加 ollama 模型的接入
Showing
5 changed files
with
39 additions
and
20 deletions
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/config/AiChatClientFactory.java
| ... | ... | @@ -6,6 +6,7 @@ import org.springframework.ai.chat.StreamingChatClient; |
| 6 | 6 | import org.springframework.ai.models.tongyi.QianWenChatClient; |
| 7 | 7 | import org.springframework.ai.models.xinghuo.XingHuoChatClient; |
| 8 | 8 | import org.springframework.ai.models.yiyan.YiYanChatClient; |
| 9 | +import org.springframework.ai.ollama.OllamaChatClient; | |
| 9 | 10 | import org.springframework.beans.factory.annotation.Autowired; |
| 10 | 11 | import org.springframework.context.ApplicationContext; |
| 11 | 12 | import org.springframework.stereotype.Component; |
| ... | ... | @@ -36,12 +37,17 @@ public class AiChatClientFactory { |
| 36 | 37 | |
| 37 | 38 | // TODO yunai 要不再加一个接口,让他们拥有 ChatClient、StreamingChatClient 功能 |
| 38 | 39 | public StreamingChatClient getStreamingChatClient(AiPlatformEnum platformEnum) { |
| 40 | +// if (true) { | |
| 41 | +// return applicationContext.getBean(OllamaChatClient.class); | |
| 42 | +// } | |
| 39 | 43 | if (AiPlatformEnum.QIAN_WEN == platformEnum) { |
| 40 | 44 | return applicationContext.getBean(QianWenChatClient.class); |
| 41 | 45 | } else if (AiPlatformEnum.YI_YAN == platformEnum) { |
| 42 | 46 | return applicationContext.getBean(YiYanChatClient.class); |
| 43 | 47 | } else if (AiPlatformEnum.XING_HUO == platformEnum) { |
| 44 | 48 | return applicationContext.getBean(XingHuoChatClient.class); |
| 49 | + } else if (AiPlatformEnum.OLLAMA == platformEnum) { | |
| 50 | + return applicationContext.getBean(OllamaChatClient.class); | |
| 45 | 51 | } |
| 46 | 52 | throw new IllegalArgumentException("不支持的 chat client!"); |
| 47 | 53 | } | ... | ... |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/impl/AiChatServiceImpl.java
| ... | ... | @@ -127,7 +127,7 @@ public class AiChatServiceImpl implements AiChatService { |
| 127 | 127 | // 1.1 校验对话存在 |
| 128 | 128 | AiChatConversationDO conversation = chatConversationService.validateExists(sendReqVO.getConversationId()); |
| 129 | 129 | if (ObjUtil.notEqual(conversation.getUserId(), userId)) { |
| 130 | - throw exception(CHAT_CONVERSATION_NOT_EXISTS); | |
| 130 | + throw exception(CHAT_CONVERSATION_NOT_EXISTS); // TODO 芋艿:异常情况的对接; | |
| 131 | 131 | } |
| 132 | 132 | // 1.2 校验模型 |
| 133 | 133 | AiChatModelDO model = chatModalService.validateChatModel(conversation.getModelId()); | ... | ... |
yudao-module-ai/yudao-spring-boot-starter-ai/pom.xml
| ... | ... | @@ -25,6 +25,12 @@ |
| 25 | 25 | </dependency> |
| 26 | 26 | |
| 27 | 27 | <dependency> |
| 28 | + <groupId>io.springboot.ai</groupId> | |
| 29 | + <artifactId>spring-ai-ollama-spring-boot-starter</artifactId> | |
| 30 | + <version>1.0.3</version> | |
| 31 | + </dependency> | |
| 32 | + | |
| 33 | + <dependency> | |
| 28 | 34 | <groupId>cn.iocoder.boot</groupId> |
| 29 | 35 | <artifactId>yudao-common</artifactId> |
| 30 | 36 | </dependency> | ... | ... |
yudao-module-ai/yudao-spring-boot-starter-ai/src/main/java/cn/iocoder/yudao/framework/ai/core/enums/AiPlatformEnum.java
| 1 | 1 | package cn.iocoder.yudao.framework.ai.core.enums; |
| 2 | 2 | |
| 3 | -import com.google.common.collect.Lists; | |
| 4 | 3 | import lombok.AllArgsConstructor; |
| 5 | 4 | import lombok.Getter; |
| 6 | 5 | |
| 7 | -import java.util.List; | |
| 8 | - | |
| 9 | 6 | // TODO 芋艿:这块,看看要不要调整下; |
| 10 | 7 | /** |
| 11 | 8 | * ai 模型平台 |
| ... | ... | @@ -17,29 +14,31 @@ import java.util.List; |
| 17 | 14 | @AllArgsConstructor |
| 18 | 15 | public enum AiPlatformEnum { |
| 19 | 16 | |
| 17 | + OPENAI("OpenAI", "OpenAI"), | |
| 18 | + OLLAMA("dall", "dall"), | |
| 19 | + | |
| 20 | 20 | YI_YAN("yiyan", "一言"), |
| 21 | 21 | QIAN_WEN("qianwen", "千问"), |
| 22 | 22 | XING_HUO("xinghuo", "星火"), |
| 23 | - OPENAI("OpenAI", "OpenAI"), | |
| 24 | 23 | OPEN_AI_DALL("dall", "dall"), |
| 25 | - MIDJOURNEY("midjourney", "midjourney"), | |
| 24 | + MIDJOURNEY("Ollama", "Ollama"), | |
| 26 | 25 | |
| 27 | 26 | ; |
| 28 | 27 | |
| 29 | - private String platform; | |
| 30 | - private String name; | |
| 31 | - | |
| 32 | - public static List<AiPlatformEnum> CHAT_PLATFORM_LIST = Lists.newArrayList( | |
| 33 | - AiPlatformEnum.YI_YAN, | |
| 34 | - AiPlatformEnum.QIAN_WEN, | |
| 35 | - AiPlatformEnum.XING_HUO, | |
| 36 | - AiPlatformEnum.OPENAI | |
| 37 | - ); | |
| 38 | - | |
| 39 | - public static List<AiPlatformEnum> IMAGE_PLATFORM_LIST = Lists.newArrayList( | |
| 40 | - AiPlatformEnum.OPEN_AI_DALL, | |
| 41 | - AiPlatformEnum.MIDJOURNEY | |
| 42 | - ); | |
| 28 | + private final String platform; | |
| 29 | + private final String name; | |
| 30 | + | |
| 31 | +// public static List<AiPlatformEnum> CHAT_PLATFORM_LIST = Lists.newArrayList( | |
| 32 | +// AiPlatformEnum.YI_YAN, | |
| 33 | +// AiPlatformEnum.QIAN_WEN, | |
| 34 | +// AiPlatformEnum.XING_HUO, | |
| 35 | +// AiPlatformEnum.OPENAI | |
| 36 | +// ); | |
| 37 | +// | |
| 38 | +// public static List<AiPlatformEnum> IMAGE_PLATFORM_LIST = Lists.newArrayList( | |
| 39 | +// AiPlatformEnum.OPEN_AI_DALL, | |
| 40 | +// AiPlatformEnum.MIDJOURNEY | |
| 41 | +// ); | |
| 43 | 42 | |
| 44 | 43 | public static AiPlatformEnum validatePlatform(String platform) { |
| 45 | 44 | for (AiPlatformEnum platformEnum : AiPlatformEnum.values()) { | ... | ... |
yudao-server/src/main/resources/application.yaml
| ... | ... | @@ -142,6 +142,14 @@ spring: |
| 142 | 142 | listener: |
| 143 | 143 | missing-topics-fatal: false # 消费监听接口监听的主题不存在时,默认会报错。所以通过设置为 false ,解决报错 |
| 144 | 144 | |
| 145 | +--- #################### AI 相关配置 #################### | |
| 146 | + | |
| 147 | +spring.ai: | |
| 148 | + ollama: | |
| 149 | + base-url: http://127.0.0.1:11434 | |
| 150 | + chat: | |
| 151 | + model: llama3 | |
| 152 | + | |
| 145 | 153 | --- #################### 芋道相关配置 #################### |
| 146 | 154 | |
| 147 | 155 | yudao: | ... | ... |