阿里云百炼大模型使用

阿里云百炼大模型使用

由于阿里云百炼大模型有个新用户福利,有免费的4000000 tokens,我开通了相应的服务试试水。
在这里插入图片描述

使用

这里使用Android开发了一个简单的demo。

安装SDK

    implementation group: 'com.alibaba', name: 'dashscope-sdk-java', version: '2.14.4'

核心代码


    // 自定义的AsyncTask
    private class NetworkTask extends AsyncTask<Void, Void, String> {

        private String text;

        public NetworkTask(String text) {
            this.text = text;
        }

        @Override
        protected String doInBackground(Void... voids) {
            // 在这里执行网络请求

            //请设置为自己的API-Key,这是有风险的。
            Constants.apiKey = "";

            Generation gen = new Generation();

            com.alibaba.dashscope.common.Message systemMsg = com.alibaba.dashscope.common.Message.builder()
                    .role(Role.SYSTEM.getValue())
                    .content(text)
                    .build();

            com.alibaba.dashscope.common.Message userMsg = com.alibaba.dashscope.common.Message.builder()
                    .role(Role.USER.getValue())
                    .content(text)
                    .build();

            GenerationParam param = GenerationParam.builder()
                    .model("qwen-plus")
                    .messages(Arrays.asList(userMsg))
                    .resultFormat(GenerationParam.ResultFormat.MESSAGE)
                    .topP(0.8)
                    .build();
            GenerationResult result = null;
            try {
                result = gen.call(param);
                Gson gson = new Gson();
                return gson.toJson(result);
            } catch (NoApiKeyException e) {
                e.printStackTrace();
            } catch (InputRequiredException e) {
                e.printStackTrace();
            }
            return "";
        }

        @Override
        protected void onPostExecute(String response) {
            if ("".equals(response)) {
                return;
            }

            // 在这里更新UI,因为此方法在主线程中运行
            Gson gson = new Gson();
            GenerationResult result = gson.fromJson(response, GenerationResult.class);

            GenerationOutput output = result.getOutput();
            output.getChoices().get(0).getMessage();
            String serverOutput = "";
            serverOutput = output.getChoices().stream().map(x -> x.getMessage().toString()).collect(Collectors.joining(","));

            Message serverResponse = new Message(serverOutput, false);
            List<Message> newMessages = new ArrayList<>();
            newMessages.add(new Message(text, true));
            newMessages.add(serverResponse);
            adapter.getMessageList().addAll(newMessages);
            adapter.notifyDataSetChanged();

        }
        
    }

实现效果
这里只支持了单轮对话,官方是支持多轮对话的,同时支持流式处理,这样用户的体验效果会更好。
在这里插入图片描述

参考

  1. 新用户限时免费策略公告
  2. 开通DashScope并创建API-KEY
  3. API详情
  • 10
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值