Turn Plain English Into SQL Queries with Go and LLMs
Why It Matters
It lowers the barrier for organizations to provide natural‑language data access, cutting development time and enabling on‑prem LLM deployments that keep sensitive data inside the firewall.
Key Takeaways
- •Use LLMs in Go to translate English to SQL queries.
- •Validate generated SQL to allow only SELECT statements for safety.
- •Convert query results to CSV before feeding back to LLM.
- •Chron library runs LLM models locally without external server.
- •Debug flag reveals intermediate prompts, aiding prompt engineering.
Summary
The video walks through a Go‑based prototype that lets users ask plain‑English questions about a DuckDB database and have a large language model generate the corresponding SQL, execute it, and return a natural‑language answer.
The implementation follows a two‑prompt workflow: the first prompt asks the LLM to produce a SELECT‑only SQL statement given the schema and user query; the resulting query is run against the database, its rows are serialized to CSV, and a second prompt feeds the original question, the SQL, and the CSV results back to the model to craft the final answer. Safety checks strip non‑SELECT commands, and a debug flag can expose intermediate outputs for prompt tuning.
The presenter highlights practical details such as embedding prompt templates with Go embed, using the Chron library to host LLMs locally via the OpenAI API contract, and leveraging LangChain‑go for provider‑agnostic calls. An example interaction—“how many rides in the database?”—demonstrates the system returning roughly 650 000 rides after cleaning unwanted markdown, with the entire codebase under 150 lines.
By abstracting the natural‑language‑to‑SQL translation into a few hundred lines of Go, the demo shows how businesses can rapidly build secure, on‑prem data‑access assistants without extensive custom parsing logic, opening the door to broader self‑service analytics while preserving data privacy.
Comments
Want to join the conversation?
Loading comments...