欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 文化 > java对接阿里通义千问

java对接阿里通义千问

2025/3/15 23:56:27 来源:https://blog.csdn.net/YXWik/article/details/144988397  浏览:    关键词:java对接阿里通义千问

在这里插入图片描述
创建一个应用
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

pom.xml引入依赖
在这里插入图片描述

<dependency><groupId>com.alibaba</groupId><artifactId>dashscope-sdk-java</artifactId><!-- 请将 'the-latest-version' 替换为查询到的最新版本号: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>

在这里插入图片描述
使用发现pom.xml已经引入了dashscope依赖 但是依赖中没有dashscope这个包
在这里插入图片描述
在这里插入图片描述
解决方案清理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()// 若没有配置环境变量,可用百炼API Key将下行替换为:.apiKey("sk-xxx")。但不建议在生产环境中直接将API Key硬编码到代码中,以减少API Key泄露风险。.apiKey(System.getenv("DASHSCOPE_API_KEY"))// 替换为实际的应用 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);}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词