AWS bedrock-agentcore documentation change
Summary
Added code examples with context managers and explicit session lifecycle management
Security assessment
Demonstrates resource management best practices but doesn't specifically address security vulnerabilities
Diff
diff --git a/bedrock-agentcore/latest/devguide/code-interpreter-getting-started.md b/bedrock-agentcore/latest/devguide/code-interpreter-getting-started.md index 70392a04e..830e9d245 100644 --- a//bedrock-agentcore/latest/devguide/code-interpreter-getting-started.md +++ b//bedrock-agentcore/latest/devguide/code-interpreter-getting-started.md @@ -9 +9 @@ Amazon Bedrock AgentCore is in preview release and is subject to change. -# Getting started with AgentCore Code Interpreter by running a hello world example +# Get started with Amazon Bedrock AgentCore Code Interpreter @@ -11 +11 @@ Amazon Bedrock AgentCore is in preview release and is subject to change. -The following sections show how you can get started with the AgentCore Code Interpreter tool. +The following sections show how you can get started with the AgentCore Code Interpreter tool by illustrating a hello world example. @@ -18,0 +19,2 @@ Before using the AgentCore Code Interpreter, ensure you meet the following requi + * Python 3.10+ installed + @@ -24 +26 @@ Before using the AgentCore Code Interpreter, ensure you meet the following requi -Install the necessary packages: +Install the necessary packages. This guide uses the AgentCore starter toolkit for streamlined integration: @@ -39,0 +42,26 @@ You can quickly get started with the AgentCore Code Interpreter using either the +Using code_session + + +This example uses the `code_session` context manager for automatic session management: + + + from bedrock_agentcore.tools.code_interpreter_client import code_session + + # Create a code interpreter session using the context manager + with code_session("<Region>") as client: + # The session is automatically created and managed + print(f"Code interpreter session created") + + # Execute Python code + result = client.invoke("executeCode", { + "language": "python", + "code": 'print("Hello World!!!")' + }) + + # Process the streaming results + for event in result["stream"]: + print(event["result"]["content"]) + + # The session is automatically closed when exiting the context manager + + @@ -117,0 +146,25 @@ This example uses the Amazon Bedrock AgentCore SDK, which provides a more stream +If you need more control over the session lifecycle, you can also use the client without a context manager: + + + from bedrock_agentcore.tools.code_interpreter_client import CodeInterpreter + + # Create a code interpreter client + client = CodeInterpreter(region="<Region>") + + # Start a code interpreter session + client.start() + + try: + # Use the code interpreter + result = client.invoke("executeCode", { + "language": "python", + "code": 'print("Hello World!!!")' + }) + for event in result["stream"]: + print(event["result"]["content"]) + + finally: + # Always close the session when done + client.stop() + +