001 Spring AI Alibaba框架整合百炼大模型平台 — 快速入门

Spring AI Alibaba框架整合百炼大模型平台

文章目录

版本springboot 3.5.x, jdk17, springaiAlibaba1.1.2.2

快速入门

1.pom依赖文件

xml 复制代码
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.5.0</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud.ai</groupId>
                <artifactId>spring-ai-alibaba-bom</artifactId>
                <version>1.1.2.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.ai</groupId>
                <artifactId>spring-ai-bom</artifactId>
                <version>1.1.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.alibaba.cloud.ai</groupId>
                <artifactId>spring-ai-alibaba-extensions-bom</artifactId>
                <version>1.1.2.2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Spring Web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Spring AI Alibaba DashScope Starter(阿里云百炼平台适配) -->
        <dependency>
            <groupId>com.alibaba.cloud.ai</groupId>
            <artifactId>spring-ai-alibaba-starter-dashscope</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud.ai</groupId>
            <artifactId>spring-ai-alibaba-dashscope</artifactId>
        </dependency>


        <dependency>
            <groupId>com.alibaba.cloud.ai</groupId>
            <artifactId>spring-ai-alibaba-agent-framework</artifactId>
        </dependency>


        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>      <!-- 必须为 provided,否则可能打包冲突 -->
        </dependency>



    </dependencies>

yml配置

yml 复制代码
spring:
  ai:
    # 禁用默认的 Chat Client(如果需要同时使用多个模型,需禁用自动创建的bean改为手动自己去创建)
    chat:
      client:
        enabled: false
    dashscope:
      api-key: ${AI_DASHSCOPE_API_KEY}
      # 聊天模型
      chat:
        enabled: true
        options:
          # 指定使用的模型型号(如 qwen-turbo、qwen-plus、qwen-max 等)
          model: qwen-plus-2025-07-28
配置Chat聊天模型
java 复制代码
/**
 * ChatClient 配置类
 *
 * ChatClient 是 Spring AI 推荐的现代调用方式,支持链式编程和插件机制。
 * 相比直接使用 ChatModel,ChatClient 提供了更丰富的功能:
 * - 链式调用 API
 * - 默认系统提示词
 * - Advisor 插件机制(日志、记忆、RAG 等)
 */
@Configuration
public class ChatClientConfig {

    /**
     * 创建 ChatClient Bean
     *
     * @param chatModel 自动注入的 ChatModel(由 DashScope Starter 提供)
     * @return 配置好的 ChatClient 实例
     */
    @Bean
    public ChatClient chatClient(DashScopeChatModel chatModel) {
        return ChatClient.builder(chatModel)
                // 设置默认系统提示词,定义了 AI 的基础人设和行为规范
                .defaultSystem("你是一个专业、友好的AI助手,请用中文回答问题。")
                // 添加日志插件,自动打印每次请求和响应的详细信息,方便调试
                // SimpleLoggerAdvisor 是 Spring AI 内置的日志 Advisor
                .defaultAdvisors(
                        new SimpleLoggerAdvisor()
                        //自定义插件
                        //,new MyLoggerAdvisor()
                )
                // 设置默认选项(可选)
                // 【设置默认参数】
                .defaultOptions(ChatOptions.builder()
                        .temperature(0.7) // 温度:控制回答的随机性。0=确定,1=创造性高
                        .topP(0.9)        // 核采样:控制词汇选择的多样性
                        .build())
                .build();
    }



    /**
     * 创建 ChatClient.Builder Bean,供需要动态构建 ChatClient 的场景使用
     * (例如多轮对话中需要为每个会话创建独立的 ChatClient)
     */
    @Bean
    public ChatClient.Builder chatClientBuilder(DashScopeChatModel chatModel) {
        return ChatClient.builder(chatModel);
    }
    
}
测试
java 复制代码
    @Test
    public void chatTest() {
        String content = "讲一个笑话";
        //String response2 = chatClient.prompt(content).call().content();
        String response = chatClient
                .prompt()            // 1. 开始构建提示词请求
                .user(content)       // 2. 设置用户输入的问题
                .call()              // 3. 同步调用 AI 接口
                .content();          // 4. 获取 AI 返回的文本内容
        System.out.println("AI助手:" + response);
    }
相关推荐
希望永不加班1 天前
SpringBoot 消费者并发控制:线程池配置
java·spring boot·后端·spring
code_Bo1 天前
apple gpt 礼品卡订阅失败解决方案
前端·人工智能·后端
传感器与混合集成电路1 天前
深度解读:定向传感器技术演进路线图与未来方向
人工智能·材料工程
码农阿强1 天前
OpenCode 快速配置指南:三步完成部署与接口对接
人工智能·ai·aigc·ai编程·gpu算力
星恒随风1 天前
从零开始理解 ResNet(上):为什么 CNN 需要“残差连接”?
人工智能·笔记·神经网络·学习·cnn
月光船幽幽1 天前
四层门禁+自愈机制
人工智能·科技·动态规划·拓扑学
无忧智库1 天前
某能源集团多Agent协同的电力交易策略优化与实时调度决策系统建设方案(WORD)
大数据·人工智能·自动化
乌恩大侠1 天前
X5G:一个基于 NVIDIA ARC 与 OpenAirInterface 的开放、可编程、多厂商私有 5G O-RAN 测试平台
人工智能·5g·o-ru
学习中.........1 天前
大语言模型的采样参数与输出控制机制
人工智能·机器学习·语言模型
Agent手记1 天前
传统工厂的工单自动排程如何用AI解决?从大模型推理到端到端自动化的闭环实战
运维·人工智能·ai·自动化