Esempi di riferimento dell'API Code Interpreter
Questa sezione fornisce esempi di riferimento per le operazioni più comuni di Code Interpreter che utilizzano approcci diversi. Ogni esempio mostra come eseguire la stessa operazione utilizzando AWS CLI, Boto3 SDK e chiamate API dirette.
Esecuzione del codice
Questi esempi dimostrano come eseguire il codice in una sessione di Code Interpreter.
Esempio
- 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!\")" } }'
-
Comandi del terminale
Questi esempi dimostrano come eseguire i comandi del terminale in una sessione di Code Interpreter.
Esegui comando
Esempio
- 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" } }'
-
Avvia l'esecuzione del comando
Esempio
- 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" } }'
-
Ottieni attività
Esempio
- 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>" } }'
-
Interrompi l'attività di esecuzione del comando
Esempio
- 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>" } }'
-
Gestione dei file
Questi esempi dimostrano come gestire i file in una sessione di Code Interpreter.
Scrivi file
Esempio
- 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"}] } }'
-
Leggi i file
Esempio
- 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"] } }'
-
Rimuovi file
Esempio
- 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"] } }'
-
Elenca i file
Esempio
- 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": "" } }'
-
Interruzione di una AgentCore sessione di Code Interpreter
Osservabilità