java對接阿里通義千問的簡單教程
創(chuàng)建一個(gè)應(yīng)用
pom.xml引入依賴
<dependency> <groupId>com.alibaba</groupId> <artifactId>dashscope-sdk-java</artifactId> <!-- 請將 'the-latest-version' 替換為查詢到的最新版本號(hào):https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java --> <version>the-latest-version</version> </dependency>
https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java
<!-- https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java --> <dependency> <groupId>com.alibaba</groupId> <artifactId>dashscope-sdk-java</artifactId> <version>2.17.1</version> </dependency>
使用發(fā)現(xiàn)pom.xml已經(jīng)引入了dashscope依賴 但是依賴中沒有dashscope這個(gè)包
解決方案清理maven和idea緩存
然后就解決了
下面是官方提供的demo
import com.alibaba.dashscope.app.*; import com.alibaba.dashscope.exception.ApiException; import com.alibaba.dashscope.exception.InputRequiredException; import com.alibaba.dashscope.exception.NoApiKeyException; public class Main { public static void callWithSession() throws ApiException, NoApiKeyException, InputRequiredException { ApplicationParam param = ApplicationParam.builder() // 若沒有配置環(huán)境變量,可用百煉API Key將下行替換為:.apiKey("sk-xxx")。但不建議在生產(chǎn)環(huán)境中直接將API Key硬編碼到代碼中,以減少API Key泄露風(fēng)險(xiǎn)。 .apiKey(System.getenv("DASHSCOPE_API_KEY")) // 替換為實(shí)際的應(yīng)用 ID .appId("YOUR_APP_ID") .prompt("你是誰?") .build(); Application application = new Application(); ApplicationResult result = application.call(param); param.setSessionId(result.getOutput().getSessionId()); param.setPrompt("你有什么技能?"); result = application.call(param); System.out.printf("%s\n, session_id: %s\n", result.getOutput().getText(), result.getOutput().getSessionId()); } public static void main(String[] args) { try { callWithSession(); } catch (ApiException | NoApiKeyException | InputRequiredException e) { System.out.printf("Exception: %s", e.getMessage()); System.out.println("請參考文檔:https://help.aliyun.com/zh/model-studio/developer-reference/error-code"); } System.exit(0); } }
總結(jié)
到此這篇關(guān)于java對接阿里通義千問的文章就介紹到這了,更多相關(guān)java對接阿里通義千問內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
windows下java -jar 后臺(tái)運(yùn)行以及殺死后臺(tái)進(jìn)程的操作
這篇文章主要介紹了windows下java -jar 后臺(tái)運(yùn)行以及殺死后臺(tái)進(jìn)程的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12關(guān)于spring5的那些事:@Indexed 解密
這篇文章主要介紹了關(guān)于spring5的那些事:@Indexed 解密,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11基于Java設(shè)計(jì)一個(gè)高并發(fā)的秒殺系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了如何基于Java設(shè)計(jì)一個(gè)高并發(fā)的秒殺系統(tǒng),文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,有需要的小伙伴可以參考下2023-10-10Springboot整合實(shí)現(xiàn)郵件發(fā)送的原理詳解
SpringBoot集成郵件服務(wù)非常簡單,通過簡單的學(xué)習(xí)即可快速掌握郵件業(yè)務(wù)類的核心邏輯和企業(yè)郵件的日常服務(wù),本文給大家分享Springboot整合實(shí)現(xiàn)郵件發(fā)送的原理,一起看看吧2021-06-06