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);
    }
相关推荐
阿里云大数据AI技术15 小时前
从算力到智能体,面向 Agentic AI 的基础设施演进
人工智能·agent
hangyuekejiGEO15 小时前
GEO技术服务选型指南
大数据·人工智能·python
阿里云大数据AI技术16 小时前
EMR Serverless Spark AI Function 的双维降本实践
人工智能·sql·spark
软萌萌的116 小时前
Java Spring Boot 修改yml配置&加载顺序规则
java·spring boot·python
维基框架16 小时前
GitHub源码处理提速 一趟扫描反而更慢
人工智能·github
冬奇Lab16 小时前
代码库知识库系列(05):向量检索 vs 知识图谱——加了调用图并没有变更好
人工智能
AKAMAI16 小时前
你的源服务器可能是你做出的最昂贵决定
运维·人工智能·云计算
冬奇Lab16 小时前
【无标题】
人工智能·开源
专业贴准17 小时前
苏州SMT供料器设备选型分析:本地主流自动化企业技术特点与行业趋势
人工智能·自动化·制造