Erez Weinstein e2d9590a86
feat(market-trend-agent): market trends agent added (LangGraph sample) (#306)
* Add Market Trends Agent with Bedrock AgentCore browser integration

- Implements Strands-based agent for real-time stock data and business news
- Uses Bedrock AgentCore browser_client for web scraping
- Includes tools for Yahoo Finance stock data and Bloomberg news search
- Features professional market analysis with AI-powered insights
- Supports concurrent browser sessions for multiple stock comparisons
- Complete with tests, documentation, and clean requirements

* feat: Add Market Trends Agent with AgentCore integration

- Implement conversational broker card system with memory integration
- Add browser automation for real-time market data and news
- Create LangGraph-based agent architecture with tool orchestration
- Include deployment scripts and comprehensive documentation
- Add test scripts demonstrating broker profile workflow
- Configure Docker containerization for AgentCore Runtime

* feat: optimize memory management and fix deployment issues

- Fix memory creation to check existing memory first, eliminating ValidationException errors
- Extract memory tools into separate module for better organization
- Update deployment script with enhanced error handling and IAM permissions
- Add architecture documentation and diagrams
- Successfully deployed to burner account with Claude Sonnet 4 and multi-strategy memory

* fix: remove unsupported auto_update_on_conflict parameter and enable dynamic config

- Remove auto_update_on_conflict parameter from runtime.launch() call
- Allow .bedrock_agentcore.yaml to be generated dynamically by deploy script
- Successfully deployed to burner account (484363822803) with Claude Sonnet 4
- All tests passing: 4/4 (100% success rate)
- Agent fully functional with memory, market data, and news search capabilities

* fix:image link in readme

* fix:replaced image

* fix: resolve all linting errors and code quality issues

- Remove unused imports: tool, MemoryClient, ClientError, argparse, json, os, datetime, Any, re
- Fix f-strings without placeholders by converting to regular strings
- Remove unused variables: config_response, launch_result
- Clean up code to pass GitHub checks and improve maintainability

Fixes:
- deploy.py: 4 issues (f-strings + unused variables)
- market_trends_agent.py: 6 unused imports
- test_broker_card.py: 2 issues (unused import + f-string)
- tools/broker_card_tools.py: 2 unused imports
- tools/memory_tools.py: 3 issues (unused import + 2 f-strings)

* Changes to be committed:
	modified:   02-use-cases/market-trends-agent/.gitignore
	modified:   02-use-cases/market-trends-agent/tools/memory_tools.py

* fix: add back datetime import needed for session_id generation

The datetime import was accidentally removed but is still needed on line 27 for creating session IDs in the format 'market-{datetime.now().strftime('%Y%m%d%H%M%S')}'.

* refactor: remove Dockerfile from repo, let deployment auto-generate

- Remove Dockerfile from version control
- Add Dockerfile to .gitignore
- Deployment script will auto-generate Dockerfile with proper defaults
- Reduces repo clutter while maintaining deployment functionality
- Auto-generated Dockerfile will include necessary configurations

* Add uv support and fix news source reliability

- Add pyproject.toml with uv configuration
- Update README to use only uv commands
- Fix Reuters and CNBC URL issues with better error handling
- Add interactive chat instructions for users
- Improve browser tool with retry logic and reliable fallbacks"

* Fix memory management and add uv integration

- Fix broker identity confusion by enforcing identify_broker() workflow
- Improve actor_id consistency across memory operations
- Add enhanced error handling for news sources (Reuters/CNBC 503 errors)
- Add uv integration with pyproject.toml for faster dependency management
- Streamline README to uv-only workflow with interactive chat instructions
- Add reliable fallback news sources (Yahoo Finance, MarketWatch)

* Fix broker identification workflow and message filtering

- Enhanced system prompt to enforce mandatory broker identification
- Fixed tool_use/tool_result message pairing in filtering logic
- Added comprehensive test suite for broker memory storage
- Verified multi-strategy memory works for Tim Dunk and Maria Rodriguez

* Implement distributed memory coordination using SSM Parameter Store

- Added SSM Parameter Store for distributed memory ID coordination
- Enhanced race condition protection for deployment scenarios
- Updated IAM permissions to include SSM parameter access
- Prevents multiple memory instances during AgentCore deployment
- Maintains backward compatibility with local .memory_id file

* Add comprehensive cleanup script and documentation

- Created cleanup.py script to remove all deployed AWS resources
- Cleans up AgentCore runtime, memory, ECR, CodeBuild, S3, IAM, SSM
- Added safety features: dry-run mode, confirmation prompts, detailed logging
- Updated README with complete cleanup section and troubleshooting
- Tested cleanup successfully removes all resources except runtime (manual)
- Supports selective cleanup options (--skip-iam, --region, --dry-run)

* fix: resolve all lint errors in market trends agent

- Remove unused imports (json, time, extract_actor_id)
- Fix f-strings without placeholders in browser_tool.py
- Replace bare except clauses with specific exception handling
- Add proper logging for exception cases
- All files now pass ruff lint checks

---------

Signed-off-by: Erez Weinstein <125476602+erezweinstein5@users.noreply.github.com>
Co-authored-by: Erez Weinstein <erweinst@amazon.com>
2025-09-04 13:26:50 -04:00
..

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

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)
# 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
  1. Install Dependencies
uv sync
  1. Deploy the Agent (One Command!)
# 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
  1. Test the Agent
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

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:

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:

# 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:

# 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.