

# Generate, run, and test code for your application by enabling code interpretation
<a name="agents-code-interpretation"></a>

The code interpretation enables your agent to generate, run, and troubleshoot your application code in a secure test environment. With code interpretation you can use the agent’s foundation model to generate code for implementing basic capabilities while you focus on building generative AI applications. 

You can perform the following tasks with code interpretation in Amazon Bedrock:
+ Understand user requests for specific tasks, generate code that can perform the tasks requested by the user , execute the code, and provide the result from the code execution.
+ Understand user’s generic queries, generate and run code to provide response to the user.
+ Generate code for performing analysis, visualization, and evaluation of the data.
+ Extract information from the files uploaded by the user, process the information and answer user queries. 
+ Generate code based on the interactive conversations with the user for rapid prototyping. 

By default, the maximum number of concurrently active code interpretation per session per AWS account is 25. This means , each AWS account can have up to 25 ongoing conversations with the agents at once using code interpreter.

The following are some of the use cases where code interpretation can help by generating and running the code within an Amazon Bedrock 

1. Analyzing financial transactions from a data file such as a .csv to determine if they resulted a profit or a loss. 

1. Converting date format, such as *14th March 2020* to standard API format `YYYY-MM-DD` for file formats such as .txt or .csv

1. Performing data analysis on a spreadsheet (XLS) to calculate metrics such as quarterly/yearly company revenues or population growth rate.

To use the code interpretation in Amazon Bedrock, perform the following steps, 
+ Enable code interpretation when you build your agent. Once you’ve enabled code interpretation, you can start to use it.
+ Start using code interpretation in Amazon Bedrock by providing prompts. For example you can ask “calculate the square root of pi to 127 digits”. Code interpretation will generate and run python code to provide a response.
+ You can also attach files. You can use the information in the files to ask questions and summarize or analyze data. You can attach the files from either your computer or from Amazon S3 bucket.

**Supported Regions**

Code Interpretation for Amazon Bedrock Agents is supported in the following Regions:


****  

| Region | 
| --- | 
| US East (N.Virginia) | 
| US West (Oregon) | 
| Europe (Frankfurt) | 

**File support**

With code interpretation, you can attach files and then use the attached files to ask questions and summarize or analyze data that’s based on the content of the attached files. 

You can attach a maximum of 5 files. The total size of all the files can be up to 10 MB.
+ **Supported input file types**: CSV, XLS, XLSX, YAML, JSON, DOC, DOCX, HTML, MD, TXT, and PDF 
+ **Supported output file types**: CSV, XLS, XLSX, YAML, JSON, DOC, DOCX, HTML, MD, TXT, PDF, and PNG 

# Enable code interpretation in Amazon Bedrock
<a name="agents-enable-code-interpretation"></a>

You can enable code interpretation in the Amazon Bedrock console when you [create](https://docs.aws.amazon.com//bedrock/latest/userguide/agents-create.html) or [update](https://docs.aws.amazon.com//bedrock/latest/userguide/agents-manage.html#agents-edit) your agent. If you are using API or SDKs, you can enable code interpretation when you [create](https://docs.aws.amazon.com//bedrock/latest/APIReference/API_agent_CreateAgentActionGroup.html) or [update](https://docs.aws.amazon.com//bedrock/latest/APIReference/API_agent_UpdateAgentActionGroup.html) action group.

To learn how to enable code interpretation in Amazon Bedrock, choose the tab for your preferred method, and then follow the steps:

------
#### [ Console ]

**To enable code interpretation for your agent**

1. If you're not already in the agent builder, do the following:

   1. Sign in to the AWS Management Console with an IAM identity that has permissions to use the Amazon Bedrock console. Then, open the Amazon Bedrock console at [https://console.aws.amazon.com/bedrock](https://console.aws.amazon.com/bedrock).

   1. Select **Agents** from the left navigation pane. Then, choose an agent in the **Agents** section.

   1. Choose **Edit in Agent Builder**

1. Go to **Additional settings** and expand the section.

1. For **Code Interpreter**, select **Enable**.

1. Make sure to first **Save** and then **Prepare** to apply the changes you have made to the agent before testing it.

------
#### [ API ]

To enable code interpretation for your agent, send an [CreateActionGroup](https://docs.aws.amazon.com//bedrock/latest/APIReference/API_agent_CreateAgentActionGroup.html) request (see link for request and response formats and field details) with an [Agents for Amazon Bedrock build-time endpoint](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#bra-bt) and specify the following fields:


****  

| Field | Short description | 
| --- | --- | 
| actionGroupName | Name of the action group | 
| parentActionGroupSignature | Specify AMAZON.CodeInterpreter to allow the agent to generate and test code | 
| actionGroupState | Specify ENABLED to allow the agent to invoke code interpretation | 

The following shows the general format of the required fields for enabling code interpretation with an [CreateActionGroup](https://docs.aws.amazon.com//bedrock/latest/APIReference/API_agent_CreateAgentActionGroup.html) request.

```
CreateAgentActionGroup:
{
  "actionGroupName": "CodeInterpreterAction",
  "parentActionGroupSignature": "AMAZON.CodeInterpreter",
  "actionGroupState": "ENABLED"
}
```

------

# Test code interpretation in Amazon Bedrock
<a name="agents-test-code-interpretation"></a>

Before you test code interpretation in Amazon Bedrock, make sure to prepare your agent to apply the changes you’ve just made.

With code interpretation enabled, when you start to test your agent, you can optionally attach files and choose how you want the files you attach to be used by code interpretation. Depending on your use case, you can ask code interpretation to use the information in the attached files to summarize the contents of the file and to answer queries about the file content during an interactive chat conversation. Or, you can ask code interpretation to analyze the content in the attached files and provide metrics and data visualization reports.

**Attach files**

To learn how to attach files for code interpretation, choose the tab for your preferred method, and then follow the steps:

------
#### [ Console ]

**To attach files for code interpretation,**

1. If you're not already in the agent builder, do the following:

   1. Sign in to the AWS Management Console with an IAM identity that has permissions to use the Amazon Bedrock console. Then, open the Amazon Bedrock console at [https://console.aws.amazon.com/bedrock](https://console.aws.amazon.com/bedrock).

   1. Select **Agents** from the left navigation pane. Then, choose an agent in the **Agents** section.

   1. Choose **Edit in Agent Builder**

   1. Expand **Additional settings** and confirm that **Code Interpreter** is enabled.

   1. Make sure agent is prepared.

1. If test window is not open, choose **Test**.

1. In the bottom of the test window, select the paper clip icon to attach files.

1. In the **Attach files** page,

   1. 

**For **Choose function**, specify the following:**
      + If you are attaching files for the agent to use to answer your queries and summarize content, choose **Attach files to chat (faster)**.
      + If you are attaching files for code interpretation to analyze the content and provide metrics, choose **Attach files to code interpreter**.

   1. 

**For **Choose upload method**, choose from where you want to upload your files:**
      + If you are uploading from your computer, choose **Choose files** and select files to attach.
      + If you are uploading from Amazon S3, choose **Browse S3**, select files, choose **Choose**, and then choose **Add**.

1. Choose **Attach**.

------
#### [ API ]

To test code interpretation, send an [InvokeAgent](https://docs.aws.amazon.com//bedrock/latest/APIReference/API_agent_InvokeAgent.html) request (see link for request and response formats and field details) with an [Agents for Amazon Bedrock build-time endpoint](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#bra-bt).

**To attach files for agent to use for answering your queries and summarizing the content, specify the following fields:**


****  

| Field | Short description | 
| --- | --- | 
| name | Name of the attached file. | 
| sourceType | Location of the file to be attached. Specify s3 if your file is located in Amazon S3 bucket. Specify byte\$1content if your file is located on your computer. | 
| S3Location | The S3 path where your file is located. Required if the sourceType is S3. | 
| mediaType | File type of the attached file. **Supported input file types**: CSV, XLS, XLSX, YAML, JSON, DOC, DOCX, HTML, MD, TXT, and PDF   | 
| data | Base64 encoded string. Max file size 10MB. If you are using SDK, you just need to provide file byte content. AWS SDK automatically encodes strings to base64. | 
| useCase | How you want the attached files to be used. Valid values: CHAT \$1 CODE\$1INTERPRETER | 

 The following example shows the general format for specifying the required fields to attach files to chat.

```
                  
"sessionState": {
        "promptSessionAttributes": {
            "string": "string"
        },
        "sessionAttributes": {
            "string": "string"
        },
        "files": [ 
            {
                "name": "banking_data", 
                "source": { 
                    "sourceType": "S3", 
                    "s3Location": 
                        "uri": "s3Uri" 
                    }
                },
                "useCase": "CHAT" 
            },
            {
                "name": "housing_stats.csv", 
                "source": { 
                    "sourceType": "BYTE_CONTENT", 
                    "byteContent": {
                        "mediaType": "text/csv", 
                        "data": "file byte content"
                    }
                },
                "useCase": "CHAT"
            }
        ]
    }
```

The following example shows the general format for specifying the required fields to attach files for code interpretation.

```
"sessionState": {
        "promptSessionAttributes": {
            "string": "string"
        },
        "sessionAttributes": {
            "string": "string"
        },
        "files": [ 
            {
                "name": "banking_data", 
                "source": { 
                    "sourceType": "S3",
                    "s3Location": {
                        "uri": "s3Uri" 
                    }
                },
                "useCase": "CODE_INTERPRETER" 
            },
            {
                "name": "housing_stats.csv", 
                "source": { 
                    "sourceType": "BYTE_CONTENT", 
                    "byteContent": {
             "mediaType": "text/csv", 
             "data": "file byte content" 
                    }
                },
                "useCase": "CODE_INTERPRETER"
            }
        ]
    }
```

------

# Disable code interpretation in Amazon Bedrock
<a name="agents-disable-code-interpretation"></a>

You can disable code interpretation in Amazon Bedrock at any time.

To learn how to disable code interpretation, choose the tab for your preferred method, and then follow the steps:

------
#### [ Console ]

**To disable code interpretation,**

1. Sign in to the AWS Management Console with an IAM identity that has permissions to use the Amazon Bedrock console. Then, open the Amazon Bedrock console at [https://console.aws.amazon.com/bedrock](https://console.aws.amazon.com/bedrock).

1. Select **Agents** from the left navigation pane. Then, choose an agent in the **Agents** section.

1. Choose **Edit in Agent builder**.

1. Expand **Additional setting** section, choose **Disable** for **Code Interpreter**.

1. Select **Prepare** at the top of the page. And then select **Save** to save the changes to your agent.

------
#### [ API ]

To disable code interpretation, send an [UpdateAgentActionGroup](https://docs.aws.amazon.com//bedrock/latest/APIReference/API_agent_UpdateAgentActionGroup.html) request (see link for request and response formats and field details) with an [Agents for Amazon Bedrock build-time endpoint](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#bra-bt) and specify the following fields:


****  

| Field | Short description | 
| --- | --- | 
| actionGroupName | Name of the action group | 
| parentActionGroupSignature | Specify AMAZON.CodeInterpreter to disable the code interpreter | 
| actionGroupState | Specify DISABLED to disable the code interpreter | 

The following example shows the general format for specifying the required fields to disable code interpretation.

```
UpdateAgentActionGroup:
{
  "actionGroupName": "CodeInterpreterAction",
  "parentActionGroupSignature": "AMAZON.CodeInterpreter",
  "actionGroupState": "DISABLED"
}
```

------

After you've disabled code interpretation for your agent, make sure to send a [PrepareAgent](https://docs.aws.amazon.com//bedrock/latest/APIReference/API_agent_PrepareAgent.html) request (see link for request and response formats and field details) with an [Agents for Amazon Bedrock build-time endpoint](https://docs.aws.amazon.com/general/latest/gr/bedrock.html#bra-bt).