利用api方式部署通义千问无法流式输出的问题,请求大佬的帮助

@Value("${ai-api-key}")
private String apiKey;

@Resource
private Generation generation;

@SaIgnore
@GetMapping(value = "/send", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public Flux<ServerSentEvent<String>> streamAsk(String text) throws Exception {

    Generation gen = new Generation();

    // 创建用户消息对象
    Message userMsg = Message
        .builder()
        .role(Role.USER.getValue())
        .content(text)
        .build();

    // 创建QwenParam对象,设置参数
    QwenParam param = QwenParam.builder()
        .model(Generation.Models.QWEN_PLUS)
        .messages(Arrays.asList(userMsg))
        .resultFormat(QwenParam.ResultFormat.MESSAGE)
        .topP(0.8)
        .enableSearch(true)
        .apiKey(apiKey)
        // get streaming output incrementally
        .incrementalOutput(false)
        .build();

    // 调用生成接口,获取Flowable对象
    Flowable<GenerationResult> result = gen.streamCall(param);
    // 将Flowable转换成Flux<ServerSentEvent<String>>并进行处理
    return Flux.from(result)
        .publishOn(Schedulers.boundedElastic())
        // add delay between each event
        // .delayElements(Duration.ofMillis(5))
        .map(message -> {
            String requestId = message.getRequestId();
            System.out.println("request:"+requestId);
            String output = message.getOutput().getChoices().get(0).getMessage().getContent();
            System.out.println(output); // print the output
            try {
                Thread.sleep(10);
                return ServerSentEvent.<String>builder()
                    .data(output)
                    .build();

            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }


        })
        .concatWith(Flux.just(ServerSentEvent.<String>builder().comment("").build()))
        .doOnError(e -> {
            if (e instanceof NoApiKeyException) {
                // 处理 NoApiKeyException
            } else if (e instanceof InputRequiredException) {
                // 处理 InputRequiredException
            } else if (e instanceof ApiException) {
                // 处理其他 ApiException
            } else {
                // 处理其他异常
            }
        });
}

我想通过接口的形式调用阿里的通义千问,本地这边就是一段一段的返回给前端,接口调用前端这边就建立了连接,但是到了服务器里面之后,前端调用接口,服务器也是一段一段的打印了出来,但是是全部打印出来之后,才发给前端,然后建立的连接.很奇怪!

如果您也遇到了这方面的问题并且有解决问题的思路,请帮帮我,好人一生平安!

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值