langchain-agentcore-codeinterpreter 0.0.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- langchain_agentcore_codeinterpreter-0.0.1/.gitignore +186 -0
- langchain_agentcore_codeinterpreter-0.0.1/LICENSE +21 -0
- langchain_agentcore_codeinterpreter-0.0.1/Makefile +84 -0
- langchain_agentcore_codeinterpreter-0.0.1/PKG-INFO +156 -0
- langchain_agentcore_codeinterpreter-0.0.1/README.md +141 -0
- langchain_agentcore_codeinterpreter-0.0.1/langchain_agentcore_codeinterpreter/__init__.py +5 -0
- langchain_agentcore_codeinterpreter-0.0.1/langchain_agentcore_codeinterpreter/sandbox.py +273 -0
- langchain_agentcore_codeinterpreter-0.0.1/pyproject.toml +100 -0
- langchain_agentcore_codeinterpreter-0.0.1/scripts/check_imports.py +18 -0
- langchain_agentcore_codeinterpreter-0.0.1/tests/__init__.py +0 -0
- langchain_agentcore_codeinterpreter-0.0.1/tests/integration_tests/__init__.py +0 -0
- langchain_agentcore_codeinterpreter-0.0.1/tests/integration_tests/test_compile.py +13 -0
- langchain_agentcore_codeinterpreter-0.0.1/tests/integration_tests/test_integration.py +97 -0
- langchain_agentcore_codeinterpreter-0.0.1/tests/unit_tests/__init__.py +0 -0
- langchain_agentcore_codeinterpreter-0.0.1/tests/unit_tests/test_imports.py +17 -0
- langchain_agentcore_codeinterpreter-0.0.1/tests/unit_tests/test_sandbox.py +206 -0
- langchain_agentcore_codeinterpreter-0.0.1/tests/unit_tests/test_stream_parsing.py +243 -0
- langchain_agentcore_codeinterpreter-0.0.1/uv.lock +2378 -0
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
.vs/
|
|
2
|
+
.vscode/
|
|
3
|
+
.idea/
|
|
4
|
+
|
|
5
|
+
# Byte-compiled / optimized / DLL files
|
|
6
|
+
__pycache__/
|
|
7
|
+
*.py[cod]
|
|
8
|
+
*$py.class
|
|
9
|
+
|
|
10
|
+
# C extensions
|
|
11
|
+
*.so
|
|
12
|
+
|
|
13
|
+
# Distribution / packaging
|
|
14
|
+
.Python
|
|
15
|
+
build/
|
|
16
|
+
develop-eggs/
|
|
17
|
+
dist/
|
|
18
|
+
downloads/
|
|
19
|
+
eggs/
|
|
20
|
+
.eggs/
|
|
21
|
+
lib/
|
|
22
|
+
lib64/
|
|
23
|
+
parts/
|
|
24
|
+
sdist/
|
|
25
|
+
var/
|
|
26
|
+
wheels/
|
|
27
|
+
pip-wheel-metadata/
|
|
28
|
+
share/python-wheels/
|
|
29
|
+
*.egg-info/
|
|
30
|
+
.installed.cfg
|
|
31
|
+
*.egg
|
|
32
|
+
MANIFEST
|
|
33
|
+
|
|
34
|
+
# Google GitHub Actions credentials files created by:
|
|
35
|
+
# https://github.com/google-github-actions/auth
|
|
36
|
+
#
|
|
37
|
+
# That action recommends adding this gitignore to prevent accidentally committing keys.
|
|
38
|
+
gha-creds-*.json
|
|
39
|
+
|
|
40
|
+
# PyInstaller
|
|
41
|
+
# Usually these files are written by a python script from a template
|
|
42
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
43
|
+
*.manifest
|
|
44
|
+
*.spec
|
|
45
|
+
|
|
46
|
+
# Installer logs
|
|
47
|
+
pip-log.txt
|
|
48
|
+
pip-delete-this-directory.txt
|
|
49
|
+
|
|
50
|
+
# Unit test / coverage reports
|
|
51
|
+
htmlcov/
|
|
52
|
+
.tox/
|
|
53
|
+
.nox/
|
|
54
|
+
.coverage
|
|
55
|
+
.coverage.*
|
|
56
|
+
.cache
|
|
57
|
+
nosetests.xml
|
|
58
|
+
coverage.xml
|
|
59
|
+
*.cover
|
|
60
|
+
*.py,cover
|
|
61
|
+
.hypothesis/
|
|
62
|
+
.pytest_cache/
|
|
63
|
+
.mypy_cache_test/
|
|
64
|
+
|
|
65
|
+
# Translations
|
|
66
|
+
*.mo
|
|
67
|
+
*.pot
|
|
68
|
+
|
|
69
|
+
# Django stuff:
|
|
70
|
+
*.log
|
|
71
|
+
local_settings.py
|
|
72
|
+
db.sqlite3
|
|
73
|
+
db.sqlite3-journal
|
|
74
|
+
|
|
75
|
+
# Flask stuff:
|
|
76
|
+
instance/
|
|
77
|
+
.webassets-cache
|
|
78
|
+
|
|
79
|
+
# Scrapy stuff:
|
|
80
|
+
.scrapy
|
|
81
|
+
|
|
82
|
+
# Sphinx documentation
|
|
83
|
+
docs/_build/
|
|
84
|
+
docs/docs/_build/
|
|
85
|
+
|
|
86
|
+
# PyBuilder
|
|
87
|
+
target/
|
|
88
|
+
|
|
89
|
+
# Jupyter Notebook
|
|
90
|
+
.ipynb_checkpoints
|
|
91
|
+
notebooks/
|
|
92
|
+
|
|
93
|
+
# IPython
|
|
94
|
+
profile_default/
|
|
95
|
+
ipython_config.py
|
|
96
|
+
|
|
97
|
+
# pyenv
|
|
98
|
+
.python-version
|
|
99
|
+
|
|
100
|
+
# pipenv
|
|
101
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
102
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
103
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
104
|
+
# install all needed dependencies.
|
|
105
|
+
#Pipfile.lock
|
|
106
|
+
|
|
107
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
|
108
|
+
__pypackages__/
|
|
109
|
+
|
|
110
|
+
# Celery stuff
|
|
111
|
+
celerybeat-schedule
|
|
112
|
+
celerybeat.pid
|
|
113
|
+
|
|
114
|
+
# SageMath parsed files
|
|
115
|
+
*.sage.py
|
|
116
|
+
|
|
117
|
+
# Environments
|
|
118
|
+
.env
|
|
119
|
+
.envrc
|
|
120
|
+
.venv*
|
|
121
|
+
venv*
|
|
122
|
+
env/
|
|
123
|
+
ENV/
|
|
124
|
+
env.bak/
|
|
125
|
+
|
|
126
|
+
# Spyder project settings
|
|
127
|
+
.spyderproject
|
|
128
|
+
.spyproject
|
|
129
|
+
|
|
130
|
+
# Rope project settings
|
|
131
|
+
.ropeproject
|
|
132
|
+
|
|
133
|
+
# mkdocs documentation
|
|
134
|
+
/site
|
|
135
|
+
|
|
136
|
+
# mypy
|
|
137
|
+
.mypy_cache/
|
|
138
|
+
.dmypy.json
|
|
139
|
+
dmypy.json
|
|
140
|
+
|
|
141
|
+
# Pyre type checker
|
|
142
|
+
.pyre/
|
|
143
|
+
|
|
144
|
+
# macOS display setting files
|
|
145
|
+
.DS_Store
|
|
146
|
+
|
|
147
|
+
# Wandb directory
|
|
148
|
+
wandb/
|
|
149
|
+
|
|
150
|
+
# asdf tool versions
|
|
151
|
+
.tool-versions
|
|
152
|
+
|
|
153
|
+
# ruff
|
|
154
|
+
/.ruff_cache/
|
|
155
|
+
.ruff_cache/
|
|
156
|
+
|
|
157
|
+
*.pkl
|
|
158
|
+
*.bin
|
|
159
|
+
|
|
160
|
+
# integration test artifacts
|
|
161
|
+
data_map*
|
|
162
|
+
\[('_type', 'fake'), ('stop', None)]
|
|
163
|
+
|
|
164
|
+
# Replit files
|
|
165
|
+
*replit*
|
|
166
|
+
|
|
167
|
+
node_modules
|
|
168
|
+
docs/.yarn/
|
|
169
|
+
docs/node_modules/
|
|
170
|
+
docs/.docusaurus/
|
|
171
|
+
docs/.cache-loader/
|
|
172
|
+
docs/_dist
|
|
173
|
+
docs/api_reference/*api_reference.rst
|
|
174
|
+
docs/api_reference/_build
|
|
175
|
+
docs/api_reference/*/
|
|
176
|
+
!docs/api_reference/_static/
|
|
177
|
+
!docs/api_reference/templates/
|
|
178
|
+
!docs/api_reference/themes/
|
|
179
|
+
docs/docs/build
|
|
180
|
+
docs/docs/node_modules
|
|
181
|
+
docs/docs/yarn.lock
|
|
182
|
+
_dist
|
|
183
|
+
docs/docs/templates
|
|
184
|
+
|
|
185
|
+
prof
|
|
186
|
+
.kiro/*
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) LangChain, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
######################
|
|
2
|
+
# NON FILE TARGETS
|
|
3
|
+
######################
|
|
4
|
+
.PHONY: all format lint test tests integration_tests help coverage_tests coverage_integration_tests coverage_report coverage_html
|
|
5
|
+
|
|
6
|
+
######################
|
|
7
|
+
# ALL TARGETS
|
|
8
|
+
######################
|
|
9
|
+
|
|
10
|
+
all: help ## Default target = help
|
|
11
|
+
|
|
12
|
+
.EXPORT_ALL_VARIABLES:
|
|
13
|
+
UV_FROZEN = true
|
|
14
|
+
|
|
15
|
+
######################
|
|
16
|
+
# TEST CASES
|
|
17
|
+
######################
|
|
18
|
+
|
|
19
|
+
# Define a variable for the test file path.
|
|
20
|
+
test test_watch tests: TEST_FILE ?= tests/unit_tests/
|
|
21
|
+
integration_test integration_tests: TEST_FILE = tests/integration_tests/
|
|
22
|
+
|
|
23
|
+
# Define a variable for Python and notebook files.
|
|
24
|
+
PYTHON_FILES=.
|
|
25
|
+
|
|
26
|
+
tests: ## Run all unit tests
|
|
27
|
+
env -u AWS_ACCESS_KEY_ID -u AWS_SECRET_ACCESS_KEY -u AWS_SESSION_TOKEN uv run --group test pytest --disable-socket --allow-unix-socket $(TEST_FILE)
|
|
28
|
+
|
|
29
|
+
test: ## Run individual unit test: make test TEST_FILE=tests/unit_tests/test.py
|
|
30
|
+
env -u AWS_ACCESS_KEY_ID -u AWS_SECRET_ACCESS_KEY -u AWS_SESSION_TOKEN uv run --group test pytest --disable-socket --allow-unix-socket $(TEST_FILE)
|
|
31
|
+
|
|
32
|
+
integration_tests: ## Run all integration tests
|
|
33
|
+
uv run --group test --group test_integration pytest -n auto $(TEST_FILE)
|
|
34
|
+
|
|
35
|
+
integration_test: ## Run individual integration test: make integration_test TEST_FILE=tests/integration_tests/integ_test.py
|
|
36
|
+
uv run --group test --group test_integration pytest -n auto $(TEST_FILE)
|
|
37
|
+
|
|
38
|
+
coverage_tests: ## Run unit tests with coverage
|
|
39
|
+
env -u AWS_ACCESS_KEY_ID -u AWS_SECRET_ACCESS_KEY -u AWS_SESSION_TOKEN uv run --group test pytest --cov=langchain_agentcore_codeinterpreter --cov-report=html --cov-report=term-missing --cov-branch tests/unit_tests/
|
|
40
|
+
|
|
41
|
+
coverage_integration_tests: ## Run integration tests with coverage
|
|
42
|
+
uv run --group test --group test_integration pytest --cov=langchain_agentcore_codeinterpreter --cov-report=html --cov-report=term-missing --cov-branch tests/integration_tests/
|
|
43
|
+
|
|
44
|
+
coverage_report: ## Generate coverage report
|
|
45
|
+
uv run --group test coverage report
|
|
46
|
+
|
|
47
|
+
coverage_html: ## Generate HTML coverage report
|
|
48
|
+
uv run --group test coverage html
|
|
49
|
+
|
|
50
|
+
######################
|
|
51
|
+
# LINTING AND FORMATTING
|
|
52
|
+
######################
|
|
53
|
+
|
|
54
|
+
# Define a variable for Python and notebook files.
|
|
55
|
+
PYTHON_FILES=.
|
|
56
|
+
MYPY_CACHE=.mypy_cache
|
|
57
|
+
lint format: PYTHON_FILES=.
|
|
58
|
+
lint_diff format_diff: PYTHON_FILES=$(shell git diff --relative=libs/agentcore-codeinterpreter --name-only --diff-filter=d main | grep -E '\.py$$|\.ipynb$$')
|
|
59
|
+
lint_package: PYTHON_FILES=langchain_agentcore_codeinterpreter
|
|
60
|
+
lint_tests: PYTHON_FILES=tests
|
|
61
|
+
lint_tests: MYPY_CACHE=.mypy_cache_test
|
|
62
|
+
|
|
63
|
+
lint lint_diff lint_package lint_tests: ## Run linter
|
|
64
|
+
[ "$(PYTHON_FILES)" = "" ] || uv run --all-groups ruff check $(PYTHON_FILES)
|
|
65
|
+
[ "$(PYTHON_FILES)" = "" ] || uv run --all-groups ruff format $(PYTHON_FILES) --diff
|
|
66
|
+
[ "$(PYTHON_FILES)" = "" ] || mkdir -p $(MYPY_CACHE) && uv run --all-groups mypy $(PYTHON_FILES) --cache-dir $(MYPY_CACHE)
|
|
67
|
+
|
|
68
|
+
format format_diff: ## Run code formatter
|
|
69
|
+
[ "$(PYTHON_FILES)" = "" ] || uv run --all-groups ruff format $(PYTHON_FILES)
|
|
70
|
+
[ "$(PYTHON_FILES)" = "" ] || uv run --all-groups ruff check --fix $(PYTHON_FILES)
|
|
71
|
+
|
|
72
|
+
######################
|
|
73
|
+
# DEPENDENCIES
|
|
74
|
+
######################
|
|
75
|
+
|
|
76
|
+
check_imports: $(shell find langchain_agentcore_codeinterpreter -name '*.py') ## Check missing imports
|
|
77
|
+
uv run --all-groups python ./scripts/check_imports.py $^
|
|
78
|
+
|
|
79
|
+
######################
|
|
80
|
+
# HELP
|
|
81
|
+
######################
|
|
82
|
+
|
|
83
|
+
help: ## Print this help
|
|
84
|
+
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: langchain-agentcore-codeinterpreter
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Amazon Bedrock AgentCore Code Interpreter sandbox integration for Deep Agents
|
|
5
|
+
Project-URL: Source Code, https://github.com/langchain-ai/langchain-aws/tree/main/libs/agentcore-codeinterpreter
|
|
6
|
+
Project-URL: Repository, https://github.com/langchain-ai/langchain-aws
|
|
7
|
+
License-Expression: MIT
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Keywords: agentcore,aws,bedrock,code-interpreter,deepagents,langchain,sandbox
|
|
10
|
+
Requires-Python: <4.0,>=3.11
|
|
11
|
+
Requires-Dist: bedrock-agentcore>=1.1.4
|
|
12
|
+
Requires-Dist: boto3>=1.42.42
|
|
13
|
+
Requires-Dist: deepagents>=0.1.0
|
|
14
|
+
Description-Content-Type: text/markdown
|
|
15
|
+
|
|
16
|
+
# langchain-agentcore-codeinterpreter
|
|
17
|
+
|
|
18
|
+
[](https://pypi.org/project/langchain-agentcore-codeinterpreter/#history)
|
|
19
|
+
[](https://opensource.org/licenses/MIT)
|
|
20
|
+
[](https://pypistats.org/packages/langchain-agentcore-codeinterpreter)
|
|
21
|
+
|
|
22
|
+
Amazon Bedrock AgentCore Code Interpreter sandbox integration for [Deep Agents](https://github.com/langchain-ai/deepagents).
|
|
23
|
+
|
|
24
|
+
This package provides `AgentCoreSandbox` — a [`SandboxBackendProtocol`](https://docs.langchain.com/oss/deepagents/sandboxes) implementation that wraps AgentCore's [Code Interpreter](https://docs.aws.amazon.com/bedrock/latest/userguide/agentcore-code-interpreter.html), a secure, isolated MicroVM environment for executing code. The caller manages the interpreter lifecycle (`start()` / `stop()`); the sandbox backend handles command execution and file operations.
|
|
25
|
+
|
|
26
|
+
> **Note:** For the LangChain `BaseTool` integration (used with `create_react_agent` and LangGraph agents), see [`langchain-aws[tools]`](https://github.com/langchain-ai/langchain-aws). This package is specifically for the Deep Agents `BaseSandbox` protocol.
|
|
27
|
+
|
|
28
|
+
## Prerequisites
|
|
29
|
+
|
|
30
|
+
**1. AWS credentials** configured via one of the following methods:
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
# Option 1: Long-lived IAM credentials
|
|
34
|
+
export AWS_ACCESS_KEY_ID="your-access-key"
|
|
35
|
+
export AWS_SECRET_ACCESS_KEY="your-secret-key"
|
|
36
|
+
export AWS_REGION="us-west-2"
|
|
37
|
+
|
|
38
|
+
# Option 2: Temporary credentials (IAM roles, SSO, STS AssumeRole)
|
|
39
|
+
export AWS_ACCESS_KEY_ID="your-access-key"
|
|
40
|
+
export AWS_SECRET_ACCESS_KEY="your-secret-key"
|
|
41
|
+
export AWS_SESSION_TOKEN="your-session-token"
|
|
42
|
+
export AWS_REGION="us-west-2"
|
|
43
|
+
|
|
44
|
+
# Option 3: AWS CLI profile (picks up ~/.aws/credentials + ~/.aws/config)
|
|
45
|
+
aws configure
|
|
46
|
+
# or for SSO:
|
|
47
|
+
aws configure sso
|
|
48
|
+
aws sso login --profile your-profile
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Any method supported by the [boto3 credential chain](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html) works, including EC2 instance profiles, ECS task roles, and environment variables.
|
|
52
|
+
|
|
53
|
+
**2. IAM permissions** — your credentials must allow `bedrock-agentcore:InvokeCodeInterpreter` (or the equivalent action for your region). See the [AgentCore Code Interpreter docs](https://docs.aws.amazon.com/bedrock/latest/userguide/agentcore-code-interpreter.html) for the required IAM policy.
|
|
54
|
+
|
|
55
|
+
**3. Region availability** — Code Interpreter is available in select AWS regions. `us-west-2` is a safe default. Pass the region to `CodeInterpreter(region=...)`.
|
|
56
|
+
|
|
57
|
+
## Quick Install
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
pip install langchain-agentcore-codeinterpreter
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Usage
|
|
64
|
+
|
|
65
|
+
### Standalone
|
|
66
|
+
|
|
67
|
+
```python
|
|
68
|
+
from bedrock_agentcore.tools.code_interpreter_client import CodeInterpreter
|
|
69
|
+
|
|
70
|
+
from langchain_agentcore_codeinterpreter import AgentCoreSandbox
|
|
71
|
+
|
|
72
|
+
interpreter = CodeInterpreter(region="us-west-2")
|
|
73
|
+
interpreter.start()
|
|
74
|
+
|
|
75
|
+
backend = AgentCoreSandbox(interpreter=interpreter)
|
|
76
|
+
|
|
77
|
+
result = backend.execute("echo hello")
|
|
78
|
+
print(result.output) # "hello"
|
|
79
|
+
|
|
80
|
+
interpreter.stop()
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### With Deep Agents
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
from bedrock_agentcore.tools.code_interpreter_client import CodeInterpreter
|
|
87
|
+
from deepagents import create_deep_agent
|
|
88
|
+
|
|
89
|
+
from langchain_agentcore_codeinterpreter import AgentCoreSandbox
|
|
90
|
+
from langchain_aws import ChatBedrockConverse
|
|
91
|
+
|
|
92
|
+
interpreter = CodeInterpreter(region="us-west-2")
|
|
93
|
+
interpreter.start()
|
|
94
|
+
|
|
95
|
+
model = ChatBedrockConverse(
|
|
96
|
+
model="us.anthropic.claude-sonnet-4-6",
|
|
97
|
+
region_name="us-west-2",
|
|
98
|
+
)
|
|
99
|
+
backend = AgentCoreSandbox(interpreter=interpreter)
|
|
100
|
+
agent = create_deep_agent(
|
|
101
|
+
model=model,
|
|
102
|
+
backend=backend,
|
|
103
|
+
system_prompt="You are a coding assistant with sandbox access.",
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
try:
|
|
107
|
+
result = agent.invoke(
|
|
108
|
+
{
|
|
109
|
+
"messages": [
|
|
110
|
+
{"role": "user", "content": "Create and run a hello world script"}
|
|
111
|
+
]
|
|
112
|
+
}
|
|
113
|
+
)
|
|
114
|
+
print(result["messages"][-1].content)
|
|
115
|
+
finally:
|
|
116
|
+
interpreter.stop()
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### File operations
|
|
120
|
+
|
|
121
|
+
```python
|
|
122
|
+
# Upload files
|
|
123
|
+
backend.upload_files([
|
|
124
|
+
("data.csv", b"name,value\nalice,42\nbob,17"),
|
|
125
|
+
("analyze.py", b"import csv\nprint('ready')"),
|
|
126
|
+
])
|
|
127
|
+
|
|
128
|
+
# Download files
|
|
129
|
+
results = backend.download_files(["data.csv"])
|
|
130
|
+
for r in results:
|
|
131
|
+
if r.content is not None:
|
|
132
|
+
print(f"{r.path}: {r.content.decode()}")
|
|
133
|
+
else:
|
|
134
|
+
print(f"Failed: {r.path}: {r.error}")
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## Session behavior
|
|
138
|
+
|
|
139
|
+
AgentCore sessions cannot be reconnected after `interpreter.stop()` is called. Each `start()` creates a fresh, isolated MicroVM. Sessions auto-expire after a configurable timeout (default 15 minutes, maximum 8 hours).
|
|
140
|
+
|
|
141
|
+
## Contributing
|
|
142
|
+
|
|
143
|
+
See the [langchain-aws contributing guide](https://github.com/langchain-ai/langchain-aws/blob/main/.github/CONTRIBUTING.md).
|
|
144
|
+
|
|
145
|
+
```bash
|
|
146
|
+
cd libs/agentcore-codeinterpreter
|
|
147
|
+
|
|
148
|
+
# Run unit tests (no network, no AWS credentials needed)
|
|
149
|
+
make tests
|
|
150
|
+
|
|
151
|
+
# Run linter
|
|
152
|
+
make lint
|
|
153
|
+
|
|
154
|
+
# Run integration tests (requires AWS credentials)
|
|
155
|
+
make integration_tests
|
|
156
|
+
```
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
# langchain-agentcore-codeinterpreter
|
|
2
|
+
|
|
3
|
+
[](https://pypi.org/project/langchain-agentcore-codeinterpreter/#history)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
[](https://pypistats.org/packages/langchain-agentcore-codeinterpreter)
|
|
6
|
+
|
|
7
|
+
Amazon Bedrock AgentCore Code Interpreter sandbox integration for [Deep Agents](https://github.com/langchain-ai/deepagents).
|
|
8
|
+
|
|
9
|
+
This package provides `AgentCoreSandbox` — a [`SandboxBackendProtocol`](https://docs.langchain.com/oss/deepagents/sandboxes) implementation that wraps AgentCore's [Code Interpreter](https://docs.aws.amazon.com/bedrock/latest/userguide/agentcore-code-interpreter.html), a secure, isolated MicroVM environment for executing code. The caller manages the interpreter lifecycle (`start()` / `stop()`); the sandbox backend handles command execution and file operations.
|
|
10
|
+
|
|
11
|
+
> **Note:** For the LangChain `BaseTool` integration (used with `create_react_agent` and LangGraph agents), see [`langchain-aws[tools]`](https://github.com/langchain-ai/langchain-aws). This package is specifically for the Deep Agents `BaseSandbox` protocol.
|
|
12
|
+
|
|
13
|
+
## Prerequisites
|
|
14
|
+
|
|
15
|
+
**1. AWS credentials** configured via one of the following methods:
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
# Option 1: Long-lived IAM credentials
|
|
19
|
+
export AWS_ACCESS_KEY_ID="your-access-key"
|
|
20
|
+
export AWS_SECRET_ACCESS_KEY="your-secret-key"
|
|
21
|
+
export AWS_REGION="us-west-2"
|
|
22
|
+
|
|
23
|
+
# Option 2: Temporary credentials (IAM roles, SSO, STS AssumeRole)
|
|
24
|
+
export AWS_ACCESS_KEY_ID="your-access-key"
|
|
25
|
+
export AWS_SECRET_ACCESS_KEY="your-secret-key"
|
|
26
|
+
export AWS_SESSION_TOKEN="your-session-token"
|
|
27
|
+
export AWS_REGION="us-west-2"
|
|
28
|
+
|
|
29
|
+
# Option 3: AWS CLI profile (picks up ~/.aws/credentials + ~/.aws/config)
|
|
30
|
+
aws configure
|
|
31
|
+
# or for SSO:
|
|
32
|
+
aws configure sso
|
|
33
|
+
aws sso login --profile your-profile
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
Any method supported by the [boto3 credential chain](https://boto3.amazonaws.com/v1/documentation/api/latest/guide/credentials.html) works, including EC2 instance profiles, ECS task roles, and environment variables.
|
|
37
|
+
|
|
38
|
+
**2. IAM permissions** — your credentials must allow `bedrock-agentcore:InvokeCodeInterpreter` (or the equivalent action for your region). See the [AgentCore Code Interpreter docs](https://docs.aws.amazon.com/bedrock/latest/userguide/agentcore-code-interpreter.html) for the required IAM policy.
|
|
39
|
+
|
|
40
|
+
**3. Region availability** — Code Interpreter is available in select AWS regions. `us-west-2` is a safe default. Pass the region to `CodeInterpreter(region=...)`.
|
|
41
|
+
|
|
42
|
+
## Quick Install
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
pip install langchain-agentcore-codeinterpreter
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Usage
|
|
49
|
+
|
|
50
|
+
### Standalone
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from bedrock_agentcore.tools.code_interpreter_client import CodeInterpreter
|
|
54
|
+
|
|
55
|
+
from langchain_agentcore_codeinterpreter import AgentCoreSandbox
|
|
56
|
+
|
|
57
|
+
interpreter = CodeInterpreter(region="us-west-2")
|
|
58
|
+
interpreter.start()
|
|
59
|
+
|
|
60
|
+
backend = AgentCoreSandbox(interpreter=interpreter)
|
|
61
|
+
|
|
62
|
+
result = backend.execute("echo hello")
|
|
63
|
+
print(result.output) # "hello"
|
|
64
|
+
|
|
65
|
+
interpreter.stop()
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
### With Deep Agents
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
from bedrock_agentcore.tools.code_interpreter_client import CodeInterpreter
|
|
72
|
+
from deepagents import create_deep_agent
|
|
73
|
+
|
|
74
|
+
from langchain_agentcore_codeinterpreter import AgentCoreSandbox
|
|
75
|
+
from langchain_aws import ChatBedrockConverse
|
|
76
|
+
|
|
77
|
+
interpreter = CodeInterpreter(region="us-west-2")
|
|
78
|
+
interpreter.start()
|
|
79
|
+
|
|
80
|
+
model = ChatBedrockConverse(
|
|
81
|
+
model="us.anthropic.claude-sonnet-4-6",
|
|
82
|
+
region_name="us-west-2",
|
|
83
|
+
)
|
|
84
|
+
backend = AgentCoreSandbox(interpreter=interpreter)
|
|
85
|
+
agent = create_deep_agent(
|
|
86
|
+
model=model,
|
|
87
|
+
backend=backend,
|
|
88
|
+
system_prompt="You are a coding assistant with sandbox access.",
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
try:
|
|
92
|
+
result = agent.invoke(
|
|
93
|
+
{
|
|
94
|
+
"messages": [
|
|
95
|
+
{"role": "user", "content": "Create and run a hello world script"}
|
|
96
|
+
]
|
|
97
|
+
}
|
|
98
|
+
)
|
|
99
|
+
print(result["messages"][-1].content)
|
|
100
|
+
finally:
|
|
101
|
+
interpreter.stop()
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### File operations
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
# Upload files
|
|
108
|
+
backend.upload_files([
|
|
109
|
+
("data.csv", b"name,value\nalice,42\nbob,17"),
|
|
110
|
+
("analyze.py", b"import csv\nprint('ready')"),
|
|
111
|
+
])
|
|
112
|
+
|
|
113
|
+
# Download files
|
|
114
|
+
results = backend.download_files(["data.csv"])
|
|
115
|
+
for r in results:
|
|
116
|
+
if r.content is not None:
|
|
117
|
+
print(f"{r.path}: {r.content.decode()}")
|
|
118
|
+
else:
|
|
119
|
+
print(f"Failed: {r.path}: {r.error}")
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## Session behavior
|
|
123
|
+
|
|
124
|
+
AgentCore sessions cannot be reconnected after `interpreter.stop()` is called. Each `start()` creates a fresh, isolated MicroVM. Sessions auto-expire after a configurable timeout (default 15 minutes, maximum 8 hours).
|
|
125
|
+
|
|
126
|
+
## Contributing
|
|
127
|
+
|
|
128
|
+
See the [langchain-aws contributing guide](https://github.com/langchain-ai/langchain-aws/blob/main/.github/CONTRIBUTING.md).
|
|
129
|
+
|
|
130
|
+
```bash
|
|
131
|
+
cd libs/agentcore-codeinterpreter
|
|
132
|
+
|
|
133
|
+
# Run unit tests (no network, no AWS credentials needed)
|
|
134
|
+
make tests
|
|
135
|
+
|
|
136
|
+
# Run linter
|
|
137
|
+
make lint
|
|
138
|
+
|
|
139
|
+
# Run integration tests (requires AWS credentials)
|
|
140
|
+
make integration_tests
|
|
141
|
+
```
|