跳到主要内容
Open In ColabOpen on GitHub

在进行SQL问答时如何处理大型数据库

为了针对数据库编写有效的查询,我们需要向模型提供表名、表结构和特征值以供其查询。当存在许多表、列和/或高基数列时,我们不可能在每个提示中倾倒关于数据库的全部信息。相反,我们必须找到一种方法,仅将最相关的信息动态地插入到提示中。

本指南将演示识别此类相关信息并将其输入到查询生成步骤中的方法。我们将涵盖:

  1. 识别相关的表子集;
  2. 识别相关的列值子集。

设置

首先,获取所需的包并设置环境变量

%pip install --upgrade --quiet  langchain langchain-community langchain-openai
# Uncomment the below to use LangSmith. Not required.
# import os
# os.environ["LANGSMITH_API_KEY"] = getpass.getpass()
# os.environ["LANGSMITH_TRACING"] = "true"

以下示例将使用 SQLite 连接和 Chinook 数据库。请按照这些安装步骤在与本 Notebook 相同的目录中创建 Chinook.db

  • 此文件保存为 Chinook_Sqlite.sql
  • 运行 sqlite3 Chinook.db
  • 运行 .read Chinook_Sqlite.sql
  • 测试 SELECT * FROM Artist LIMIT 10;

现在,Chinook.db 位于我们的目录中,我们可以使用 SQLAlchemy 驱动的 SQLDatabase 类与之交互。

from langchain_community.utilities import SQLDatabase

db = SQLDatabase.from_uri("sqlite:///Chinook.db")
print(db.dialect)
print(db.get_usable_table_names())
print(db.run("SELECT * FROM Artist LIMIT 10;"))
API 参考:SQLDatabase
sqlite
['Album', 'Artist', 'Customer', 'Employee', 'Genre', 'Invoice', 'InvoiceLine', 'MediaType', 'Playlist', 'PlaylistTrack', 'Track']
[(1, 'AC/DC'), (2, 'Accept'), (3, 'Aerosmith'), (4, 'Alanis Morissette'), (5, 'Alice In Chains'), (6, 'Antônio Carlos Jobim'), (7, 'Apocalyptica'), (8, 'Audioslave'), (9, 'BackBeat'), (10, 'Billy Cobham')]

许多表

我们需要在提示中包含的主要信息之一是相关表的模式。当我们有非常多的表时,无法将所有模式都放入一个提示中。在这种情况下,我们可以首先提取与用户输入相关的表名,然后仅包含它们的模式。

一种简单可靠的方法是使用工具调用。下面,我们展示如何使用此功能获取符合所需格式(在本例中为表名列表)的输出。我们使用聊天模型的 .bind_tools 方法绑定一个 Pydantic 格式的工具,并将其输入到输出解析器中,以从模型的响应中重建对象。

pip install -qU "langchain[google-genai]"
import getpass
import os

if not os.environ.get("GOOGLE_API_KEY"):
os.environ["GOOGLE_API_KEY"] = getpass.getpass("Enter API key for Google Gemini: ")

from langchain.chat_models import init_chat_model

llm = init_chat_model("gemini-2.0-flash", model_provider="google_genai")
from langchain_core.output_parsers.openai_tools import PydanticToolsParser
from langchain_core.prompts import ChatPromptTemplate
from pydantic import BaseModel, Field


class Table(BaseModel):
"""Table in SQL database."""

name: str = Field(description="Name of table in SQL database.")


table_names = "\n".join(db.get_usable_table_names())
system = f"""Return the names of ALL the SQL tables that MIGHT be relevant to the user question. \
The tables are:

{table_names}

Remember to include ALL POTENTIALLY RELEVANT tables, even if you're not sure that they're needed."""

prompt = ChatPromptTemplate.from_messages(
[
("system", system),
("human", "{input}"),
]
)
llm_with_tools = llm.bind_tools([Table])
output_parser = PydanticToolsParser(tools=[Table])

table_chain = prompt | llm_with_tools | output_parser

table_chain.invoke({"input": "What are all the genres of Alanis Morissette songs"})
[Table(name='Genre')]

这非常有效!除了,正如我们将在下面看到的,我们实际上还需要一些其他表。仅根据用户问题,模型很难知道这些。在这种情况下,我们可能会考虑通过将表分组来简化模型的工作。我们只要求模型在“音乐”和“业务”类别之间进行选择,然后从那里负责选择所有相关表。

system = """Return the names of any SQL tables that are relevant to the user question.
The tables are:

Music
Business
"""

prompt = ChatPromptTemplate.from_messages(
[
("system", system),
("human", "{input}"),
]
)

category_chain = prompt | llm_with_tools | output_parser
category_chain.invoke({"input": "What are all the genres of Alanis Morissette songs"})
[Table(name='Music'), Table(name='Business')]
from typing import List


def get_tables(categories: List[Table]) -> List[str]:
tables = []
for category in categories:
if category.name == "Music":
tables.extend(
[
"Album",
"Artist",
"Genre",
"MediaType",
"Playlist",
"PlaylistTrack",
"Track",
]
)
elif category.name == "Business":
tables.extend(["Customer", "Employee", "Invoice", "InvoiceLine"])
return tables


table_chain = category_chain | get_tables
table_chain.invoke({"input": "What are all the genres of Alanis Morissette songs"})
['Album',
'Artist',
'Genre',
'MediaType',
'Playlist',
'PlaylistTrack',
'Track',
'Customer',
'Employee',
'Invoice',
'InvoiceLine']

现在我们有了一个可以为任何查询输出相关表的链,我们可以将其与我们的 create_sql_query_chain 结合起来,该链可以接受一个 table_names_to_use 列表来确定提示中包含哪些表模式。

from operator import itemgetter

from langchain.chains import create_sql_query_chain
from langchain_core.runnables import RunnablePassthrough

query_chain = create_sql_query_chain(llm, db)
# Convert "question" key to the "input" key expected by current table_chain.
table_chain = {"input": itemgetter("question")} | table_chain
# Set table_names_to_use using table_chain.
full_chain = RunnablePassthrough.assign(table_names_to_use=table_chain) | query_chain
query = full_chain.invoke(
{"question": "What are all the genres of Alanis Morissette songs"}
)
print(query)
SELECT DISTINCT "g"."Name"
FROM "Genre" g
JOIN "Track" t ON "g"."GenreId" = "t"."GenreId"
JOIN "Album" a ON "t"."AlbumId" = "a"."AlbumId"
JOIN "Artist" ar ON "a"."ArtistId" = "ar"."ArtistId"
WHERE "ar"."Name" = 'Alanis Morissette'
LIMIT 5;
db.run(query)
"[('Rock',)]"

我们可以在此处查看此运行的 LangSmith 跟踪。

我们已经了解了如何在链中动态地在提示中包含表模式的子集。解决此问题的另一种可能方法是让 Agent 通过为其提供一个工具来决定何时查找表。您可以在 SQL:Agent 指南中看到一个示例。

高基数列

为了过滤包含专有名称(如地址、歌曲名称或艺术家)的列,我们首先需要仔细检查拼写,以便正确过滤数据。

一种朴素的策略是创建一个包含数据库中所有不同专有名称的向量存储。然后,我们可以查询该向量存储的每个用户输入,并将最相关的专有名称注入到提示中。

首先我们需要我们想要的每个实体的唯一值,为此我们定义一个函数将结果解析为元素列表。

import ast
import re


def query_as_list(db, query):
res = db.run(query)
res = [el for sub in ast.literal_eval(res) for el in sub if el]
res = [re.sub(r"\b\d+\b", "", string).strip() for string in res]
return res


proper_nouns = query_as_list(db, "SELECT Name FROM Artist")
proper_nouns += query_as_list(db, "SELECT Title FROM Album")
proper_nouns += query_as_list(db, "SELECT Name FROM Genre")
len(proper_nouns)
proper_nouns[:5]
['AC/DC', 'Accept', 'Aerosmith', 'Alanis Morissette', 'Alice In Chains']

现在我们可以将所有值嵌入并存储在向量数据库中。

from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings

vector_db = FAISS.from_texts(proper_nouns, OpenAIEmbeddings())
retriever = vector_db.as_retriever(search_kwargs={"k": 15})
API 参考:FAISS | OpenAIEmbeddings

并组合一个查询构建链,该链首先从数据库中检索值并将其插入到提示中。

from operator import itemgetter

from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough

system = """You are a SQLite expert. Given an input question, create a syntactically
correct SQLite query to run. Unless otherwise specificed, do not return more than
{top_k} rows.

Only return the SQL query with no markup or explanation.

Here is the relevant table info: {table_info}

Here is a non-exhaustive list of possible feature values. If filtering on a feature
value make sure to check its spelling against this list first:

{proper_nouns}
"""

prompt = ChatPromptTemplate.from_messages([("system", system), ("human", "{input}")])

query_chain = create_sql_query_chain(llm, db, prompt=prompt)
retriever_chain = (
itemgetter("question")
| retriever
| (lambda docs: "\n".join(doc.page_content for doc in docs))
)
chain = RunnablePassthrough.assign(proper_nouns=retriever_chain) | query_chain

为了测试我们的链,让我们看看在不使用检索和使用检索的情况下,当我们尝试过滤“elenis moriset”(Alanis Morissette 的拼写错误)时会发生什么。

# Without retrieval
query = query_chain.invoke(
{"question": "What are all the genres of elenis moriset songs", "proper_nouns": ""}
)
print(query)
db.run(query)
SELECT DISTINCT g.Name 
FROM Track t
JOIN Album a ON t.AlbumId = a.AlbumId
JOIN Artist ar ON a.ArtistId = ar.ArtistId
JOIN Genre g ON t.GenreId = g.GenreId
WHERE ar.Name = 'Elenis Moriset';
''
# With retrieval
query = chain.invoke({"question": "What are all the genres of elenis moriset songs"})
print(query)
db.run(query)
SELECT DISTINCT g.Name
FROM Genre g
JOIN Track t ON g.GenreId = t.GenreId
JOIN Album a ON t.AlbumId = a.AlbumId
JOIN Artist ar ON a.ArtistId = ar.ArtistId
WHERE ar.Name = 'Alanis Morissette';
"[('Rock',)]"

我们可以看到,通过检索,我们能够将“Elenis Moriset”的拼写纠正为“Alanis Morissette”并获得有效结果。

解决此问题的另一种可能方法是让 Agent 通过为其提供工具来决定何时查找专有名称。您可以在 SQL:Agent 指南中看到一个示例。