SpringBoot调用阿里通义千问大语言模型API-JAVA新手详细教程

SpringBoot调用阿里通义千问大语言模型API

阿里百炼大模型介绍

官网地址:https://www.aliyun.com/product/bailian
什么是大模型服务平台百炼
大模型服务平台百炼是一站式的企业专属大模型生产平台,基于通义基础大模型,提供企业专属大模型开发和应用的整套工具链。

面向对象:企业、开发者及ISV的技术人员。

核心能力:支持开箱即用的应用调用,大模型训练微调和一站式在线灵活部署。

服务形式:通过API服务输出给客户,方便客户进行集成和使用专属大模型能力。

应用编排:支持客户打通自己的业务能力API,使得客户可以将专属大模型能力集成到自己的业务链路中。

工具多样:系统内置插件能力,支持Python解释器、夸克搜索、图片生成、计算器等,更方便客户一键使用;同时也可以通过自定义插件实现工具能力更加便捷。

大模型key申请

官网地址:https://www.aliyun.com/product/bailian

点击并登录,然后点击管理控制台
在这里插入图片描述
点击API-KEY
在这里插入图片描述
创建key完成就OK了
在这里插入图片描述
放心使用有免费次数
在这里插入图片描述

阿里百炼大模型使用

导入坐标依赖
 <!--阿里巴巴大模型-->
        <!-- https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>dashscope-sdk-java</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-simple</artifactId>
                </exclusion>
            </exclusions>
            <version>2.8.3</version>
        </dependency>
        
         <dependency>
            <groupId>com.squareup.okhttp3</groupId>
            <artifactId>okhttp</artifactId>
            <version>4.10.0</version>
        </dependency>
        
         <dependency>
             <groupId>com.alibaba</groupId>
             <artifactId>fastjson</artifactId>
             <version>1.2.76</version>
         </dependency>
          <dependency>
              <groupId>org.projectlombok</groupId>
              <artifactId>lombok</artifactId>
              <version>1.18.20</version>
          </dependency>
简单演示 根据关键词生成java面试题目
import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.common.Message;
import com.alibaba.dashscope.common.MessageManager;
import com.alibaba.dashscope.common.Role;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.utils.Constants;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sky.result.Result;
import com.sky.vo.LabelSubject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/ai")
public class ChatController {
    @RequestMapping("/chat")
    public Result chatAi() throws NoApiKeyException, InputRequiredException {

        Constants.apiKey = "你的密钥";
        Generation gen = new Generation();
        MessageManager msgManager = new MessageManager(10);
        Message systemMsg =
                Message.builder().role(Role.SYSTEM.getValue()).content("You are a helpful assistant.").build();
        Message userMsg = Message.builder().role(Role.USER.getValue()).content(
                "根据以下关键字生成1道面试题和标签 java基础 " +
                "并按照数据结{\\\"labelName\\\":\\\"分类名称\\\",\\\"subjectName\\\":\\\"题目\\\"}构只返回json数据"
        ).build();
        msgManager.add(systemMsg);
        msgManager.add(userMsg);
        QwenParam param =
                QwenParam.builder().model(Generation.Models.QWEN_TURBO).messages(msgManager.get())
                        .resultFormat(QwenParam.ResultFormat.MESSAGE)
                        .topP(0.8)
                        .enableSearch(true)
                        .build();
        GenerationResult result = gen.call(param);
        String content = result.getOutput().getChoices().get(0).getMessage().getContent();
        //得到json
        content = content.replace("```json","").replace("```","");
        System.out.println(content);
        //content 转换为对象
        ObjectMapper objectMapper = new ObjectMapper();
        LabelSubject labelSubject = null;

        try {
            labelSubject = objectMapper.readValue(content, LabelSubject.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(labelSubject);
        return Result.success(labelSubject);
    }
}

LabelSubject 对象

@Data
public class LabelSubject implements Serializable {
    private String labelName;
    private String subjectName;
}
测试

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值