# Market Trends Agent ## Overview This use case implements an intelligent financial analysis agent using Amazon Bedrock AgentCore that provides real-time market intelligence, stock analysis, and personalized investment recommendations. The agent combines LLM-powered analysis with live market data and maintains persistent memory of broker preferences across sessions. ## Use Case Architecture ![Market Trends Agent Architecture](images/market-trends-agent-architecture.png) | Information | Details | |-------------|---------| | Use case type | Conversational | | Agent type | Graph | | Use case components | Memory, Tools, Browser Automation | | Use case vertical | Financial Services | | Example complexity | Advanced | | SDK used | Amazon Bedrock AgentCore SDK, LangGraph, Playwright | ## Features ### ๐Ÿง  Advanced Memory Management - **Multi-Strategy Memory**: Uses both USER_PREFERENCE and SEMANTIC memory strategies - **Broker Profiles**: Maintains persistent financial profiles for each broker/client - **LLM-Based Identity**: Intelligently extracts and matches broker identities across sessions - **Investment Preferences**: Stores risk tolerance, investment styles, and sector preferences ### ๐Ÿ“Š Real-Time Market Intelligence - **Conversational Broker Profiles**: Users provide structured broker information through chat โœ… **TESTED & READY** - **Automatic Profile Parsing**: Intelligently extracts and stores broker preferences from structured input - **Personalized Market Briefings**: Tailored analysis based on stored broker profiles - **Multi-Source News**: Bloomberg, Reuters, WSJ, Financial Times, CNBC support - **Live Stock Data**: Current prices, changes, and market performance metrics - **Professional Standards**: Delivers institutional-quality analysis aligned with broker's risk tolerance and investment style ### ๐ŸŒ Browser Automation - **Web Scraping**: Automated data collection from financial websites - **Dynamic Content**: Handles JavaScript-rendered pages and interactive elements - **Rate Limiting**: Built-in delays and retry logic for reliable data collection The Market Trends Agent leverages Amazon Bedrock AgentCore's comprehensive capabilities to deliver personalized financial intelligence: - **AgentCore Runtime**: Serverless execution environment for the LangGraph-based agent - **AgentCore Memory**: Multi-strategy memory system storing broker preferences and financial insights - **AgentCore Browser Tool**: Secure web scraping for real-time market data from financial websites - **Claude Sonnet 4**: Advanced LLM for financial analysis and broker interaction - **Multi-Source Integration**: Real-time data from Bloomberg, Reuters, WSJ, and other financial sources ## Quick Start ### Prerequisites - Python 3.10+ - AWS CLI configured with appropriate credentials - Docker or Podman installed and running - Access to Amazon Bedrock AgentCore ### Installation & Deployment 1. **Install uv** (if not already installed) ```bash # macOS/Linux curl -LsSf https://astral.sh/uv/install.sh | sh # Windows powershell -c "irm https://astral.sh/uv/install.ps1 | iex" # Or via pip pip install uv ``` 2. **Install Dependencies** ```bash uv sync ``` 3. **Deploy the Agent** (One Command!) ```bash # Simple deployment uv run python deploy.py # Custom configuration (optional) uv run python deploy.py \ --agent-name "my-market-agent" \ --region "us-west-2" \ --role-name "MyCustomRole" ``` **Available Options:** - `--agent-name`: Name for the agent (default: market_trends_agent) - `--role-name`: IAM role name (default: MarketTrendsAgentRole) - `--region`: AWS region (default: us-east-1) - `--skip-checks`: Skip prerequisite validation 4. **Test the Agent** ```bash uv run python test_agent.py ``` ## Usage Examples ### ๐Ÿ“‹ Broker Profile Setup (First Interaction) Send your broker information in this structured format: ``` Name: Yuval Bing Company: HSBC Role: Investment Advisor Preferred News Feed: BBC Industry Interests: oil, emerging markets Investment Strategy: dividend Risk Tolerance: low Client Demographics: younger professionals, tech workers Geographic Focus: North America, Asia-Pacific Recent Interests: middle east geopolitics ``` The agent will automatically: - Parse and store your profile in memory - Provide personalized acknowledgment - Tailor all future responses to your specific preferences ### ๐Ÿ“Š Personalized Market Analysis After setting up your profile, ask for market insights: ``` "What's happening with biotech stocks today?" "Give me an analysis of the AI sector for my tech-focused clients" "What are the latest ESG investing trends in Europe?" ``` The agent will provide analysis specifically tailored to: - Your industry interests - Your risk tolerance - Your client demographics - Your preferred news sources ### ๐Ÿงช Test the Broker Card Functionality ```bash uv run python test_broker_card.py ``` This demonstrates the complete workflow: 1. Sending structured broker profile 2. Agent parsing and storing preferences 3. Receiving personalized market analysis ### ๐Ÿ’ฌ Continue Interactive Conversations After testing, continue chatting with your agent: **Quick one-liner for immediate chat:** ```bash uv run python -c " import boto3, json client = boto3.client('bedrock-agentcore', region_name='us-east-1') with open('.agent_arn', 'r') as f: arn = f.read().strip() print('๐Ÿ’ฌ Market Trends Agent Chat (type \"quit\" to exit)') while True: try: msg = input('\n๐Ÿค– You: ') if msg.lower() in ['quit', 'exit']: break resp = client.invoke_agent_runtime(agentRuntimeArn=arn, payload=json.dumps({'prompt': msg})) print('๐Ÿ“ˆ Agent:', resp['response'].read().decode('utf-8')) except KeyboardInterrupt: break " ``` ## Architecture ### Use case Architecture ``` โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ” โ”‚ Market Trends Agent โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ LangGraph Agent Framework โ”‚ โ”‚ โ”œโ”€โ”€ Claude Sonnet 4 (LLM) โ”‚ โ”‚ โ”œโ”€โ”€ Browser Automation Tools โ”‚ โ”‚ โ””โ”€โ”€ Memory Management Tools โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ AgentCore Multi-Strategy Memory โ”‚ โ”‚ โ”œโ”€โ”€ USER_PREFERENCE: Broker profiles & preferences โ”‚ โ”‚ โ””โ”€โ”€ SEMANTIC: Financial facts & market insights โ”‚ โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค โ”‚ External Data Sources โ”‚ โ”‚ โ”œโ”€โ”€ Real-time Stock Data (Google Finance, Yahoo Finance) โ”‚ โ”‚ โ”œโ”€โ”€ Financial News (Bloomberg) โ”‚ โ”‚ โ””โ”€โ”€ Market Analysis APIs โ”‚ โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜ ``` ### Memory Strategies - **USER_PREFERENCE**: Captures broker preferences, risk tolerance, investment styles - **SEMANTIC**: Stores financial facts, market analysis, investment insights ### Available Tools **Market Data & News** (`tools/browser_tool.py`): - `get_stock_data(symbol)`: Real-time stock prices and market data - `search_news(query, news_source)`: Multi-source news search (Bloomberg, Reuters, CNBC, WSJ, Financial Times, Dow Jones) **Broker Profile Management** (`tools/broker_card_tools.py`): - `parse_broker_profile_from_message()`: Parse structured broker cards - `generate_market_summary_for_broker()`: Tailored market analysis - `get_broker_card_template()`: Provide broker card format template - `collect_broker_preferences_interactively()`: Guide preference collection **Memory & Identity Management** (`tools/memory_tools.py`): - `identify_broker(message)`: LLM-based broker identity extraction - `get_broker_financial_profile()`: Retrieve stored financial profiles - `update_broker_financial_interests()`: Store new preferences and interests - `list_conversation_history()`: Retrieve recent conversation history ## Monitoring ### CloudWatch Logs After deployment, monitor your agent: ```bash # View logs (replace with your agent ID) aws logs tail /aws/bedrock-agentcore/runtimes/{agent-id}-DEFAULT --follow ``` ### Health Checks - Built-in health check endpoints - Monitor agent availability and response times ## Cleanup ### Complete Resource Cleanup When you're done with the agent, use the cleanup script to remove all AWS resources: ```bash # Complete cleanup (removes everything) uv run python cleanup.py # Preview what would be deleted (dry run) uv run python cleanup.py --dry-run # Keep IAM roles (useful if shared with other projects) uv run python cleanup.py --skip-iam # Cleanup in different region uv run python cleanup.py --region us-west-2 ``` **What gets cleaned up:** - โœ… AgentCore Runtime instances - โœ… AgentCore Memory instances - โœ… ECR repositories and container images - โœ… CodeBuild projects - โœ… S3 build artifacts - โœ… SSM parameters - โœ… IAM roles and policies (unless `--skip-iam`) - โœ… Local deployment files ### Manual Cleanup (if needed) If the automated cleanup fails, you can manually remove resources: 1. **AgentCore Runtime**: AWS Console โ†’ Bedrock โ†’ AgentCore โ†’ Runtimes 2. **AgentCore Memory**: AWS Console โ†’ Bedrock โ†’ AgentCore โ†’ Memory 3. **ECR Repository**: AWS Console โ†’ ECR โ†’ Repositories 4. **IAM Roles**: AWS Console โ†’ IAM โ†’ Roles (search for "MarketTrendsAgent") 5. **CodeBuild**: AWS Console โ†’ CodeBuild โ†’ Build projects ## Troubleshooting ### Common Issues 1. **Throttling Errors** - Wait a few minutes between requests - Your account may have lower rate limits - Check CloudWatch logs for details 2. **Container Build Fails** - Ensure Docker/Podman is running - Check network connectivity - Verify all required files are present 3. **Permission Errors** - The deployment script creates all required IAM permissions - Check AWS credentials are configured correctly 4. **Memory Instance Duplicates** - The agent uses SSM Parameter Store to prevent race conditions - If you see multiple memory instances, run: `uv run python cleanup.py` - Then redeploy with: `uv run python deploy.py` ### Debug Information The deployment script includes comprehensive error reporting and will guide you through any issues. ## Security ### IAM Permissions The deployment script automatically creates a role with: - `bedrock:InvokeModel` (for Claude Sonnet) - `bedrock-agentcore:*` (for memory and runtime operations) - `ecr:*` (for container registry access) - `xray:*` (for tracing) - `logs:*` (for CloudWatch logging) ### Data Privacy - Financial profiles are stored securely in Bedrock AgentCore Memory - No sensitive data is logged or exposed - All communications are encrypted in transit ## Contributing 1. Fork the repository 2. Create a feature branch 3. Make your changes 4. Add tests for new functionality 5. Submit a pull request ## License This project is licensed under the MIT License - see the LICENSE file for details.