开源 AI 食谱文档

用于文本到 SQL 的自动纠错代理

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

Open In Colab

带自动纠错功能的Text-to-SQL代理

作者:Aymeric Roucher

在本教程中,我们将了解如何实现一个利用SQL并带自动纠错功能的代理,使用smolagents

与标准Text-to-SQL管道相比,它的优势是什么?

标准的Text-to-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表可检索。

我们的sql_engine工具需要以下内容:(详情请阅读文档

  • 一个带有Args:部分的文档字符串。此文档字符串将被解析为工具的description属性,该属性将用作驱动代理的LLM的说明手册,因此提供它很重要!
  • 输入和输出的类型提示。
from smolagents 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

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

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

llm_engine是驱动代理系统的LLM。InferenceClientModel允许您使用Hugging Face的推理API调用LLM,无论是通过无服务器还是专用端点,但您也可以使用任何专有API:请查看本教程,了解如何进行适配。

from smolagents import CodeAgent, InferenceClientModel

agent = CodeAgent(
    tools=[sql_engine],
    model=InferenceClientModel("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 = CodeAgent(
    tools=[sql_engine],
    model=InferenceClientModel("Qwen/Qwen2.5-72B-Instruct"),
)

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

它直接就奏效了!这个设置出奇地简单,不是吗?

✅ 现在你可以去构建你一直梦想的Text-to-SQL系统了!✨

< > 在 GitHub 上更新