Skip to content

Commit

Permalink
Python: Requested additions to Bedrock Agents scenario (awsdocs#5815)
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisTraub authored Jan 11, 2024
1 parent 54c3b82 commit 1edc351
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 54 deletions.
38 changes: 36 additions & 2 deletions .doc_gen/metadata/bedrock-agent_metadata.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,38 @@ bedrock-agent_ListAgents:
services:
bedrock-agent: {ListAgents}

bedrock-agent_ListAgentActionGroups:
title: List the action groups for a &BR; agent using an &AWS; SDK
title_abbrev: List the action groups for an agent
synopsis: list the action groups for a &BR; agent.
languages:
Python:
versions:
- sdk_version: 3
github: python/example_code/bedrock-agent
excerpts:
- description: List the action groups for an agent.
snippet_tags:
- python.example_code.bedrock-agent.ListAgentActionGroups
services:
bedrock-agent: {ListAgentActionGroups}

bedrock-agent_ListAgentKnowledgeBases:
title: List the knowledge bases associated with a &BR; agent using an &AWS; SDK
title_abbrev: List the knowledge bases associated with an agent
synopsis: list the knowledge bases associated with a &BR; agent.
languages:
Python:
versions:
- sdk_version: 3
github: python/example_code/bedrock-agent
excerpts:
- description: List the knowledge bases associated with an agent.
snippet_tags:
- python.example_code.bedrock-agent.ListAgentKnowledgeBases
services:
bedrock-agent: {ListAgentKnowledgeBases}

bedrock-agent_PrepareAgent:
title: Prepare an &BR; agent using an &AWS; SDK
title_abbrev: Prepare an agent
Expand All @@ -128,7 +160,7 @@ bedrock-agent_PrepareAgent:
bedrock-agent: {PrepareAgent}
bedrock-agent_GettingStartedWithBedrockAgents:
title: An end-to-end example showing how to create and invoke &BR; agents using an &AWS; SDK
title_abbrev: Create and invoke agents
title_abbrev: Create and invoke an agent
synopsis_list:
- Create an execution role for the agent.
- Create the agent and deploy a DRAFT version.
Expand All @@ -144,7 +176,7 @@ bedrock-agent_GettingStartedWithBedrockAgents:
- sdk_version: 3
github: python/example_code/bedrock-agent
excerpts:
- description:
- description: Create and invoke an agent.
snippet_tags:
- python.example_code.bedrock-agent.Scenario_GettingStartedBedrockAgents
services:
Expand All @@ -156,7 +188,9 @@ bedrock-agent_GettingStartedWithBedrockAgents:
DeleteAgent,
DeleteAgentAlias,
GetAgent,
ListAgentActionGroups,
ListAgents,
ListAgentKnowledgeBases,
PrepareAgent,
}
bedrock-agent-runtime:
Expand Down
16 changes: 0 additions & 16 deletions python/example_code/bedrock-agent-runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ python -m pip install -r requirements.txt
```

<!--custom.prerequisites.start-->

> ⚠ You must request access to a model before you can use it. If you try to use the model (with the API or console) before you have requested access to it, you will receive an error message. For more information, see [Model access](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access.html).
<!--custom.prerequisites.end-->

### Single actions
Expand All @@ -45,19 +42,6 @@ Code excerpts that show you how to call individual service functions.


<!--custom.examples.start-->

#### Create and invoke agents

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.

<!--custom.examples.end-->

## Run the examples
Expand Down
8 changes: 5 additions & 3 deletions python/example_code/bedrock-agent/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,17 @@ Code excerpts that show you how to call individual service functions.
- [Delete an agent](bedrock_agent_wrapper.py#L119) (`DeleteAgent`)
- [Delete an agent alias](bedrock_agent_wrapper.py#L140) (`DeleteAgentAlias`)
- [Get information about an agent](bedrock_agent_wrapper.py#L162) (`GetAgent`)
- [List the action groups for an agent](bedrock_agent_wrapper.py#L209) (`ListAgentActionGroups`)
- [List the agents](bedrock_agent_wrapper.py#L186) (`ListAgents`)
- [Prepare an agent](bedrock_agent_wrapper.py#L209) (`PrepareAgent`)
- [List the knowledge bases associated with an agent](bedrock_agent_wrapper.py#L238) (`ListAgentKnowledgeBases`)
- [Prepare an agent](bedrock_agent_wrapper.py#L267) (`PrepareAgent`)

### Scenarios

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

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


<!--custom.examples.start-->
Expand All @@ -68,7 +70,7 @@ functions within the same service.



#### Create and invoke agents
#### Create and invoke an agent

This example shows you how to do the following:

Expand Down
58 changes: 58 additions & 0 deletions python/example_code/bedrock-agent/bedrock_agent_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,64 @@ def list_agents(self):

# snippet-end:[python.example_code.bedrock-agent.ListAgents]

# snippet-start:[python.example_code.bedrock-agent.ListAgentActionGroups]
def list_agent_action_groups(self, agent_id, agent_version):
"""
List the action groups for a version of an Amazon Bedrock Agent.
:param agent_id: The unique identifier of the agent.
:param agent_version: The version of the agent.
:return: The list of action group summaries for the version of the agent.
"""

try:
action_groups = []

paginator = self.client.get_paginator("list_agent_action_groups")
for page in paginator.paginate(
agentId=agent_id,
agentVersion=agent_version,
PaginationConfig={"PageSize": 10},
):
action_groups.extend(page["actionGroupSummaries"])

except ClientError as e:
logger.error(f"Couldn't list action groups. {e}")
raise
else:
return action_groups

# snippet-end:[python.example_code.bedrock-agent.ListAgentActionGroups]

# snippet-start:[python.example_code.bedrock-agent.ListAgentKnowledgeBases]
def list_agent_knowledge_bases(self, agent_id, agent_version):
"""
List the knowledge bases associated with a version of an Amazon Bedrock Agent.
:param agent_id: The unique identifier of the agent.
:param agent_version: The version of the agent.
:return: The list of knowledge base summaries for the version of the agent.
"""

try:
knowledge_bases = []

paginator = self.client.get_paginator("list_agent_knowledge_bases")
for page in paginator.paginate(
agentId=agent_id,
agentVersion=agent_version,
PaginationConfig={"PageSize": 10},
):
knowledge_bases.extend(page["agentKnowledgeBaseSummaries"])

except ClientError as e:
logger.error(f"Couldn't list knowledge bases. {e}")
raise
else:
return knowledge_bases

# snippet-end:[python.example_code.bedrock-agent.ListAgentKnowledgeBases]

# snippet-start:[python.example_code.bedrock-agent.PrepareAgent]
def prepare_agent(self, agent_id):
"""
Expand Down
Loading

0 comments on commit 1edc351

Please sign in to comment.