more complex sql chain (#619)

add a more complex sql chain that first subsets the necessary tables
This commit is contained in:
Harrison Chase
2023-01-15 17:07:21 -08:00
committed by GitHub
parent 49b3d6c78c
commit 1c71fadfdc
6 changed files with 184 additions and 8 deletions

View File

@@ -179,10 +179,80 @@
"db_chain.run(\"How many employees are there in the foobar table?\")"
]
},
{
"cell_type": "markdown",
"id": "c12ae15a",
"metadata": {},
"source": [
"## SQLDatabaseSequentialChain\n",
"\n",
"Chain for querying SQL database that is a sequential chain.\n",
"\n",
"The chain is as follows:\n",
"\n",
" 1. Based on the query, determine which tables to use.\n",
" 2. Based on those tables, call the normal SQL database chain.\n",
"\n",
"This is useful in cases where the number of tables in the database is large."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e59a4740",
"metadata": {},
"outputs": [],
"source": [
"from langchain.chains.sql_database.base import SQLDatabaseSequentialChain"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "58bb49b6",
"metadata": {},
"outputs": [],
"source": [
"chain = SQLDatabaseSequentialChain.from_llm(llm, db, verbose=True)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "95017b1a",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"\n",
"\u001b[1m> Entering new SQLDatabaseSequentialChain chain...\u001b[0m\n",
"Table names to use:\n",
"\u001b[33;1m\u001b[1;3m['Employee', 'Customer']\u001b[0m\n",
"\u001b[1m> Finished chain.\u001b[0m\n"
]
},
{
"data": {
"text/plain": [
"' 0 employees are also customers.'"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"chain.run(\"How many employees are also customers?\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e59a4740",
"id": "b2998b03",
"metadata": {},
"outputs": [],
"source": []