Class StreamingChatGeneratorUtil

java.lang.Object
com.alibaba.cloud.ai.util.StreamingChatGeneratorUtil

public class StreamingChatGeneratorUtil extends Object
StreamingChatGenerator utility class for creating and configuring StreamingChatGenerator instances
Author:
zhangshenghang
  • Constructor Details

    • StreamingChatGeneratorUtil

      public StreamingChatGeneratorUtil()
  • Method Details

    • createEmptyGenerator

      public static AsyncGenerator<? extends NodeOutput> createEmptyGenerator(String nodeName, OverAllState state, reactor.core.publisher.Flux<org.springframework.ai.chat.model.ChatResponse> flux)
      Create a basic StreamingChatGenerator with an empty mapResult
      Parameters:
      nodeName - node name
      state - state
      flux - response stream
      Returns:
      AsyncGenerator instance
    • createStreamPrintGenerator

      public static AsyncGenerator<? extends NodeOutput> createStreamPrintGenerator(String text)
      Create a StreamingChatGenerator for streaming and printing text
      Parameters:
      text - text to print
      Returns:
      AsyncGenerator instance
    • createEmptyGenerator

      public static AsyncGenerator<? extends NodeOutput> createEmptyGenerator(reactor.core.publisher.Flux<org.springframework.ai.chat.model.ChatResponse> flux)
      Create an empty StreamingChatGenerator with an empty mapResult
      Parameters:
      flux - response stream
      Returns:
      AsyncGenerator instance
    • createGenerator

      public static AsyncGenerator<? extends NodeOutput> createGenerator(String nodeName, OverAllState state, Function<org.springframework.ai.chat.model.ChatResponse,Map<String,Object>> mapResultFunction, reactor.core.publisher.Flux<org.springframework.ai.chat.model.ChatResponse> flux)
      Create a StreamingChatGenerator with a custom result mapping function
      Parameters:
      nodeName - node name
      state - state
      mapResultFunction - result mapping function
      flux - response stream
      Returns:
      AsyncGenerator instance
    • createEmptyGenerator

      public static AsyncGenerator<? extends NodeOutput> createEmptyGenerator(Class<?> nodeClass, OverAllState state, reactor.core.publisher.Flux<org.springframework.ai.chat.model.ChatResponse> flux)
      Create a StreamingChatGenerator using the class object to obtain the node name
      Parameters:
      nodeClass - node class
      state - state
      flux - response stream
      Returns:
      AsyncGenerator instance
    • createGenerator

      public static AsyncGenerator<? extends NodeOutput> createGenerator(Class<?> nodeClass, OverAllState state, Function<org.springframework.ai.chat.model.ChatResponse,Map<String,Object>> mapResultFunction, reactor.core.publisher.Flux<org.springframework.ai.chat.model.ChatResponse> flux)
      Create a StreamingChatGenerator using the class object to obtain the node name and a custom result mapping function
      Parameters:
      nodeClass - node class
      state - state
      mapResultFunction - result mapping function
      flux - response stream
      Returns:
      AsyncGenerator instance
    • createGeneratorWithCallback

      public static AsyncGenerator<? extends NodeOutput> createGeneratorWithCallback(Class<?> nodeClass, OverAllState state, Function<org.springframework.ai.chat.model.ChatResponse,Map<String,Object>> mapResultFunction, reactor.core.publisher.Flux<org.springframework.ai.chat.model.ChatResponse> flux, Consumer<Map<String,Object>> onCompleteCallback)
      Create a StreamingChatGenerator with a completion callback When the generator finishes processing, the completion callback will be executed
      Parameters:
      nodeClass - node class
      state - state
      mapResultFunction - result mapping function
      flux - response stream
      onCompleteCallback - callback function executed upon completion, providing the final processing result as a parameter
      Returns:
      AsyncGenerator instance
    • createGeneratorWithCallback

      public static AsyncGenerator<? extends NodeOutput> createGeneratorWithCallback(String nodeName, OverAllState state, Function<org.springframework.ai.chat.model.ChatResponse,Map<String,Object>> mapResultFunction, reactor.core.publisher.Flux<org.springframework.ai.chat.model.ChatResponse> flux, Consumer<Map<String,Object>> onCompleteCallback)
      Create a StreamingChatGenerator with a completion callback When the generator finishes processing, the completion callback will be executed
      Parameters:
      nodeName - node name
      state - state
      mapResultFunction - result mapping function
      flux - response stream
      onCompleteCallback - callback function executed upon completion, providing the final processing result as a parameter
      Returns:
      AsyncGenerator instance
    • createGeneratorWithOrderedNotifications

      public static AsyncGenerator<? extends NodeOutput> createGeneratorWithOrderedNotifications(Class<?> nodeClass, OverAllState state, Function<org.springframework.ai.chat.model.ChatResponse,Map<String,Object>> mapResultFunction, reactor.core.publisher.Flux<org.springframework.ai.chat.model.ChatResponse> flux, String startMessage, String completionMessage)
      Create a generator with ordered notifications, ensuring sequential output: start message -> main processing -> completion message
      Parameters:
      nodeClass - node class
      state - state
      mapResultFunction - result mapping function
      flux - response stream
      startMessage - start message
      completionMessage - completion message
      Returns:
      combined generator
    • createGeneratorWithComposeCompletion

      public static AsyncGenerator<? extends NodeOutput> createGeneratorWithComposeCompletion(Class<?> nodeClass, OverAllState state, Function<org.springframework.ai.chat.model.ChatResponse,Map<String,Object>> mapResultFunction, reactor.core.publisher.Flux<org.springframework.ai.chat.model.ChatResponse> flux, String completionMessage)
      Create a simple generator with a completion callback, using AsyncGenerator's composeWith mechanism
      Parameters:
      nodeClass - node class
      state - state
      mapResultFunction - result mapping function
      flux - response stream
      completionMessage - completion message
      Returns:
      generator with completion notification
    • createStreamingProcessor

      public static StreamingChatGeneratorUtil.StreamingProcessorBuilder createStreamingProcessor()
      Create streaming processor builder instance
      Returns:
      StreamingProcessorBuilder instance
    • createStreamingGeneratorWithMessages

      public static AsyncGenerator<? extends NodeOutput> createStreamingGeneratorWithMessages(Class<?> nodeClass, OverAllState state, String startMessage, String completionMessage, Function<String,Map<String,Object>> resultMapper, reactor.core.publisher.Flux<org.springframework.ai.chat.model.ChatResponse> sourceFlux)
      Quickly create streaming generator with start and end messages
      Parameters:
      nodeClass - node class
      state - state
      startMessage - start message
      completionMessage - completion message
      resultMapper - result mapping function
      sourceFlux - source data stream
      Returns:
      AsyncGenerator instance
    • createStreamingGeneratorWithMessages

      public static AsyncGenerator<? extends NodeOutput> createStreamingGeneratorWithMessages(Class<?> nodeClass, OverAllState state, String startMessage, String completionMessage, Function<String,Map<String,Object>> resultMapper, reactor.core.publisher.Flux<org.springframework.ai.chat.model.ChatResponse> sourceFlux, StreamResponseType type)
    • createStreamingGeneratorWithMessages

      public static AsyncGenerator<? extends NodeOutput> createStreamingGeneratorWithMessages(Class<?> nodeClass, OverAllState state, Function<String,Map<String,Object>> resultMapper, reactor.core.publisher.Flux<org.springframework.ai.chat.model.ChatResponse> sourceFlux, StreamResponseType type)
    • createStreamingGeneratorWithMessages

      public static AsyncGenerator<? extends NodeOutput> createStreamingGeneratorWithMessages(Class<?> nodeClass, OverAllState state, Function<String,Map<String,Object>> resultMapper, reactor.core.publisher.Flux<org.springframework.ai.chat.model.ChatResponse> sourceFlux)
    • createMultiStepGenerator

      public static AsyncGenerator<? extends NodeOutput> createMultiStepGenerator(Class<?> nodeClass, OverAllState state, List<StreamingChatGeneratorUtil.ProcessingStep> processingSteps, Function<Map<String,Object>,Map<String,Object>> finalResultMapper)
      Create multi-step processing streaming generator Supports executing multiple steps in the display process, each step has its own message and logic
      Parameters:
      nodeClass - node class
      state - state
      processingSteps - processing steps list
      finalResultMapper - final result mapping function
      Returns:
      AsyncGenerator instance