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_小站4 小时前
6个GitHub爆火的免费大模型教程,助你快速进阶AI编程
人工智能·langchain·github·知识图谱·agent·llama·rag
xindoo4 小时前
GitHub Trending霸榜!深度解析AI Coding辅助神器 Superpowers
人工智能·github
时间之里4 小时前
【深度学习】:RF-DETR与yolo对比
人工智能·深度学习·yolo
北京阿法龙科技有限公司5 小时前
数智化升级:AR 智能眼镜驱动工业运维效能革新
人工智能
风落无尘5 小时前
《智能重生:从垃圾堆到AI工程师》——第二章 概率与生存
大数据·人工智能
j_xxx404_5 小时前
Linux:静态链接与动态链接深度解析
linux·运维·服务器·c++·人工智能
收获不止数据库5 小时前
达梦9发布会归来:AI 时代,我们需要一款什么样的数据库?
数据库·人工智能·ai·语言模型·数据分析
hhb_6185 小时前
AI全栈编程生存指南
人工智能
AI-Frontiers5 小时前
transformer进阶之路:#2 工作原理详解
人工智能·深度学习·transformer
xskukuku5 小时前
VSCode中的Codex插件如何调用第三方API
vscode·ai·codex