In this project, we will set up an Amazon Bedrock agent with an action group that dynamically creates an investment company portfolio based on specific parameters. The agent also has Q&A capabilities for Federal Open Market Committee (FOMC) reports, leveraging a Streamlit framework for the user interface. Additionally, this exercise includes a method for sending emails, although the email functionality will not be fully configured.
For those who prefer an Infrastructure-as-Code (IaC) solution, we provide an AWS CloudFormation template that will deploy most of the necessary resources, including S3 buckets, an action group, and a Lambda function. You will still need to manually create the knowledge base with the already provided resources, but the steps are relatively straightforward. If you would like to deploy this workflow via AWS CloudFormation, please refer to the workshop guide here.
Alternatively, this README will walk you through the step-by-step process to set up the Amazon Bedrock agent manually using the AWS Console.
theRegion
in the invoke_agent.py
file code. knowledgebase-bedrock-agent-{alias}
. We will use the default settings. Next, we will download the domain data from here. On your computer, open terminal or command prompt, and run the following curl
commands to download the data:
For Mac
curl https://raw.githubusercontent.com/build-on-aws/bedrock-agents-streamlit/main/S3docs/fomcminutes20230201.pdf --output ~/Documents/fomcminutes20230201.pdf
curl https://raw.githubusercontent.com/build-on-aws/bedrock-agents-streamlit/main/S3docs/fomcminutes20230322.pdf --output ~/Documents/fomcminutes20230322.pdf
curl https://raw.githubusercontent.com/build-on-aws/bedrock-agents-streamlit/main/S3docs/fomcminutes20230614.pdf --output ~/Documents/fomcminutes20230614.pdf
curl https://raw.githubusercontent.com/build-on-aws/bedrock-agents-streamlit/main/S3docs/fomcminutes20230726.pdf --output ~/Documents/fomcminutes20230726.pdf
curl https://raw.githubusercontent.com/build-on-aws/bedrock-agents-streamlit/main/S3docs/fomcminutes20230920.pdf --output ~/Documents/fomcminutes20230920.pdf
curl https://raw.githubusercontent.com/build-on-aws/bedrock-agents-streamlit/main/S3docs/fomcminutes20231101.pdf --output ~/Documents/fomcminutes20231101.pdf
For Windows
curl https://raw.githubusercontent.com/build-on-aws/bedrock-agents-streamlit/main/S3docs/fomcminutes20230201.pdf --output %USERPROFILE%\Documents\fomcminutes20230201.pdf
curl https://raw.githubusercontent.com/build-on-aws/bedrock-agents-streamlit/main/S3docs/fomcminutes20230322.pdf --output %USERPROFILE%\Documents\fomcminutes20230322.pdf
curl https://raw.githubusercontent.com/build-on-aws/bedrock-agents-streamlit/main/S3docs/fomcminutes20230614.pdf --output %USERPROFILE%\Documents\fomcminutes20230614.pdf
curl https://raw.githubusercontent.com/build-on-aws/bedrock-agents-streamlit/main/S3docs/fomcminutes20230726.pdf --output %USERPROFILE%\Documents\fomcminutes20230726.pdf
curl https://raw.githubusercontent.com/build-on-aws/bedrock-agents-streamlit/main/S3docs/fomcminutes20230920.pdf --output %USERPROFILE%\Documents\fomcminutes20230920.pdf
curl https://raw.githubusercontent.com/build-on-aws/bedrock-agents-streamlit/main/S3docs/fomcminutes20231101.pdf --output %USERPROFILE%\Documents\fomcminutes20231101.pdf
Also, you have the option to download the .pdf files from here. These files will download to your Documents folder. Upload these files to S3 bucket knowledgebase-bedrock-agent-{alias}
. These files are the Federal Open Market Committee documents describing monetary policy decisions made at the Federal Reserved board meetings. The documents include discussions of economic conditions, policy directives to the Federal Reserve Bank of New York for open market operations, and votes on the federal funds rate. More information can be found here. Once uploaded, please select one of the documents to open and review the content.
Select the checkbox for the base model columns Amazon: Titan Embeddings G1 - Text and Anthropic: Claude 3 Haiku. This will provide you access to the required models. After, scroll down to the bottom right and select Request model access.
After, verify that the Access status of the Models are green with Access granted.
knowledgebase-bedrock-agent-{alias}
to this knowledge base.PortfolioCreator-actions
. import json
def lambda_handler(event, context):
print(event)
# Mock data for demonstration purposes
company_data = [
#Technology Industry
{"companyId": 1, "companyName": "TechStashNova Inc.", "industrySector": "Technology", "revenue": 10000, "expenses": 3000, "profit": 7000, "employees": 10},
{"companyId": 2, "companyName": "QuantumPirateLeap Technologies", "industrySector": "Technology", "revenue": 20000, "expenses": 4000, "profit": 16000, "employees": 10},
{"companyId": 3, "companyName": "CyberCipherSecure IT", "industrySector": "Technology", "revenue": 30000, "expenses": 5000, "profit": 25000, "employees": 10},
{"companyId": 4, "companyName": "DigitalMyricalDreams Gaming", "industrySector": "Technology", "revenue": 40000, "expenses": 6000, "profit": 34000, "employees": 10},
{"companyId": 5, "companyName": "NanoMedNoLand Pharmaceuticals", "industrySector": "Technology", "revenue": 50000, "expenses": 7000, "profit": 43000, "employees": 10},
{"companyId": 6, "companyName": "RoboSuperBombTech Industries", "industrySector": "Technology", "revenue": 60000, "expenses": 8000, "profit": 52000, "employees": 12},
{"companyId": 7, "companyName": "FuturePastNet Solutions", "industrySector": "Technology", "revenue": 60000, "expenses": 9000, "profit": 51000, "employees": 10},
{"companyId": 8, "companyName": "InnovativeCreativeAI Corp", "industrySector": "Technology", "revenue": 65000, "expenses": 10000, "profit": 55000, "employees": 15},
{"companyId": 9, "companyName": "EcoLeekoTech Energy", "industrySector": "Technology", "revenue": 70000, "expenses": 11000, "profit": 59000, "employees": 10},
{"companyId": 10, "companyName": "TechyWealthHealth Systems", "industrySector": "Technology", "revenue": 80000, "expenses": 12000, "profit": 68000, "employees": 10},
#Real Estate Industry
{"companyId": 11, "companyName": "LuxuryToNiceLiving Real Estate", "industrySector": "Real Estate", "revenue": 90000, "expenses": 13000, "profit": 77000, "employees": 10},
{"companyId": 12, "companyName": "UrbanTurbanDevelopers Inc.", "industrySector": "Real Estate", "revenue": 100000, "expenses": 14000, "profit": 86000, "employees": 10},
{"companyId": 13, "companyName": "SkyLowHigh Towers", "industrySector": "Real Estate", "revenue": 110000, "expenses": 15000, "profit": 95000, "employees": 18},
{"companyId": 14, "companyName": "GreenBrownSpace Properties", "industrySector": "Real Estate", "revenue": 120000, "expenses": 16000, "profit": 104000, "employees": 10},
{"companyId": 15, "companyName": "ModernFutureHomes Ltd.", "industrySector": "Real Estate", "revenue": 130000, "expenses": 17000, "profit": 113000, "employees": 10},
{"companyId": 16, "companyName": "CityCountycape Estates", "industrySector": "Real Estate", "revenue": 140000, "expenses": 18000, "profit": 122000, "employees": 10},
{"companyId": 17, "companyName": "CoastalFocalRealty Group", "industrySector": "Real Estate", "revenue": 150000, "expenses": 19000, "profit": 131000, "employees": 10},
{"companyId": 18, "companyName": "InnovativeModernLiving Spaces", "industrySector": "Real Estate", "revenue": 160000, "expenses": 20000, "profit": 140000, "employees": 10},
{"companyId": 19, "companyName": "GlobalRegional Properties Alliance", "industrySector": "Real Estate", "revenue": 170000, "expenses": 21000, "profit": 149000, "employees": 11},
{"companyId": 20, "companyName": "NextGenPast Residences", "industrySector": "Real Estate", "revenue": 180000, "expenses": 22000, "profit": 158000, "employees": 260}
]
def get_named_parameter(event, name):
return next(item for item in event['parameters'] if item['name'] == name)['value']
def companyResearch(event):
companyName = get_named_parameter(event, 'name').lower()
print("NAME PRINTED: ", companyName)
for company_info in company_data:
if company_info["companyName"].lower() == companyName:
return company_info
return None
def createPortfolio(event, company_data):
numCompanies = int(get_named_parameter(event, 'numCompanies'))
industry = get_named_parameter(event, 'industry').lower()
industry_filtered_companies = [company for company in company_data
if company['industrySector'].lower() == industry]
sorted_companies = sorted(industry_filtered_companies, key=lambda x: x['profit'], reverse=True)
top_companies = sorted_companies[:numCompanies]
return top_companies
def sendEmail(event, company_data):
emailAddress = get_named_parameter(event, 'emailAddress')
fomcSummary = get_named_parameter(event, 'fomcSummary')
# Retrieve the portfolio data as a string
portfolioDataString = get_named_parameter(event, 'portfolio')
# Prepare the email content
email_subject = "Portfolio Creation Summary and FOMC Search Results"
#email_body = f"FOMC Search Summary:\n{fomcSummary}\n\nPortfolio Details:\n{json.dumps(portfolioData, indent=4)}"
# Email sending code here (commented out for now)
return "Email sent successfully to {}".format(emailAddress)
result = ''
response_code = 200
action_group = event['actionGroup']
api_path = event['apiPath']
print("api_path: ", api_path )
if api_path == '/companyResearch':
result = companyResearch(event)
elif api_path == '/createPortfolio':
result = createPortfolio(event, company_data)
elif api_path == '/sendEmail':
result = sendEmail(event, company_data)
else:
response_code = 404
result = f"Unrecognized api path: {action_group}::{api_path}"
response_body = {
'application/json': {
'body': result
}
}
action_response = {
'actionGroup': event['actionGroup'],
'apiPath': event['apiPath'],
'httpMethod': event['httpMethod'],
'httpStatusCode': response_code,
'responseBody': response_body
}
api_response = {'messageVersion': '1.0', 'response': action_response}
return api_response
Select AWS service, then use the following settings to configure the resource based policy:
Service - Other
Statement ID - allow-bedrock-agent
Principal - bedrock.amazonaws.com
Source ARN - arn:aws:bedrock:us-west-2:{account-id}:agent/*
- (Please note, AWS recommends least privilage so only an allowed agent can invoke this Lambda function. A * at the end of the ARN grants any agent in the account access to invoke this Lambda. Ideally, we would not use this in a production environment.)
Action - lambda:InvokeFunction
Navigate to the Bedrock console. Go to the toggle on the left, and under Builder tools select Agents. Provide an agent name, like PortfolioCreator then create the agent.
The agent description is optional, and we will use the default new service role. For the model, select Anthropic: Claude 3 Haiku. Next, provide the following instruction for the agent:
You are an investment analyst. Your job is to assist in investment analysis, create research summaries, generate profitable company portfolios, and facilitate communication through emails. Here is how I want you to think step by step:
1. Portfolio Creation:
Analyze the user's request to extract key information such as the desired number of companies and industry.
Based on the criteria from the request, create a portfolio of companies. Use the template provided to format the portfolio.
2. Company Research and Document Summarization:
For each company in the portfolio, conduct detailed research to gather relevant financial and operational data.
When a document, like the FOMC report, is mentioned, retrieve the document and provide a concise summary.
3. Email Communication:
Using the email template provided, format an email that includes the newly created company portfolio and any summaries of important documents.
Utilize the provided tools to send an email upon request, That includes a summary of provided responses and portfolios created.
After, scroll to the top and Save
The instructions for the Generative AI Investment Analyst Tool outlines a comprehensive framework designed to assist in investment analysis. This tool is tasked with creating tailored portfolios of companies based on specific industry criteria, conducting thorough research on these companies, and summarizing relevant financial documents. Additionally, the tool formats and sends professional emails containing the portfolios and document summaries. The process involves continuous adaptation to user feedback and maintaining a contextual understanding of ongoing requests to ensure accurate and efficient responses.
Next, we will add an action group. Scroll down to Action groups
then select Add.
Call the action group PortfolioCreator-actions
. We will set the Action group type
to Define with API schemas. Action group invocations
should be set to select an existing Lambda function. For the Lambda function, select PortfolioCreator-actions
.
For the Action group Schema
, we will choose Define via in-line schema editor. Replace the default schema in the In-line OpenAPI schema editor with the schema provided below. You can also retrieve the schema from the repo here. After, select Add.
(This API schema is needed so that the bedrock agent knows the format structure and parameters needed for the action group to interact with the Lambda function.)
{
"openapi": "3.0.1",
"info": {
"title": "PortfolioCreatorAssistant API",
"description": "API for creating a company portfolio, search company data, and send summarized emails",
"version": "1.0.0"
},
"paths": {
"/companyResearch": {
"post": {
"description": "Get financial data for a company by name",
"parameters": [
{
"name": "name",
"in": "query",
"description": "Name of the company to research",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "Successful response with company data",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CompanyData"
}
}
}
}
}
}
},
"/createPortfolio": {
"post": {
"description": "Create a company portfolio of top profit earners by specifying number of companies and industry",
"parameters": [
{
"name": "numCompanies",
"in": "query",
"description": "Number of companies to include in the portfolio",
"required": true,
"schema": {
"type": "integer",
"format": "int32"
}
},
{
"name": "industry",
"in": "query",
"description": "Industry sector for the portfolio companies",
"required": true,
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "Successful response with generated portfolio",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Portfolio"
}
}
}
}
}
}
},
"/sendEmail": {
"post": {
"description": "Send an email with FOMC search summary and created portfolio",
"parameters": [
{
"name": "emailAddress",
"in": "query",
"description": "Recipient's email address",
"required": true,
"schema": {
"type": "string",
"format": "email"
}
},
{
"name": "fomcSummary",
"in": "query",
"description": "Summary of FOMC search results",
"required": true,
"schema": {
"type": "string"
}
},
{
"name": "portfolio",
"in": "query",
"description": "Details of the created stock portfolio",
"required": true,
"schema": {
"$ref": "#/components/schemas/Portfolio"
}
}
],
"responses": {
"200": {
"description": "Email sent successfully",
"content": {
"text/plain": {
"schema": {
"type": "string",
"description": "Confirmation message"
}
}
}
}
}
}
}
},
"components": {
"schemas": {
"CompanyData": {
"type": "object",
"description": "Financial data for a single company",
"properties": {
"name": {
"type": "string",
"description": "Company name"
},
"expenses": {
"type": "string",
"description": "Annual expenses"
},
"revenue": {
"type": "number",
"description": "Annual revenue"
},
"profit": {
"type": "number",
"description": "Annual profit"
}
}
},
"Portfolio": {
"type": "object",
"description": "Stock portfolio with specified number of companies",
"properties": {
"companies": {
"type": "array",
"items": {
"$ref": "#/components/schemas/CompanyData"
},
"description": "List of companies in the portfolio"
}
}
}
}
}
}
This API schema defines three primary endpoints, /companyResearch
, /createPortfolio
, and /sendEmail
detailing how to interact with the API, the required parameters, and the expected responses.
Now, we need to provide the Bedrock agent a prompt that are examples of a formatted response for an investment company portfolio, and email. In the creation of an agent, it's initially configured with four foundational prompt templates for pre-processing, orchestration, knowledge base response generation, and post-processing. These prompts guide how the agent interacts with the foundation model across various steps of the process. These templates are crucial for processing user inputs, orchestrating the flow between the foundation model, action groups, and knowledge bases, as well as formatting the responses sent to users. By customizing these templates and incorporating advanced prompts or few-shot examples, you can significantly improve the agent's precision and performance in handling specific tasks. More information on advanced prompting for an agent can be found here. Additionally, there is an option to use a custom parser Lambda function for more granular formatting.
Next, scroll down to Advanced prompts and select Edit.
Select the Builder tools tab. Toggle on the radio button Override orchestration template defaults. Make sure Activate orchestration template is enabled as well.
In the Prompt template editor, scroll down to line 22-23, then copy/paste in the following portfolio example and email format:
Here is an example of a company portfolio.
<portfolio_example>
Here is a portfolio of the top 3 real estate companies:
1. NextGenPast Residences with revenue of $180,000, expenses of $22,000 and profit of $158,000 employing 260 people.
2. GlobalRegional Properties Alliance with revenue of $170,000, expenses of $21,000 and profit of $149,000 employing 11 people.
3. InnovativeModernLiving Spaces with revenue of $160,000, expenses of $20,000 and profit of $140,000 employing 10 people.
</portfolio_example>
Here is an example of an email formatted.
<email_format>
Company Portfolio:
1. NextGenPast Residences with revenue of $180,000, expenses of $22,000 and profit of $158,000 employing 260 people.
2. GlobalRegional Properties Alliance with revenue of $170,000, expenses of $21,000 and profit of $149,000 employing 11 people.
3. InnovativeModernLiving Spaces with revenue of $160,000, expenses of $20,000 and profit of $140,000 employing 10 people.
FOMC Report:
Participants noted that recent indicators pointed to modest growth in spending and production. Nonetheless, job gains had been robust in recent months, and the unemployment rate remained low. Inflation had eased somewhat but remained elevated.
Participants recognized that Russia’s war against Ukraine was causing tremendous human and economic hardship and was contributing to elevated global uncertainty. Against this background, participants continued to be highly attentive to inflation risks.
</email_format>
Scroll to the bottom and select the Save and exit button.
Now, check to confirm that the Orchestration in the Advance prompt section is Overridden.
While on the Bedrock agent console, scroll down to Knowledge base and select Add. When integrating the KB with the agent, you will need to provide basic instructions on how to handle the knowledge base. For example, use the following:
Use this knowledge base when a user asks about data, such as economic trends, company financial statements, or the outcomes of the Federal Open Market Committee meetings.
Review your input, then select Add.
Scroll to the top and select Prepare so that the changes made are updated. Then select Save and exit.
Give me a summary of financial market developments and open market operations in January 2023.
Can you provide information about inflation or rising prices?
What can you tell me about the Staff Review of the Economic & Financial Situation?
Example prompts for Knowledge base:
Give me a summary of financial market developments and open market operations in January 2023
Tell me the participants view on economic conditions and economic outlook
Provide any important information I should know about inflation, or rising prices
Tell me about the Staff Review of the Economic & financial Situation
Example prompts for Action groups:
Create a portfolio with 3 companies in the real estate industry
Create portfolio of 3 companies that are in the technology industry
Create a new investment portfolio of companies
Do company research on TechStashNova Inc.
Example prompt for KB & AG
Send an email to test@example.com that includes the company portfolio and FOMC summary
(The logic for this method is not implemented to send emails)
Obtain CF template to launch the streamlit app: Download the Cloudformation template from here. This template will be used to deploy an EC2 instance that has the Streamlit code to run the UI. Please note, the CIDR range and VPC used in the CloudFormation template may need to be modified if the defined subnet and VPC are not applicable.
Deploy template via Cloudformation:
On the Configure stack options screen, leave every setting as default, then go to Next.
Scroll down to the capabilities section, and acknowledge the warning message before submitting.
Once the stack is complete, go to the next step.
Edit the app to update agent IDs:
EC2-Streamlit-App
. Select the checkbox next to it, then connect to it via EC2 Instance Connect
.Next, use the following command to edit the invoke_agent.py file:
sudo vi app/streamlit_app/invoke_agent.py
Press i to go into edit mode. Then, update the AGENT ID and Agent ALIAS ID values.
After, hit Esc
, then save the file changes with the following command:
:wq!
Now, start the streamlit app by running the following command:
streamlit run app/streamlit_app/app.py
You should see an external URL. Copy & paste the URL into a web browser to start the streamlit application.
After completing the setup and testing of the Bedrock Agent and Streamlit app, follow these steps to clean up your AWS environment and avoid unnecessary charges:
Delete S3 Buckets:
Remove Lambda Function:
Delete Bedrock Agent:
Deregister Knowledge Base in Bedrock:
Clean Up EC2 Environment:
See CONTRIBUTING for more information.
This library is licensed under the MIT-0 License. See the LICENSE file.