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

相关推荐

  • ORM框架与数据库交互

    — title: ORM框架与数据库交互 date: 2024/12/22 updated: 2024/12/22 author: cmdragon excerpt: 对象关系映射(ORM)框架是连接数据库与编程语言的桥梁,它极大地简化了两者之间的交互。通过ORM,开发者能够以面向对象的方式处理数据库操作,避免了直接编写SQL语句的繁琐,从而提升开发效率…

    未分类 2024 年 12 月 27 日
    31100
  • java: JDK isn‘t specified for module ‘product-service‘问题解决

    目录 问题 解决方法 1.打开File->Project Structure… 2.将Project SDK修改为17 Oracle OpenJDK 17.0.12,并Apply,OK 问题 添加module后报错:java: JDK isn’t specified for module ‘product-service’ 查看pom.xml文件也添加了…

    2025 年 1 月 11 日
    37200
  • MySQL连接IDEA(Java Web)保姆级教程

    第一步:新建项目(File)->Project 第二步:New Project(JDK最好设置1.8版本与数据库适配,详细适配网请到MySQL官网查询MySQL :: MySQL 8.3 Reference Manual :: Search Results) 第三步:点中MySQLTest(项目名)并连续双击shift键->搜索Add Framework S…

    2025 年 1 月 15 日
    42000
  • 思维导图xmind如何安装?附安装包

    前言 大家好,我是小徐啊。我们在Java开发中,有时候是需要用到思维导图的,这可以帮助我们更好的理清思路,提高开发的效率。而说到思维导图,最有名的就是xmind了,它的功能十分强大,几乎是思维导图里面最强大的那一个。但是,默认只能使用初级功能,高级功能需要额外再开通,今天小徐就来介绍下如何安装xmind以及升级,让我们可以使用pro的功能。文末附获取方式。 …

    2025 年 1 月 11 日
    51600
  • 2025年最新IDEA激活码及永久破解教程 – 支持JetBrains全家桶

    全面破解JetBrains系列开发工具指南 本教程适用于IntelliJ IDEA、PyCharm、DataGrip、GoLand等JetBrains全系列开发工具,让您轻松实现永久激活! 先来看看最新IDEA版本成功破解的截图展示,可以看到已经完美激活至2099年! 下面将详细介绍如何一步步完成IDEA的永久激活,本方法同样适用于旧版本,无论您使用什么操作…

    IDEA破解教程 2025 年 8 月 1 日
    78800

发表回复

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

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信