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

相关推荐

  • Claude Pro充值订阅开通完整教程

    Claude Pro充值订阅开通完整教程,适合国内用户使用微信支付宝为自己的账号完成充值、订阅或代充开通。

    未分类 2天前
    900
  • 亲测有效webstorm激活码资源,图解webstorm破解教程

    申明:本教程 WebStorm破解补丁、激活码均收集于网络,请勿商用,仅供个人学习使用,如有侵权,请联系作者删除。若条件允许,希望大家购买正版 ! 废话不多说,先上 WebStorm 2025.2.1 版本破解成功的截图,如下图,可以看到已经成功破解到 2099 年辣,舒服的很! 接下来就给大家通过图文的方式分享一下如何破解最新的WebStorm。 如果觉得…

    2025 年 10 月 21 日
    28400
  • 全站同步2025goland激活码免费申领,独家破解教程

    声明:以下教程中的 GoLand 破解补丁与激活码均源自网络公开渠道,仅供个人学习研究,禁止商业用途。若遇侵权,请联系删除。条件允许请支持正版! 先放一张 2025.2.1 版 GoLand 成功激活到 2099 年的截图,爽到飞起! 下面用图文一步步带你搞定最新版 GoLand 的激活。 前期准备 ⚠️ 如果你之前装过其他补丁没成功,建议先卸载干净,再重新…

    2025 年 11 月 26 日
    29500
  • 自动发放官方最新idea激活码和图文破解教程

    声明:以下破解补丁与激活码均搜集自互联网,仅供个人学习研究,禁止商业用途。若条件允许,请支持正版 JetBrains! IntelliJ IDEA 是 JetBrains 家族的明星 IDE,跨 Windows、macOS、Linux 三大平台,功能全面。本文手把手教你用破解补丁实现“永久授权”,解锁全部高级特性。 无论你装的是哪个版本、哪种系统,教程都帮你…

    IDEA破解教程 2025 年 10 月 23 日
    47200
  • Discord技术架构调研(IM即时通讯技术架构分析)

    一、目标 调研 discord 的整体架构,发掘可为所用的设计思想 二、调研背景 Discord作为目前比较火的一个在线聊天和语音通信平台且具有丰富的功能。另外其 “超级”群 概念号称可支持百万级群聊 以及 永久保留用户聊天记录。探究其相关技术架构与技术实现 三、产品介绍 目前广泛使用的在线聊天和语音通信平台。最初于2015年发布,旨在为游戏社区提供一个交流…

    2025 年 1 月 10 日
    71000

发表回复

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

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信