Skip to main content
This example shows how to build a text-to-SQL system that:
  1. Uses Agentic RAG to search for table metadata, sample queries and rules for writing better SQL queries.
  2. Uses dynamic few-shot examples and rules to improve query construction.
  3. Provides an interactive Streamlit UI for users to query the database.
We’ll use the F1 dataset as an example, but you can easily extend it to other datasets.

Key capabilities

  • Natural language to SQL conversion
  • Retrieve table metadata, sample queries and rules using Agentic RAG
  • Better query construction with the help of dynamic few-shot examples and rules
  • Interactive Streamlit UI

Simple queries to try

  • “Who are the top 5 drivers with the most race wins?”
  • “Compare Mercedes vs Ferrari performance in constructors championships”
  • “Show me the progression of fastest lap times at Monza”
  • “Which drivers have won championships with multiple teams?”
  • “What tracks have hosted the most races?”
  • “Show me Lewis Hamilton’s win percentage by season”

Advanced queries with table joins

  • “How many races did the championship winners win each year?”
  • “Compare the number of race wins vs championship positions for constructors in 2019”
  • “Show me Lewis Hamilton’s race wins and championship positions by year”
  • “Which drivers have both won races and set fastest laps at Monaco?”
  • “Show me Ferrari’s race wins and constructor championship positions from 2015-2020”

Code

The complete code is available in the Agno repository.

Usage

1

Clone the repository

2

Create virtual environment

3

Install dependencies

4

Run PgVector

First, install Docker Desktop.Then run either using the helper script:
Or directly with Docker:
5

Load F1 data

6

Load knowledge base

The knowledge base contains table metadata, rules and sample queries that help the Agent generate better responses.
Pro tips for enhancing the knowledge base:
  • Add table_rules and column_rules to guide the Agent on query formats
  • Add sample queries to cookbook/examples/apps/sql_agent/knowledge_base/sample_queries.sql
7

Set up API keys

We recommend using gpt-4o for optimal performance.
8

Launch the app

Open localhost:8501 to start using the SQL Agent.
Need help? Join our Discourse community for support!