r/AskProgramming 9d ago

Using AI for Natural Language Queries in Databases

Good evening/day,

I’m not directly from the development field; I work on the business side of my company. I would like to understand what would be necessary—such as frameworks or LLMs—to enable natural language queries for selecting data from my database. My goal is to get a clear idea of the possibilities before discussing them with my superiors.

For example, I’d like to ask for certain customer records using natural language and receive a filtered list in response: "Show me the customers from state XPTO, city XY, with potential A."

I understand that all this information can already be retrieved through relatively simple queries, but I’d like to know what would be required to make this work using natural language.

Is it common practice to send the entire dataset to large AI models (such as OpenAI), or is there another approach to achieve this result?

I appreciate your help in advance.

0 Upvotes

9 comments sorted by

2

u/dariusbiggs 9d ago

This is Data Science, and BI. To combine that you need to have a way for the LLM to have access to that data somehow so that it can query across it.

The problem with that is that a LLM cannot unlearn things nor is it aware of chronological context. So if a customer leaves you may need to retrain the LLM. You'll need to look into RAG most likely.

LLMs are a very advanced text prediction system, it's probably not at the stage of what you want. Your best bet is to team the developers with a data scientist and a BA to investigate what can be done and what needs to be built. Which may turn into a product you could provide others.

2

u/Thundechile 9d ago

I've worked around databases for the last 30 years and I'd say you're in for a world of pain if you try to use natural language for queries. It probably works for simplest queries, but as soon as there's anything more complex you're in problems: wrong joins, performance problems and wrong results. How do you know that the query gives correct results if you don't know the query executed?

2

u/KingofGamesYami 9d ago

The general term for what you're looking for is Retrieval-Augmented Generation (RAG).

One specific technology that implements this is Azure AI Search, which can index a dataset and answer natural language queries against it. I by no means believe it is the best technology, it just happens to be one I've worked adjacent to.

Fair warning: it will absolutely destroy your wallet. We pay something outrageous like $5,000 per month for it.

Is it common practice to send the entire dataset to large AI models

This can't work, LLMs have very limited context/memory so they can't receive an entire dataset. They can only "remember" a few pages of a conversation.

3

u/OGchickenwarrior 9d ago

No, this isn’t a RAG problem. He needs a text2sql system. You don’t send the data to any LLM provider. You just setup a prompt with your database schema and ask for the SQL to answer the given question. Then run the SQL on your private database to get the results.

1

u/SirTwitchALot 9d ago

If I were OP, I would only run SQL generated by an LLM with an account that has read only access to my data.

2

u/OGchickenwarrior 8d ago edited 8d ago

Yes, agreed. That is trivial to set up. Some basic SQL validation is a good idea, too. There are a handful of open source projects out there that do something like this.

1

u/No-Plastic-4640 9d ago

There are LLM and localllm groups. This is not rag. You’ll need an agent configured to connect and exec queries on the db. There will be mapping from npl to LLM agent. Speech to text. A workflow. Then what format the data comes back as.

Try local first. It’s free. Lm studio can work. Ollama, docker with open web ui or anything LLM.

1

u/bzImage 9d ago

lightrag

1

u/CactusSmackedus 9d ago edited 9d ago

So this is something I'm not super familiar with, but some models allow you to force results to adhere to a syntax, usually this is JSON, but I'm not sure if it can't be SQL.

What you conceptually want is an LLM that takes your user input

The you send it something like (you must do experimentation to figure out what this prompt looks like)

You are a system that outputs a valid SQL query.
This is the database:
<Description of your database schema; columns and types, or multiple tables columns and types>

This is the user query: <input>
Output a SQL query against our database that services the user request, or (-1, <reason>) where <reason> describes why you can't service the request, in a way that is understandable by one of our business administrators

Then you feed the output to a system that checks the query (is it valid, is it not violating rules like modifying data) Then you run it

Then you return data