-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bedrock : add example for the BedrockChatModel converseAPI implementa…
…tion (#140) discussed in langchain4j/langchain4j#2404
- Loading branch information
1 parent
307809d
commit 5738cb0
Showing
4 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
bedrock-examples/src/main/java/converse/BedrockChatModelExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package converse; | ||
|
||
import dev.langchain4j.data.message.ImageContent; | ||
import dev.langchain4j.data.message.PdfFileContent; | ||
import dev.langchain4j.data.message.TextContent; | ||
import dev.langchain4j.data.message.UserMessage; | ||
import dev.langchain4j.model.bedrock.BedrockChatModel; | ||
import dev.langchain4j.model.chat.ChatLanguageModel; | ||
import dev.langchain4j.model.chat.request.ChatRequest; | ||
import dev.langchain4j.model.chat.request.ChatRequestParameters; | ||
import dev.langchain4j.model.chat.response.ChatResponse; | ||
|
||
import java.nio.file.Paths; | ||
|
||
public class BedrockChatModelExample { | ||
|
||
static class Simple_Prompt { | ||
|
||
public static void main(String[] args) { | ||
|
||
// For authentication, set the following environment variables: | ||
// AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY | ||
// More info on creating the API keys: | ||
// https://docs.aws.amazon.com/bedrock/latest/userguide/api-setup.html | ||
ChatLanguageModel chatModel = BedrockChatModel.builder() | ||
.modelId("us.amazon.nova-lite-v1:0") | ||
.build(); | ||
|
||
String joke = chatModel.chat("Tell me a joke about Java"); | ||
|
||
System.out.println(joke); | ||
} | ||
} | ||
|
||
static class Image_Inputs { | ||
|
||
public static void main(String[] args) { | ||
|
||
ChatLanguageModel chatModel = BedrockChatModel.builder() | ||
.modelId("us.amazon.nova-lite-v1:0") | ||
.build(); | ||
|
||
UserMessage userMessage = UserMessage.from( | ||
TextContent.from("What do you see?"), | ||
ImageContent.from("https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png") | ||
); | ||
|
||
ChatResponse chatResponse = chatModel.chat(userMessage); | ||
|
||
System.out.println(chatResponse.aiMessage().text()); | ||
} | ||
} | ||
|
||
static class PDF_Inputs { | ||
|
||
public static void main(String[] args) { | ||
|
||
ChatLanguageModel chatModel = BedrockChatModel.builder() | ||
.modelId("us.amazon.nova-lite-v1:0") | ||
.build(); | ||
|
||
UserMessage userMessage = UserMessage.from( | ||
TextContent.from("Summarize attached document"), | ||
PdfFileContent.from(Paths.get("bedrock-examples/src/main/resources/test-file.pdf").toUri()) | ||
); | ||
|
||
ChatResponse chatResponse = chatModel.chat(userMessage); | ||
|
||
System.out.println(chatResponse.aiMessage().text()); | ||
} | ||
} | ||
|
||
static class Setting_Common_ChatRequestParameters { | ||
|
||
public static void main(String[] args) { | ||
|
||
ChatRequestParameters defaultParameters = ChatRequestParameters.builder() | ||
.temperature(0.7) | ||
.maxOutputTokens(100) | ||
// there are many more common parameters, see ChatRequestParameters for more info | ||
.build(); | ||
|
||
ChatLanguageModel chatModel = BedrockChatModel.builder() | ||
.modelId("us.amazon.nova-lite-v1:0") | ||
.defaultRequestParameters(defaultParameters) | ||
.logRequests(true) | ||
.build(); | ||
|
||
ChatRequestParameters parameters = ChatRequestParameters.builder() | ||
// Model choice can be overridden with request parameter | ||
.modelName("anthropic.claude-3-haiku-20240307-v1:0") | ||
.temperature(1.0) | ||
.maxOutputTokens(50) | ||
.build(); | ||
|
||
ChatRequest chatRequest = ChatRequest.builder() | ||
.messages(UserMessage.from("Tell me a funny story about Java")) | ||
.parameters(parameters) // merges with and overrides default parameters | ||
.build(); | ||
|
||
ChatResponse chatResponse = chatModel.chat(chatRequest); | ||
|
||
System.out.println(chatResponse); | ||
} | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
...rc/main/java/BedrockChatModelExample.java → .../java/invoke/BedrockChatModelExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions
2
...ava/BedrockStreamingChatModelExample.java → ...oke/BedrockStreamingChatModelExample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.