

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 建立 AgentCore 程式碼解譯器
<a name="code-interpreter-create"></a>

您可以使用 Amazon Bedrock AgentCore 主控台、 AWS CLI 或 AWS SDK 建立程式碼解譯器。

**Example**  

1. ====== 使用主控台建立程式碼解譯器

1. 在 https：//\# 開啟 AgentCore 主控台。 [https://console.aws.amazon.com/bedrock-agentcore/home](https://console.aws.amazon.com/bedrock-agentcore/home#)

1. 在導覽窗格中，選擇**內建工具**。

1. 選擇**建立程式碼解譯器工具**。

1. 提供唯一的**工具名稱**和選用**的描述**。

1. 在**網路設定** 下，選擇下列其中一個選項：
   +  **沙盒** - 具有有限外部網路存取的環境
   +  **公有網路** - 允許存取公有網際網路資源

1. 在**許可** 下，指定 IAM 執行期角色，以定義程式碼解譯器可存取 AWS 的資源。

1. 選擇**建立**。

   建立程式碼解譯器工具後，主控台會顯示工具的重要詳細資訊：
可唯一識別 Code Interpreter 工具資源的 Amazon Resource Name (ARN) （例如 arn：aws：bedrock-agentcore：<Region>：123456789012：code-interpreter/code-interpreter-custom)。
Code Interpreter 工具的唯一識別符，用於 API 呼叫 （例如 code-interpreter-custom-abc123)。
Code Interpreter 在執行程式碼時擔任的 IAM 角色，決定可以存取哪些 AWS 資源。
Code Interpreter (Sandbox 或 Public) 的網路組態。
建立 Code Interpreter 工具的日期和時間。

1. 若要使用 CLI AWS 建立程式碼解譯器，請使用 `create-code-interpreter`命令：

   ```
   aws bedrock-agentcore create-code-interpreter \
     --region <Region> \
     --name "my-code-interpreter" \
     --description "My Code Interpreter for data analysis" \
     --network-configuration '{
       "networkMode": "PUBLIC"
     }' \
     --execution-role-arn "arn:aws:iam::123456789012:role/my-execution-role"
   ```

1. 若要使用適用於 Python 的 AWS SDK 建立程式碼解譯器，請使用 `create_code_interpreter`方法：

   ```
   import boto3
   
   # Initialize the boto3 client
   cp_client = boto3.client(
       'bedrock-agentcore-control',
       region_name="<Region>",
       endpoint_url="https://bedrock-agentcore-control.<Region>.amazonaws.com"
   )
   
   # Create a Code Interpreter
   response = cp_client.create_code_interpreter(
       name="myTestSandbox1",
       description="Test code sandbox for development",
       executionRoleArn="arn:aws:iam::123456789012:role/my-execution-role",
       networkConfiguration={
           "networkMode": "PUBLIC"
       }
   )
   
   # Print the Code Interpreter ID
   code_interpreter_id = response["codeInterpreterId"]
   print(f"Code Interpreter ID: {code_interpreter_id}")
   ```

1. 若要使用 API 建立新的 Code Interpreter 執行個體，請使用下列呼叫：

   ```
   # Using awscurl
   awscurl -X PUT "https://bedrock-agentcore-control.<Region>.amazonaws.com/code-interpreters" \
   -H "Content-Type: application/json" \
   --region <Region> \
   --service bedrock-agentcore \
   -d '{
       "name": "codeinterpreter'$(date +%m%d%H%M%S)'",
       "description": "Test code sandbox for development",
       "executionRoleArn": "'${ROLE_ARN}'",
       "networkConfiguration": {
           "networkMode": "PUBLIC"
       }
   }'
   ```