Skip to main content

Python REPL

Sometimes, for complex calculations, rather than have an LLM generate the answer directly, it can be better to have the LLM generate code to calculate the answer, and then run that code to get the answer. In order to easily do that, we provide a simple Python REPL to execute commands in.

This interface will only return things that are printed - therefore, if you want to use it to calculate an answer, make sure to have it print out the answer.

from import Tool
from langchain_experimental.utilities import PythonREPL
API Reference:Tool | PythonREPL
python_repl = PythonREPL()"print(1+1)")
Python REPL can execute arbitrary code. Use with caution.
# You can create the tool to pass to an agent
repl_tool = Tool(
description="A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.",,

Was this page helpful?

You can leave detailed feedback on GitHub.