Skip to content

Added KB examples and tests and updated metadata file #7451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
65 changes: 65 additions & 0 deletions .doc_gen/metadata/bedrock-agent_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -521,3 +521,68 @@ bedrock-agent_GettingStartedWithBedrockPrompts:
services:
bedrock-agent: {CreatePrompt, CreatePromptVersion, DeletePrompt}
bedrock-runtime: {Converse}

bedrock-agent_CreateKnowledgeBase:
languages:
Python:
versions:
- sdk_version: 3
github: python/example_code/bedrock-agent
excerpts:
- description: Create an Amazon Bedrock knowledge base.
snippet_tags:
- python.example_code.bedrock-agent.create_knowledge_base
services:
bedrock-agent: {CreateKnowledgeBase}

bedrock-agent_GetKnowledgeBase:
languages:
Python:
versions:
- sdk_version: 3
github: python/example_code/bedrock-agent
excerpts:
- description: Get an Amazon Bedrock knowledge base.
snippet_tags:
- python.example_code.bedrock-agent.get_knowledge_base
services:
bedrock-agent: {GetKnowledgeBase}

bedrock-agent_UpdateKnowledgeBase:
languages:
Python:
versions:
- sdk_version: 3
github: python/example_code/bedrock-agent
excerpts:
- description: Update an Amazon Bedrock knowledge base.
snippet_tags:
- python.example_code.bedrock-agent.update_knowledge_base
services:
bedrock-agent: {UpdateKnowledgeBase}

bedrock-agent_DeleteKnowledgeBase:
languages:
Python:
versions:
- sdk_version: 3
github: python/example_code/bedrock-agent
excerpts:
- description: Delete an Amazon Bedrock knowledge base.
snippet_tags:
- python.example_code.bedrock-agent.delete_knowledge_base
services:
bedrock-agent: {DeleteKnowledgeBase}

bedrock-agent_ListKnowledgeBases:
languages:
Python:
versions:
- sdk_version: 3
github: python/example_code/bedrock-agent
excerpts:
- description: List Amazon Bedrock knowledge Bases.
snippet_tags:
- python.example_code.bedrock-agent.list_knowledge_bases
services:
bedrock-agent: {ListKnowledgeBases}
142 changes: 70 additions & 72 deletions python/example_code/bedrock-agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@

## Overview

Shows how to use the AWS SDK for Python (Boto3) to work with Amazon Bedrock agents, flows, and managed prompts.
Shows how to use the AWS SDK for Python (Boto3) to work with Amazon Bedrock Agents.

<!--custom.overview.start-->
<!--custom.overview.end-->

_Amazon Bedrock Agents 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).
- 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).

<!--custom.important.start-->
<!--custom.important.end-->
Expand All @@ -43,47 +41,46 @@ Code excerpts that show you how to call individual service functions.
- [CreateAgent](bedrock_agent_wrapper.py#L32)
- [CreateAgentActionGroup](bedrock_agent_wrapper.py#L61)
- [CreateAgentAlias](bedrock_agent_wrapper.py#L96)
- [CreateFlow](flows/flow.py#L18)
- [CreateFlowAlias](flows/flow_alias.py#L15)
- [CreateFlowVersion](flows/flow_version.py#L18)
- [CreateKnowledgeBase](knowledge_bases/knowledge_base.py#L29)
- [CreatePrompt](prompts/prompt.py#L18)
- [CreatePromptVersion](prompts/prompt.py#L84)
- [DeleteAgent](bedrock_agent_wrapper.py#L118)
- [DeleteAgentAlias](bedrock_agent_wrapper.py#L139)
- [DeleteFlow](flows/flow.py#L155)
- [DeleteFlowAlias](flows/flow_alias.py#L98)
- [DeleteFlowVersion](flows/flow_version.py#L91)
- [DeleteKnowledgeBase](knowledge_bases/knowledge_base.py#L165)
- [DeletePrompt](prompts/prompt.py#L159)
- [GetAgent](bedrock_agent_wrapper.py#L161)
- [GetFlow](flows/flow.py#L192)
- [GetFlowVersion](flows/flow_version.py#L54)
- [GetKnowledgeBase](knowledge_bases/knowledge_base.py#L86)
- [GetPrompt](prompts/prompt.py#L124)
- [ListAgentActionGroups](bedrock_agent_wrapper.py#L208)
- [ListAgentKnowledgeBases](bedrock_agent_wrapper.py#L237)
- [ListAgents](bedrock_agent_wrapper.py#L185)
- [ListFlowAliases](flows/flow_alias.py#L132)
- [ListFlowVersions](flows/flow_version.py#L128)
- [ListFlows](flows/flow.py#L229)
- [ListKnowledgeBases](knowledge_bases/knowledge_base.py#L195)
- [ListPrompts](prompts/prompt.py#L191)
- [PrepareAgent](bedrock_agent_wrapper.py#L266)
- [InvokeFlow](flows/run_flow.py#L23)
- [CreateFlow](flows/flow.py#L18)
- [PrepareFlow](flows/flow.py#L58)
- [PrepareFlow](flows/flow.py#L58)
- [UpdateFlow](flows/flow.py#L112)
- [DeleteFlow](flows/flow.py#L156)
- [GetFlow](flows/flow.py#L192)
- [ListFlows](flows/flow.py#L229)
- [CreateFlowVersion](flows/flow_version.py#L18)
- [GetFlowVersion](flows/flow_version.py#L54)
- [DeleteFlowVersion](flows/flow_version.py#L91)
- [ListFlowVersions](flows/flow_version.py#L128)
- [CreateFlowAlias](flows/flow_alias.py#L15)
- [UpdateFlowAlias](flows/flow_alias.py#L55)
- [DeleteFlowAlias](flows/flow_alias.py#L98)
- [ListFlowAliases](flows/flow_alias.py#L132)
- [FlowConversation](flows/flow-conversation.py)
- [CreatePrompt](prompts/prompt.py#L32)
- [CreatePromptVersion](prompts/prompt.py#L61)
- [GetPrompt](prompts/prompt.py#L71)
- [DeletePrompt](prompts/prompt.py#L154)
- [ListPrompts](prompts/prompt.py#L187)



- [UpdateFlowAlias](flows/flow_alias.py#L55)
- [UpdateKnowledgeBase](knowledge_bases/knowledge_base.py#L116)

### Scenarios

Code examples that show you how to accomplish a specific task by calling multiple
functions within the same service.

- [Create and invoke an agent](scenario_get_started_with_agents.py)
- [Create and invoke a flow](flows/playlist_flow.py)
- [Create and invoke a managed prompt](prompts/scenario_get_started_with_prompts.py)

- [Create and invoke an agent](scenario_get_started_with_agents.py)

<!--custom.examples.start-->
<!--custom.examples.end-->
Expand All @@ -92,69 +89,60 @@ functions within the same service.

### Instructions


<!--custom.instructions.start-->
<!--custom.instructions.end-->



#### Create and invoke an agent
#### Create and invoke a flow

This example shows you how to do the following:

- Create an execution role for the agent.
- Create the agent and deploy a DRAFT version.
- Create a Lambda function that implements the agent's capabilities.
- Create an action group that connects the agent to the Lambda function.
- Deploy the fully configured agent.
- Invoke the agent with user-provided prompts.
- Create an execution role for the flow.
- Create the flow.
- Deploy the fully configured flow.
- Invoke the flow with user-provided prompts.
- Delete all created resources.

<!--custom.scenario_prereqs.bedrock-agent_GettingStartedWithBedrockAgents.start-->
<!--custom.scenario_prereqs.bedrock-agent_GettingStartedWithBedrockAgents.end-->

Start the example by running the following at a command prompt:

```
python scenario_get_started_with_agents.py
```


<!--custom.scenarios.bedrock-agent_GettingStartedWithBedrockAgents.start-->
<!--custom.scenarios.bedrock-agent_GettingStartedWithBedrockAgents.end-->
<!--custom.scenario_prereqs.bedrock-agent_GettingStartedWithBedrockFlows.start-->


#### Create and invoke a flow

Shows how to create a simple flow that generates music playlists.
The flow includes a prompt node that generates a playlist for a chosen genre
and number of songs. The example creates the nodes and permissions
for the flow.

<!--custom.scenario_prereqs.bedrock-agent_GettingStartedWithBedrockFlows.end-->

Start the example by running the following at a command prompt:

```
python flows/playlist_flow.py
```

<!--custom.scenarios.bedrock-agent_GettingStartedWithBedrockFlows.start-->

When prompted, enter the genre of music and the number of songs you want
in the playlist.
Optionally, the script can delete the resources that it creates. If you want to use the flow later, such as in the Amazon Bedrock console, enter `n` when the script prompts you to delete resources. Note that you will then need to manually delete the resources.

<!--custom.scenarios.bedrock-agent_GettingStartedWithBedrockFlows.end-->

#### Create and invoke a managed prompt

This example shows you how to do the following:

#### List flows
- Create a managed prompt.
- Create a version of the prompt.
- Invoke the prompt using the version.
- Clean up resources (optional).

Shows how to List Amazon Bedrock flows, versions of a flow, and aliases of a flow.
<!--custom.scenario_prereqs.bedrock-agent_GettingStartedWithBedrockPrompts.start-->
<!--custom.scenario_prereqs.bedrock-agent_GettingStartedWithBedrockPrompts.end-->

Start the example by running the following at a command prompt:

```
python flows/list_flows.py
python prompts/scenario_get_started_with_prompts.py
```
The example first lists the flows in the current AWS Region. It
then prompts for a flow ID, which you can get from the list of flows. Finally, the example lists the flow versions and flow aliases for the flow ID that you entered.

#### Create and invoke a managed prompt
<!--custom.scenarios.bedrock-agent_GettingStartedWithBedrockPrompts.start-->

This example shows you how to do the following:

Expand Down Expand Up @@ -184,29 +172,39 @@ You can also specify a different AWS region or model ID:
python prompts/scenario_get_started_with_prompts.py --region us-west-2 --model-id anthropic.claude-3-sonnet-20240229-v1:0
```

#### List prompts
<!--custom.scenarios.bedrock-agent_GettingStartedWithBedrockPrompts.end-->

#### Create and invoke an agent

This example shows you how to do the following:

- Create an execution role for the agent.
- Create the agent and deploy a DRAFT version.
- Create a Lambda function that implements the agent's capabilities.
- Create an action group that connects the agent to the Lambda function.
- Deploy the fully configured agent.
- Invoke the agent with user-provided prompts.
- Delete all created resources.

Shows how to list Amazon Bedrock managed prompts and versions of a prompt.
<!--custom.scenario_prereqs.bedrock-agent_GettingStartedWithBedrockAgents.start-->
<!--custom.scenario_prereqs.bedrock-agent_GettingStartedWithBedrockAgents.end-->

Start the example by running the following at a command prompt:

```
python prompts/list_prompts.py
python scenario_get_started_with_agents.py
```

The example first lists the prompts in the current AWS Region.

<!--custom.scenarios.bedrock-agent_GettingStartedWithBedrockAgents.start-->
<!--custom.scenarios.bedrock-agent_GettingStartedWithBedrockAgents.end-->

### 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 `python` folder.



<!--custom.tests.start-->
<!--custom.tests.end-->

Expand Down
Loading
Loading