Accessing structured data from databases traditionally requires knowledge of SQL and familiarity with the underlying schema. This creates a barrier for non-technical users who need timely insights but lack technical expertise.
Recent advancements in Large Language Models (LLMs) and tools like LlamaIndex have enabled a new approach—allowing users to interact with databases using natural language. By combining LlamaIndex with Azure OpenAI and SQLAlchemy, developers can build intelligent interfaces that translate plain English into accurate SQL queries, execute them, and return meaningful results.
This blog demonstrates how to set up a simple Text-to-SQL application using LlamaIndex’s NLSQLTableQueryEngine, making structured data easily accessible through conversational language.
LlamaIndex (formerly known as GPT Index) is a data framework for LLM applications, designed to help developers build powerful interfaces between large language models (LLMs) and private or structured data sources like documents, databases, and APIs.
Text-to-SQL allows users to ask natural language questions and automatically convert them into valid SQL queries. LlamaIndex makes this possible using a Query Engine and optional Retrievers that:
Let’s look at how to implement a simple Text-to-SQL pipeline using Azure OpenAI, LlamaIndex, and a Microsoft SQL Server.
os.environ["OPENAI_API_KEY"] = "your_api_key" os.environ["AZURE_OPENAI_ENDPOINT"] = "end_point" os.environ["OPENAI_API_VERSION"] = "api_version"
This configures your Azure OpenAI connection so the LLM can be used for query generation.
llm = AzureOpenAI(engine="deployment_name", model="model_name", temperature=0.0)
We use llama_index.llms.azure_openai.AzureOpenAI to instantiate a deterministic LLM client via Azure.
connection_string = f"mssql+pymssql://{sqluser}:{sqlpassword}@server_name/database_name" engine = create_engine(connection_string)
This sets up a SQLAlchemy engine to connect to your Microsoft SQL database.
sql_database = SQLDatabase(engine, include_tables=["table_1", "table_2"])
We wrap the database connection using SQLDatabase, limiting access to specific tables (which is good for both performance and security).
query_engine = NLSQLTableQueryEngine(sql_database=sql_database, llm=llm)
The NLSQLTableQueryEngine enables natural language queries by translating them to SQL based on schema introspection.
response = query_engine.query("your natural language query")
This takes the input question, converts it to SQL, queries the database, and returns the results.
By combining LlamaIndex, Azure OpenAI, and SQLAlchemy, you can build a seamless Natural Language to SQL interface that:
This setup is ideal for internal analytics tools, chatbots for database queries, or any application where users need answers from structured data—just by asking in plain English.
Sanjay N