当前位置:   article > 正文

通义千问最新api java调用_java调用通义千问 sdk

java调用通义千问 sdk

获取自己的api-key:按照官方手册进行获取即可

快速入门_模型服务灵积(DashScope)-阿里云帮助中心 (aliyun.com)

使用官网最佳的解决方案:java sdk_模型服务灵积(DashScope)-阿里云帮助中心 (aliyun.com)

导入dashscope最新依赖、pool2依赖

  1. <!-- https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java -->
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>dashscope-sdk-java</artifactId>
  5. <version>2.12.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.apache.commons</groupId>
  9. <artifactId>commons-pool2</artifactId>
  10. <version>2.11.1</version>
  11. </dependency>

【代码来自阿里云官网解决方案】 

PooledDashScopeObjectFactory

  1. import org.apache.commons.pool2.BasePooledObjectFactory;
  2. import org.apache.commons.pool2.PooledObject;
  3. import org.apache.commons.pool2.impl.DefaultPooledObject;
  4. import com.alibaba.dashscope.aigc.generation.Generation;
  5. public class PooledDashScopeObjectFactory extends BasePooledObjectFactory<Generation> {
  6. @Override
  7. public Generation create() throws Exception {
  8. return new Generation();
  9. }
  10. @Override
  11. public PooledObject<Generation> wrap(Generation obj) {
  12. return new DefaultPooledObject<>(obj);
  13. }
  14. }

 

 PooledDashScopeObjectUsage :使用代码的方式导入(文件的方式我不知道目录在哪...)

  1. import java.util.Arrays;
  2. import org.apache.commons.pool2.impl.GenericObjectPool;
  3. import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
  4. import com.alibaba.dashscope.aigc.generation.Generation;
  5. import com.alibaba.dashscope.aigc.generation.GenerationParam;
  6. import com.alibaba.dashscope.aigc.generation.GenerationResult;
  7. import com.alibaba.dashscope.common.Message;
  8. import com.alibaba.dashscope.common.Role;
  9. import com.alibaba.dashscope.utils.Constants;
  10. public class PooledDashScopeObjectUsage {
  11. public static void main(String[] args) throws Exception {
  12. Constants.apiKey="xxx";//设置自己的apiKey
  13. PooledDashScopeObjectFactory pooledDashScopeObjectFactory =
  14. new PooledDashScopeObjectFactory();
  15. GenericObjectPoolConfig<Generation> config = new GenericObjectPoolConfig<>();
  16. config.setMaxTotal(32);
  17. config.setMinIdle(32);
  18. GenericObjectPool<Generation> generationPool =
  19. new GenericObjectPool<>(pooledDashScopeObjectFactory, config);
  20. Generation gen = null;
  21. try {
  22. Message systemMsg = Message.builder().role(Role.SYSTEM.getValue())
  23. .content("You are a helpful assistant.").build();
  24. //实际内容的输入位置
  25. Message userMsg = Message.builder().role(Role.USER.getValue()).content("生成一段李白风格的诗句").build();
  26. GenerationParam param = GenerationParam.builder().model("qwen-plus")
  27. .messages(Arrays.asList(systemMsg, userMsg))
  28. .resultFormat(GenerationParam.ResultFormat.MESSAGE).topP(0.8).enableSearch(true)
  29. .build();
  30. gen = generationPool.borrowObject();
  31. GenerationResult result = gen.call(param);
  32. System.out.println(result);
  33. } finally {
  34. if (gen != null) {
  35. generationPool.returnObject(gen);
  36. }
  37. }
  38. System.out.println("completed");
  39. generationPool.close();
  40. }
  41. }

最后运行main即可,一天可以调用10次。

官网给出的demo中默认进行了多轮对话的设置,具体在下面这一部分,

通过传入历史对话记录(即systemMsguserMsg)来让模型理解上下文,从而支持多轮对话的功能。当调用gen.call(param)时,模型会基于提供的消息上下文生成相应的回答。

  1. GenerationParam param = GenerationParam.builder()
  2. .model("qwen-plus")
  3. .messages(Arrays.asList(systemMsg, userMsg))
  4. // ...
  5. .build();

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/615120
推荐阅读
相关标签
  

闽ICP备14008679号