This repository contains the code for implementing a web search agent using Amazon Bedrock, as described in the AWS blog post "How to use Amazon Bedrock agents with a web search API to integrate dynamic web content in your generative AI application."
This project demonstrates how to create an AI agent that can perform web searches using Amazon Bedrock. It integrates two web search APIs (SerpAPI and Tavily AI) and showcases how to build and deploy the agent using both the AWS Management Console and AWS CDK.
- Integration with Amazon Bedrock for AI agent creation
- Web search functionality using SerpAPI (Google Search) and Tavily AI
- AWS CDK deployment script for infrastructure as code
- Lambda function for handling web search requests
- Example of how to use AWS Secrets Manager for API key storage
- An active AWS Account
- AWS CDK version 2.174.3 or later
- Python 3.11 or later
- API keys for SerpAPI and Tavily AI
-
Clone this repository:
git clone https://github.com/aws-samples/websearch_agent -
Create and activate a Python virtual environment:
python -m venv .venv source .venv/bin/activate -
Install the required dependencies:
pip install -r requirements.txt -
Store your API keys in AWS Secrets Manager:
aws secretsmanager create-secret --name SERPER_API_KEY --secret-string "your_serper_api_key" aws secretsmanager create-secret --name TAVILY_API_KEY --secret-string "your_tavily_api_key" -
Deploy the CDK stack:
cdk deploy
After deployment, you can test the agent using the provided Python script:
python invoke-agent.py --agent_id <agent_id> --agent_alias_id <agent_alias_id> --prompt "What are the latest AWS news?"
Replace <agent_id> and <agent_alias_id> with the values output by the CDK deployment.
/app.py: Top-level definition of the AWS CDK app/cdk/: Contains the stack definition for the web search agent/lambda/: Contains the Lambda function code for handling API calls/test/: Contains a Python script to test the deployed agent
To remove all resources created by this project:
- Run
cdk destroyto delete the CDK-managed resources. - Delete the secrets from AWS Secrets Manager:
aws secretsmanager delete-secret --secret-id SERPER_API_KEY aws secretsmanager delete-secret --secret-id TAVILY_API_KEY
See CONTRIBUTING for more information.
This project is licensed under the MIT-0 License. See the LICENSE file for details.