View a markdown version of this page

资源和会话管理 - Amazon Bedrock AgentCore

资源和会话管理

以下主题展示了 Amazon Bedrock AgentCore 代码解释器的工作原理,以及如何创建资源和管理会话。

IAM 权限

以下 IAM 策略提供了使用 AgentCore 代码解释器的必要权限:

{ "Version":"2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "bedrock-agentcore:CreateCodeInterpreter", "bedrock-agentcore:StartCodeInterpreterSession", "bedrock-agentcore:InvokeCodeInterpreter", "bedrock-agentcore:StopCodeInterpreterSession", "bedrock-agentcore:DeleteCodeInterpreter", "bedrock-agentcore:ListCodeInterpreters", "bedrock-agentcore:GetCodeInterpreter", "bedrock-agentcore:GetCodeInterpreterSession", "bedrock-agentcore:ListCodeInterpreterSessions" ], "Resource": "arn:aws:bedrock-agentcore:us-east-1:111122223333:code-interpreter/*" } ] }

您还应向执行角色添加以下信任策略:

{ "Version":"2012-10-17", "Statement": [{ "Sid": "BedrockAgentCoreBuiltInTools", "Effect": "Allow", "Principal": { "Service": "bedrock-agentcore.amazonaws.com" }, "Action": "sts:AssumeRole", "Condition": { "StringEquals": { "aws:SourceAccount": "111122223333" }, "ArnLike": { "aws:SourceArn": "arn:aws:bedrock-agentcore:us-east-1:111122223333:*" } } }] }

工作原理

  1. 创建代码解释器

    构建自己的代码解释器或使用系统代码解释器来启用诸如编写和运行代码或执行复杂计算之类的功能。代码解释器允许您增加代理运行时间,以便在完全托管的低延迟环境中安全地执行代码。

  2. 将其集成到代理中以进行调用

    将内置工具资源 ID 复制到运行时代理代码中,以将其作为会话的一部分进行调用。对于代码解释器工具,您可以执行代码并实时查看结果。

  3. 使用可观测性评估性能

    监控每个工具的关键指标 CloudWatch ,以获得实时性能见解。

创建代码解释器并启动会话

  1. 创建代码解释器

    配置代码解释器时,可以选择网络设置(沙盒或公共),以及定义代码解释器可以访问哪些 AWS 资源的执行角色角色。

  2. 启动会话

    代码解释器使用基于会话的模型。创建代码解释器后,您可以启动具有可配置的超时时间(默认为 15 分钟)的会话。超时时间过后,会话将自动终止。单个代码解释器可以同时激活多个会话,每个会话都保持自己的状态和环境。

  3. 执行代码

    在活动会话中,您可以使用支持的语言(Python、 JavaScript、 TypeScript)执行代码,并在两次执行之间保持状态。您还可以执行文件 upload/download 操作,并使用为 shell 命令和 AWS CLI 命令提供的支持。

  4. 停止会话并清理

    使用完会话后,应停止会话以释放资源并避免不必要的费用。如果您不再打算使用代码解释器,也可以将其删除。