This project demonstrates how to build an Intelligent SQL Query Assistant that enables users to interact with their Neon database using natural language. The assistant uses Azure OpenAI, pgvector extension in Neon, and Retrieval-Augmented Generation (RAG) approach to dynamically generate and execute SQL queries. Read more on how to guide blog post.
- Natural Language Querying: Ask questions in plain English, and the assistant translates them into SQL queries.
- Vector-Based Schema Matching: Uses embeddings and Neon's
pgvector
extension to find the most relevant database schema. - Customizable APIs:
- SchemaTraining API: Extracts the database schema, generates embeddings, and stores them in Neon.
- QueryAssistant API: Processes user queries, matches the schema, generates SQL, and executes it.
- Azure OpenAI Integration: Leverages GPT models like
gpt-4
andtext-embedding-ada-002
.
- .NET Core: Backend logic and API development.
- Azure Functions: Serverless platform for API hosting.
- Neon: Vector storage using the pgvector extension.
- Azure OpenAI: For generating embeddings and SQL queries.
- Azure AI Foundry: For deploying and managing AI models.
Before we begin, make sure you have the following:
- Install Tools
- .NET Core SDK
- Azure Functions Core Tools installed
- A free Neon account
- An Azure account with an active subscription
- Create Neon Project
- Sign up for Neon.
- Create the database tables
- Azure OpenAI Resource
- Create an Azure OpenAI resource.
- Deploy models:
gpt-4
andtext-embedding-ada-002
.
SqlQueryAssistant │ SqlQueryAssistant.sln | ├───SqlQueryAssistant.Common │ │ ChatCompletionService.cs │ │ EmbeddingService.cs │ │ SchemaService.cs │ │ SchemaConverter.cs │ │ SqlExecutorService.cs │ │ SchemaRetrievalService.cs │ │ VectorStorageService.cs │ └───SqlQueryAssistant.Common.csproj │ ├───SqlQueryAssistant.Data | | customers.sql | | schema.sql └───SqlQueryAssistant.Functions │ host.json │ local.settings.json │ QueryAssistantFunction.cs └───SqlQueryAssistant.Functions.csproj
git clone https://github.com/neondatabase-labs/sql-query-assistant.git cd sql-query-assistant
Create a local.settings.json
file in the SqlQueryAssistant.Functions
project:
{ "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated", "AzureOpenAI__Endpoint": "https://your-azure-openai-endpoint/", "AzureOpenAI__ApiKey": "your-api-key", "AzureOpenAI__EmbeddingDeploymentName": "text-embedding-ada-002", "NeonDatabaseConnectionString": "Host=your-neon-host;Database=your-database;Username=your-username;Password=your-password;SSL Mode=Require;Trust Server Certificate=true" } }
dotnet restore build
func start
- Endpoint:
http://localhost:7071/api/schema-training
- Method:
POST
- Description: Trains the assistant by extracting the schema from Neon, generating embeddings, and storing them in the database.
curl -X POST http://localhost:7071/api/schema-training
- Endpoint:
http://localhost:7071/api/query-assistant
- Method:
POST
- Description: Processes user queries, retrieves the relevant schema, generates SQL, and returns the query result.
curl -X POST http://localhost:7071/api/query-assistant \ -H "Content-Type: application/json" \ -d '{"Query": "List all customers who signed up after 2022-01-01."}'