All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.langchain4j.model.LambdaStreamingResponseHandler Maven / Gradle / Ivy

package dev.langchain4j.model;

import java.util.function.Consumer;

/**
 * Utility class with lambda-based streaming response handlers.
 *
 * Lets you use Java lambda functions to receive onNext and onError events,
 * from your streaming chat model, instead of creating an anonymous inner class
 * implementing StreamingResponseHandler.
 *
 * Example:
 * 
 * import static dev.langchain4j.model.LambdaStreamingResponseHandler.*;
 *
 * model.generate("Why is the sky blue?",
 *       onNext(text -> System.out.println(text));
 * model.generate("Why is the sky blue?",
 *       onNext(System.out::println);
 * model.generate("Why is the sky blue?",
 *       onNextAndError(System.out::println, Throwable::printStackTrace));
 * 
* * @param The type of the response. * * @see StreamingResponseHandler#onNext(String) * @see StreamingResponseHandler#onError(Throwable) */ public class LambdaStreamingResponseHandler { public static StreamingResponseHandler onNext(Consumer nextLambda) { return new StreamingResponseHandler() { @Override public void onNext(String text) { nextLambda.accept(text); } @Override public void onError(Throwable error) { throw new RuntimeException(error); } }; } public static StreamingResponseHandler onNextAndError(Consumer nextLambda, Consumer errorLambda) { return new StreamingResponseHandler() { @Override public void onNext(String text) { nextLambda.accept(text); } @Override public void onError(Throwable error) { errorLambda.accept(error); } }; } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy