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..e2cf0df239e --- /dev/null +++ b/javav2/example_code/bedrock-agents-runtime/README.md @@ -0,0 +1,75 @@ +# Amazon Bedrock Agents Runtime 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. + + + + +_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 + +* 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 + +* [InvokeFlow](src/main/java/com/example/bedrockagents/runtime/InvokeFlow.java#L30) + +### 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 + +- [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_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) + + + + +--- + +Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 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..5d10b33f06e --- /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 = ""; + String flowAliasId = ""; + 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."); + } +}