mirror of
https://github.com/Mintplex-Labs/langchain-python.git
synced 2026-07-18 18:34:27 -04:00
c7ca350cd3
In LangChain, all module classes are enumerated in the `__init__.py` file of the correspondent module. But some classes were missed and were not included in the module `__init__.py` This PR: - added the missed classes to the module `__init__.py` files - `__init__.py:__all_` variable value (a list of the class names) was sorted - `langchain.tools.sql_database.tool.QueryCheckerTool` was renamed into the `QuerySQLCheckerTool` because it conflicted with `langchain.tools.spark_sql.tool.QueryCheckerTool` - changes to `pyproject.toml`: - added `pgvector` to `pyproject.toml:extended_testing` - added `pandas` to `pyproject.toml:[tool.poetry.group.test.dependencies]` - commented out the `streamlit` from `collbacks/__init__.py`, It is because now the `streamlit` requires Python >=3.7, !=3.9.7 - fixed duplicate names in `tools` - fixed correspondent ut-s #### Who can review? @hwchase17 @dev2049
151 lines
4.9 KiB
Python
151 lines
4.9 KiB
Python
# flake8: noqa
|
|
"""Tools for interacting with a SQL database."""
|
|
from typing import Any, Dict, Optional
|
|
|
|
from pydantic import BaseModel, Extra, Field, root_validator
|
|
|
|
from langchain.base_language import BaseLanguageModel
|
|
from langchain.callbacks.manager import (
|
|
AsyncCallbackManagerForToolRun,
|
|
CallbackManagerForToolRun,
|
|
)
|
|
from langchain.chains.llm import LLMChain
|
|
from langchain.prompts import PromptTemplate
|
|
from langchain.sql_database import SQLDatabase
|
|
from langchain.tools.base import BaseTool
|
|
from langchain.tools.sql_database.prompt import QUERY_CHECKER
|
|
|
|
|
|
class BaseSQLDatabaseTool(BaseModel):
|
|
"""Base tool for interacting with a SQL database."""
|
|
|
|
db: SQLDatabase = Field(exclude=True)
|
|
|
|
# Override BaseTool.Config to appease mypy
|
|
# See https://github.com/pydantic/pydantic/issues/4173
|
|
class Config(BaseTool.Config):
|
|
"""Configuration for this pydantic object."""
|
|
|
|
arbitrary_types_allowed = True
|
|
extra = Extra.forbid
|
|
|
|
|
|
class QuerySQLDataBaseTool(BaseSQLDatabaseTool, BaseTool):
|
|
"""Tool for querying a SQL database."""
|
|
|
|
name = "sql_db_query"
|
|
description = """
|
|
Input to this tool is a detailed and correct SQL query, output is a result from the database.
|
|
If the query is not correct, an error message will be returned.
|
|
If an error is returned, rewrite the query, check the query, and try again.
|
|
"""
|
|
|
|
def _run(
|
|
self,
|
|
query: str,
|
|
run_manager: Optional[CallbackManagerForToolRun] = None,
|
|
) -> str:
|
|
"""Execute the query, return the results or an error message."""
|
|
return self.db.run_no_throw(query)
|
|
|
|
async def _arun(
|
|
self,
|
|
query: str,
|
|
run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
|
|
) -> str:
|
|
raise NotImplementedError("QuerySqlDbTool does not support async")
|
|
|
|
|
|
class InfoSQLDatabaseTool(BaseSQLDatabaseTool, BaseTool):
|
|
"""Tool for getting metadata about a SQL database."""
|
|
|
|
name = "sql_db_schema"
|
|
description = """
|
|
Input to this tool is a comma-separated list of tables, output is the schema and sample rows for those tables.
|
|
|
|
Example Input: "table1, table2, table3"
|
|
"""
|
|
|
|
def _run(
|
|
self,
|
|
table_names: str,
|
|
run_manager: Optional[CallbackManagerForToolRun] = None,
|
|
) -> str:
|
|
"""Get the schema for tables in a comma-separated list."""
|
|
return self.db.get_table_info_no_throw(table_names.split(", "))
|
|
|
|
async def _arun(
|
|
self,
|
|
table_name: str,
|
|
run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
|
|
) -> str:
|
|
raise NotImplementedError("SchemaSqlDbTool does not support async")
|
|
|
|
|
|
class ListSQLDatabaseTool(BaseSQLDatabaseTool, BaseTool):
|
|
"""Tool for getting tables names."""
|
|
|
|
name = "sql_db_list_tables"
|
|
description = "Input is an empty string, output is a comma separated list of tables in the database."
|
|
|
|
def _run(
|
|
self,
|
|
tool_input: str = "",
|
|
run_manager: Optional[CallbackManagerForToolRun] = None,
|
|
) -> str:
|
|
"""Get the schema for a specific table."""
|
|
return ", ".join(self.db.get_usable_table_names())
|
|
|
|
async def _arun(
|
|
self,
|
|
tool_input: str = "",
|
|
run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
|
|
) -> str:
|
|
raise NotImplementedError("ListTablesSqlDbTool does not support async")
|
|
|
|
|
|
class QuerySQLCheckerTool(BaseSQLDatabaseTool, BaseTool):
|
|
"""Use an LLM to check if a query is correct.
|
|
Adapted from https://www.patterns.app/blog/2023/01/18/crunchbot-sql-analyst-gpt/"""
|
|
|
|
template: str = QUERY_CHECKER
|
|
llm: BaseLanguageModel
|
|
llm_chain: LLMChain = Field(init=False)
|
|
name = "sql_db_query_checker"
|
|
description = """
|
|
Use this tool to double check if your query is correct before executing it.
|
|
Always use this tool before executing a query with query_sql_db!
|
|
"""
|
|
|
|
@root_validator(pre=True)
|
|
def initialize_llm_chain(cls, values: Dict[str, Any]) -> Dict[str, Any]:
|
|
if "llm_chain" not in values:
|
|
values["llm_chain"] = LLMChain(
|
|
llm=values.get("llm"),
|
|
prompt=PromptTemplate(
|
|
template=QUERY_CHECKER, input_variables=["query", "dialect"]
|
|
),
|
|
)
|
|
|
|
if values["llm_chain"].prompt.input_variables != ["query", "dialect"]:
|
|
raise ValueError(
|
|
"LLM chain for QueryCheckerTool must have input variables ['query', 'dialect']"
|
|
)
|
|
|
|
return values
|
|
|
|
def _run(
|
|
self,
|
|
query: str,
|
|
run_manager: Optional[CallbackManagerForToolRun] = None,
|
|
) -> str:
|
|
"""Use the LLM to check the query."""
|
|
return self.llm_chain.predict(query=query, dialect=self.db.dialect)
|
|
|
|
async def _arun(
|
|
self,
|
|
query: str,
|
|
run_manager: Optional[AsyncCallbackManagerForToolRun] = None,
|
|
) -> str:
|
|
return await self.llm_chain.apredict(query=query, dialect=self.db.dialect)
|