Amazon Bedrock Multi-Agents Collaboration Workshop

repository·main·Indexed 41 days ago

https://github.com/aws-samples/bedrock-multi-agents-collaboration-workshop

A hands-on workshop for building an Energy Efficiency Management System using Amazon Bedrock. It demonstrates a supervisor-sub-agent pattern to orchestrate specialized agents for energy forecasting, solar panel maintenance, and peak load management. The guide covers the use of AgentsForAmazonBedrock and KnowledgeBasesForAmazonBedrock helper classes, implementing Lambda action groups, and configuring RAG-based knowledge bases.

Tokens
9.4K
Snippets
23
Records
32
Agent score
47%

What's inside Bedrock Multi-Agents Collaboration Workshop

  1. How the Energy Efficiency Management System architecture works

    main

    The system uses a supervisor-sub-agent pattern to manage energy tasks. An Energy Efficiency Management Agent (Supervisor) acts as the central orchestrator, routing user queries to one of three specialized sub-agents while maintaining conversation context.

    Sub-Agents

    1. Forecasting Agent: Handles energy consumption data, forecasts, and statistics. It includes code interpretation capabilities for data analysis.
    2. Solar Panel Agent: Manages installation guidelines, maintenance instructions, and support ticket lifecycle (creation and tracking).
    3. Peak Load Manager Agent: Analyzes usage patterns (peak vs. off-peak), identifies non-essential processes, and optimizes grid allocation.
  2. Invoke Bedrock Data Automation (BDA) async tasks

    main

    To process video or audio files, upload them to S3 and then invoke the BDA runtime using invoke_data_automation_async.

    Workflow:

    1. Upload file to S3 (e.g., bda/input/video/).
    2. Call invoke_data_automation_async providing the inputConfiguration (S3 URI), outputConfiguration (S3 URI), and the dataAutomationProjectArn.
    3. Poll the status using get_data_automation_status with the returned invocationArn until the status is Success, ServiceError, or ClientError.
    4. Download the resulting result.json from the BDA output location to prepare it for Knowledge Base ingestion.
    response_vid = bda_runtime_client.invoke_data_automation_async(
        inputConfiguration={'s3Uri':  f"s3://{bucket_name}/{file_input}"},
        outputConfiguration={'s3Uri': f"s3://{bucket_name}/{output_name}"},
        dataAutomationProfileArn= f'arn:aws:bedrock:{region}:{account_id}:data-automation-profile/us.data-automation-v1',
        dataAutomationConfiguration={
            'dataAutomationProjectArn':kb_project_arn,
            'stage': 'DEVELOPMENT'
        })
  3. Define and Associate Action Groups for the Agent

    main

    Action groups allow the agent to perform tasks like detecting peaks or redistributing energy quotas. This involves two steps: defining a Lambda function and defining the function metadata.

    1. Lambda Function: The Lambda must handle the event and return a response in the format: {'response': {'actionGroup': ..., 'function': ..., 'functionResponse': {'responseBody': {'TEXT': {'body': ...}}}}}.

    2. Function Definitions: Define a list of dictionaries (functions_def) specifying the name, description, and parameters (name, type, required status) for each tool.

    3. Association: Use agents.add_action_group_with_lambda to link the Lambda, the function definitions, and the agent together.

    functions_def = [
        {
            "name": "detect_peak",
            "description": "detect consumption peak during current month",
            "parameters": {
                "customer_id": {
                    "description": "The ID of the customer",
                    "required": True,
                    "type": "string"
                }
            }
        },
        # ... other functions
    ]
    
    resp = agents.add_action_group_with_lambda(
        agent_name=peak_agent_name,
        lambda_function_name=peak_lambda_name,
        source_code_file="peak_load.py",
        agent_functions=functions_def,
        agent_action_group_name="peak_load_actions",
        agent_action_group_description="Function to get usage, peaks, redistribution for a user",
        dynamo_args=dynamoDB_args
    )
  4. Setup the Peak Load Manager environment

    main

    Before starting, ensure your notebook kernel is set to Python 3. You must upgrade boto3 to at least version 1.37.4 and install a specific version of numpy to ensure compatibility with the workshop components.

    Run the following commands to prepare your environment:

    # Install latest dependencies
    python3 -m pip install --upgrade "boto3>=1.37.4"
    
    # Fix numpy version
    pip uninstall -y numpy
    pip install "numpy==1.26.4"

    Additionally, ensure a .workshop_id file exists in the parent directory to retrieve your resource_suffix.

    python3 -m pip install --upgrade "boto3>=1.37.4"
  5. Prerequisites for the workshop

    main

    Before starting, ensure you have the following environment ready:

    • AWS Account: Must have appropriate permissions configured.
    • Amazon Bedrock: Access must be enabled for the models used in the workshop.
    • AWS CLI: Must be installed and configured with valid credentials.
    • Python: Version 3.8 or higher.
    • Boto3: The latest version of the AWS SDK for Python.
  6. Configure a Bedrock Data Automation (BDA) project

    main

    To process multimodal content (video/audio) for the agent's Knowledge Base, you must create a BDA project. This project defines how unstructured data is extracted and summarized.

    When creating the project via create_data_automation_project, you can configure:

    • Video Extraction: Content moderation, text detection, transcript, bounding boxes, and generative fields (summaries, chapters).
    • Audio Extraction: Audio content moderation, topic moderation, transcript, and generative fields (summaries, topic summaries).

    The project is reusable across multiple files sharing the same configuration.

    response = bda_client.create_data_automation_project(
        projectName=project_name,
        projectDescription='BDA video processing project',
        projectStage='DEVELOPMENT',
        standardOutputConfiguration={
            "video": {
                "extraction": {
                    "category": {
                        "state": "ENABLED",
                        "types": ["CONTENT_MODERATION", "TEXT_DETECTION", "TRANSCRIPT"]
                    },
                    "boundingBox": {"state": "ENABLED"}
                },
                "generativeField": {
                    "state": "ENABLED",
                    "types": ["VIDEO_SUMMARY", "CHAPTER_SUMMARY", "IAB"]
                }
            },
            "audio": {
                "extraction": {
                    "category": {
                        "state": "ENABLED", 
                        "types": ["AUDIO_CONTENT_MODERATION", "TOPIC_CONTENT_MODERATION", "TRANSCRIPT"]
                    }
                },
                "generativeField": {
                    "state": "ENABLED",
                    "types": ["AUDIO_SUMMARY", "TOPIC_SUMMARY", "IAB"]
                }
            }
        }
    )
  7. Implement a Lambda handler for Agent Action Groups

    main

    When an agent invokes an action group, it calls an AWS Lambda function. The Lambda must parse the event object to identify the requested function and its parameters, then return a response in a specific format compatible with Bedrock Agents.

    Required Response Format:

    def populate_function_response(event, response_body):
        return {
            'response': {
                'actionGroup': event['actionGroup'], 
                'function': event['function'],
                'functionResponse': {
                    'responseBody': {
                        'TEXT': {'body': str(response_body)}
                    }
                }
            }
        }

    Lambda Logic Pattern:

    1. Extract function name from event.get('function').
    2. Extract parameters using a helper (e.g., get_named_parameter(event, name)).
    3. Execute business logic (e.g., DynamoDB query).
    4. Return the formatted response using populate_function_response.
  8. Create a Solar Panel Agent with Knowledge Base and Action Groups

    main

    To build a functional Solar Panel agent, you must combine three components:

    1. Instructions: A system prompt defining the agent's persona (e.g., 'Solar Energy Assistant'), capabilities, and response style.
    2. Knowledge Base (RAG): Provides the agent with technical documentation (e.g., installation and maintenance manuals) via associate_kb_with_agent.
    3. Action Group (Lambda): Provides the agent with tools to perform tasks, such as open_ticket or get_ticket_status. This is configured using add_action_group_with_lambda and requires a defined list of agent_functions (name, description, and parameters).
    from utils.bedrock_agent_helper import AgentsForAmazonBedrock
    
    agents = AgentsForAmazonBedrock()
    
    # 1. Create Agent
    solar_agent = agents.create_agent(
        solar_agent_name,
        agent_description,
        agent_instruction,
        agent_foundation_model,
        kb_arns=[kb_arn]
    )
    
    # 2. Associate Knowledge Base
    agents.associate_kb_with_agent(
        solar_agent[0], 
        'Access the knowledge base when customers ask about to install and maintain solar panels',
        kb_id
    )
    
    # 3. Add Action Group
    functions_def = [
        {
            "name": "open_ticket",
            "description": "Create a ticket to get help...",
            "parameters": {
                "customer_id": {"description": "Unique customer identifier", "required": True, "type": "string"},
                "msg": {"description": "The reason...", "required": True, "type": "string"}
            }
        }
    ]
    
    agents.add_action_group_with_lambda(
        agent_name=solar_agent_name,
        lambda_function_name=solar_lambda_name,
        source_code_file="solar_energy.py",
        agent_functions=functions_def,
        agent_action_group_name="solar_energy_actions",
        agent_action_group_description="Function to open an energy ticket...",
        dynamo_args=dynamoDB_args
    )
  9. How multi-agent collaboration works in Amazon Bedrock

    main

    Multi-agent collaboration is a native Amazon Bedrock capability that enables hierarchical agent structures.

    The Supervisor Pattern:

    1. A Supervisor Agent is configured with agent_collaboration='SUPERVISOR_ROUTER'.
    2. The Supervisor is associated with multiple Sub-Agents (collaborators).
    3. When a user provides a prompt, the Supervisor analyzes the request and determines which sub-agent is best suited to handle the task based on the sub_agent_instruction provided during association.
    4. The Supervisor can delegate tasks to any existing agent in the same account, including agents that are themselves supervisors, allowing for complex, composable chains of intelligence.
  10. How multi-agent collaboration traces work

    main
    Amazon Bedrock Agents' multi-agent collaboration feature allows a supervisor agent to orchestrate sub-agents. When enable_trace=True is used during an invocation, the supervisor's trace includes the chain-of-thought reasoning and the specific reasoning details of its sub-agents. This allows developers to see how the supervisor decides which sub-agent to call and how the sub-agents contribute to the final answer.