執行程式碼
啟動 Code Interpreter 工作階段後,您可以在工作階段中執行程式碼。
範例
- Boto3
-
-
若要使用適用於 Python 的 AWS SDK 執行程式碼,請使用
invoke_code_interpreter方法: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
-
-
若要使用 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 '{ "id": "1", "name": "executeCode", "arguments": { "language": "python", "code": "print(\"Hello, world!\")" } }'
-
啟動 AgentCore Code Interpreter 工作階段
執行時間選擇