
Retrieval Augmented Generation with SQL
Large Language Model (LLM) systems like ChatGPT or Anthropic seem genuinely impressive initially. Playing with them, however, makes it easy to start noticing some gaps that make them less valuable.
They aren't up to date.
They make confident assertions about things that aren't true.
Retrieval Augmented Generation (RAG) aims to fill in these gaps by utilizing the LLM's language process capabilities.
RAG addresses the first by extending the model to include data not included in the LLM. It works on two fronts: First, it utilizes specific domain knowledge and creates prompts so the LLM will not make a confidently false assertion. Second, it queries current data and uses the LLM to interpret it.
The diagram below has the LLM component in the blue circles and the RAG client in the yellow.

How Well Does the RAG Client Answer Business Questions?
The LLM (It's anthropic) does a good job of generating queries and answering questions.
I asked, "What are Bob Mould's top hits this past year?"
The prompt generated the following SQL:
SELECT song, COUNT(*) as play_count FROM import_kexp_playlist WHERE artist = 'Bob Mould' AND airdate >= NOW() - INTERVAL '1 year' GROUP BY song ORDER BY play_count DESC;
The LLM summarized the answer with, "Based on the results, we can see Bob Mould's most played songs in the past year, with "Here We Go Crazy" being the most popular with 22 plays, followed by "Neanderthal" and "See a Little Light" with 6 plays each.
Links to the Code for Example of RAG with SQL
I've linked to the code here: https://github.com/timowlmtn/sql-rag/tree/main
The Folders
client - The client will run the server through a bash call.
python agent/client/MCPClient.py agent/bash/run_query_agent.sh
bash - Wrapping your server in a bash script will make debugging easier. It sets your environment variables to connect to your database.
server - This code will contain the server connection classes to connect to your database and format the data.
Comments