Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amazon Bedrock Agent Samples - [Bug] #4

Open
jayasimhag1reddy opened this issue Dec 12, 2024 · 2 comments
Open

Amazon Bedrock Agent Samples - [Bug] #4

jayasimhag1reddy opened this issue Dec 12, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@jayasimhag1reddy
Copy link

Describe the bug
I have seen 2 issues till now, in the notebook examples/amazon-bedrock-multi-agent-collaboration/devops_agent/01_Create_Grafana_Assistant_Agent/01_create_grafana_assistant.ipynb:

  1. The import code uses __file__ which is causing errors as it's not properly defined in the notebook context:
sys.path.append(str(Path(__file__).parent.parent.parent.parent.parent))
from src.utils.bedrock_agent_helper import create_lambda_layer, upload_api_schema, create_agent, invoke_agent_helper
  1. The functions create_lambda_layer and upload_api_schema are incorrectly imported from src.utils.bedrock_agent_helper. These functions actually exist in amazon_bedrock_agents.restaurant_booking_custom_orchestration_agent.agents.

Steps to Reproduce

  1. Open the notebook at examples/amazon-bedrock-multi-agent-collaboration/devops_agent/01_Create_Grafana_Assistant_Agent/01_create_grafana_assistant.ipynb
  2. Run the cell containing library imports
  3. Observe the import errors due to incorrect path and module references

Expected behavior
The following code should be used instead as it works correctly:

current_path = Path.cwd().parent.parent.parent
sys.path.append(str(current_path))
from amazon_bedrock_agents.restaurant_booking_custom_orchestration_agent.agents import (
    create_lambda_layer,
    upload_api_schema,
)

Screenshots
while running the code give in this repo
Screenshot 2024-12-12 at 2 15 45 PM

After making changes:

Screenshot 2024-12-12 at 2 16 26 PM

Additional context

  • The DevOps agent examples need clearer documentation as multiple users are having difficulty implementing them
  • The path handling in notebooks should be more robust and not rely on __file__
  • The import statements should be updated to reflect the correct module structure

Suggested improvements

  1. Update the notebook with the correct import path and module references
  2. Enhance the documentation for the DevOps agent examples to make implementation clearer
  3. Consider adding troubleshooting guides for common issues
@jayasimhag1reddy jayasimhag1reddy added the bug Something isn't working label Dec 12, 2024
EashanKaushik added a commit that referenced this issue Dec 15, 2024
@EashanKaushik
Copy link
Contributor

Added new files agents.py and kb.py in devops agents. These will be removed in future. Can you check if this fix works and you get the desired behaviour ? Thanks.

@jayasimhag1reddy
Copy link
Author

Still I am facing many issues while implementing this POC.
1)In agents.py, create_lambda_layer function is giving me error stating some errors, After debugging I came to know that it has some platform dependecny, So the following changes is working for me have a look and let me know what you think.

def create_lambda_layer(agent_name: str, requirements: str) -> str:
    """
    Create an AWS Lambda layer with specified requirements, supporting multiple operating systems.
    
    Args:
        agent_name (str): Name of the agent/layer
        requirements (str): Path to requirements.txt file
    
    Returns:
        str: Lambda layer ARN if successful, None otherwise
    """
    try:
        # Clean up any existing layer directory
        if os.path.exists("layer"):
            shutil.rmtree("layer")
        
        # Create directory structure
        layer_path = Path("layer/python")
        layer_path.mkdir(parents=True, exist_ok=True)

        # Install packages
        print("Installing required packages...")
        subprocess.check_call([
            sys.executable,  # Use the current Python interpreter
            "-m",
            "pip",
            "install",
            "-r",
            requirements,
            "-t",
            str(layer_path),
            "--no-cache-dir"  # Avoid caching issues
        ])

        # Create ZIP file using platform-specific methods
        print("Creating ZIP file...")
        if platform.system() == "Windows":
            _create_zip_windows("layer")
        else:  # macOS or Linux
            _create_zip_unix("layer")

        # Create Lambda layer
        print("Publishing Lambda layer...")
        lambda_client = boto3.client('lambda')
        
        with open("layer/layer.zip", 'rb') as zip_file:
            response = lambda_client.publish_layer_version(
                LayerName=f'{agent_name}-layer',
                Description='Action group lambda layer',
                Content={
                    'ZipFile': zip_file.read()
                },
                CompatibleRuntimes=[
                    'python3.7', 'python3.8', 'python3.9', 
                    'python3.10', 'python3.11', 'python3.12'
                ],
                CompatibleArchitectures=['x86_64']
            )

        layer_arn = response['LayerVersionArn']
        print(f"Layer created successfully.")
        print(f"Version: {response['Version']}")
        print(f"Layer ARN: {layer_arn}")

        return layer_arn

    except subprocess.CalledProcessError as e:
        print(f"Command failed: {e.cmd}")
        print(f"Output: {e.output if hasattr(e, 'output') else 'No output'}")
        raise
    except Exception as e:
        print(f"Error creating layer: {str(e)}")
        raise
    finally:
        # Clean up
        if os.path.exists("layer"):
            try:
                shutil.rmtree("layer")
                print("Cleaned up temporary files")
            except Exception as e:
                print(f"Warning: Could not clean up temporary files: {str(e)}")

def _create_zip_windows(directory: str):
    """Create ZIP file on Windows using built-in Python zipfile module."""
    import zipfile
    
    def zipdir(path, ziph):
        for root, _, files in os.walk(path):
            for file in files:
                file_path = os.path.join(root, file)
                arc_path = os.path.relpath(file_path, path)
                ziph.write(file_path, arc_path)

    with zipfile.ZipFile(os.path.join(directory, "layer.zip"), 'w', zipfile.ZIP_DEFLATED) as zipf:
        zipdir(os.path.join(directory, "python"), zipf)

def _create_zip_unix(directory: str):
    """Create ZIP file on Unix-like systems using zip command."""
    try:
        # First, check if zip is available
        subprocess.run(["zip", "-h"], capture_output=True, check=True)
    except (subprocess.CalledProcessError, FileNotFoundError):
        print("Warning: zip command not found, falling back to Python zipfile")
        _create_zip_windows(directory)
        return

    # Use zip command if available
    subprocess.check_call([
        "zip",
        "-r",
        "layer.zip",
        "python"
    ], cwd=directory)

2)While testing the grafana agent: After running this cell

%%time
import uuid
session_id:str = str(uuid.uuid1())
query = """can you get me alert history of memory alert for the app app1"""
response = invoke_agent_helper(
    query, session_id, grafana_agent_id, grafana_assistant_agent_alias_id, enable_trace=True
)
print(response)

I am getting this error.

Screenshot 2024-12-16 at 11 11 37 AM

3)Getting this error while running 3rd cell in creating supervisor agents.

%store -r

Screenshot 2024-12-16 at 11 08 20 AM.

  1. After creating the supervisor agents, I am getting some the following error
supervisor_agent_alias_id, supervisor_agent_alias_arn = associate_sub_agents(
    supervisor_agent_id, sub_agents_list
)
supervisor_agent_alias_id, supervisor_agent_alias_arn
{
	"name": "ValidationException",
	"message": "An error occurred (ValidationException) when calling the AssociateAgentCollaborator operation: 1 validation error detected: Value 'arn:aws:bedrock:us-west-2:061051234909:agent/1XLLTBIRCF' at 'agentDescriptor.aliasArn' failed to satisfy constraint: Member must satisfy regular expression pattern: arn:aws(-[^:]+)?:bedrock:[a-z0-9-]{1,20}:[0-9]{12}:agent-alias/[0-9a-zA-Z]{10}/[0-9a-zA-Z]{10}",
	"stack": "---------------------------------------------------------------------------
ValidationException                       Traceback (most recent call last)
Cell In[17], line 1
----> 1 supervisor_agent_alias_id, supervisor_agent_alias_arn = associate_sub_agents(
      2     supervisor_agent_id, sub_agents_list
      3 )
      4 supervisor_agent_alias_id, supervisor_agent_alias_arn

File ~/Downloads/Poc/amazon-bedrock-agent-samples/examples/amazon-bedrock-multi-agent-collaboration/devops_agent/agents.py:420, in associate_sub_agents(supervisor_agent_id, sub_agents_list)
    418 for sub_agent in sub_agents_list:
    419     wait_agent_status_update(supervisor_agent_id)
--> 420     association_response = bedrock_agent_client.associate_agent_collaborator(
    421         agentId=supervisor_agent_id,
    422         agentVersion='DRAFT',
    423         agentDescriptor={
    424             'aliasArn': sub_agent['sub_agent_alias_arn']
    425         },
    426         collaboratorName=sub_agent['sub_agent_association_name'],
    427         collaborationInstruction=sub_agent['sub_agent_instruction'],
    428         relayConversationHistory=sub_agent['relay_conversation_history']
    429     )
    430     wait_agent_status_update(supervisor_agent_id)
    431     bedrock_agent_client.prepare_agent(
    432         agentId=supervisor_agent_id
    433     )

File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/botocore/client.py:569, in ClientCreator._create_api_method.<locals>._api_call(self, *args, **kwargs)
    565     raise TypeError(
    566         f\"{py_operation_name}() only accepts keyword arguments.\"
    567     )
    568 # The \"self\" in this scope is referring to the BaseClient.
--> 569 return self._make_api_call(operation_name, kwargs)

File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/botocore/client.py:1023, in BaseClient._make_api_call(self, operation_name, api_params)
   1019     error_code = error_info.get(\"QueryErrorCode\") or error_info.get(
   1020         \"Code\"
   1021     )
   1022     error_class = self.exceptions.from_code(error_code)
-> 1023     raise error_class(parsed_response, operation_name)
   1024 else:
   1025     return parsed_response

ValidationException: An error occurred (ValidationException) when calling the AssociateAgentCollaborator operation: 1 validation error detected: Value 'arn:aws:bedrock:us-west-2:061051234909:agent/1XLLTBIRCF' at 'agentDescriptor.aliasArn' failed to satisfy constraint: Member must satisfy regular expression pattern: arn:aws(-[^:]+)?:bedrock:[a-z0-9-]{1,20}:[0-9]{12}:agent-alias/[0-9a-zA-Z]{10}/[0-9a-zA-Z]{10}"
}
Screenshot 2024-12-16 at 11 21 54 AM

I feel it will be helpful, if there is more detailed steps on how to run this poc. Because there are many errors while running it, many might face issues while implementing it. So, please have a look at it.

If you need any help, I am happy to contribute to this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants