Class LambdaStreamingResponseHandler

java.lang.Object
dev.langchain4j.model.LambdaStreamingResponseHandler

public class LambdaStreamingResponseHandler extends Object
Utility class with lambda-based streaming response handlers.

Lets you use Java lambda functions to receive onPartialResponse and onError events, from your streaming chat model, instead of creating an anonymous inner class implementing StreamingChatResponseHandler.

Example:

 import static dev.langchain4j.model.LambdaStreamingResponseHandler.*;

 model.chat("Why is the sky blue?",
       onPartialResponse(text -> System.out.println(text));
 model.chat("Why is the sky blue?",
       onPartialResponse(System.out::println);
 model.chat("Why is the sky blue?",
       onPartialResponseAndError(System.out::println, Throwable::printStackTrace));

 // Blocking
 onPartialResponseBlocking(model, "Why is the sky blue?", System.out::print);
 onPartialResponseAndErrorBlocking(model, "Why is the sky blue?",
      System.out::print, Throwable::printStackTrace);
 
See Also:
  • Constructor Details

    • LambdaStreamingResponseHandler

      public LambdaStreamingResponseHandler()
  • Method Details

    • onPartialResponse

      public static StreamingChatResponseHandler onPartialResponse(Consumer<String> onPartialResponse)
    • onPartialResponseAndError

      public static StreamingChatResponseHandler onPartialResponseAndError(Consumer<String> onPartialResponseLambda, Consumer<Throwable> onErrorLambda)
    • onPartialResponseBlocking

      public static void onPartialResponseBlocking(StreamingChatModel model, String message, Consumer<String> onPartialResponse) throws InterruptedException
      Creates a streaming response handler that processes partial responses with the given consumer and blocks until the streaming is complete.
      Parameters:
      model - the streaming chat model to use
      message - the message to send
      onPartialResponse - consumer to handle each partial response
      Throws:
      InterruptedException - if the thread is interrupted while waiting for completion
    • onPartialResponseAndErrorBlocking

      public static void onPartialResponseAndErrorBlocking(StreamingChatModel model, String message, Consumer<String> onPartialResponse, Consumer<Throwable> onError) throws InterruptedException
      Creates a streaming response handler that processes partial responses and errors with the given consumers and blocks until the streaming is complete.
      Parameters:
      model - the streaming chat model to use
      message - the message to send
      onPartialResponse - consumer to handle each partial response
      onError - consumer to handle errors
      Throws:
      InterruptedException - if the thread is interrupted while waiting for completion