View a markdown version of this page

程式碼解譯器 API 參考範例 - Amazon Bedrock AgentCore

程式碼解譯器 API 參考範例

本節提供使用不同方法的常見程式碼解譯器操作參考範例。每個範例都會示範如何使用 AWS CLI、Boto3 SDK 和直接 API 呼叫執行相同的操作。

程式碼執行

這些範例示範如何在 Code Interpreter 工作階段中執行程式碼。

範例
Boto3
  1. params = { "language": "python", "code": "print(\"Hello, world!\")" } client.invoke_code_interpreter( **{ "codeInterpreterIdentifier": "aws.codeinterpreter.v1", "sessionId": "<your-session-id>", "name": "executeCode", "arguments": params })
API
  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!\")" } }'

終端機命令

這些範例示範如何在 Code Interpreter 工作階段中執行終端機命令。

執行命令

範例
Boto3
  1. params = { "command": "ls -l" } client.invoke_code_interpreter( **{ "codeInterpreterIdentifier": "aws.codeinterpreter.v1", "sessionId": "<your-session-id>", "name": "executeCommand", "arguments": params })
API
  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" } }'

啟動命令執行

範例
Boto3
  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 })
API
  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" } }'

取得任務

範例
Boto3
  1. params = { "taskId": "<your-task-id>" } client.invoke_code_interpreter( **{ "codeInterpreterIdentifier": "aws.codeinterpreter.v1", "sessionId": "<your-session-id>", "name": "getTask", "arguments": params })
API
  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>" } }'

停止命令執行任務

範例
Boto3
  1. params = { "taskId": "<your-task-id>" } client.invoke_code_interpreter( **{ "codeInterpreterIdentifier": "aws.codeinterpreter.v1", "sessionId": "<your-session-id>", "name": "stopTask", "arguments": params })
API
  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>" } }'

檔案管理

這些範例示範如何在 Code Interpreter 工作階段中管理檔案。

寫入檔案

範例
Boto3
  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 })
API
  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"}] } }'

讀取檔案

範例
Boto3
  1. params = { "paths": ["tmp.txt"] } client.invoke_code_interpreter( **{ "codeInterpreterIdentifier": "aws.codeinterpreter.v1", "sessionId": "<your-session-id>", "name": "readFiles", "arguments": params })
API
  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"] } }'

移除檔案

範例
Boto3
  1. params = { "paths": ["tmp.txt"] } client.invoke_code_interpreter( **{ "codeInterpreterIdentifier": "aws.codeinterpreter.v1", "sessionId": "<your-session-id>", "name": "removeFiles", "arguments": params })
API
  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"] } }'

列出檔案

範例
Boto3
  1. params = { "directoryPath": "" } client.invoke_code_interpreter( **{ "codeInterpreterIdentifier": "aws.codeinterpreter.v1", "sessionId": "<your-session-id>", "name": "listFiles", "arguments": params })
API
  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": "" } }'