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

相关推荐

  • 最新IDEA破解与永久IDEA激活码组合教程

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

    IDEA破解教程 2025 年 11 月 30 日
    10500
  • WebStorm破解对性能有影响吗?系统资源实测对比!

    声明:下文提到的 WebStorm 破解补丁与激活码均来自互联网公开分享,仅限个人学习研究,禁止商业用途。若遇侵权,请邮件联系删除。条件允许请直接购买官方授权,支持正版! WebStorm 是 JetBrains 家族中备受好评的 JavaScript IDE,跨 Windows、macOS、Linux 三大平台通用。下面手把手教你用「永久补丁」解锁全部高级…

    2025 年 9 月 9 日
    16100
  • WebStorm激活过程中是否需要联网?支持离线使用吗?

    免责声明:以下教程中的 WebStorm 破解补丁与激活码均源自互联网公开渠道,仅供个人学习研究,禁止商业用途。若条件允许,请支持正版!官方正版低至 32 元/年:https://panghu.hicxy.com/shop/?id=18 先放一张成果图镇楼:WebStorm 2025.2.1 已成功激活到 2099 年,爽到飞起! 下面用图文方式带你一步步搞…

    2025 年 9 月 22 日
    17400
  • 无标题文章

    免责声明:本教程提供的IntelliJ IDEA破解方法及激活码均来源于网络资源,仅供个人学习交流使用,请勿用于商业用途。如有侵犯版权,请联系删除。建议有条件用户支持正版软件! IntelliJ IDEA作为JetBrains公司开发的旗舰级集成开发环境,凭借其强大的代码编辑功能和智能辅助特性,在Windows、Mac和Linux三大平台上广受开发者青睐。本…

    2026 年 1 月 16 日
    8500
  • WebStorm破解版和正版区别大吗?附使用体验对比!

    免责声明:以下补丁与激活码均来自互联网公开渠道,仅供个人学习研究,禁止商业用途。若条件允许,请支持正版!官方正版低至 32 元/年,全家桶一键登录:https://panghu.hicxy.com/shop/?id=18 先放张图镇楼——WebStorm 2025.2.1 已顺利解锁到 2099 年,爽翻! 下面用图文带你一步步搞定最新版 WebStorm …

    2025 年 9 月 22 日
    32800

发表回复

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

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信