|
| 1 | +"""Tools for interacting with a watsonx SQL databases via pyarrow.flight.FlightClient. |
| 2 | +
|
| 3 | +Based on the langchain_community.tools.sql_database.tool module.""" |
| 4 | + |
| 5 | +from typing import Any, Dict, Optional, Type, cast |
| 6 | + |
| 7 | +from langchain_core.callbacks import ( |
| 8 | + AsyncCallbackManagerForToolRun, |
| 9 | + CallbackManagerForToolRun, |
| 10 | +) |
| 11 | +from langchain_core.language_models import BaseLanguageModel |
| 12 | +from langchain_core.prompts import PromptTemplate |
| 13 | +from langchain_core.tools import BaseTool |
| 14 | +from pydantic import BaseModel, ConfigDict, Field, model_validator |
| 15 | + |
| 16 | +from langchain_ibm.utilities.sql_database import WatsonxSQLDatabase |
| 17 | + |
| 18 | +QUERY_CHECKER = """ |
| 19 | +{query} |
| 20 | +Double check the query above for common mistakes, including: |
| 21 | +- Using NOT IN with NULL values |
| 22 | +- Using UNION when UNION ALL should have been used |
| 23 | +- Using BETWEEN for exclusive ranges |
| 24 | +- Data type mismatch in predicates |
| 25 | +- Properly quoting identifiers |
| 26 | +- Using the correct number of arguments for functions |
| 27 | +- Casting to the correct data type |
| 28 | +- Using the proper columns for joins |
| 29 | +- Make sure that schema name `{schema}` is added to the table name, e.g. {schema}.table1 |
| 30 | +
|
| 31 | +If there are any of the above mistakes, rewrite the query. If there are no mistakes, just reproduce the original query. |
| 32 | +
|
| 33 | +Output the final SQL query only. |
| 34 | +
|
| 35 | +SQL Query: """ # noqa: E501 |
| 36 | + |
| 37 | + |
| 38 | +class BaseSQLDatabaseTool(BaseModel): |
| 39 | + """Base tool for interacting with a SQL database.""" |
| 40 | + |
| 41 | + db: WatsonxSQLDatabase = Field(exclude=True) |
| 42 | + |
| 43 | + model_config = ConfigDict( |
| 44 | + arbitrary_types_allowed=True, |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +class _QuerySQLDatabaseToolInput(BaseModel): |
| 49 | + query: str = Field(..., description="A detailed and correct SQL query.") |
| 50 | + |
| 51 | + |
| 52 | +class QuerySQLDatabaseTool(BaseSQLDatabaseTool, BaseTool): |
| 53 | + """Tool for querying a SQL database.""" |
| 54 | + |
| 55 | + name: str = "sql_db_query" |
| 56 | + description: str = """ |
| 57 | + Execute a SQL query against the database and get back the result. |
| 58 | + If the query is not correct, an error message will be returned. |
| 59 | + If an error is returned, rewrite the query, check the query correctness, |
| 60 | + and try again. |
| 61 | + """ |
| 62 | + args_schema: Type[BaseModel] = _QuerySQLDatabaseToolInput |
| 63 | + |
| 64 | + def _run( |
| 65 | + self, |
| 66 | + query: str, |
| 67 | + run_manager: Optional[CallbackManagerForToolRun] = None, |
| 68 | + ) -> str: |
| 69 | + """Execute the query, return the results or an error message.""" |
| 70 | + return self.db.run_no_throw(query) |
| 71 | + |
| 72 | + |
| 73 | +class _InfoSQLDatabaseToolInput(BaseModel): |
| 74 | + table_names: str = Field( |
| 75 | + ..., |
| 76 | + description=( |
| 77 | + "A comma-separated list of the table names " |
| 78 | + "for which to return the schema. " |
| 79 | + "Example input: 'table1, table2, table3'" |
| 80 | + ), |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +class InfoSQLDatabaseTool(BaseSQLDatabaseTool, BaseTool): |
| 85 | + """Tool for getting metadata about a SQL database.""" |
| 86 | + |
| 87 | + name: str = "sql_db_schema" |
| 88 | + description: str = "Get the schema and sample rows for the specified SQL tables." |
| 89 | + args_schema: Type[BaseModel] = _InfoSQLDatabaseToolInput |
| 90 | + |
| 91 | + def _run( |
| 92 | + self, |
| 93 | + table_names: str, |
| 94 | + run_manager: Optional[CallbackManagerForToolRun] = None, |
| 95 | + ) -> str: |
| 96 | + """Get the schema for tables in a comma-separated list.""" |
| 97 | + return self.db.get_table_info_no_throw( |
| 98 | + [t.strip() for t in table_names.split(",")] |
| 99 | + ) |
| 100 | + |
| 101 | + |
| 102 | +class _ListSQLDatabaseToolInput(BaseModel): |
| 103 | + tool_input: str = Field("", description="An empty string") |
| 104 | + |
| 105 | + |
| 106 | +class ListSQLDatabaseTool(BaseSQLDatabaseTool, BaseTool): |
| 107 | + """Tool for getting tables names.""" |
| 108 | + |
| 109 | + name: str = "sql_db_list_tables" |
| 110 | + description: str = ( |
| 111 | + "Input is an empty string, output is a comma-separated list " |
| 112 | + "of tables in the database." |
| 113 | + ) |
| 114 | + args_schema: Type[BaseModel] = _ListSQLDatabaseToolInput |
| 115 | + |
| 116 | + def _run( |
| 117 | + self, |
| 118 | + tool_input: str = "", |
| 119 | + run_manager: Optional[CallbackManagerForToolRun] = None, |
| 120 | + ) -> str: |
| 121 | + """Get a comma-separated list of table names.""" |
| 122 | + return ", ".join(self.db.get_usable_table_names()) |
| 123 | + |
| 124 | + |
| 125 | +class _QuerySQLCheckerToolInput(BaseModel): |
| 126 | + query: str = Field(..., description="A detailed and SQL query to be checked.") |
| 127 | + |
| 128 | + |
| 129 | +class QuerySQLCheckerTool(BaseSQLDatabaseTool, BaseTool): |
| 130 | + """Use an LLM to check if a query is correct.""" |
| 131 | + |
| 132 | + template: str = QUERY_CHECKER |
| 133 | + llm: BaseLanguageModel |
| 134 | + llm_chain: Any = Field(init=False) |
| 135 | + name: str = "sql_db_query_checker" |
| 136 | + description: str = """ |
| 137 | + Use this tool to double check if your query is correct before executing it. |
| 138 | + Always use this tool before executing a query with sql_db_query! |
| 139 | + """ |
| 140 | + args_schema: Type[BaseModel] = _QuerySQLCheckerToolInput |
| 141 | + |
| 142 | + @model_validator(mode="before") |
| 143 | + @classmethod |
| 144 | + def initialize_llm_chain(cls, values: Dict[str, Any]) -> Any: |
| 145 | + if "llm_chain" not in values: |
| 146 | + prompt = PromptTemplate( |
| 147 | + template=QUERY_CHECKER, input_variables=["query", "schema"] |
| 148 | + ) |
| 149 | + llm = cast(BaseLanguageModel, values.get("llm")) |
| 150 | + |
| 151 | + values["llm_chain"] = prompt | llm |
| 152 | + |
| 153 | + if values["llm_chain"].first.input_variables != ["query", "schema"]: |
| 154 | + raise ValueError( |
| 155 | + "LLM chain for QueryCheckerTool must have input variables ['query', 'schema']" # noqa: E501 |
| 156 | + ) |
| 157 | + |
| 158 | + return values |
| 159 | + |
| 160 | + def _run( |
| 161 | + self, |
| 162 | + query: str, |
| 163 | + run_manager: Optional[CallbackManagerForToolRun] = None, |
| 164 | + ) -> str: |
| 165 | + """Use the LLM to check the query.""" |
| 166 | + return self.llm_chain.invoke( |
| 167 | + {"query": query, "schema": self.db.schema}, |
| 168 | + callbacks=run_manager.get_child() if run_manager else None, |
| 169 | + ).content |
| 170 | + |
| 171 | + async def _arun( |
| 172 | + self, |
| 173 | + query: str, |
| 174 | + run_manager: Optional[AsyncCallbackManagerForToolRun] = None, |
| 175 | + ) -> str: |
| 176 | + return await self.llm_chain.apredict( |
| 177 | + query=query, |
| 178 | + schema=self.db.schema, |
| 179 | + callbacks=run_manager.get_child() if run_manager else None, |
| 180 | + ) |
0 commit comments