Package com.alibaba.cloud.ai.util
Class StreamingChatGeneratorUtil
java.lang.Object
com.alibaba.cloud.ai.util.StreamingChatGeneratorUtil
StreamingChatGenerator utility class for creating and configuring
StreamingChatGenerator instances
- Author:
- zhangshenghang
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic class -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic 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 namestatic 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 mapResultstatic AsyncGenerator<? extends NodeOutput>createEmptyGenerator(reactor.core.publisher.Flux<org.springframework.ai.chat.model.ChatResponse> flux) Create an empty StreamingChatGenerator with an empty mapResultstatic 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 functionstatic 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 functionstatic 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 executedstatic 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 executedstatic 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 mechanismstatic 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 messagestatic 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 logicstatic 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 messagesstatic 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) 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) 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) Create streaming processor builder instancestatic AsyncGenerator<? extends NodeOutput>Create a StreamingChatGenerator for streaming and printing text
-
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 namestate- stateflux- response stream- Returns:
- AsyncGenerator instance
-
createStreamPrintGenerator
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 namestate- statemapResultFunction- result mapping functionflux- 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 classstate- stateflux- 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 classstate- statemapResultFunction- result mapping functionflux- 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 classstate- statemapResultFunction- result mapping functionflux- response streamonCompleteCallback- 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 namestate- statemapResultFunction- result mapping functionflux- response streamonCompleteCallback- 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 classstate- statemapResultFunction- result mapping functionflux- response streamstartMessage- start messagecompletionMessage- 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 classstate- statemapResultFunction- result mapping functionflux- response streamcompletionMessage- completion message- Returns:
- generator with completion notification
-
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 classstate- statestartMessage- start messagecompletionMessage- completion messageresultMapper- result mapping functionsourceFlux- 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 classstate- stateprocessingSteps- processing steps listfinalResultMapper- final result mapping function- Returns:
- AsyncGenerator instance
-