View a markdown version of this page

Ejecutando código - Amazon Bedrock AgentCore

Ejecutando código

Una vez iniciada una sesión de intérprete de código, puede ejecutar el código en la sesión.

ejemplo
Boto3
  1. Para ejecutar código mediante el AWS SDK para Python, utilice el invoke_code_interpreter método:

    import boto3 import json # Initialize the boto3 client dp_client = boto3.client( 'bedrock-agentcore', region_name="<Region>", endpoint_url="https://bedrock-agentcore.<Region>.amazonaws.com" ) # Execute code in the Code Interpreter session response = dp_client.invoke_code_interpreter( codeInterpreterIdentifier="aws.codeinterpreter.v1", sessionId="<your-session-id>", name="executeCode", arguments={ "language": "python", "code": 'print("Hello World!!!")' } ) # Process the event stream for event in response["stream"]: if "result" in event: result = event["result"] if "content" in result: for content_item in result["content"]: if content_item["type"] == "text": print(content_item["text"])
API
  1. Para ejecutar código en una sesión de intérprete de código mediante la API, usa la siguiente llamada:

    # 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 '{ "id": "1", "name": "executeCode", "arguments": { "language": "python", "code": "print(\"Hello, world!\")" } }'