

# AgentCore Code Interpreter 入門
<a name="code-interpreter-getting-started"></a>

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

此頁面涵蓋先決條件，並提供兩種開始使用的方法：
+  **使用 AWS Strands** - 高階代理程式架構，可透過內建的工具整合、對話管理和自動工作階段處理，簡化建置 AI 代理程式。
+  **直接使用** - SDK 和 Boto3 方法，可為自訂實作提供更多工作階段管理和程式碼執行的控制權。

**Topics**
+ [先決條件](#code-interpreter-prerequisites)
+ [設定您的憑證](#code-interpreter-credentials-config)
+ [透過 AWS Strands 使用 AgentCore Code Interpreter](code-interpreter-using-strands.md)
+ [直接使用 AgentCore Code Interpreter](code-interpreter-using-directly.md)

## 先決條件
<a name="code-interpreter-prerequisites"></a>

開始之前，請確定您已：
+  AWS 已設定登入資料的 帳戶。請參閱[設定您的登入](#code-interpreter-credentials-config)資料。
+ 已安裝 Python 3.10\+
+ 已安裝 Boto3。請參閱 [Boto3 文件](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html)。
+ 具有必要許可的 IAM 執行角色。請參閱[設定您的登入](#code-interpreter-credentials-config)資料。
+ 模型存取：Amazon Bedrock 主控台中[啟用](https://docs.aws.amazon.com/bedrock/latest/userguide/model-access-modify.html) Anthropic Claude Sonnet 4.0。如需搭配 Strands Agents 使用不同模型的詳細資訊，請參閱 [Strands Agents SDK 文件](https://strandsagents.com/latest/documentation/docs/)中的*模型提供者*一節。
+  AWS 可使用 Amazon Bedrock AgentCore 的區域。請參閱[支援的 AWS 區域](agentcore-regions.md)。

## 設定您的憑證
<a name="code-interpreter-credentials-config"></a>

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

1.  **驗證您的 AWS 登入資料** 

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

   ```
   aws sts get-caller-identity
   ```
**注意**  
請記下此處傳回的使用者或登入資料，因為您將在連接所需許可時使用它。

   如果此命令失敗，請設定您的登入資料。如需詳細資訊，請參閱 [CLI AWS 中的組態和登入資料檔案設定](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html)。

1.  **連接必要的許可** 

   您的 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 主控台。

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

1. 選擇**新增許可**，然後選擇**建立內嵌政策** 

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

1. 命名`AgentCoreCodeInterpreterAccess`並儲存

**注意**  
如果您要將代理程式部署到 Amazon Bedrock AgentCore 執行期，您也需要使用服務信任政策建立 IAM 執行角色。如需詳細資訊，請參閱[開始使用 AgentCore 執行期](runtime-getting-started.md)。

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