Plugin which translates a question into SQL, fetches relevant data from the database, and formulates a response based on the retrieved data
⭐ Winner of Microsoft's first Semantic Kernel hackathon in the "Most Useful for the Enterprise" category.
This project is my entry to the Semantic Kernel Plugin Hackathon. It's designed to be a drop-in plugin service that can expose an existing database to be queried via natural language. It accomplishes this by leveraging the power of Entity Framework Core and OpenAI's embedding/GPT models to generate and construct SQL queries that retrieve relevant information for RAG based responses.
The demo connects to a modified version of the SQLite movies database available here.
Via Chat Copilot:
Via Swagger:
- Semantic Kernel
- .NET 7 Minimal Web APIs
- Visual Studio Code (C# and C# Dev Kit Extensions)
- Database Creation Script Generation: Use Entity Framework Core to generate the database creation script.
- Embedding Creation: Create embeddings for each part of the database creation script.
- User Input Processing: Take a user's input and get the most relevant parts of the database creation script that will help build a SQL query.
- SQL Query Construction: Build the SQL query using a GPT model.
- Query Execution: Run the query and attempt to retry and have the model fix its query if it fails.
- Response Formatting: Format the response data as a CSV which the model can easily parse.
- Answer Generation: Answer the user's question using the retrieved data for grounding (RAG).
- Code cleanup
- Modifying the prompts to produce better results
- Moving hardcoded options to be environment configurable
- Ensure the user connecting to the database has the appropriate permissions (or lack thereof) to prevent SQL injection or users viewing data they shouldn't.
- Seed the kernel database schema memories as part of a preprocessing pipeline instead of every run.
- Be aware of responses overloading the model token window.
To get a local copy up and running follow the below steps.
- .NET 7
- Visual Studio Code
- C# and C# Dev Kit Extensions
- Clone the repo
git clone https://github.com/anthonypuppo/sk-nl2ef-plugin.git
- Install .NET packages
dotnet restore
- Open
appsettings.json
- Update the
SemanticKernel:AIService
configuration section:- Update
Type
to the AI service you will be using (i.e.,AzureOpenAI
orOpenAI
). - If your are using Azure OpenAI, add/update
Endpoint
to your Azure OpenAI resource Endpoint address.If you are using OpenAI, this property will be ignored.
- Set your Azure OpenAI or OpenAI key by opening a terminal in the project directory and using
dotnet user-secrets
dotnet user-secrets set "SemanticKernel:AIService:Key" "MY_AZUREOPENAI_OR_OPENAI_KEY"
- Update
- Update the
- Run the project
dotnet run
Configure the DB context to expose the relevant parts of the database to the model. The service will automatically seed the database creation script embeddings at startup.
When running locally the plugin will be exposed at https://localhost:7012/.well-known/ai-plugin.json. CORS defaults to allowing ChatGPT as well as https://localhost:7012.