Springboot集成通义大模型

1.先到阿里云平台开头阿里云白炼账号,创建apiKey

2. 引入maven依赖

<dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dashscope-sdk-java</artifactId>
                <version>2.8.3</version>
            </dependency>
            <!-- https://mvnrepository.com/artifact/io.projectreactor/reactor-core -->
            <dependency>
                <groupId>io.projectreactor</groupId>
                <artifactId>reactor-core</artifactId>
                <version>3.6.0</version>
            </dependency>

3.调用通义api

  String content ;
        // 构建消息对象
        try{
            Message message = Message.builder().role(Role.USER.getValue()).content(question.toString()).build();

            // 构建通义千问参数对象
            GenerationParam param = GenerationParam.builder()
                    .model(Generation.Models.QWEN_PLUS)
                    .messages(Collections.singletonList(message))
                    .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                    .topP(0.8)
                    .apiKey(apiKey)//正式
//                    .apiKey("sk-86fadc62eeaf4e3a9a24b55925b9a80d")//正式
//                .apiKey("sk-6f335e7853a64e3e9b8384e338d22e50")//测试
                    .build();


            GenerationResult result = new Generation().call(param);

            // 获取生成的内容
            content = result.getOutput().getChoices().get(0).getMessage().getContent();

            // 设置响应内容类型
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");

            // 构建成功响应
            Map<String, Object> successMap = new HashMap<>();
            successMap.put("data", content);
            response.getWriter().write(objectMapper.writeValueAsString(successMap));
        } catch (Exception e) {
            throw new ServiceException(GlobalErrorCodeConstants.NOT_FOUND.getCode(), "调用AI接口异常:"+e.getMessage());
        }
        return content;

pipost测试接口返回:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值