View a markdown version of this page

AgentCore Code Interpreter 入門 - Amazon Bedrock AgentCore

AgentCore Code Interpreter 入門

AgentCore Code Interpreter 可讓您的代理程式在安全、受管的環境中執行 Python 程式碼。代理程式可以執行計算、分析資料、產生視覺化效果,並透過程式碼執行驗證答案。

此頁面涵蓋先決條件,並提供兩種開始使用的方法:

  • 使用 AWS Strands - 高階代理程式架構,可透過內建的工具整合、對話管理和自動工作階段處理,簡化建置 AI 代理程式。

  • 直接使用 - SDK 和 Boto3 方法,可為自訂實作提供更多工作階段管理和程式碼執行的控制權。

先決條件

開始之前,請確定您已:

  • AWS 已設定登入資料的 帳戶。請參閱設定您的登入資料。

  • 已安裝 Python 3.10+

  • 已安裝 Boto3。請參閱 Boto3 文件

  • 具有必要許可的 IAM 執行角色。請參閱設定您的登入資料。

  • 模型存取:Amazon Bedrock 主控台中啟用 Anthropic Claude Sonnet 4.0。如需搭配 Strands Agents 使用不同模型的詳細資訊,請參閱 Strands Agents SDK 文件中的模型提供者一節。

  • AWS 可使用 Amazon Bedrock AgentCore 的區域。請參閱支援的 AWS 區域

設定您的憑證

執行下列步驟來設定您的 AWS 登入資料並連接必要的許可。

  1. 驗證您的 AWS 登入資料

    確認您的 AWS 登入資料已設定:

    aws sts get-caller-identity
    注意

    請記下此處傳回的使用者或登入資料,因為您將在連接所需許可時使用它。

    如果此命令失敗,請設定您的登入資料。如需詳細資訊,請參閱 CLI AWS 中的組態和登入資料檔案設定

  2. 連接必要的許可

    您的 IAM 使用者或角色需要許可才能使用 AgentCore Code Interpreter。將此政策連接至您的 IAM 身分:

    注意

    將 取代<region>為您選擇的區域 us-west-2 (例如 ),並將 <account_id>取代為下列政策中的 AWS 帳戶 ID:

    { "Version":"2012-10-17", "Statement": [ { "Sid": "BedrockAgentCoreCodeInterpreterFullAccess", "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:<region>:++<account_id>++:code-interpreter/*" } ] }

連接此政策

請遵循下列步驟:

  1. 前往 IAM 主控台。

  2. 從針對 get-caller-identity API 操作傳回的回應中尋找您的使用者或角色。

  3. 選擇新增許可,然後選擇建立內嵌政策

  4. 切換到 JSON 檢視並貼上上面的政策

  5. 命名AgentCoreCodeInterpreterAccess並儲存

注意

如果您要將代理程式部署到 Amazon Bedrock AgentCore 執行期,您也需要使用服務信任政策建立 IAM 執行角色。如需詳細資訊,請參閱開始使用 AgentCore 執行期

下列各節說明如何使用 Amazon Bedrock AgentCore Code Interpreter 搭配和不使用代理程式架構。當您想要以程式設計方式執行特定程式碼片段時,直接使用程式碼解譯器而不使用代理程式架構特別有用。