> ## Documentation Index
> Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
> Use this file to discover all available pages before exploring further.

# Amazon Bedrock AgentCore 代码解释器集成

> 使用 LangChain Python 与 Amazon Bedrock AgentCore 代码解释器工具进行集成。

[Amazon Bedrock AgentCore Code Interpreter](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/code-interpreter-tool.html) 使代理能够在安全、托管的沙箱环境中执行代码。代理可以运行 Python、JavaScript 和 TypeScript 代码，用于计算、数据分析、文件操作和可视化。

## 概述

### 集成详情

| 类                                                                                                                | 包                                                          | 可序列化 | [JS 支持](https://js.langchain.com/docs/integrations/tools/) |                                              版本                                             |
| :--------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------------- | :--: | :--------------------------------------------------------: | :-----------------------------------------------------------------------------------------: |
| [`CodeInterpreterToolkit`](https://github.com/langchain-ai/langchain-aws/tree/main/libs/aws/langchain_aws/tools) | [`langchain-aws`](https://pypi.org/project/langchain-aws/) |   ✅  |                              ❌                             | ![PyPI - Version](https://img.shields.io/pypi/v/langchain-aws?style=flat-square\&label=%20) |

### 工具特性

| [返回工件](/oss/python/langchain/tools) | 原生异步 |   返回数据   |     定价     |
| :---------------------------------: | :--: | :------: | :--------: |
|                  ✅                  |   ✅  | 文本、文件、图片 | 按量付费 (AWS) |

### 可用工具

工具包提供多种用于代码执行和文件管理的工具：

| 工具                        | 描述                                        |
| :------------------------ | :---------------------------------------- |
| `execute_code`            | 运行具有持久状态的 Python/JavaScript/TypeScript 代码 |
| `execute_command`         | 在环境中运行 shell 命令                           |
| `read_files`              | 读取环境中文件的内容                                |
| `write_files`             | 创建或更新文件                                   |
| `list_files`              | 列出目录中的文件                                  |
| `delete_files`            | 从环境中删除文件                                  |
| `upload_file`             | 上传带有语义描述的文件                               |
| `install_packages`        | 安装 Python 包                               |
| `start_command_execution` | 异步启动长时间运行的命令                              |
| `get_task`                | 通过 task\_id 检查异步任务的状态                     |
| `stop_task`               | 通过 task\_id 停止正在运行的异步任务                   |

## 设置

该集成位于 `langchain-aws` 包中，它封装了 `bedrock-agentcore` SDK。

<CodeGroup>
  ```bash pip theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  pip install -U langchain-aws bedrock-agentcore
  ```

  ```bash uv theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  uv add langchain-aws bedrock-agentcore
  ```
</CodeGroup>

### 凭据

您需要配置具有 Bedrock AgentCore Code Interpreter 权限的 AWS 凭据。请参阅 [Amazon Bedrock AgentCore 文档](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/what-is-bedrock-agentcore.html) 以了解所需的 IAM 权限。

设置 LangSmith 以获得一流的观测能力也很有帮助（但不是必需的）：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import os

os.environ["LANGSMITH_API_KEY"] = "your-api-key"
os.environ["LANGSMITH_TRACING"] = "true"
```

## 实例化

工具包是使用 **async** 工厂函数创建的：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_aws.tools import create_code_interpreter_toolkit

# 创建设备包并获取工具（异步）
toolkit, code_tools = await create_code_interpreter_toolkit(region="us-west-2")
```

## 调用

### 直接使用工具

获取特定工具并调用它们：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# 按名称获取工具
tools_by_name = toolkit.get_tools_by_name()

# 执行 Python 代码
result = tools_by_name["execute_code"].invoke({
    "code": """
import numpy as np
data = [1, 2, 3, 4, 5]
print(f"Mean: {np.mean(data)}")
print(f"Sum: {np.sum(data)}")
""",
    "language": "python"
})
print(result)
```

### 在代理中使用

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
import asyncio
from langchain.agents import create_react_agent
from langchain.chat_models import init_chat_model
from langchain_aws.tools import create_code_interpreter_toolkit

async def main():
    # 创建设备包
    toolkit, code_tools = await create_code_interpreter_toolkit(region="us-west-2")

    # 初始化聊天模型
    llm = init_chat_model(
        "us.anthropic.claude-sonnet-4-20250514-v1:0",
        model_provider="bedrock_converse",
    )

    # 使用代码解释器工具创建代理
    agent = create_react_agent(
        model=llm,
        tools=code_tools,
    )

    # 使用 thread_id 创建配置以实现会话隔离
    config = {"configurable": {"thread_id": "session-123"}}

    # 运行代理
    result = await agent.ainvoke(
        {"messages": [{"role": "user", "content": "Calculate the factorial of 10"}]},
        config=config
    )
    print(result["messages"][-1].content)

    # 完成后清理
    await toolkit.cleanup()

asyncio.run(main())
```

## 基于线程的会话隔离

工具包通过 `thread_id` 支持多个并发会话。每个线程维护自己的代码解释器会话，状态相互隔离：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# 不同的线程具有隔离的会话
config_user1 = {"configurable": {"thread_id": "user-1"}}
config_user2 = {"configurable": {"thread_id": "user-2"}}

# 在 user-1 的会话中定义的变量不会存在于 user-2 的会话中
await agent.ainvoke(
    {"messages": [{"role": "user", "content": "Set x = 100"}]},
    config=config_user1
)

await agent.ainvoke(
    {"messages": [{"role": "user", "content": "What is x?"}]},  # 此处 x 未定义
    config=config_user2
)
```

## 处理文件

### 写入和读取文件

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
tools_by_name = toolkit.get_tools_by_name()

# 写入文件
tools_by_name["write_files"].invoke({
    "files": [{"path": "data.csv", "text": "name,value\nAlice,100\nBob,200"}]
})

# 读回内容
content = tools_by_name["read_files"].invoke({"paths": ["data.csv"]})
print(content)

# 列出当前目录中的文件
files = tools_by_name["list_files"].invoke({"directory_path": "."})
print(files)
```

### 上传带描述的文件

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
tools_by_name["upload_file"].invoke({
    "path": "sales.csv",
    "content": "date,revenue,product\n2024-01-01,1000,Widget\n2024-01-02,1500,Gadget",
    "description": "Sales data with columns: date, revenue, product_id"
})
```

## 安装软件包

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
tools_by_name["install_packages"].invoke({
    "packages": ["pandas>=2.0", "matplotlib", "scikit-learn"],
    "upgrade": False
})
```

## 异步任务管理

对于长时间运行的命令，您可以异步启动它们并检查其状态：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
tools_by_name = toolkit.get_tools_by_name()
config = {"configurable": {"thread_id": "session-123"}}

# 异步启动长时间运行的命令
result = tools_by_name["start_command_execution"].invoke(
    {"command": "python long_running_script.py"},
    config=config
)
# 返回 task_id

# 检查任务状态
status = tools_by_name["get_task"].invoke(
    {"task_id": "task-abc123"},
    config=config
)
print(status)

# 如有需要，停止正在运行的任务
tools_by_name["stop_task"].invoke(
    {"task_id": "task-abc123"},
    config=config
)
```

## 会话清理

完成后请始终清理会话以释放资源：

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# 清理所有会话
await toolkit.cleanup()

# 或者清理特定线程的会话
await toolkit.cleanup(thread_id="session-123")
```

***

## API 参考

有关所有功能和配置的详细文档，请参阅：

* [langchain-aws API 参考](https://reference.langchain.com/python/langchain-aws)
* [Amazon Bedrock AgentCore 文档](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/what-is-bedrock-agentcore.html)

***

<div className="source-links">
  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/i18n\zh-CN\oss\python\integrations\tools\bedrock_agentcore_code_interpreter.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>

  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>
</div>
