Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion other-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j</artifactId>
<version>1.0.1</version>
<version>1.1.0-SNAPSHOT</version>
</dependency>

<dependency>
Expand Down
48 changes: 48 additions & 0 deletions other-examples/src/main/java/LambdaStreamingExamples.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import static dev.langchain4j.model.LambdaStreamingResponseHandler.onPartialResponseAndErrorBlocking;
import static dev.langchain4j.model.LambdaStreamingResponseHandler.onPartialResponseBlocking;

import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.openai.OpenAiChatModelName;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;


public class LambdaStreamingExamples {

static class LambdaChatModelExample {

public static void main(String[] args) throws InterruptedException {
// Note: "demo" key does not support streaming, please use your own key.
StreamingChatModel model = OpenAiStreamingChatModel.builder()
.apiKey(ApiKeys.OPENAI_API_KEY)
.modelName(OpenAiChatModelName.GPT_4_1_NANO)
.build();

// Example of streaming a response with partial updates
onPartialResponseBlocking(model, "Why is the sky blue?", System.out::print);

// Example of streaming a response with error handling but no error expected
onPartialResponseAndErrorBlocking(
model,
"Explain quantum physics",
System.out::print,
error -> System.err.println("Error: " + error.getMessage()));
}
}

static class LambdaChatModelErrorExample {

public static void main(String[] args) throws InterruptedException {
StreamingChatModel invalidModel = OpenAiStreamingChatModel.builder()
.apiKey("demo")
.modelName(OpenAiChatModelName.GPT_4_1_NANO)
.build();

onPartialResponseAndErrorBlocking(
invalidModel,
"'demo' key does not support streaming",
System.out::print,
error -> System.err.println("Error caught: " + error.getMessage()));
System.out.println("\nError handling test completed!");
}
}
}