spring-ai icon indicating copy to clipboard operation
spring-ai copied to clipboard

How to get this assitantMessage in the streaming response

Open fanxt0218 opened this issue 1 year ago • 2 comments

desc: It's easy to get an assitantMessage from the result in a non-streaming response, but there doesn't seem to be a straightforward way to get an assitantMessage in a streaming response, so I need to manually implement a complex logic to accomplish this. I'm wondering if there's a relatively simple and straightforward way to accomplish this?

Minimal: chatClient.prompt().stream().chatClientResponse(); ...-> AssistentMessage

env: java 17 1.1.0-snapshot

fanxt0218 avatar Jul 18 '25 05:07 fanxt0218

In my realization, I achieved it roughly like this Flux<AssistantMessage> assistantMessageFlux = chatClient.prompt().stream().chatClientResponse().map(chatClientResponse -> chatClientResponse.chatResponse().getResult().getOutput()); Mono<List<AssistantMessage>> listMono = assistantMessageFlux.collectList(); listMono.flatMap(messages -> { ... retuen Mono.just(new AssistantMessage( ... )); } But it's inconvenient, and I'd like to see an easier way. Or what implementation I didn't notice, please let me know, thanks!

fanxt0218 avatar Jul 18 '25 06:07 fanxt0218

In my realization, I achieved it roughly like this But it's inconvenient, and I'd like to see an easier way. Or what implementation I didn't notice, please let me know, thanks!Flux<AssistantMessage> assistantMessageFlux = chatClient.prompt().stream().chatClientResponse().map(chatClientResponse -> chatClientResponse.chatResponse().getResult().getOutput()); Mono<List<AssistantMessage>> listMono = assistantMessageFlux.collectList(); listMono.flatMap(messages -> { ... retuen Mono.just(new AssistantMessage( ... )); }

Perhaps you can do it this way,package com.robot.openai.common;

import com.robot.openai.model.request.AIRequest;

import java.util.List;

/**

  • AI客户端接口,定义所有AI客户端需要实现的方法 / public interface AIClient { /*

    • 发送请求到AI服务
    • @param requestBody 请求体
    • @param callback 回调函数,用于处理响应 */ void sendRequest(AIRequest requestBody, AIClientCallback callback);

    /**

    • AI客户端回调接口 / interface AIClientCallback { /*

      • 成功处理单条流式响应
      • @param response 单条JSON数据
      • @param contentBuilder 内容构建器 */ void onSuccess(String response, StringBuilder contentBuilder);

      /**

      • 发送暂时的消息
      • @param message */ void onInterim(String message);

      /**

      • 处理错误响应
      • @param errorMessage 错误消息 */ void onError(String errorMessage);

      /**

      • 处理请求失败
      • @param e 异常 */ void onFailure(Exception e);

      /**

      • 完成处理
      • @param contentBuilder */ void onComplete(StringBuilder contentBuilder); } }

    aiClient.sendRequest(requestBody, new AIClient.AIClientCallback() { @Override public void onSuccess(String response, StringBuilder contentBuilder) { handleStreamingResponse(response, contentBuilder); } }

    private void handleStreamingResponse(String response, StringBuilder contentBuilder){ if(contentBuilder == null){ return; } // 解码 Unicode 字符串 // String decodedAnswer = decodeUnicode(response); contentBuilder.append(response); sendStreamMessageToClient(response, false); sessionManager.getOrCreateSession(receiverKey); }

CodeCodeAscension avatar Jul 18 '25 09:07 CodeCodeAscension