赞
踩
前提条件
1.已经获取申请名额,开通服务并获得API-KEY:开通DashScope并创建API-KEY。
2.maven安装对应的jar包组件
<!-- https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dashscope-sdk-java</artifactId>
<version>2.8.2</version>
</dependency>
<!--okhttp3 依赖-->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.3</version>
</dependency>
3.java代码调用接口
package com.ruoyi; 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.ApiException; import com.alibaba.dashscope.exception.InputRequiredException; import com.alibaba.dashscope.exception.NoApiKeyException; import com.alibaba.dashscope.utils.Constants; public class Test { public static void callWithMessage() throws NoApiKeyException, ApiException, InputRequiredException { Generation gen = new Generation(); Constants.apiKey="XXXXXXXXXXXXXXXXXXXXXXXXXXXX";//这里填写自己申请的APIKEY 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("如何做西红柿鸡蛋?").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); System.out.println(result); } public static void main(String[] args){ try { callWithMessage(); } catch (ApiException | NoApiKeyException | InputRequiredException e) { System.out.println(e.getMessage()); } System.exit(0); } }
返回结果
GenerationResult(requestId=076fcec1-4823-9e13-b336-9fc0e1cbd343, usage=GenerationUsage(inputTokens=11, outputTokens=153), output=GenerationOutput(text=null, finishReason=null, choices=[GenerationOutput.Choice(finishReason=stop, message=Message(role=assistant, content=做西红柿鸡蛋的方法有很多种,我这里为您提供一种方法:
材料:
鸡蛋3个、西红柿3个
步骤:
1. 将鸡蛋打入碗中,并朝着一个方向打散。
2. 锅里热油到几乎冒烟的时候,倒入打好的鸡蛋。
3. 鸡蛋入锅后要立即用铲子快速搅动,从而让所有鸡蛋都吸收足油脂,都受到油的高热。
4. 待鸡蛋快凝固时,加入切好的西红柿块一起翻炒。
5. 翻炒至西红柿变软出汁,然后加入适量的盐和糖调味。
6. 最后淋上一些香油,即可出锅。
希望这个方法可以帮助到您。))]))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。