Files
langchain-python/langchain/tools/python/tool.py
T
Ankush Gola 82baecc892 Add a SQL agent for interacting with SQL Databases and JSON Agent for interacting with large JSON blobs (#1150)
This PR adds 

* `ZeroShotAgent.as_sql_agent`, which returns an agent for interacting
with a sql database. This builds off of `SQLDatabaseChain`. The main
advantages are 1) answering general questions about the db, 2) access to
a tool for double checking queries, and 3) recovering from errors
* `ZeroShotAgent.as_json_agent` which returns an agent for interacting
with json blobs.
* Several examples in notebooks

---------

Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
2023-02-28 19:44:39 -08:00

31 lines
927 B
Python

"""A tool for running python code in a REPL."""
from pydantic import Field
from langchain.python import PythonREPL
from langchain.tools.base import BaseTool
def _get_default_python_repl() -> PythonREPL:
return PythonREPL(_globals=globals(), _locals=None)
class PythonREPLTool(BaseTool):
"""A tool for running python code in a REPL."""
name = "Python REPL"
description = (
"A Python shell. Use this to execute python commands. "
"Input should be a valid python command. "
"If you expect output it should be printed out."
)
python_repl: PythonREPL = Field(default_factory=_get_default_python_repl)
def _run(self, query: str) -> str:
"""Use the tool."""
return self.python_repl.run(query)
async def _arun(self, query: str) -> str:
"""Use the tool asynchronously."""
raise NotImplementedError("PythonReplTool does not support async")