View a markdown version of this page

资源和会话管理 - Amazon Bedrock AgentCore
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)。

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

资源和会话管理

以下主题展示了 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. 创建代码解释器

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

  2. 启动会话

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

  3. 执行代码

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

  4. 停止会话并进行清理

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