赞
踩
获取自己的api-key:按照官方手册进行获取即可
快速入门_模型服务灵积(DashScope)-阿里云帮助中心 (aliyun.com)
使用官网最佳的解决方案:java sdk_模型服务灵积(DashScope)-阿里云帮助中心 (aliyun.com)
导入dashscope最新依赖、pool2依赖
- <!-- https://mvnrepository.com/artifact/com.alibaba/dashscope-sdk-java -->
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>dashscope-sdk-java</artifactId>
- <version>2.12.0</version>
- </dependency>
- <dependency>
- <groupId>org.apache.commons</groupId>
- <artifactId>commons-pool2</artifactId>
- <version>2.11.1</version>
- </dependency>
【代码来自阿里云官网解决方案】
PooledDashScopeObjectFactory
- import org.apache.commons.pool2.BasePooledObjectFactory;
- import org.apache.commons.pool2.PooledObject;
- import org.apache.commons.pool2.impl.DefaultPooledObject;
- import com.alibaba.dashscope.aigc.generation.Generation;
-
- public class PooledDashScopeObjectFactory extends BasePooledObjectFactory<Generation> {
-
- @Override
- public Generation create() throws Exception {
- return new Generation();
- }
-
- @Override
- public PooledObject<Generation> wrap(Generation obj) {
- return new DefaultPooledObject<>(obj);
- }
- }
PooledDashScopeObjectUsage :使用代码的方式导入(文件的方式我不知道目录在哪...)
- import java.util.Arrays;
- import org.apache.commons.pool2.impl.GenericObjectPool;
- import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
- import com.alibaba.dashscope.aigc.generation.Generation;
- import com.alibaba.dashscope.aigc.generation.GenerationParam;
- import com.alibaba.dashscope.aigc.generation.GenerationResult;
- import com.alibaba.dashscope.common.Message;
- import com.alibaba.dashscope.common.Role;
- import com.alibaba.dashscope.utils.Constants;
-
- public class PooledDashScopeObjectUsage {
- public static void main(String[] args) throws Exception {
- Constants.apiKey="xxx";//设置自己的apiKey
- PooledDashScopeObjectFactory pooledDashScopeObjectFactory =
- new PooledDashScopeObjectFactory();
- GenericObjectPoolConfig<Generation> config = new GenericObjectPoolConfig<>();
- config.setMaxTotal(32);
- config.setMinIdle(32);
- GenericObjectPool<Generation> generationPool =
- new GenericObjectPool<>(pooledDashScopeObjectFactory, config);
- Generation gen = null;
- try {
- 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();
- GenerationParam param = GenerationParam.builder().model("qwen-plus")
- .messages(Arrays.asList(systemMsg, userMsg))
- .resultFormat(GenerationParam.ResultFormat.MESSAGE).topP(0.8).enableSearch(true)
- .build();
- gen = generationPool.borrowObject();
- GenerationResult result = gen.call(param);
- System.out.println(result);
- } finally {
- if (gen != null) {
- generationPool.returnObject(gen);
- }
- }
- System.out.println("completed");
- generationPool.close();
- }
-
- }
最后运行main即可,一天可以调用10次。
官网给出的demo中默认进行了多轮对话的设置,具体在下面这一部分,
通过传入历史对话记录(即systemMsg
和userMsg
)来让模型理解上下文,从而支持多轮对话的功能。当调用gen.call(param)
时,模型会基于提供的消息上下文生成相应的回答。
- GenerationParam param = GenerationParam.builder()
- .model("qwen-plus")
- .messages(Arrays.asList(systemMsg, userMsg))
- // ...
- .build();
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。