Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/main/smolagents_doc/en/text_to_sql.ipynb
Views: 2935
Text-to-SQL
In this tutorial, we’ll see how to implement an agent that leverages SQL using smolagents
.
Let's start with the golden question: why not keep it simple and use a standard text-to-SQL pipeline?
A standard text-to-sql pipeline is brittle, since the generated SQL query can be incorrect. Even worse, the query could be incorrect, but not raise an error, instead giving some incorrect/useless outputs without raising an alarm.
👉 Instead, an agent system is able to critically inspect outputs and decide if the query needs to be changed or not, thus giving it a huge performance boost.
Let’s build this agent! 💪
Run the line below to install required dependencies:
To call the HF Inference API, you will need a valid token as your environment variable HF_TOKEN
. We use python-dotenv to load it.
Then, we setup the SQL environment:
Build our agent
Now let’s make our SQL table retrievable by a tool.
The tool’s description attribute will be embedded in the LLM’s prompt by the agent system: it gives the LLM information about how to use the tool. This is where we want to describe the SQL table.
Now let’s build our tool. It needs the following: (read the tool doc for more detail)
A docstring with an
Args:
part listing arguments.Type hints on both inputs and output.
Now let us create an agent that leverages this tool.
We use the CodeAgent
, which is smolagents’ main agent class: an agent that writes actions in code and can iterate on previous output according to the ReAct framework.
The model is the LLM that powers the agent system. HfApiModel
allows you to call LLMs using HF’s Inference API, either via Serverless or Dedicated endpoint, but you could also use any proprietary API.
Level 2: Table joins
Now let’s make it more challenging! We want our agent to handle joins across multiple tables.
So let’s make a second table recording the names of waiters for each receipt_id!
Since we changed the table, we update the SQLExecutorTool
with this table’s description to let the LLM properly leverage information from this table.
Since this request is a bit harder than the previous one, we’ll switch the LLM engine to use the more powerful Qwen/Qwen2.5-Coder-32B-Instruct!
It directly works! The setup was surprisingly simple, wasn’t it?
This example is done! We've touched upon these concepts:
Building new tools.
Updating a tool's description.
Switching to a stronger LLM helps agent reasoning.
✅ Now you can go build this text-to-SQL system you’ve always dreamt of! ✨