To demonstrate use of a Bedrock Agents Action Group as a reusable tool across multiple Bedrock Agents, here we have created a lightweight StockData tool. It is able to take a stock ticker and return stock price history data by leveraging Yahoo Finance (hyperlink here). It can be easily extended to cover many other functions as well. The Portfolio Assistant Agent supervisor example demonstrates reusing this StockData tool. Note the implementation of the Lambda function currently ignores which Agent was used to call the Action Group and is not tightly coupled to any single Agent.
This tool consists of an AWS Lambda function named "stock_data_lookup" to retrieve stock data using the yfinance Python library. The "stock_data_lookup" Lambda function can then be invoked to retrieve stock data for a given ticker symbol. Here's a breakdown:
- AgentLambdaFunction: This is the AWS Lambda function that implements the "stock_data_lookup" functionality. It uses the Python 3.11 runtime and attaches two layers: a custom layer, and the AWSSDKPandas layer managed by AWS.
- AgentLambdaRole: This is an AWS Identity and Access Management (IAM) role that grants the Lambda function the necessary permissions to execute.
- AgentAliasLambdaPermission and AgentLambdaPermission: These resources grant permissions for Amazon Bedrock Agents to invoke the Lambda function.
Deploy stock_data_stack.yaml
Region | development.yaml |
---|---|
us-east-1 | |
us-west-2 |
from src.utils.bedrock_agent import (
Agent,
region,
account_id,
)
import uuid
stock_data_agent = Agent.direct_create(
name="stock_data_agent",
role="Financial Data Collector",
goal="Retrieve accurate stock trends for a given ticker.",
instructions="Specialist in real-time financial data extraction.",
tool_code=f"arn:aws:lambda:{region}:{account_id}:function:stock_data_lookup",
tool_defs=[
{ # lambda_layers: yfinance_layer.zip, numpy_layer.zip
"name": "stock_data_lookup",
"description": "Gets the 1 month stock price history for a given stock ticker, formatted as JSON",
"parameters": {
"ticker": {
"description": "The ticker to retrieve price history for",
"type": "string",
"required": True,
}
},
}
],
)
response = stock_data_agent.invoke(
input_text="What is the stock trend for AMZN?",
session_id: str = str(uuid.uuid1()),
enable_trace: bool = False,
)
print(response)
- Open the CloudFormation console.
- Select the stack
StockDataLookup
you created, then click Delete. Wait for the stack to be deleted.
See CONTRIBUTING for more information.
This project is licensed under the Apache-2.0 License.s