From 87b2cef2f89d4264639b9b58de7195eea1b37cd5 Mon Sep 17 00:00:00 2001 From: Emanuele Levi Date: Wed, 6 Nov 2024 10:31:57 +0000 Subject: [PATCH 1/5] Added Amazon Bedrock Agents Runtime Invoke Flow example --- .../bedrock-agents-runtime/README.md | 71 +++++++++++ .../bedrock-agents-runtime/pom.xml | 73 +++++++++++ .../bedrockagents/runtime/InvokeFlow.java | 113 ++++++++++++++++++ .../bedrockagents/runtime/InvokeFlowTest.java | 29 +++++ 4 files changed, 286 insertions(+) create mode 100644 javav2/example_code/bedrock-agents-runtime/README.md create mode 100644 javav2/example_code/bedrock-agents-runtime/pom.xml create mode 100644 javav2/example_code/bedrock-agents-runtime/src/main/java/com/example/bedrockagents/runtime/InvokeFlow.java create mode 100644 javav2/example_code/bedrock-agents-runtime/src/test/java/com/example/bedrockagents/runtime/InvokeFlowTest.java diff --git a/javav2/example_code/bedrock-agents-runtime/README.md b/javav2/example_code/bedrock-agents-runtime/README.md new file mode 100644 index 00000000000..79df98a9557 --- /dev/null +++ b/javav2/example_code/bedrock-agents-runtime/README.md @@ -0,0 +1,71 @@ +# Agents for Amazon Bedrock code examples for the SDK for Java 2.x + +## Overview + +Shows how to use the AWS SDK for Java 2.x to work with Amazon Bedrock Agents, Amazon Bedrock Knowledge Bases and Amazon Bedrock Prompt flows. + + + + +_Agents for Amazon Bedrock offer you the ability to build and configure autonomous agents in your application._ + +## ⚠ Important + +* Running this code might result in charges to your AWS account. For more details, see [AWS Pricing](https://aws.amazon.com/pricing/) and [Free Tier](https://aws.amazon.com/free/). +* Running the tests might result in charges to your AWS account. +* We recommend that you grant your code least privilege. At most, grant only the minimum permissions required to perform the task. For more information, see [Grant least privilege](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege). +* This code is not tested in every AWS Region. For more information, see [AWS Regional Services](https://aws.amazon.com/about-aws/global-infrastructure/regional-product-services). + + + + +## Code examples + +### Prerequisites + +For prerequisites, see the [README](../../README.md#Prerequisites) in the `javav2` folder. + + + + + + + + +## Run the examples + +### Instructions + + + + + + + +### Tests + +⚠ Running tests might result in charges to your AWS account. + + +To find instructions for running these tests, see the [README](../../README.md#Tests) +in the `javav2` folder. + + + + + + +## Additional resources + +- [Agents for Amazon Bedrock User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/agents.html) +- [Agents for Amazon Bedrock API Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Agents_for_Amazon_Bedrock.html) +- [SDK for Java 2.x Agents for Amazon Bedrock reference](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/bedrock-agent/package-summary.html) + + + + +--- + +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 \ No newline at end of file diff --git a/javav2/example_code/bedrock-agents-runtime/pom.xml b/javav2/example_code/bedrock-agents-runtime/pom.xml new file mode 100644 index 00000000000..7ce27134197 --- /dev/null +++ b/javav2/example_code/bedrock-agents-runtime/pom.xml @@ -0,0 +1,73 @@ + + + + 4.0.0 + + com.example.bedrockagents.runtime + bedrockagentsruntime + 1.0-SNAPSHOT + + bedrockagentsruntime + + + + UTF-8 + 17 + ${java.version} + ${java.version} + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + ${java.version} + ${java.version} + + + + + + + + + software.amazon.awssdk + bom + 2.28.10 + pom + import + + + + + + + software.amazon.awssdk + bedrockruntime + + + software.amazon.awssdk + bedrockagentruntime + + + software.amazon.awssdk + sts + + + org.junit.jupiter + junit-jupiter-api + 5.8.2 + test + + + org.junit.vintage + junit-vintage-engine + 5.8.2 + test + + + diff --git a/javav2/example_code/bedrock-agents-runtime/src/main/java/com/example/bedrockagents/runtime/InvokeFlow.java b/javav2/example_code/bedrock-agents-runtime/src/main/java/com/example/bedrockagents/runtime/InvokeFlow.java new file mode 100644 index 00000000000..d2b419b02e9 --- /dev/null +++ b/javav2/example_code/bedrock-agents-runtime/src/main/java/com/example/bedrockagents/runtime/InvokeFlow.java @@ -0,0 +1,113 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +package com.example.bedrockagents.runtime; + +// snippet-start:[bedrock-agent.java2.InvokeFlow] +import java.util.concurrent.CompletableFuture; +import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; +import software.amazon.awssdk.core.document.Document; +import software.amazon.awssdk.regions.Region; +import software.amazon.awssdk.services.bedrockagentruntime.BedrockAgentRuntimeAsyncClient; +import software.amazon.awssdk.services.bedrockagentruntime.model.InvokeFlowRequest; +import software.amazon.awssdk.services.bedrockagentruntime.model.InvokeFlowResponse; +import software.amazon.awssdk.services.bedrockagentruntime.model.InvokeFlowResponseHandler; +import software.amazon.awssdk.services.bedrockagentruntime.model.FlowInput; +import software.amazon.awssdk.services.bedrockagentruntime.model.FlowInputContent; +import software.amazon.awssdk.core.SdkBytes; +import software.amazon.awssdk.core.async.SdkPublisher; + + +/** + * Before running this Java V2 code example, set up your development + * environment, including your credentials. + * + * For more information, see the following documentation topic: + * + * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html + */ + + +public class InvokeFlow { + + public static String invokeFlowString(String[] args) { + final String usage = """ + + Usage: + + + Where: + flowId - An instance id value that you can obtain from the AWS Management Console.\s + flowAliasId - An instance id value that you can obtain from the AWS Management Console.\s + inputText - A monitoring status (true|false)"""; + + + if (args.length != 3) { + System.out.println(usage); + System.exit(1); + } + + String flowId = args[0]; + String flowAliasId = args[1]; + String inputText = args[2]; + + //Create Agent Runtime Async Client + BedrockAgentRuntimeAsyncClient AgentClient = BedrockAgentRuntimeAsyncClient.builder() + .credentialsProvider(DefaultCredentialsProvider.create()) + .region(Region.US_EAST_1) + .build(); + + + //Create input prompt + Document doc = Document.fromString(inputText); + FlowInputContent flowInputContent = FlowInputContent.builder().document(doc).build(); + FlowInput flowInput = FlowInput.builder().nodeName("FlowInputNode").nodeOutputName("document").content(flowInputContent).build(); + + //Create Invoke Flow Request + InvokeFlowRequest invokeFlowRequest = InvokeFlowRequest.builder().flowAliasIdentifier(flowAliasId).flowIdentifier(flowId).inputs(flowInput).build(); + + //Build a string buffer to contain all events + var completeResponseTextBuffer = new StringBuilder(); + + //Invoke the Invoke Flow endpoint + CompletableFuture future = AgentClient.invokeFlow(invokeFlowRequest, + new InvokeFlowResponseHandler() { + @Override + public void responseReceived(InvokeFlowResponse response) { + System.out.println("Flow response received: " + response +"\n"); + completeResponseTextBuffer.append(response.toString()); + } + + @Override + @SuppressWarnings("unchecked") + public void onEventStream(SdkPublisher publisher) { + publisher.subscribe(event -> { + if (event instanceof SdkBytes) { + SdkBytes bytes = (SdkBytes) event; + System.out.println("Message: " + bytes.asUtf8String()+"\n"); + completeResponseTextBuffer.append(bytes.asUtf8String()); + } + else { + System.out.println("Received event: " + event + "\n"); + completeResponseTextBuffer.append(event.toString()); + } + }); + } + + @Override + public void exceptionOccurred(Throwable throwable) { + System.err.println("Error occurred: " + throwable.getMessage()); + } + + @Override + public void complete() { + System.out.println("Flow invocation completed"); + } + }); + + future.join(); + // Return the complete response text. + return completeResponseTextBuffer.toString(); + + } +} +// snippet-end:[bedrock-agent.java2.InvokeFlow] \ No newline at end of file diff --git a/javav2/example_code/bedrock-agents-runtime/src/test/java/com/example/bedrockagents/runtime/InvokeFlowTest.java b/javav2/example_code/bedrock-agents-runtime/src/test/java/com/example/bedrockagents/runtime/InvokeFlowTest.java new file mode 100644 index 00000000000..5dfe944ef38 --- /dev/null +++ b/javav2/example_code/bedrock-agents-runtime/src/test/java/com/example/bedrockagents/runtime/InvokeFlowTest.java @@ -0,0 +1,29 @@ +// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +// SPDX-License-Identifier: Apache-2.0 +package com.example.bedrockagents.runtime; + +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Tag; + +import com.example.bedrockagents.runtime.InvokeFlow; + +public class InvokeFlowTest { + + // Fill in with the Prompt Flow Id and Alias. + String flowId = "MKM4D2W4RG"; + String flowAliasId = "JOIUMSMGLC"; + String inputText = "Is putting pineapple on pizza a good idea?"; + String[] args = {flowId, flowAliasId, inputText}; + + @Test + @Order(1) + @Tag("IntegrationTest") + void assertInvokeFlowAnswer() { + String response = InvokeFlow.invokeFlowString(args); + assertNotNull(response); + assertFalse(response.isEmpty()); + System.out.println("Test 1 passed."); + } +} From 302a236f230495d73840117b520bcdd44a8753ac Mon Sep 17 00:00:00 2001 From: emanuele-levi Date: Wed, 6 Nov 2024 10:42:25 +0000 Subject: [PATCH 2/5] Update README.md to reference InvokeFlow example --- javav2/example_code/bedrock-agents-runtime/README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/javav2/example_code/bedrock-agents-runtime/README.md b/javav2/example_code/bedrock-agents-runtime/README.md index 79df98a9557..0a502b7bb98 100644 --- a/javav2/example_code/bedrock-agents-runtime/README.md +++ b/javav2/example_code/bedrock-agents-runtime/README.md @@ -1,4 +1,4 @@ -# Agents for Amazon Bedrock code examples for the SDK for Java 2.x +# Amazon Bedrock Agents Runtime code examples for the SDK for Java 2.x ## Overview @@ -7,7 +7,7 @@ Shows how to use the AWS SDK for Java 2.x to work with Amazon Bedrock Agents, Am -_Agents for Amazon Bedrock offer you the ability to build and configure autonomous agents in your application._ +_Amazon Bedrock Agents Runtime offers you the ability to interact with your Amazon Bedrock Agents, Amazon Bedrock Knowledge Bases, and Amazon Bedrock Prompt Flows._ ## ⚠ Important @@ -21,6 +21,8 @@ _Agents for Amazon Bedrock offer you the ability to build and configure autonomo ## Code examples +* [InvokeFlow](src/main/java/com/example/bedrockagents/runtime/InvokeFlow.java#L30) + ### Prerequisites For prerequisites, see the [README](../../README.md#Prerequisites) in the `javav2` folder. @@ -68,4 +70,4 @@ in the `javav2` folder. Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. -SPDX-License-Identifier: Apache-2.0 \ No newline at end of file +SPDX-License-Identifier: Apache-2.0 From 63b7bb78b923c5f8333dbdd7a3daebbd2312d0bd Mon Sep 17 00:00:00 2001 From: emanuele-levi Date: Wed, 6 Nov 2024 13:08:34 +0000 Subject: [PATCH 3/5] Update README.md with latest relevant links --- javav2/example_code/bedrock-agents-runtime/README.md | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/javav2/example_code/bedrock-agents-runtime/README.md b/javav2/example_code/bedrock-agents-runtime/README.md index 0a502b7bb98..7af112c769f 100644 --- a/javav2/example_code/bedrock-agents-runtime/README.md +++ b/javav2/example_code/bedrock-agents-runtime/README.md @@ -59,9 +59,11 @@ in the `javav2` folder. ## Additional resources -- [Agents for Amazon Bedrock User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/agents.html) -- [Agents for Amazon Bedrock API Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Agents_for_Amazon_Bedrock.html) -- [SDK for Java 2.x Agents for Amazon Bedrock reference](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/bedrock-agent/package-summary.html) +- [Amazon Bedrock Agents User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/agents.html) +- [Amazon Bedrock Knowledge Bases User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/knowledge-base.html) +- [Amazon Bedrock Propt Flow User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/flows.html) +- [Amazon Bedrock Agents Runtime API Reference]([https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Agents_for_Amazon_Bedrock.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Agents_for_Amazon_Bedrock_Runtime.html)) +- [SDK for Java 2.x Amazon Bedrock Agents Runtime reference](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/bedrockagentruntime/package-summary.html) From 3a3319f9f1d44aff3561f0836eb858641d49fc1b Mon Sep 17 00:00:00 2001 From: emanuele-levi Date: Wed, 6 Nov 2024 13:10:37 +0000 Subject: [PATCH 4/5] Fixed broken link in README.md --- javav2/example_code/bedrock-agents-runtime/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/javav2/example_code/bedrock-agents-runtime/README.md b/javav2/example_code/bedrock-agents-runtime/README.md index 7af112c769f..e2cf0df239e 100644 --- a/javav2/example_code/bedrock-agents-runtime/README.md +++ b/javav2/example_code/bedrock-agents-runtime/README.md @@ -62,7 +62,7 @@ in the `javav2` folder. - [Amazon Bedrock Agents User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/agents.html) - [Amazon Bedrock Knowledge Bases User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/knowledge-base.html) - [Amazon Bedrock Propt Flow User Guide](https://docs.aws.amazon.com/bedrock/latest/userguide/flows.html) -- [Amazon Bedrock Agents Runtime API Reference]([https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Agents_for_Amazon_Bedrock.html](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Agents_for_Amazon_Bedrock_Runtime.html)) +- [Amazon Bedrock Agents Runtime API Reference](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_Operations_Agents_for_Amazon_Bedrock_Runtime.html) - [SDK for Java 2.x Amazon Bedrock Agents Runtime reference](https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/services/bedrockagentruntime/package-summary.html) From bd8e731b6d19cdd54275a1e3be73f4205169bd92 Mon Sep 17 00:00:00 2001 From: emanuele-levi Date: Wed, 6 Nov 2024 13:40:11 +0000 Subject: [PATCH 5/5] Removed non-existent test ids --- .../com/example/bedrockagents/runtime/InvokeFlowTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/javav2/example_code/bedrock-agents-runtime/src/test/java/com/example/bedrockagents/runtime/InvokeFlowTest.java b/javav2/example_code/bedrock-agents-runtime/src/test/java/com/example/bedrockagents/runtime/InvokeFlowTest.java index 5dfe944ef38..5d10b33f06e 100644 --- a/javav2/example_code/bedrock-agents-runtime/src/test/java/com/example/bedrockagents/runtime/InvokeFlowTest.java +++ b/javav2/example_code/bedrock-agents-runtime/src/test/java/com/example/bedrockagents/runtime/InvokeFlowTest.java @@ -12,8 +12,8 @@ public class InvokeFlowTest { // Fill in with the Prompt Flow Id and Alias. - String flowId = "MKM4D2W4RG"; - String flowAliasId = "JOIUMSMGLC"; + String flowId = ""; + String flowAliasId = ""; String inputText = "Is putting pineapple on pizza a good idea?"; String[] args = {flowId, flowAliasId, inputText};