

# 代码解释器 API 参考示例
<a name="code-interpreter-api-reference-examples"></a>

本节提供了使用不同方法的常见代码解释器操作的参考示例。每个示例都展示了如何使用 AWS CLI、Boto3 SDK 和直接 API 调用执行相同的操作。

## 代码执行
<a name="code-interpreter-api-code-execution"></a>

这些示例演示了如何在代码解释器会话中执行代码。

**Example**  

1. 

   ```
   params = {
         "language": "python",
         "code": "print(\"Hello, world!\")"
       }
   
   client.invoke_code_interpreter(
       **{
           "codeInterpreterIdentifier": "aws.codeinterpreter.v1",
           "sessionId": "<your-session-id>",
           "name": "executeCode",
           "arguments": params
       })
   ```

1. 

   ```
   # Using awscurl
   awscurl -X POST \
     "https://bedrock-agentcore.<Region>.amazonaws.com/code-interpreters/aws.codeinterpreter.v1/tools/invoke" \
     -H "Content-Type: application/json" \
     -H "Accept: application/json" \
     -H "x-amzn-code-interpreter-session-id: your-session-id" \
     --service bedrock-agentcore \
     --region <Region> \
     -d '{
       "name": "executeCode",
       "arguments": {
         "language": "python",
         "code": "print(\"Hello, world!\")"
       }
     }'
   ```

## 终端命令
<a name="code-interpreter-api-terminal-commands"></a>

这些示例演示了如何在代码解释器会话中执行终端命令。

### 执行命令
<a name="code-interpreter-api-execute-command"></a>

**Example**  

1. 

   ```
   params = {
         "command": "ls -l"
       }
   
   client.invoke_code_interpreter(
       **{
           "codeInterpreterIdentifier": "aws.codeinterpreter.v1",
           "sessionId": "<your-session-id>",
           "name": "executeCommand",
           "arguments": params
       })
   ```

1. 

   ```
   # Using awscurl
   awscurl -X POST \
     "https://bedrock-agentcore.<Region>.amazonaws.com/code-interpreters/aws.codeinterpreter.v1/tools/invoke" \
     -H "Content-Type: application/json" \
     -H "Accept: application/json" \
     -H "x-amzn-code-interpreter-session-id: your-session-id" \
     --service bedrock-agentcore \
     --region <Region> \
     -d '{
       "name": "executeCommand",
       "arguments": {
         "command": "ls -l"
       }
     }'
   ```

### 开始执行命令
<a name="code-interpreter-api-start-command"></a>

**Example**  

1. 

   ```
   params = {
         "command": "sleep 15 && echo Task completed successfully"
       }
   
   client.invoke_code_interpreter(
       **{
           "codeInterpreterIdentifier": "aws.codeinterpreter.v1",
           "sessionId": "<your-session-id>",
           "name": "startCommandExecution",
           "arguments": params
       })
   ```

1. 

   ```
   # Using awscurl
   awscurl -X POST \
     "https://bedrock-agentcore.<Region>.amazonaws.com/code-interpreters/aws.codeinterpreter.v1/tools/invoke" \
     -H "Content-Type: application/json" \
     -H "Accept: application/json" \
     -H "x-amzn-code-interpreter-session-id: your-session-id" \
     --service bedrock-agentcore \
     --region <Region> \
     -d '{
       "name": "startCommandExecution",
       "arguments": {
         "command": "sleep 15 && echo Task completed successfully"
       }
     }'
   ```

### 获取任务
<a name="code-interpreter-api-get-task"></a>

**Example**  

1. 

   ```
   params = {
         "taskId": "<your-task-id>"
       }
   
   client.invoke_code_interpreter(
       **{
           "codeInterpreterIdentifier": "aws.codeinterpreter.v1",
           "sessionId": "<your-session-id>",
           "name": "getTask",
           "arguments": params
       })
   ```

1. 

   ```
   # Using awscurl
   awscurl -X POST \
     "https://bedrock-agentcore.<Region>.amazonaws.com/code-interpreters/aws.codeinterpreter.v1/tools/invoke" \
     -H "Content-Type: application/json" \
     -H "Accept: application/json" \
     -H "x-amzn-code-interpreter-session-id: your-session-id" \
     --service bedrock-agentcore \
     --region <Region> \
     -d '{
       "name": "getTask",
       "arguments": {
         "taskId": "<your-task-id>"
       }
     }'
   ```

### 停止命令执行任务
<a name="code-interpreter-api-stop-task"></a>

**Example**  

1. 

   ```
   params = {
         "taskId": "<your-task-id>"
       }
   
   client.invoke_code_interpreter(
       **{
           "codeInterpreterIdentifier": "aws.codeinterpreter.v1",
           "sessionId": "<your-session-id>",
           "name": "stopTask",
           "arguments": params
       })
   ```

1. 

   ```
   # Using awscurl
   awscurl -X POST \
     "https://bedrock-agentcore.<Region>.amazonaws.com/code-interpreters/aws.codeinterpreter.v1/tools/invoke" \
     -H "Content-Type: application/json" \
     -H "Accept: application/json" \
     -H "x-amzn-code-interpreter-session-id: your-session-id" \
     --service bedrock-agentcore \
     --region <Region> \
     -d '{
       "name": "stopTask",
       "arguments": {
         "taskId": "<your-task-id>"
       }
     }'
   ```

## 文件管理
<a name="code-interpreter-api-file-management"></a>

这些示例演示了如何在代码解释器会话中管理文件。

### 写文件
<a name="code-interpreter-api-write-files"></a>

**Example**  

1. 

   ```
   params = {
           "content": [{"path": "dir1/samename.txt", "text": "File in dir1"}]
       }
   
   client.invoke_code_interpreter(
       **{
           "codeInterpreterIdentifier": "aws.codeinterpreter.v1",
           "sessionId": "<your-session-id>",
           "name": "writeFiles",
           "arguments": params
       })
   ```

1. 

   ```
   # Using awscurl
   awscurl -X POST \
     "https://bedrock-agentcore.<Region>.amazonaws.com/code-interpreters/aws.codeinterpreter.v1/tools/invoke" \
     -H "Content-Type: application/json" \
     -H "Accept: application/json" \
     -H "x-amzn-code-interpreter-session-id: your-session-id" \
     --service bedrock-agentcore \
     --region <Region> \
     -d '{
       "name": "writeFiles",
       "arguments": {
         "content": [{"path": "dir1/samename.txt", "text": "File in dir1"}]
       }
     }'
   ```

### 读取文件
<a name="code-interpreter-api-read-files"></a>

**Example**  

1. 

   ```
   params = {
         "paths": ["tmp.txt"]
       }
   
   client.invoke_code_interpreter(
       **{
           "codeInterpreterIdentifier": "aws.codeinterpreter.v1",
           "sessionId": "<your-session-id>",
           "name": "readFiles",
           "arguments": params
       })
   ```

1. 

   ```
   # Using awscurl
   awscurl -X POST \
     "https://bedrock-agentcore.<Region>.amazonaws.com/code-interpreters/aws.codeinterpreter.v1/tools/invoke" \
     -H "Content-Type: application/json" \
     -H "Accept: application/json" \
     -H "x-amzn-code-interpreter-session-id: your-session-id" \
     --service bedrock-agentcore \
     --region <Region> \
     -d '{
       "name": "readFiles",
       "arguments": {
         "paths": ["tmp.txt"]
       }
     }'
   ```

### 移除文件
<a name="code-interpreter-api-remove-files"></a>

**Example**  

1. 

   ```
   params = {
         "paths": ["tmp.txt"]
       }
   
   client.invoke_code_interpreter(
       **{
           "codeInterpreterIdentifier": "aws.codeinterpreter.v1",
           "sessionId": "<your-session-id>",
           "name": "removeFiles",
           "arguments": params
       })
   ```

1. 

   ```
   # Using awscurl
   awscurl -X POST \
     "https://bedrock-agentcore.<Region>.amazonaws.com/code-interpreters/aws.codeinterpreter.v1/tools/invoke" \
     -H "Content-Type: application/json" \
     -H "Accept: application/json" \
     -H "x-amzn-code-interpreter-session-id: your-session-id" \
     --service bedrock-agentcore \
     --region <Region> \
     -d '{
       "name": "removeFiles",
       "arguments": {
         "paths": ["tmp.txt"]
       }
     }'
   ```

### 列出文件
<a name="code-interpreter-api-list-files"></a>

**Example**  

1. 

   ```
   params = {
         "directoryPath": ""
       }
   
   client.invoke_code_interpreter(
       **{
           "codeInterpreterIdentifier": "aws.codeinterpreter.v1",
           "sessionId": "<your-session-id>",
           "name": "listFiles",
           "arguments": params
       })
   ```

1. 

   ```
   # Using awscurl
   awscurl -X POST \
     "https://bedrock-agentcore.<Region>.amazonaws.com/code-interpreters/aws.codeinterpreter.v1/tools/invoke" \
     -H "Content-Type: application/json" \
     -H "Accept: application/json" \
     -H "x-amzn-code-interpreter-session-id: your-session-id" \
     --service bedrock-agentcore \
     --region <Region> \
     -d '{
       "name": "listFiles",
       "arguments": {
         "directoryPath": ""
       }
     }'
   ```