Spring-Ai-Alibaba [03] multiple-llm-client-demo概述开发环境项目结构pom.xml 文件config 配置类控制层启动类验证千问模型DeepSeek 模型概述本文是 Spring AI Alibaba 框架学习系列第三篇介绍 项目中如何使用多个模型当前 Demo 中同时使用千问、DeepSeek两个模型。代码上传至 Giteehttps://gitee.com/xbjct/spring-ai-alibaba-demo开发环境基础框架: Spring Boot 3.5.14AI 框架: Spring AI 1.1.2 Spring AI Alibaba 1.1.2.2大模型: 阿里云通义千问 (qwen-plus)构建工具: Maven 3.9.11JDK 版本: 21.0.10项目结构pom.xml 文件projectxmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdcom.junjiu.spring.ai.alibaba.demo/groupIdartifactIdSpring-AI-Alibaba-Demo/artifactIdversion1.0-SNAPSHOT/version/parentartifactId03-multiple-llm-client-demo/artifactIdpackagingjar/packagingdescription实现多个 LLM 模型并使用不同的模型进行对话/descriptionname03-multiple-llm-client-demo/nameurlhttp://maven.apache.org/urlpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/properties/projectconfig 配置类packagecom.junjiu.spring.ai.alibaba.demo.config;importcom.alibaba.cloud.ai.dashscope.api.DashScopeApi;importcom.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;importcom.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;importorg.springframework.ai.chat.client.ChatClient;importorg.springframework.ai.chat.model.ChatModel;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;/** * program: Spring-AI-Alibaba-Demo * ClassName: LLMConfig * description: * * author: 君九 * create: 2026-05-22 22:02 * version: 1.0 **/ConfigurationpublicclassLLMConfig{privatefinalStringDEEP_SEEK_MODELdeepseek-v4-pro;privatefinalStringQWEN_MODELqwen3.7-max;Value(${spring.ai.dashscope.base-url})privateStringbaseUrl;Value(${spring.ai.dashscope.api-key})privateStringapiKey;/** * 创建 ChatModel 模型对象并注入到 Spring 容器中。 * return */BeanpublicChatModelqwenChatModel(){returnDashScopeChatModel.builder().dashScopeApi(DashScopeApi.builder().baseUrl(baseUrl).apiKey(apiKey).build()).defaultOptions(DashScopeChatOptions.builder().model(QWEN_MODEL).build()).build();}Bean(nameqwenChatClient)publicChatClientqwenChatClient(){returnChatClient.create(qwenChatModel());}/** * 创建 ChatModel 模型对象并注入到 Spring 容器中。 * return */BeanpublicChatModeldeepSeekChatModel(){returnDashScopeChatModel.builder().dashScopeApi(DashScopeApi.builder().baseUrl(baseUrl).apiKey(apiKey).build()).defaultOptions(DashScopeChatOptions.builder().model(DEEP_SEEK_MODEL).build()).build();}Bean(namedeepSeekChatClient)publicChatClientdeepSeekChatClient(){returnChatClient.create(deepSeekChatModel());}}控制层package com.junjiu.spring.ai.alibaba.demo.controller; import jakarta.annotation.Resource; import org.springframework.ai.chat.client.ChatClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * program: Spring-AI-Alibaba-Demo * ClassName: HelloController * description: * * author: 君九 * create: 2026-05-22 22:13 * version: 1.0 **/ RestController RequestMapping(/hello) public class HelloController { Resource(name qwenChatClient) private ChatClient qwenChatClient; Resource(name deepSeekChatClient) private ChatClient deepSeekChatClient; /** * 调用 qwen 模型 * param message * return */ GetMapping(/qwen) public String qwen(RequestParam(name message, defaultValue 你是谁) String message) { return qwenChatClient.prompt() .user(message) .call() .content(); } /** * 调用 deepseek 模型 * param message * return */ GetMapping(/deepSeek) public String deepSeek(RequestParam(name message, defaultValue 你是谁) String message) { return deepSeekChatClient.prompt() .user(message) .call() .content(); } }启动类packagecom.junjiu.spring.ai.alibaba.demo;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.boot.context.event.ApplicationReadyEvent;importorg.springframework.context.ApplicationListener;importorg.springframework.core.env.Environment;/** * program: Spring-AI-Alibaba-Demo * ClassName: MultipleLLMApplication * description: * * author: 君九 * create: 2026-05-22 21:29 * version: 1.0 **/SpringBootApplicationpublicclassMultipleLLMApplication{publicstaticvoidmain(String[]args){SpringApplication.run(MultipleLLMApplication.class,args);}publicApplicationListenerApplicationReadyEventreadyEventApplicationListener(Environmentenv){returnevent-{System.out.println(\n);System.out.println(✅ Application is ready!);System.out.println(AIALI_API_KEYSystem.getenv(AIALI_API_KEY));System.out.println(\n);};}}验证打开浏览器访问千问模型http://localhost:5826/hello/qwenDeepSeek 模型http://localhost:5826/hello/deepSeek代码上传至 Giteehttps://gitee.com/xbjct/spring-ai-alibaba-demo若有转载请标明出处https://blog.csdn.net/CharlesYuangc/article/details/161324362
Spring-Ai-Alibaba [03] multiple-llm-client-demo
Spring-Ai-Alibaba [03] multiple-llm-client-demo概述开发环境项目结构pom.xml 文件config 配置类控制层启动类验证千问模型DeepSeek 模型概述本文是 Spring AI Alibaba 框架学习系列第三篇介绍 项目中如何使用多个模型当前 Demo 中同时使用千问、DeepSeek两个模型。代码上传至 Giteehttps://gitee.com/xbjct/spring-ai-alibaba-demo开发环境基础框架: Spring Boot 3.5.14AI 框架: Spring AI 1.1.2 Spring AI Alibaba 1.1.2.2大模型: 阿里云通义千问 (qwen-plus)构建工具: Maven 3.9.11JDK 版本: 21.0.10项目结构pom.xml 文件projectxmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentgroupIdcom.junjiu.spring.ai.alibaba.demo/groupIdartifactIdSpring-AI-Alibaba-Demo/artifactIdversion1.0-SNAPSHOT/version/parentartifactId03-multiple-llm-client-demo/artifactIdpackagingjar/packagingdescription实现多个 LLM 模型并使用不同的模型进行对话/descriptionname03-multiple-llm-client-demo/nameurlhttp://maven.apache.org/urlpropertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncoding/properties/projectconfig 配置类packagecom.junjiu.spring.ai.alibaba.demo.config;importcom.alibaba.cloud.ai.dashscope.api.DashScopeApi;importcom.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;importcom.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;importorg.springframework.ai.chat.client.ChatClient;importorg.springframework.ai.chat.model.ChatModel;importorg.springframework.beans.factory.annotation.Value;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;/** * program: Spring-AI-Alibaba-Demo * ClassName: LLMConfig * description: * * author: 君九 * create: 2026-05-22 22:02 * version: 1.0 **/ConfigurationpublicclassLLMConfig{privatefinalStringDEEP_SEEK_MODELdeepseek-v4-pro;privatefinalStringQWEN_MODELqwen3.7-max;Value(${spring.ai.dashscope.base-url})privateStringbaseUrl;Value(${spring.ai.dashscope.api-key})privateStringapiKey;/** * 创建 ChatModel 模型对象并注入到 Spring 容器中。 * return */BeanpublicChatModelqwenChatModel(){returnDashScopeChatModel.builder().dashScopeApi(DashScopeApi.builder().baseUrl(baseUrl).apiKey(apiKey).build()).defaultOptions(DashScopeChatOptions.builder().model(QWEN_MODEL).build()).build();}Bean(nameqwenChatClient)publicChatClientqwenChatClient(){returnChatClient.create(qwenChatModel());}/** * 创建 ChatModel 模型对象并注入到 Spring 容器中。 * return */BeanpublicChatModeldeepSeekChatModel(){returnDashScopeChatModel.builder().dashScopeApi(DashScopeApi.builder().baseUrl(baseUrl).apiKey(apiKey).build()).defaultOptions(DashScopeChatOptions.builder().model(DEEP_SEEK_MODEL).build()).build();}Bean(namedeepSeekChatClient)publicChatClientdeepSeekChatClient(){returnChatClient.create(deepSeekChatModel());}}控制层package com.junjiu.spring.ai.alibaba.demo.controller; import jakarta.annotation.Resource; import org.springframework.ai.chat.client.ChatClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; /** * program: Spring-AI-Alibaba-Demo * ClassName: HelloController * description: * * author: 君九 * create: 2026-05-22 22:13 * version: 1.0 **/ RestController RequestMapping(/hello) public class HelloController { Resource(name qwenChatClient) private ChatClient qwenChatClient; Resource(name deepSeekChatClient) private ChatClient deepSeekChatClient; /** * 调用 qwen 模型 * param message * return */ GetMapping(/qwen) public String qwen(RequestParam(name message, defaultValue 你是谁) String message) { return qwenChatClient.prompt() .user(message) .call() .content(); } /** * 调用 deepseek 模型 * param message * return */ GetMapping(/deepSeek) public String deepSeek(RequestParam(name message, defaultValue 你是谁) String message) { return deepSeekChatClient.prompt() .user(message) .call() .content(); } }启动类packagecom.junjiu.spring.ai.alibaba.demo;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.boot.context.event.ApplicationReadyEvent;importorg.springframework.context.ApplicationListener;importorg.springframework.core.env.Environment;/** * program: Spring-AI-Alibaba-Demo * ClassName: MultipleLLMApplication * description: * * author: 君九 * create: 2026-05-22 21:29 * version: 1.0 **/SpringBootApplicationpublicclassMultipleLLMApplication{publicstaticvoidmain(String[]args){SpringApplication.run(MultipleLLMApplication.class,args);}publicApplicationListenerApplicationReadyEventreadyEventApplicationListener(Environmentenv){returnevent-{System.out.println(\n);System.out.println(✅ Application is ready!);System.out.println(AIALI_API_KEYSystem.getenv(AIALI_API_KEY));System.out.println(\n);};}}验证打开浏览器访问千问模型http://localhost:5826/hello/qwenDeepSeek 模型http://localhost:5826/hello/deepSeek代码上传至 Giteehttps://gitee.com/xbjct/spring-ai-alibaba-demo若有转载请标明出处https://blog.csdn.net/CharlesYuangc/article/details/161324362