

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

# 创建 AgentCore 代码解释器
<a name="code-interpreter-create"></a>

您可以使用亚马逊 Bedrock AgentCore 控制台、 Amazon CLI 或 Amazon SDK 创建代码解释器。

**Example**  

1. ===== 使用控制台创建代码解释器

1. 在 [ https://console.aws.amazon.com/bedrock-agentcore/home \# 处打开 AgentCore 控制台](https://console.amazonaws.cn/bedrock-agentcore/home#)。

1. 在导航窗格中，选择**Built-in 工具**。

1. 选择 “**创建代码解释器工具” **。

1. 提供唯一的**工具名称**和可选**描述**。

1. 在 “**网络设置” 下**，选择以下选项之一：
   +  **沙盒 **-外部网络访问受限的环境
   +  **公共网络 **-允许访问公共互联网资源

1. 在 “**权限” 下**，指定 IAM 运行时角色来定义代码解释器可以访问的 Amazon 资源。

1. 选择**创建**。

   创建代码解释器工具后，控制台会显示有关该工具的重要详细信息：
<Region>唯一标识代码解释器工具资源的亚马逊资源名称（ARN）（例如 arn: aws: bedrock-agentcore:: 123456789012: code-interpreter-custom）。interpreter/code
代码解释器工具的唯一标识符，用于 API 调用（例如，code-interpreter-custom-abc123）。
代码解释器在执行代码时扮演的 IAM 角色，决定它可以访问哪些 Amazon 资源。
代码解释器（沙盒或公共）的网络配置。
代码解释器工具的创建日期和时间。

1. 要使用 Amazon CLI 创建代码解释器，请使用以下`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. 要使用 Amazon 适用于 Python 的软件开发工具包创建代码解释器，请使用以下`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 创建新的代码解释器实例，请使用以下调用：

   ```
   # 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"
       }
   }'
   ```