前提條件
1.已經(jīng)獲取申請名額,開通服務(wù)并獲得API-KEY:開通DashScope并創(chuàng)建API-KEY。
2.maven安裝對應(yīng)的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代碼調(diào)用接口文章來源:http://www.zghlxwxcb.cn/news/detail-835128.html
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();//這里填寫對話內(nèi)容
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);
}
}
返回結(jié)果文章來源地址http://www.zghlxwxcb.cn/news/detail-835128.html
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. 翻炒至西紅柿變軟出汁,然后加入適量的鹽和糖調(diào)味。
6. 最后淋上一些香油,即可出鍋。
希望這個方法可以幫助到您。))]))
到了這里,關(guān)于java對接阿里云通義千問API的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!