{"id":29606874,"url":"https://github.com/donpotts/localtexttosqlchat","last_synced_at":"2026-01-20T16:27:24.036Z","repository":{"id":303078214,"uuid":"1014331714","full_name":"donpotts/LocalTextToSqlChat","owner":"donpotts","description":"Local RAG Sample","archived":false,"fork":false,"pushed_at":"2025-07-05T14:14:39.000Z","size":13,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-07-20T17:47:33.696Z","etag":null,"topics":["local-ai","rag-chatbot","semantic-kernel","text-sql"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/donpotts.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-07-05T14:05:07.000Z","updated_at":"2025-07-05T14:20:10.000Z","dependencies_parsed_at":null,"dependency_job_id":"b4de7444-6aa2-4924-958a-52ff9f977554","html_url":"https://github.com/donpotts/LocalTextToSqlChat","commit_stats":null,"previous_names":["donpotts/localtexttosqlchat"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/donpotts/LocalTextToSqlChat","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/donpotts%2FLocalTextToSqlChat","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/donpotts%2FLocalTextToSqlChat/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/donpotts%2FLocalTextToSqlChat/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/donpotts%2FLocalTextToSqlChat/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/donpotts","download_url":"https://codeload.github.com/donpotts/LocalTextToSqlChat/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/donpotts%2FLocalTextToSqlChat/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28607065,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-20T16:10:39.856Z","status":"ssl_error","status_checked_at":"2026-01-20T16:10:39.493Z","response_time":117,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["local-ai","rag-chatbot","semantic-kernel","text-sql"],"created_at":"2025-07-20T17:37:21.666Z","updated_at":"2026-01-20T16:27:24.030Z","avatar_url":"https://github.com/donpotts.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"﻿# Local LLM Database Chat with .NET and Semantic Kernel\n\nThis project is a C# console application that demonstrates how to chat with a local SQLite database using natural language. It leverages the power of a local Large Language Model (LLM) served by **Ollama** and the AI orchestration capabilities of **Microsoft Semantic Kernel**.\n\nThe application follows a simple, two-step RAG (Retrieval-Augmented Generation) pattern:\n1.  It converts a user's question into an SQL query.\n2.  It executes the query, retrieves the data, and then generates a friendly, natural language answer based on the results.\n\n## Demo\n\nHere is an example of what a session with the application looks like:\n\n```\nDatabase 'local_company.db' created and populated.\nDatabase Schema:\nCREATE TABLE Employees (\n            Id INTEGER PRIMARY KEY,\n            Name TEXT NOT NULL,\n            Department TEXT NOT NULL,\n            Salary INTEGER NOT NULL,\n            HireDate TEXT NOT NULL\n        )\n\nChat with your database! Type 'exit' to quit.\n\u003e Who are the engineers?\n\n🤖 Generated SQL: SELECT Name FROM Employees WHERE Department = 'Engineering';\n🗃️ Query Result:\nName\nAlice Johnson\nCharlie Brown\n\n💬 Answer: The engineers are Alice Johnson and Charlie Brown.\n\n\u003e What is the average salary in the Sales department?\n\n🤖 Generated SQL: SELECT AVG(Salary) FROM Employees WHERE Department = 'Sales'\n🗃️ Query Result:\nAVG(Salary)\n80000\n\n💬 Answer: The average salary for the Sales department is 80000.\n\n\u003e exit\n```\n\n## Features\n\n-   **Natural Language to SQL:** Ask complex questions in plain English.\n-   **Two-Step RAG Pipeline:** Ensures the LLM's final answer is grounded in real data from your database.\n-   **Local-First:** Runs entirely on your machine, with no need for cloud-based AI services. Your data stays private.\n-   **Powered by .NET \u0026 Semantic Kernel:** Built on the latest **.NET 9** and the flexible Semantic Kernel library for AI orchestration.\n-   **Easy to Customize:** Simple to modify the prompts, change the LLM model, or connect to a different database.\n\n## How It Works\n\nThe application's logic is straightforward:\n\n1.  **Database Setup:** On first run, it creates a simple `local_company.db` SQLite database with an `Employees` table and populates it with sample data.\n2.  **Schema Retrieval:** It reads the `CREATE TABLE` schema from the database. This schema is crucial context for the LLM.\n3.  **Step 1: Generate SQL Query:**\n    -   You ask a question (e.g., \"Who earns more than 90000?\").\n    -   The application sends your question and the database schema to the LLM using a specialized \"Text-to-SQL\" prompt.\n    -   The LLM returns a single, executable SQL query (`SELECT * FROM Employees WHERE Salary \u003e 90000;`).\n4.  **Step 2: Execute Query \u0026 Generate Final Answer:**\n    -   The application executes the generated SQL against the SQLite database.\n    -   The raw data results are retrieved.\n    -   The application sends the original question *and* the data results to the LLM with a second \"Final Answer\" prompt.\n    -   The LLM uses this information to formulate a friendly, human-readable response (\"Alice Johnson and Charlie Brown earn more than 90000.\").\n\n## Prerequisites\n\nBefore you begin, ensure you have the following installed:\n\n1.  **.NET 9 SDK:** [Download .NET 9](https://dotnet.microsoft.com/download/dotnet/9.0) (Note: Preview versions may be required).\n2.  **Ollama:** [Install Ollama](https://ollama.com/) on your machine and make sure the service is running.\n3.  **An Ollama Model:** This demo is configured to use `phi3`. Pull it by running the following command in your terminal:\n    ```sh\n    ollama run phi3\n    ```\n\n## Getting Started\n\n1.  **Clone the repository:**\n    ```sh\n    git clone https://github.com/donpotts/LocalTextToSqlChat.git\n    cd LocalTextToSqlChat\n    ```\n\n2.  **Run the application:**\n    ```sh\n    dotnet run\n    ```\n\nThe application will start, set up the database, and present you with a `\u003e` prompt.\n\n## Usage\n\n-   Type your question about the employees and press `Enter`.\n-   The application will show you the generated SQL, the raw data, and the final natural language answer.\n-   Type `exit` and press `Enter` to quit the application.\n\n## Customization\n\n### Using a Different LLM\n\nTo use a different model from Ollama (e.g., `llama3` or `mistral`), simply change the `modelId` in the `BuildKernel()` method in `Program.cs`.\n\n```csharp\n// Program.cs\n\nKernel BuildKernel()\n{\n    var builder = Kernel.CreateBuilder();\n\n    builder.AddOllamaChatCompletion(\n        // Change the model ID here\n        modelId: \"llama3\", // For example, use llama3\n        endpoint: new Uri(\"http://localhost:11434\")\n    );\n\n    return builder.Build();\n}\n```\nRemember to pull the new model first with `ollama run \u003cmodel_name\u003e`.\n\n### Using a Different Database\n\nTo adapt this project for your own SQLite database:\n\n1.  **Update the connection string:** Change the `dbPath` constant to point to your database file.\n2.  **Update Schema Retrieval:** Modify the `GetDatabaseSchema()` method to correctly extract the `CREATE TABLE` statements for all relevant tables in your database.\n3.  **Adjust Prompts:** You may need to tweak the prompts in `CreateTextToSqlFunction` if your schema is complex or has unique conventions.\n\n**NOTE: This project is designed to run entirely on your local machine. The AI Models require a fast and powerful computer for quick responses. It does not require any cloud services or external APIs, ensuring complete data privacy and control.**\n\n## 📞 Contact\n\nFor any questions, feedback, or inquiries, please feel free to reach out.\n\n**Don Potts** - [Don.Potts@DonPotts.com](mailto:Don.Potts@DonPotts.com)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdonpotts%2Flocaltexttosqlchat","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdonpotts%2Flocaltexttosqlchat","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdonpotts%2Flocaltexttosqlchat/lists"}