Spring Ai 04 解决 ChatClient 初始化冲突问题

在基于 Spring AI 开发 AI 应用时,很多开发者会遇到一个典型问题:当项目中引入多个 AI 智能体(如 Ollama、DeepSeek)依赖后,直接使用官网默认方式初始化ChatClient会因无法确定具体智能体而抛出 Bean 冲突异常。本文将详细讲解问题原因,并提供一种优雅的多智能体集成方案,让你可以灵活切换不同的 AI 服务。

一、问题背景:默认方式的局限性

Spring AI 官网提供的ChatClient使用示例如下:

java 复制代码
@RestController
class MyController {

    private final ChatClient chatClient;

    // 直接通过Builder构建,依赖默认的ChatModel
    public MyController(ChatClient.Builder chatClientBuilder) {
        this.chatClient = chatClientBuilder.build();
    }

    @GetMapping("/ai")
    String generation(String userInput) {
        return this.chatClient.prompt()
            .user(userInput)
            .call()
            .content();
    }
}

这种方式的核心问题在于:

  • ChatClient.Builder会自动匹配上下文唯一的ChatModel实现类
  • 当 pom.xml 中引入多个智能体依赖(如 ollama-spring-boot-starter、deepseek-spring-boot-starter)时,Spring 容器中会存在多个ChatModel Bean
  • 初始化ChatClient时无法确定具体使用哪个智能体,最终抛出NoUniqueBeanDefinitionException异常

临时解决方案:确保 pom.xml 中只保留一个智能体依赖,但这会丧失多智能体灵活切换的能力。

二、最优解决方案:自定义多 ChatClient Bean

步骤 1:配置多智能体 ChatClient Bean

创建配置类,为每个智能体单独声明ChatClient Bean,明确绑定对应的ChatModel

java 复制代码
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.ollama.OllamaChatModel;
import org.springframework.ai.deepseek.DeepSeekChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ChatConfiguration {

    /**
     * 声明Ollama智能体的ChatClient Bean
     * @param ollamaChatModel 自动注入的OllamaChatModel(由Spring AI自动配置)
     * @return Ollama专属ChatClient
     */
    @Bean
    public ChatClient ollamaChatClient(OllamaChatModel ollamaChatModel) {
        return ChatClient.builder(ollamaChatModel).build();
    }

    /**
     * 声明DeepSeek智能体的ChatClient Bean
     * @param deepSeekChatModel 自动注入的DeepSeekChatModel(由Spring AI自动配置)
     * @return DeepSeek专属ChatClient
     */
    @Bean
    public ChatClient deepseekChatClient(DeepSeekChatModel deepSeekChatModel) {
        return ChatClient.builder(deepSeekChatModel).build();
    }
}

步骤 2:在 Controller 中按需注入使用

通过@Resource注解按名称注入不同的ChatClient,实现多智能体的灵活调用:

java 复制代码
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import reactor.core.publisher.Flux;

@RestController
@RequestMapping("/chatClient")
@Slf4j
public class ChatClientController {

    // 注入DeepSeek专属ChatClient
    @Resource
    private ChatClient deepseekChatClient;

    // 注入Ollama专属ChatClient
    @Resource
    private ChatClient ollamaChatClient;

    /**
     * 调用Ollama智能体实现流式响应
     * @param userInput 用户输入的问题
     * @return 流式返回的AI回答
     */
    @GetMapping(value = "/chat/ollama", produces = MediaType.TEXT_EVENT_STREAM_VALUE + ";charset=utf-8")
    public Flux<String> streamOllama(String userInput) {
        log.info("Ollama接收用户输入:{}", userInput);
        return ollamaChatClient.prompt()
                .user(userInput)
                .stream() // 流式调用
                .content(); // 提取响应内容
    }

    /**
     * 调用DeepSeek智能体实现流式响应
     * @param userInput 用户输入的问题
     * @return 流式返回的AI回答
     */
    @GetMapping(value = "/chat/deepseek", produces = MediaType.TEXT_EVENT_STREAM_VALUE + ";charset=utf-8")
    public Flux<String> streamDeepSeek(String userInput) {
        log.info("DeepSeek接收用户输入:{}", userInput);
        return deepseekChatClient.prompt()
                .user(userInput)
                .stream()
                .content();
    }

    /**
     * 非流式调用示例(同步返回)
     */
    @GetMapping("/chat/ollama/sync")
    public String syncOllama(String userInput) {
        return ollamaChatClient.prompt()
                .user(userInput)
                .call() // 同步调用
                .content();
    }
}

步骤 3:配置文件说明

在 application.yml/application.properties 中配置各智能体的连接信息:

XML 复制代码
spring:
  ai:
    # Ollama配置
    ollama:
      base-url: http://localhost:11434
      model: gemma3:4b
    # DeepSeek配置
    deepseek:
      api-key: your-deepseek-api-key
      model: deepseek-chat

三、核心优势

  1. 解耦性 :每个智能体对应独立的ChatClient,避免 Bean 冲突
  2. 灵活性:可根据业务场景(如成本、响应速度、能力)灵活选择智能体
  3. 扩展性 :新增智能体时,只需在配置类中新增对应的ChatClient Bean 即可
  4. 兼容性:完全兼容 Spring AI 的原生 API,无需修改业务逻辑

四、注意事项

  1. 确保各智能体的依赖已正确引入 pom.xml,例如:
XML 复制代码
<!-- Ollama依赖 -->
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-ollama-spring-boot-starter</artifactId>
</dependency>
<!-- DeepSeek依赖 -->
<dependency>
    <groupId>org.springframework.ai</groupId>
    <artifactId>spring-ai-deepseek-spring-boot-starter</artifactId>
</dependency>
  1. 各智能体的配置参数需正确填写,否则会导致连接失败
  2. 流式响应需指定produces = "text/stream;charset=utf-8",避免中文乱码

总结

  1. Spring AI 默认方式初始化ChatClient仅适用于单智能体场景,多智能体下会因 Bean 冲突报错;
  2. 通过自定义配置类为每个智能体声明独立的ChatClient Bean,可彻底解决多智能体集成冲突问题;
  3. 在 Controller 中按名称注入不同的ChatClient,可实现多智能体的灵活调用与切换。
相关推荐
y = xⁿ1 小时前
【LeetCodehot100】T114:二叉树展开为链表 T105:从前序与中序遍历构造二叉树
java·算法·链表
SuniaWang1 小时前
《Spring AI + 大模型全栈实战》学习手册系列 · 专题八:《RAG 系统安全与权限管理:企业级数据保护方案》
java·前端·人工智能·spring boot·后端·spring·架构
xiaohe072 小时前
Maven Spring框架依赖包
java·spring·maven
hssfscv2 小时前
软件设计师下午题二 E-R图
java·笔记·学习
2301_805962932 小时前
ESP32远程OTA升级:从局域网到公网部署
网络·后端·http·esp32
cyforkk2 小时前
Spring Boot 3 集成 Swagger 踩坑实录:解决 doc.html 404 与 Unauthorized 拦截
spring boot·后端·html
十七号程序猿2 小时前
Java图书管理系统 | 无需配置任何环境,双击一键启动,开箱即用
java·spring boot·vue·毕业设计·毕设·源代码管理
宝耶3 小时前
Java面试2:final、finally、finalize 的区别?
java·开发语言·面试
小码哥_常3 小时前
当@RequestBody遇上Request:数据去哪儿了?
后端