Build AI Text-to-SQL with Knowledge Graphs | Pipeline Demo | Humanitarians AI

Ask a question in plain English and this pipeline searches a Neo4j knowledge graph for the relevant schema, prunes the context with one AI agent, and generates SQL with another.

4:31 video4 min readWatch on YouTube

Asking a database a question in plain English and getting back a correct SQL query sounds simple until you consider what the system actually has to know first: which tables exist, how they connect, and which ones are even relevant to the question being asked. This demo walks through a pipeline that answers that problem by representing a database schema as a knowledge graph and letting AI agents search and reason over it.

The example question

The demo starts with a real question: what is the alphabetically ordered list of all distinct medicines? Rather than writing SQL by hand, the question goes through a multi-step AI pipeline that processes it end to end, from raw English to an executed query and its results, with no manual SQL writing required.

Retrieving context from a knowledge graph

The first step retrieves context from the knowledge graph, searching for the database schema elements relevant to the question. This runs through vector similarity node search to find semantically similar nodes, so a question about "medications" can still find a table literally named "drugs" even without an exact word match. From there, the system builds a context graph around the retrieved nodes using a breadth-first search algorithm, pulling in the surrounding schema structure rather than just the isolated nodes that matched.

Pruning and generating the SQL

Once the raw context graph is built, an AI agent prunes and standardizes it, filtering out redundant information and organizing what's left into a cleaner structure. A second agent then takes that cleaned context and generates the actual SQL query, mapping the original question onto the relevant tables. Both agents use Google's Gemini AI model, guided by system prompts written specifically for their roles: the pruning agent's job is removing noise, the generation agent's job is producing a valid query grounded in the pruned schema context. In the demo, this pipeline correctly produces a query that selects distinct medications ordered alphabetically, and the results appear immediately after execution.

Building the knowledge graph itself

Behind the live demo sits the actual graph construction work. A dedicated connection module handles talking to a Neo4j Aura database, a cloud-hosted graph database chosen specifically because it's well suited to representing schemas as interconnected nodes. A database schema gets converted into that graph by creating nodes for databases, tables, and columns, then establishing relationships between them: foreign keys become connections between columns, and primary keys are tracked as well. Every schema node also gets an embedding generated with OpenAI's embedding model, which is what makes the semantic search step possible in the first place, letting the system find relevant tables even when a user's wording doesn't match the schema's naming exactly.

The full workflow, end to end

The project's main workflow file ties all of these pieces together. The process starts with the user's question, searches the knowledge graph using vector node similarity to find relevant nodes, retrieves a subgraph around those elements using the breadth-first search algorithm, and passes that context to the pruning agent for filtering and standardization. The generation agent then takes the cleaned context and produces the SQL query, which gets executed, with results returned to the user. Each step along the way is tracked with a status update, which is what powers the pipeline visualization shown live in the demo, giving a real-time view of exactly which stage the system is working through.

Why the components fit together

The system's project layout keeps these pieces separated by responsibility: an agents directory for the pruning and generation agents, connection logic for Neo4j, and the main workflow file that orchestrates the whole sequence. That separation is part of what makes the pipeline legible enough to demo live, since each stage, from schema retrieval to context pruning to query generation, can be inspected and shown independently rather than existing as one opaque black box.

Key takeaways

  • The pipeline converts a database schema into a Neo4j knowledge graph, with nodes for databases, tables, and columns and edges representing foreign key and primary key relationships.
  • OpenAI embeddings on every schema node enable semantic search, so a query about "medications" can still match a "drugs" table.
  • A breadth-first search algorithm builds a context subgraph around the nodes found through vector similarity search.
  • Two Google Gemini-powered agents divide the work: a pruning agent that filters and standardizes the retrieved context, and a generation agent that produces the final SQL query.
  • Every pipeline step is tracked with a status update, driving a real-time visualization of the system's progress from question to executed query.

Who this is for

This walkthrough is aimed at data scientists, AI engineers, and database professionals who want to bridge natural language and structured data querying, and it's a useful reference for anyone building a text-to-SQL interface for non-technical stakeholders. It's part of the applied AI tooling work coming out of Humanitarians AI.

Full transcript(auto-generated, with timestamps)

[0:03]Hi everyone. Today I'm going to show you something really cool. An AI powered system that converts natural language questions into SQL queries using knowledge graph. This is not just another chatbot. It's a sophisticated pipeline that understands database schemas and generates accurate SQL automatically. Let me demonstrate this with a real example. I will ask what is the alphabetically ordered list of all distinct medicines. Watch what happens here. The system is now processing my question through a multi-step AI pipeline. First it it's retrieving context from the knowledge graph. This is where it searches for relevant database schema elements. Then it's doing vector similarity node search to find

[0:51]Semantically similar nodes. Then it builds a context graph from the retrive nodes using breath versus search algorithm. The AI agent is now pruning and standardizing this context. Finally, it's generating the SQL query and executing it. And here we have it. The system generated a perfect SQL query that selects distinct medications ordered alphabetically. And we can see the results right here. This is all happening automatically. No manual SQL writing required. Now let me show you how this happens under the hood. We have this project with several key components. First we have the agents directory. These contains our AI agents. The pruning agent that filters and standardize the retrive context and the

[1:40]Generation agent that creates the SQL queries. Both of them uses Google's Gemini AI model. Here are the system prompts that guide these agents. The pruning agent removes the redundant information and organizes the context while the generation agent maps the question to relevant tables and generates the valid SQL query. Now the heart of the system is the knowledge graph construction. Let me show you how we have built this. First the connect.py py files handles the connection to the Neo4j aura database which is hosted on a cloud platform. We're using Neo 4G because it's perfect for representing database schemas as interconnected nodes. Now we take a database schema and

[2:29]Convert it into a knowledge graph. We create nodes for databases, tables and columns then establish relation between them. Foreign become connections between columns and we track primary keys too. Now we generate amendings for all the schema nodes using OpenAI's amending model. This allows us to do semantic search. When you ask about medications, the system can find the drugs table even if the exact word doesn't match. Now coming to the main py file to show you the main workflow. This is where everything comes together. The process starts with the user question. First we search the knowledge graph using the vector node similarity to find the relevant nodes. Then we

[3:16]Retrive a subgraph around this elements using BFS algorithm. This gives us the context that we need. We pass this context to our pruning agent which filters and standardizes it. Then the generation agent takes this clean context and generates the SQL query. Finally, we execute the query and return the results. Notice how we track each step with status update. This is what powerful the beautiful pipeline visualization you saw in the demo. Now this doesn't this is not just a technical demo. This is a system that can be deployed in real organization. It has the combination of knowledge graphs, vector search, AI agents and it creates a powerful solution for natural

[4:00]Language database quering. What we have built here is the complete NL2SQL system that combines the power of knowledge graph with the modern AI. It's not just about generating SQL. It's about understanding database schemas, finding the relevant context and creating accurate queries automatically and showing the answers to the user. Thanks for watching.

More videos

Humanitarians AI Lyrical Literacy Project