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);
    }
相关推荐
Proxy_ZZ02 小时前
用Matlab绘制BER曲线对比SPA与Min-Sum性能
人工智能·算法·机器学习
黎阳之光2 小时前
黎阳之光:以视频孪生领跑全球,赋能数字孪生水利智能监测新征程
大数据·人工智能·算法·安全·数字孪生
数据知道2 小时前
claw-code 源码分析:成本追踪(Cost)与 Hook——企业落地时,计量与策略注入该挂在哪一层?
ai·claude code·claw code
宇擎智脑科技2 小时前
基于 SAM3 + FastAPI 搭建智能图像标注工具实战
人工智能·计算机视觉
F_U_N_2 小时前
效率提升80%:AI全流程研发真实项目落地复盘
人工智能·ai编程
月诸清酒2 小时前
24-260409 AI 科技日报 (Gemma 4发布一周下载破千万,开源模型生态加速演进)
人工智能·开源
2501_933329552 小时前
技术架构深度解析:Infoseek舆情监测系统的全链路设计与GEO时代的技术实践
开发语言·人工智能·分布式·架构
X journey2 小时前
机器学习进阶(16):如何防止过拟合
人工智能·机器学习