Spring-Ai-Alibaba [03] multiple-llm-client-demo

Spring-Ai-Alibaba 03 multiple-llm-client-demo

概述

本文是 Spring AI Alibaba 框架学习系列第三篇,介绍 项目中如何使用多个模型?

当前 Demo 中,同时使用 千问DeepSeek 两个模型。

代码上传至 Gitee:https://gitee.com/xbjct/spring-ai-alibaba-demo

开发环境

  • 基础框架: Spring Boot 3.5.14
  • AI 框架: Spring AI 1.1.2 + Spring AI Alibaba 1.1.2.2
  • 大模型: 阿里云通义千问 (qwen-plus)
  • 构建工具: Maven 3.9.11
  • JDK 版本: 21.0.10

项目结构

pom.xml 文件

xml 复制代码
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.junjiu.spring.ai.alibaba.demo</groupId>
        <artifactId>Spring-AI-Alibaba-Demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <artifactId>03-multiple-llm-client-demo</artifactId>
    <packaging>jar</packaging>
    <description>
        实现多个 LLM 模型,并使用不同的模型进行对话
    </description>

    <name>03-multiple-llm-client-demo</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

</project>

config 配置类

java 复制代码
package com.junjiu.spring.ai.alibaba.demo.config;

import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatModel;
import com.alibaba.cloud.ai.dashscope.chat.DashScopeChatOptions;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * program: Spring-AI-Alibaba-Demo
 * ClassName: LLMConfig
 * description:
 *
 * @author: 君九
 * @create: 2026-05-22 22:02
 * @version: 1.0
 **/
@Configuration
public class LLMConfig {

    private final String DEEP_SEEK_MODEL = "deepseek-v4-pro";
    private final String QWEN_MODEL = "qwen3.7-max";

    @Value("${spring.ai.dashscope.base-url}")
    private String baseUrl;

    @Value("${spring.ai.dashscope.api-key}")
    private String apiKey;

    /**
     * 创建 ChatModel 模型对象,并注入到 Spring 容器中。
     * @return
     */
    @Bean
    public ChatModel qwenChatModel() {
        return DashScopeChatModel.builder()
                .dashScopeApi(
                        DashScopeApi.builder()
                                .baseUrl(baseUrl)
                                .apiKey(apiKey)
                                .build()
                )
                .defaultOptions(
                        DashScopeChatOptions.builder()
                                .model(QWEN_MODEL)
                                .build()
                )
                .build();
    }
    @Bean(name = "qwenChatClient")
    public ChatClient qwenChatClient() {
        return ChatClient.create(qwenChatModel());
    }

    /**
     * 创建 ChatModel 模型对象,并注入到 Spring 容器中。
     * @return
     */
    @Bean
    public ChatModel deepSeekChatModel() {
        return DashScopeChatModel.builder()
                .dashScopeApi(
                        DashScopeApi.builder()
                                .baseUrl(baseUrl)
                                .apiKey(apiKey)
                                .build()
                )
                .defaultOptions(DashScopeChatOptions.builder().model(DEEP_SEEK_MODEL).build())
                .build();
    }
    @Bean(name = "deepSeekChatClient")
    public ChatClient deepSeekChatClient() {
        return ChatClient.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();
    }
}

启动类

java 复制代码
package com.junjiu.spring.ai.alibaba.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.Environment;

/**
 * program: Spring-AI-Alibaba-Demo
 * ClassName: MultipleLLMApplication
 * description:
 *
 * @author: 君九
 * @create: 2026-05-22 21:29
 * @version: 1.0
 **/
@SpringBootApplication
public class MultipleLLMApplication {
    public static void main(String[] args) {
        SpringApplication.run(MultipleLLMApplication.class, args);
    }

    public ApplicationListener<ApplicationReadyEvent> readyEventApplicationListener(Environment env) {
        return event -> {
            System.out.println("\n🎉========================================🎉");
            System.out.println("✅ Application is ready!");
            System.out.println("AIALI_API_KEY=" + System.getenv("AIALI_API_KEY"));
            System.out.println("🎉========================================🎉\n");
        };
    }
}

验证

打开浏览器访问:

千问模型

http://localhost:5826/hello/qwen

DeepSeek 模型

http://localhost:5826/hello/deepSeek

代码上传至 Gitee:https://gitee.com/xbjct/spring-ai-alibaba-demo

若有转载,请标明出处:https://blog.csdn.net/CharlesYuangc/article/details/161324362

相关推荐
北城以北8888几秒前
虚拟机安装JDK,Tomcat,部署项目
java·开发语言·tomcat
Eloudy几秒前
伊辛解码(Ising Decoding)
人工智能·量子计算
财经资讯数据_灵砚智能几秒前
基于全球经济类多源新闻的NLP情感分析与数据可视化(日间)2026年6月12日
人工智能·python·ai·信息可视化·自然语言处理·ai编程·灵砚智能
deephub6 分钟前
相关性与因果性:识别伪相关以提升模型在真实环境的可用性
人工智能·机器学习·数据挖掘·数据分析
终将老去的穷苦程序员8 分钟前
基于Android Studio开发的安卓图书借阅管理系统
java·sqlite·android studio·android-studio
2601_955505258 分钟前
行业研究|AI-Ready高质量数据集建设难点与元数据标准化解决方案(基于国家数据局25号文)
人工智能·金融·能源·健康医疗·制造·政务
虾壳云官方8 分钟前
【本地 AI 自动化最新工具】 OpenClaw 2.7.9 Windows 完整部署教程(包含安装包)
人工智能·windows·openclaw·openclaw安装·openclaw一键部署
ai产品老杨11 分钟前
解耦异构安防:基于 Docker 与边缘计算的 AI 视频管理平台,如何实现 GB28181/RTSP 统一接入与全源码交付
人工智能·docker·边缘计算
趋之13 分钟前
千问大模型核心能力与实战效果全景展示
人工智能
zhangfeng113314 分钟前
ONNX Runtime 微软的推理引擎 TensorRT,NVIDIA GPU 上的深度学习推理, CUDA Graph
人工智能·深度学习·microsoft