借助SpringAI在Spring Boot中整合通义大模型

借助SpringAI在Spring Boot里整合通义大模型

前提条件

  1. 开发环境
    • JDK 11或更高版本
    • Maven或者Gradle(本文以Maven为例)
    • Spring Boot项目(建议使用3.x版本)
    • 通义大模型的API Key(需从阿里云获取)
  2. 获取通义大模型API Key
    阿里云百炼大模型官网: 通义大模型_企业拥抱AI时代首选-阿里云
    具体步骤可参考这篇文章:
    如何获取通义千问API Key密钥(分步指南) - 幂简集成

第一步:创建SpringBoot项目并配置pom.xml

想必各位大佬都能完成创建项目这一步(手动dog)。下面是pom.xml文件的配置内容

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

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

    <!-- Spring AI Alibaba(通义大模型支持) -->
    <dependency>
        <groupId>com.alibaba.cloud.ai</groupId>
        <artifactId>spring-ai-alibaba-starter</artifactId>
        <version>1.0.0-M6.1</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.ai</groupId>
        <artifactId>spring-ai-core</artifactId>
        <version>1.0.0-M6</version>
    </dependency>

    <dependency>
        <groupId>com.alibaba.cloud.ai</groupId>
        <artifactId>spring-ai-alibaba-autoconfigure</artifactId>
        <version>1.0.0-M6.1</version>
    </dependency>
</dependencies>

<!-- 添加 Spring Snapshot 仓库(若使用快照版本) -->
<repositories>
    <repository>
        <id>spring-snapshots</id>
        <url>https://repo.spring.io/snapshot</url>
        <snapshots>
            <enabled>true</enabled>
        </snapshots>
    </repository>
</repositories>

特别注意:通义大模型的依赖可前往 Maven Central 网站获取最新版本

借助SpringAI在Spring Boot中整合通义大模型


第二步:配置通义大模型

application.yml 文件中进行如下配置

spring:
  ai:
    dashscope:
      api-key: your api-key
      chat:
        model: qwq-32b  # 这里使用的是通义千问-QwQ-32B

第三步:编写调用通义大模型的代码

直接在controller文件中编写如下代码

package com.ai.controller;

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class AIController {
    private final ChatClient chatClient;

    public AIController(ChatClient.Builder chatClientBuilder) {
        this.chatClient = chatClientBuilder.build();
    }

    @GetMapping("/chat")
    public String chat(@RequestParam("message") String message) {
        return chatClient.prompt()
                .user(message)
                .call()
                .content();
    }
}

第四步:运行与测试

测试方法:
1. curl

curl "http://localhost:8080/ai/chat?message=你好,请介绍你自己?"
2. Postman
localhost:8080/chat?message=介绍一下你自己

测试结果:

借助SpringAI在Spring Boot中整合通义大模型


高级功能:

1、提示模板

@GetMapping("/template")
public String templateChat(@RequestParam("topic") String topic) {
    PromptTemplate template = new PromptTemplate("请用简洁的语言解释 {topic}");
    return chatClient.prompt()
            .user(template.render(Map.of("topic", topic)))
            .call()
            .content();
}

测试结果:

借助SpringAI在Spring Boot中整合通义大模型

2、流式响应

@GetMapping("/stream")
public Flux<String> streamChat(@RequestParam String message) {
    return chatClient.prompt()
            .user(message)
            .stream()
            .content();
}

测试结果:

借助SpringAI在Spring Boot中整合通义大模型

3、对话记忆

首先在config中进行如下配置

@Configuration
public class ChatConfig {
    @Bean
    public ChatMemory chatMemory() {
        return new InMemoryChatMemory();
    }
}

然后在controller中(若为新开的class则忽略注释部分)

// 注入 ChatClient.Builder 和 ChatMemory
public AIController(ChatClient.Builder chatClientBuilder, ChatMemory chatMemory) {
    this.chatClient = chatClientBuilder
            .defaultAdvisors(new PromptChatMemoryAdvisor(chatMemory)) // 添加 ChatMemoryAdvisor
            .build();
}

@GetMapping("/ai/chat")
public String chatAi(@RequestParam String message) {
    return chatClient.prompt()
            .user(message)
            .call()
            .content();
}

测试结果:
localhost:8080/ai/chat?message=你好,我是小明

借助SpringAI在Spring Boot中整合通义大模型

localhost:8080/ai/chat?message=你还记得我吗

借助SpringAI在Spring Boot中整合通义大模型


有兴趣的同学还可以构建前端页面让聊天界面更美观。

若有错误,欢迎大佬指正!!!

文章整理自互联网,只做测试使用。发布者:Lomu,转转请注明出处:https://www.it1024doc.com/12930.html

(0)
LomuLomu
上一篇 2025 年 7 月 21 日
下一篇 2025 年 7 月 21 日

相关推荐

  • 永久免费领取clion激活码,2025新编clion破解教程

    免责声明:下文所述的 JetBrains CLion 破解补丁、激活码均源自互联网公开分享,仅供个人学习与研究,禁止商业用途。若条件允许,请支持正版! 先放一张“战果图”:CLion 2025.2.1 已成功激活至 2099 年,爽到飞起! 下面用图文方式手把手演示最新版 CLion 的完整激活流程。 前期准备 ⚠️ 如果你之前尝试过其他补丁失败,建议彻底卸…

    2025 年 11 月 14 日
    25300
  • PyCharm激活码2026年Windows 11系统使用教程

    本指南适用于IDEA、PyCharm、DataGrip、Goland等所有Jetbrains系列产品! 直接来看效果,下图展示了最新版PyCharm成功激活至2099年的界面,过程顺利! 接下来,我将通过详细的图文步骤,为你完整演示如何将PyCharm激活到2099年。 此方法不仅适用于当前版本,对以往的旧版本同样有效! 无论你的操作系统是Windows、M…

    PyCharm激活码 2026 年 5 月 27 日
    6800
  • 最新pycharm破解与永久pycharm激活码合并教程

    本教程适用于IDEA、PyCharm、DataGrip、Goland等,支持Jetbrains全家桶! 废话不多说,先上最新PyCharm版本破解成功的截图,如下,可以看到已经成功破解到 2099 年辣,舒服! 接下来,我就将通过图文的方式, 来详细讲解如何激活 PyCharm至 2099 年。 当然这个激活方法,同样适用于之前的旧版本! 无论你是Windo…

    PyCharm激活码 2026 年 1 月 23 日
    20900
  • 永久激活idea 2026年,激活码分享

    IntelliJ IDEA破解教程:永久激活补丁与激活码下载(2025最新版) 重要提示:本文所涉及的IntelliJ IDEA破解补丁及激活码资源均来源于网络收集,仅限个人学习研究使用,严禁任何商业用途。若资源存在侵权问题,请联系本人删除。经济条件允许的情况下,强烈建议支持正版软件! 作为JetBrains旗下广受欢迎的开发工具,IntelliJ IDEA…

    IDEA破解教程 2026 年 2 月 26 日
    72600
  • datagrip激活码自动更新脚本+破解演示

    本教程同样适用于 IDEA、PyCharm、DataGrip、Goland 等 JetBrains 全家桶,无需区分版本! 话不多说,先放一张“战果”——已成功把 DataGrip 激活到 2099 年,直接爽到下个世纪! 下面我会用图文结合的方式,手把手教你把 DataGrip 一口气激活到 2099 年。注意:该方法向下兼容旧版本,无论 Windows、…

    DataGrip激活码 2025 年 11 月 6 日
    29600

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信