awslabs / generative-ai-cdk-constructs

AWS Generative AI CDK Constructs are sample implementations of AWS CDK for common generative AI patterns.
https://awslabs.github.io/generative-ai-cdk-constructs/
Apache License 2.0
359 stars 50 forks source link

bedrock: error attaching same action group to multiple agents #747

Open mccauleyp opened 1 month ago

mccauleyp commented 1 month ago

Describe the bug

I'm trying to use the same Lambda-based Action Group for multiple Bedrock Agents. When I try that using the .add_action_group() method of the Agent construct, I get the following error:

RuntimeError: Error: There is already a Construct with name 'AgentLambdaInvocationPolicy' in DockerImageFunction [MyLambdaId]

I think this is due to a limitation of the CDK constructs and not the Bedrock service disallowing the same Action Group being used for multiple agents. Potentially it could be fixed by changing the construct ID AgentLambdaInvocationPolicy to include the construct ID of each Agent.

Expected Behavior

I think same Lambda should be usable by multiple Agents.

Current Behavior

See first section.

Reproduction Steps

Create a Lambda-based Action Group and try to attach it to add it to two different agents via the .add_action_group method of the Agent construct.

Possible Solution

Use an identifier unique to the Agent construct when creating the AgentLambdaInvocationPolicy ID.

Additional Information/Context

No response

CDK CLI Version

2.162.1

Framework Version

0.1.271

Node.js Version

v20.11.0

OS

OSX

Language

Python

Language Version

No response

Region experiencing the issue

us-east-1

Code modification

No

Other information

No response

Service quota

krokoko commented 3 days ago

Just to update here, this issue will be addressed when merging #731

krokoko commented 9 hours ago

Able to reproduce, code snippet:

const agent = new bedrock.Agent(this, "Agent", {
      foundationModel: bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_V2_1,
      instruction: "You are a helpful and friendly agent that answers questions about literature.",
    });

    const agent2 = new bedrock.Agent(this, "Agent2", {
      foundationModel: bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_V2_1,
      instruction: "You are a helpful and friendly agent that answers questions about literature.",
    });

    const actionGroupFunction = new lambda_python.PythonFunction(this, "ActionGroupFunction", {
      runtime: lambda.Runtime.PYTHON_3_12,
      entry: path.join(__dirname, "../lambda/action-group"),
    });

    const actionGroup = new bedrock.AgentActionGroup(this, "MyActionGroup", {
      actionGroupName: "query-library",
      description: "Use these functions to get information about the books in the library.",
      actionGroupExecutor: {
        lambda: actionGroupFunction,
      },
      actionGroupState: "ENABLED",
      apiSchema: bedrock.ApiSchema.fromAsset(path.join(__dirname, "action-group.yaml")),
    });

    agent.addActionGroup(actionGroup);
    agent2.addActionGroup(actionGroup);

Will produce Error: There is already a Construct with name 'AgentLambdaInvocationPolicy' in PythonFunction [ActionGroupFunction]