开源AI食谱文档

带自动错误纠正功能的文本到SQL代理

Hugging Face's logo
加入Hugging Face社区

并获得增强型文档体验

开始使用

Open In Colab

带自动错误纠正功能的文本到SQL代理

作者:Aymeric Roucher

在本教程中,我们将了解如何使用transformers.agents实现一个利用SQL的代理。

与标准的文本到SQL管道相比,有什么优势?

标准的文本到SQL管道很脆弱,因为生成的SQL查询可能不正确。更糟糕的是,查询可能不正确,但不会引发错误,而是会给出一些错误/无用的输出,而不会发出警报。

👉 相反,**代理系统能够批判性地检查输出并决定是否需要更改查询**,从而极大地提高了性能。

让我们构建这个代理!💪

设置SQL表

from sqlalchemy import (
    create_engine,
    MetaData,
    Table,
    Column,
    String,
    Integer,
    Float,
    insert,
    inspect,
    text,
)

engine = create_engine("sqlite:///:memory:")
metadata_obj = MetaData()

# create city SQL table
table_name = "receipts"
receipts = Table(
    table_name,
    metadata_obj,
    Column("receipt_id", Integer, primary_key=True),
    Column("customer_name", String(16), primary_key=True),
    Column("price", Float),
    Column("tip", Float),
)
metadata_obj.create_all(engine)
rows = [
    {"receipt_id": 1, "customer_name": "Alan Payne", "price": 12.06, "tip": 1.20},
    {"receipt_id": 2, "customer_name": "Alex Mason", "price": 23.86, "tip": 0.24},
    {"receipt_id": 3, "customer_name": "Woodrow Wilson", "price": 53.43, "tip": 5.43},
    {"receipt_id": 4, "customer_name": "Margaret James", "price": 21.11, "tip": 1.00},
]
for row in rows:
    stmt = insert(receipts).values(**row)
    with engine.begin() as connection:
        cursor = connection.execute(stmt)

让我们检查一下我们的系统是否可以使用基本查询

>>> with engine.connect() as con:
...     rows = con.execute(text("""SELECT * from receipts"""))
...     for row in rows:
...         print(row)
(1, 'Alan Payne', 12.06, 1.2)
(2, 'Alex Mason', 23.86, 0.24)
(3, 'Woodrow Wilson', 53.43, 5.43)
(4, 'Margaret James', 21.11, 1.0)

构建我们的代理

现在让我们使SQL表可以通过工具检索。

工具的description属性将由代理系统嵌入到LLM的提示中:它向LLM提供有关如何使用该工具的信息。所以我们希望在这里描述SQL表。

>>> inspector = inspect(engine)
>>> columns_info = [(col["name"], col["type"]) for col in inspector.get_columns("receipts")]

>>> table_description = "Columns:\n" + "\n".join([f"  - {name}: {col_type}" for name, col_type in columns_info])
>>> print(table_description)
Columns:
  - receipt_id: INTEGER
  - customer_name: VARCHAR(16)
  - price: FLOAT
  - tip: FLOAT

现在让我们构建我们的工具。它需要以下内容:(阅读文档以获取更多详细信息)

  • 带有Args:部分的文档字符串
  • 类型提示
from transformers.agents import tool


@tool
def sql_engine(query: str) -> str:
    """
    Allows you to perform SQL queries on the table. Returns a string representation of the result.
    The table is named 'receipts'. Its description is as follows:
        Columns:
        - receipt_id: INTEGER
        - customer_name: VARCHAR(16)
        - price: FLOAT
        - tip: FLOAT

    Args:
        query: The query to perform. This should be correct SQL.
    """
    output = ""
    with engine.connect() as con:
        rows = con.execute(text(query))
        for row in rows:
            output += "\n" + str(row)
    return output

现在让我们创建一个利用此工具的代理。

我们使用ReactCodeAgent,它是transformers.agents的主要代理类:一个以代码编写操作并在ReAct框架下根据先前输出进行迭代的代理。

llm_engine是为代理系统提供动力的LLM。HfEngine允许您使用HF的推理API调用LLM,无论是通过无服务器还是专用端点,但您也可以使用任何专有API:查看此其他食谱以了解如何对其进行调整。

from transformers.agents import ReactCodeAgent, HfApiEngine

agent = ReactCodeAgent(
    tools=[sql_engine],
    llm_engine=HfApiEngine("meta-llama/Meta-Llama-3-8B-Instruct"),
)
agent.run("Can you give me the name of the client who got the most expensive receipt?")

难度提升:表连接

现在让我们使其更具挑战性!我们希望我们的代理能够处理跨多个表的连接。

所以让我们创建一个第二个表,为每个receipt_id记录服务员的姓名!

table_name = "waiters"
receipts = Table(
    table_name,
    metadata_obj,
    Column("receipt_id", Integer, primary_key=True),
    Column("waiter_name", String(16), primary_key=True),
)
metadata_obj.create_all(engine)

rows = [
    {"receipt_id": 1, "waiter_name": "Corey Johnson"},
    {"receipt_id": 2, "waiter_name": "Michael Watts"},
    {"receipt_id": 3, "waiter_name": "Michael Watts"},
    {"receipt_id": 4, "waiter_name": "Margaret James"},
]
for row in rows:
    stmt = insert(receipts).values(**row)
    with engine.begin() as connection:
        cursor = connection.execute(stmt)

我们需要使用此表的描述更新SQLExecutorTool,以使LLM能够正确地利用来自此表的信息。

>>> updated_description = """Allows you to perform SQL queries on the table. Beware that this tool's output is a string representation of the execution output.
... It can use the following tables:"""

>>> inspector = inspect(engine)
>>> for table in ["receipts", "waiters"]:
...     columns_info = [(col["name"], col["type"]) for col in inspector.get_columns(table)]

...     table_description = f"Table '{table}':\n"

...     table_description += "Columns:\n" + "\n".join([f"  - {name}: {col_type}" for name, col_type in columns_info])
...     updated_description += "\n\n" + table_description

>>> print(updated_description)
Allows you to perform SQL queries on the table. Beware that this tool's output is a string representation of the execution output.
It can use the following tables:

Table 'receipts':
Columns:
  - receipt_id: INTEGER
  - customer_name: VARCHAR(16)
  - price: FLOAT
  - tip: FLOAT

Table 'waiters':
Columns:
  - receipt_id: INTEGER
  - waiter_name: VARCHAR(16)

由于此请求比上一个请求稍微复杂一些,我们将切换llm引擎以使用功能更强大的Qwen/Qwen2.5-72B-Instruct

sql_engine.description = updated_description

agent = ReactCodeAgent(
    tools=[sql_engine],
    llm_engine=HfApiEngine("Qwen/Qwen2.5-72B-Instruct"),
)

agent.run("Which waiter got more total money from tips?")

它直接起作用了!设置出乎意料地简单,不是吗?

✅ 现在您可以构建您一直梦寐以求的文本到SQL系统了!✨

< > 在GitHub上更新