> ## 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.

# Riza 代码解释器集成

> 使用 LangChain Python 与 Riza 代码解释器工具集成。

> Riza 代码解释器是一个基于 WASM 的隔离环境，用于运行由 AI 代理生成的 Python 或 JavaScript。

在本笔记本中，我们将创建一个示例，展示一个使用 Python 解决大语言模型无法独立解决的问题的智能体：
统计单词 "strawberry" 中 'r' 的数量。

开始之前，请从 [Riza 仪表板](https://dashboard.riza.io) 获取 API 密钥。更多指南和完整的 API 参考，请访问 [Riza 代码解释器 API 文档](https://docs.riza.io)。

确保已安装必要的依赖项。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
pip install -qU langchain-community rizaio
```

将您的 API 密钥设置为环境变量。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
%env ANTHROPIC_API_KEY=<your_anthropic_api_key_here>
%env RIZA_API_KEY=<your_riza_api_key_here>
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain_community.tools.riza.command import ExecPython
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
from langchain.agents import AgentExecutor, create_tool_calling_agent
from langchain_anthropic import ChatAnthropic
from langchain_core.prompts import ChatPromptTemplate
```

初始化 `ExecPython` 工具。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
tools = [ExecPython()]
```

使用 Anthropic 的 Claude Haiku 模型初始化智能体。

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
llm = ChatAnthropic(model="claude-haiku-4-5-20251001", temperature=0)

prompt_template = ChatPromptTemplate.from_messages(
    [
        (
            "system",
            "You are a helpful assistant. Make sure to use a tool if you need to solve a problem.",
        ),
        ("human", "{input}"),
        ("placeholder", "{agent_scratchpad}"),
    ]
)

agent = create_tool_calling_agent(llm, tools, prompt_template)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)
```

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
# Ask a tough question
result = agent_executor.invoke({"input": "how many rs are in strawberry?"})
print(result["output"][0]["text"])
```

```text theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
> Entering new AgentExecutor chain...

Invoking: `riza_exec_python` with `{'code': 'word = "strawberry"\nprint(word.count("r"))'}`
responded: [{'id': 'toolu_01JwPLAAqqCNCjVuEnK8Fgut', 'input': {}, 'name': 'riza_exec_python', 'type': 'tool_use', 'index': 0, 'partial_json': '{"code": "word = \\"strawberry\\"\\nprint(word.count(\\"r\\"))"}'}]

3
[{'text': '\n\nThe word "strawberry" contains 3 "r" characters.', 'type': 'text', 'index': 0}]

> Finished chain.


The word "strawberry" contains 3 "r" characters.
```

***

<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\riza.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>
