Package dev.langchain4j.model
Class LambdaStreamingResponseHandler
java.lang.Object
dev.langchain4j.model.LambdaStreamingResponseHandler
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic StreamingChatResponseHandler
onPartialResponse
(Consumer<String> onPartialResponse) static StreamingChatResponseHandler
onPartialResponseAndError
(Consumer<String> onPartialResponseLambda, Consumer<Throwable> onErrorLambda) static void
onPartialResponseAndErrorBlocking
(StreamingChatModel model, String message, Consumer<String> onPartialResponse, Consumer<Throwable> onError) Creates a streaming response handler that processes partial responses and errors with the given consumers and blocks until the streaming is complete.static void
onPartialResponseBlocking
(StreamingChatModel model, String message, Consumer<String> onPartialResponse) Creates a streaming response handler that processes partial responses with the given consumer and blocks until the streaming is complete.
-
Constructor Details
-
LambdaStreamingResponseHandler
public LambdaStreamingResponseHandler()
-
-
Method Details
-
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 usemessage
- the message to sendonPartialResponse
- 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 usemessage
- the message to sendonPartialResponse
- consumer to handle each partial responseonError
- consumer to handle errors- Throws:
InterruptedException
- if the thread is interrupted while waiting for completion
-