코드 해석기 API 참조 예제
이 섹션에서는 다양한 접근 방식을 사용하는 일반적인 코드 해석기 작업에 대한 참조 예제를 제공합니다. 각 예제에서는 AWS CLI, Boto3 SDK 및 직접 API 호출을 사용하여 동일한 작업을 수행하는 방법을 보여줍니다.
코드 실행
이 예제에서는 Code Interpreter 세션에서 코드를 실행하는 방법을 보여줍니다.
예
- Boto3
-
-
params = { "language": "python", "code": "print(\"Hello, world!\")" } client.invoke_code_interpreter( **{ "codeInterpreterIdentifier": "aws.codeinterpreter.v1", "sessionId": "<your-session-id>", "name": "executeCode", "arguments": params })
-
- API
-
-
# 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
-
-
params = { "command": "ls -l" } client.invoke_code_interpreter( **{ "codeInterpreterIdentifier": "aws.codeinterpreter.v1", "sessionId": "<your-session-id>", "name": "executeCommand", "arguments": params })
-
- API
-
-
# 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
-
-
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
-
-
# 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
-
-
params = { "taskId": "<your-task-id>" } client.invoke_code_interpreter( **{ "codeInterpreterIdentifier": "aws.codeinterpreter.v1", "sessionId": "<your-session-id>", "name": "getTask", "arguments": params })
-
- API
-
-
# 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
-
-
params = { "taskId": "<your-task-id>" } client.invoke_code_interpreter( **{ "codeInterpreterIdentifier": "aws.codeinterpreter.v1", "sessionId": "<your-session-id>", "name": "stopTask", "arguments": params })
-
- API
-
-
# 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
-
-
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
-
-
# 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
-
-
params = { "paths": ["tmp.txt"] } client.invoke_code_interpreter( **{ "codeInterpreterIdentifier": "aws.codeinterpreter.v1", "sessionId": "<your-session-id>", "name": "readFiles", "arguments": params })
-
- API
-
-
# 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
-
-
params = { "paths": ["tmp.txt"] } client.invoke_code_interpreter( **{ "codeInterpreterIdentifier": "aws.codeinterpreter.v1", "sessionId": "<your-session-id>", "name": "removeFiles", "arguments": params })
-
- API
-
-
# 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
-
-
params = { "directoryPath": "" } client.invoke_code_interpreter( **{ "codeInterpreterIdentifier": "aws.codeinterpreter.v1", "sessionId": "<your-session-id>", "name": "listFiles", "arguments": params })
-
- API
-
-
# 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": "" } }'
-
AgentCore 코드 해석기 세션 중지
관찰성