借助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 日

相关推荐

  • 🚀 2025年最新IDEA永久破解教程 | 附最新激活码&破解补丁(支持Win/Mac/Linux)

    作为一名Java开发者,IntelliJ IDEA无疑是你的得力助手💪。这款功能强大的IDE拥有丰富的插件生态,但高昂的费用让不少开发者望而却步。今天,我将分享一套完整的IDEA永久激活方案,让你畅用到2099年!✨ 🔍 准备工作 ⚠️ 重要提示:如果你之前安装过非官方版本或尝试过其他破解方法,建议先彻底卸载干净!本教程适用于Windows/Mac/Linu…

    IDEA破解教程 2025 年 7 月 2 日
    1.6K00
  • 最新IDEA破解补丁+永久IDEA激活码说明

    本教程适用于 IDEA、PyCharm、DataGrip、Goland 等 JetBrains 全系列 IDE,无需区分版本与操作系统! 先放张图镇楼——最新版 IDEA 已成功激活到 2099 年,稳! 下面用图文手把手带你完成激活,老版本同样适用,Win / Mac / Linux 全平台打包整理,一步到位。 1. 下载并安装 IDEA 若已安装,可直接…

    IDEA破解教程 2025 年 11 月 30 日
    26500
  • 最新IDEA破解一键激活永久IDEA激活码

    免责声明:以下补丁与激活码均搜集自公开网络,仅供学习研究,禁止商业用途。若条件允许,请支持正版! IntelliJ IDEA 是 JetBrains 出品的跨平台 IDE,支持 Windows、macOS 与 Linux。下文将手把手教你利用破解补丁实现永久激活,解锁全部高级特性。 无论你的版本号或系统环境如何,本文都已为你梳理好完整流程。 激活成功预览 补…

    IDEA破解教程 2025 年 11 月 30 日
    49800
  • 无需邀请码一键免费申领最新版webstorm激活码,零基础破解教程

    声明:以下激活补丁、序列号均搜集自互联网,仅供个人学习研究,禁止商业用途;若条件允许,请支持正版!官方正版低至 32 元/年,全家桶一键登录:https://panghu.hicxy.com/shop/?id=18 先放张图镇楼:WebStorm 2025.2.1 已顺利激活到 2099 年,稳得一批! 下面用图文手把手教你搞定最新版 WebStorm 的“…

    2025 年 11 月 26 日
    21300
  • 永久免费申请最新版最新idea激活码和破解教程

    本文面向 IntelliJ IDEA、PyCharm、DataGrip、GoLand 等 JetBrains 全系产品,实测 2024.3.5 版本可一键破解到 2099 年,老版本同样适用! 先放成果图:IDEA 已顺利激活至 2099 年,稳! 下面用图文手把手演示,零门槛完成永久激活,Windows / macOS / Linux 全系统通用,步骤我已…

    IDEA破解教程 2025 年 11 月 7 日
    1.0K00

发表回复

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

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信