SpringBoot集成阿里百炼大模型(多轮对话) 原子的学习日记Day02

SpringBoot集成阿里百炼大模型(多轮对话) 原子的学习日记Day02

使用springboot集成阿里百炼大模型的第二天!进入阿里百炼大模型的学习的第二天!百炼大模型作为先进的人工智能技术成果,拥有诸多优势,尤其在处理自然语言理解和生成任务方面。其中可选择的模型有很多,我选择了,选择什么要根据自己的钱包来看,原子钱包不多,选择了免费试用的!!!!

如果对话轮数较多,可能会超过大模型可以输入的token上限。您可以采用对话历史摘要、固定窗口大小等方法,控制输入大模型的token数量

下一章SpringBoot集成阿里百炼大模型(流式输出) 原子的学习日记Day03

在这里插入图片描述

百炼大模型的优势

百炼大模型(此处假设指的是阿里云开发的语言模型“通义千问”或类似的大型预训练语言模型)作为先进的人工智能技术成果,拥有诸多优势,尤其在处理自然语言理解和生成任务方面。虽然直接提及“百炼大模型”可能不是具体已知模型的名称,但我们可以基于当前大型语言模型的一般特点,总结这类模型的优势:

广泛的知识覆盖与理解能力:通过在大规模互联网文本数据上进行训练,这些模型能够吸收和理解跨领域的广泛知识,从而在各种话题上提供有质量的回答和讨论。

语境理解与连贯对话:高级的上下文理解能力使得这些模型能够追踪对话历史,生成连贯、贴合上下文的回复,提升人机交互的自然流畅度。

多语言支持:许多大型语言模型支持多语言输入和输出,促进了跨国界、跨文化的沟通与交流。

代码生成与解释:一些先进的大模型展示了编写和解释代码的能力,这对于软件开发、教育辅导等领域有着重大意义。

创意生成与辅助创作:能够根据用户提供的信息或提示生成创意性的文字内容,包括故事、诗歌、文章草稿等,为内容创作者提供灵感和支持。

情感理解和个性化互动:通过学习人类语言中的情感表达,模型能在一定程度上理解和回应用户情绪,实现更加人性化的互动体验。

持续学习与迭代优化:大型语言模型的训练框架允许不断吸纳新数据进行微调,使其随着时间推移不断进步,适应语言和社会趋势的变化。

应用广泛性:可广泛应用于客户服务、教育、娱乐、新闻生成、研发辅助等多个领域,提高工作效率和服务质量。

总之,百炼大模型或同类的大型语言模型,凭借其深度学习能力和大数据训练基础,为推动人工智能技术的实际应用和发展提供了强大的支持。

整体架构流程

使用
Java8+Spring2+阿里百炼大模型集成+SpringBoot

技术名词解释

  • api-key —当您通过API/SDK调用大模型或应用时,需要获取API-KEY作为调用时的鉴权凭证。
  • SDK —SDK是Software Development Kit的缩写,中文意为“软件开发工具包”。它是一系列软件开发工具的集合,旨在为特定的平台、框架、硬件或服务提供便捷的开发环境。SDK通常包括但不限于以下组件:
  • 通义千问—是阿里云研发的大语言模型
  • 灵积----是阿里云推出的模型服务平台,提供了包括通义千问在内的多种模型的服务接口
  • 百炼------是阿里云推出的一站式大模型应用开发平台,同时也提供模型调用服务。
  • DashScope是灵积的英文名,两者指的是同一平台
  • 多轮对话 — 相比于单轮对话,多轮对话可以让大模型参考历史对话信息,更符合日常交流的场景。实现多轮对话的关键在于维护一个存放历史对话信息的列表,并将更新后的列表作为大模型的输入,从而使大模型可以参考历史对话信息进行回复。您可以参考以下示例代码,将每一轮的对话历史添加到messages列表中,实现多轮对话的功能。

集成步骤

1,选择大模型以及获取自己的api-key(前面还有一步开通服务就没有展示啦!)

在模型广场中选择自己需要的大模型并注册自己的apikey(要自己保存好喔!)
在这里插入图片描述
在这里插入图片描述

2,集成SpringBoot

配置文件(配置Api-key)
api:
  key: 填自己的apikey
依赖导入(主要就是导入最后一个sdk其余的看自己的情况,我这里有依赖冲突,排除了lombok)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>CommentDemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.4.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.22</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>2.4.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.fastjson2</groupId>
            <artifactId>fastjson2</artifactId>
            <version>2.0.7</version>
        </dependency>


        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>19.0</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.30</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dashscope-sdk-java</artifactId>
            <version>2.16.0</version>
            <exclusions>
                <exclusion>
                    <artifactId>lombok</artifactId>
                    <groupId>org.projectlombok</groupId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

</project>
Service文件
package com.demo.yuanzi.service.impl;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.ApiException;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class YuanziGpt2 {

    @Value("${api.key}")
    private String apiKey;

    /**
     * 创建生成参数对象。
     *
     * @param messages 消息列表
     * @return 生成参数对象
     */
    public GenerationParam createGenerationParam(List<Message> messages) {
        return GenerationParam.builder()
                .model("qwen-turbo")
                .messages(messages)
                .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                .apiKey(apiKey)
                .topP(0.8)
                .build();
    }

    /**
     * 调用带有消息的生成服务。
     *
     * @param param 生成参数
     * @return 生成结果
     * @throws ApiException      API 异常
     * @throws NoApiKeyException 缺少 API key 异常
     * @throws InputRequiredException 输入必需异常
     */
    public GenerationResult callGenerationWithMessages(GenerationParam param) throws ApiException, NoApiKeyException, InputRequiredException {
        Generation gen = new Generation();
        return gen.call(param);
    }

    /**
     * 创建消息对象。
     *
     * @param role    角色
     * @param content 内容
     * @return 消息对象
     */
    public Message createMessage(Role role, String content) {
        return Message.builder().role(role.getValue()).content(content).build();
    }
}


Controller文件
  @GetMapping("/test3")
    public String show3() {
        try {
            // 准备消息列表
            List<Message> messages = new ArrayList<>();

            // 添加系统消息
            messages.add(yuanZIGpt2.createMessage(Role.SYSTEM, "You are a helpful assistant."));

            // 循环接收用户输入,最多接收 3 条消息
            for (int i = 0; i < 3; i++) {
                Scanner scanner = new Scanner(System.in);
                System.out.print("请输入:");
                String userInput = scanner.nextLine();

                // 如果用户输入 exit,退出循环
                if ("exit".equalsIgnoreCase(userInput)) {
                    break;
                }

                // 添加用户消息到消息列表
                messages.add(yuanZIGpt2.createMessage(Role.USER, userInput));

                // 创建生成参数对象
                GenerationParam param = yuanZIGpt2.createGenerationParam(messages);

                // 调用生成服务并获取生成结果
                GenerationResult result = yuanZIGpt2.callGenerationWithMessages(param);

                // 打印模型输出的内容
                System.out.println("模型输出:" + result.getOutput().getChoices().get(0).getMessage().getContent());

                // 将模型输出的消息添加到消息列表
                messages.add(result.getOutput().getChoices().get(0).getMessage());
            }
        } catch (ApiException | NoApiKeyException | InputRequiredException e) {
            // 捕获并打印异常堆栈信息
            e.printStackTrace();
        }

        // 程序结束
        System.exit(0);

        return "测试";
    }

测试结果

在这里插入图片描述

  • 27
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值