java对接阿里云通义千问API多轮对话承接上下文

在api中调用接口发现每次都是单次对话,例如作总结和根据刚才的提问做汇总会无法识别。
但是接口是有多轮对话功能的。
java对接阿里云通义千问API操作流程
但是确定已经完成以上步骤
然后是以下代码

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="XXXXXXXXXX";//这里填写自己申请的APIKEY
        //多轮对话内容可存入数据库,加载时循环放入放入MessageManager 对象实现对话内容加载
        MessageManager msgManager = new MessageManager(5);
        Message systemMsg0 = Message.builder().role(Role.SYSTEM.getValue()).content("You are a helpful assistant.").build();
        Message userMsg1 = Message.builder().role(Role.USER.getValue()).content("你叫什么名字?").build();
        Message assistantMsg2 = Message.builder().role(Role.ASSISTANT.getValue()).content("我是来自阿里云的超大规模语言模型,我叫通义千问。").build();
        Message userMsg3 = Message.builder().role(Role.USER.getValue()).content("我刚才的提问是什么?").build();
        Message assistantMsg4 = Message.builder().role(Role.ASSISTANT.getValue()).content("您刚才的提问是“你叫什么名字?”。").build();
        Message userMsg4 = Message.builder().role(Role.USER.getValue()).content("你是基于gpt3么?").build();
        msgManager.add(systemMsg0);
        msgManager.add(userMsg1);
        msgManager.add(assistantMsg2);
        msgManager.add(userMsg3);
        msgManager.add(assistantMsg4);
        msgManager.add(userMsg4);
        
        QwenParam params = QwenParam.builder().model("qwen-turbo")//此处可根据自己需要更换AI模型
                .messages(msgManager.get())
                .seed(1234)
                .topP(0.8)
                .resultFormat("message")
                .enableSearch(false)
                .maxTokens(1500)
                .temperature((float)1.0)
                .repetitionPenalty((float)1.0)
                .build();
        
        
        GenerationResult result = gen.call(params);
        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);
    }
}

返回结果

GenerationResult(
requestId=dac00891-d84b-9734-9138-49f6446e5715, 
usage=GenerationUsage(inputTokens=47, 
outputTokens=21), 
output=GenerationOutput(text=null, 
finishReason=null, 
choices=[GenerationOutput.Choice(finishReason=stop,
message=Message(role=assistant, 
content=不是的,我是阿里云自主研发的超大规模语言模型,与GPT-3没有关系。))]))

你可能感兴趣的:(java,阿里云,oracle)