https://github.com/donpotts/localsqlragclientserver
Simple sample using Ollama RAG with Sqlite database and Blazor WASM UI
https://github.com/donpotts/localsqlragclientserver
blazor-webassembly dotnet rag-chatbot semantic-kernel sqlite
Last synced: about 2 months ago
JSON representation
Simple sample using Ollama RAG with Sqlite database and Blazor WASM UI
- Host: GitHub
- URL: https://github.com/donpotts/localsqlragclientserver
- Owner: donpotts
- Created: 2025-09-09T19:49:38.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2025-09-09T19:55:42.000Z (10 months ago)
- Last Synced: 2025-09-09T23:29:36.814Z (10 months ago)
- Topics: blazor-webassembly, dotnet, rag-chatbot, semantic-kernel, sqlite
- Language: C#
- Homepage:
- Size: 840 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# LocalSQL RAG Client-Server ๐
> **A Modern Natural Language to SQL Interface with AI Integration**
[](https://dotnet.microsoft.com/)
[](https://blazor.net/)
[](https://sqlite.org/)
A sophisticated client-server application that transforms natural language queries into SQL using local AI models. Built with .NET 9, Blazor WebAssembly, and integrated with Ollama for privacy-focused AI processing.
## ๐ Features
### ๐ค AI-Powered SQL Generation
- **Natural Language Processing**: Convert English questions to SQL queries
- **Local AI Models**: Uses Ollama (Phi3) for complete privacy
- **Smart Query Optimization**: Context-aware SQL generation with few-shot learning
- **Multi-layered Security**: SQL injection protection and query validation
### ๐ Enterprise-Grade Security
- **JWT Authentication**: Stateless, secure token-based authentication
- **Role-Based Access**: Admin/User roles with granular permissions
- **Query Restrictions**: Read-only mode for regular users
- **Input Sanitization**: Comprehensive SQL and input validation
### ๐ผ Professional UI/UX
- **Modern Blazor Interface**: Responsive, component-based architecture
- **Real-time Chat**: Interactive query interface with typing indicators
- **Admin Dashboard**: User management and system administration
- **Professional Design**: Bootstrap 5 with custom styling
### ๐ Data Management
- **Employee Database**: Complete CRUD operations with smart defaults
- **Database Schema**: Optimized SQLite with proper constraints
- **Data Validation**: Comprehensive validation at model and database levels
- **Query History**: Track and manage query interactions
## ๐๏ธ Architecture
```
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Client (Blazor WASM) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โข Authentication Service โข Admin Service โ
โ โข Chat Interface โข Employee Management โ
โ โข Bootstrap UI โข Local Storage โ
โโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ HTTPS/JWT
โโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Server (ASP.NET Core) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โข JWT Authentication โข SQL Query Validation โ
โ โข Text-to-SQL Service โข Database Service โ
โ โข Semantic Kernel โข Admin Controller โ
โโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโผโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโ
โ SQLite DBs โ โ Ollama (Phi3) โ
โ โ โ โ
โ โข app.db โ โ โข Local AI Model โ
โ โข company.db โ โ โข Privacy-focused โ
โโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโ
```
## ๐ Quick Start
### Prerequisites
- [.NET 9 SDK](https://dotnet.microsoft.com/download/dotnet/9.0)
- [Ollama](https://ollama.com/) with Phi3 model
- Modern web browser with WebAssembly support
### Installation
1. **Clone the repository**
```bash
git clone https://github.com/yourusername/LocalSQLRAGClientServer.git
cd LocalSQLRAGClientServer
```
2. **Install Ollama and Phi3 model**
```bash
# Install Ollama from https://ollama.com/
ollama pull phi3:3.8b
ollama serve
```
3. **Configure environment variables** (Production)
```bash
# Set a secure JWT secret (32+ characters)
export JWT_SECRET_KEY="your-super-secure-jwt-secret-key-here-32-chars-minimum"
```
4. **Run the application**
```bash
# Start the server (Terminal 1)
cd LocalTextToSqlChat.Server
dotnet run
# Start the client (Terminal 2)
cd LocalTextToSqlChat.Client
dotnet run
```
5. **Access the application**
- Client: `https://localhost:7115`
- Server API: `https://localhost:7267`
- Health Check: `https://localhost:7267/health`
## ๐ป Usage
### Getting Started
1. **Create an account** - First user automatically becomes admin
2. **Start chatting** - Ask natural language questions about employees
3. **Explore examples** - Click suggested prompts to see capabilities
### Example Queries
```sql
-- Natural Language โ Generated SQL
"Show me all employees"
โ SELECT * FROM Employees
"Who earns more than 80000?"
โ SELECT Name, Department, Salary FROM Employees WHERE Salary > 80000
"List employees by department"
โ SELECT Name, Department, Salary FROM Employees ORDER BY Department, Name
"Add new employee John Smith to Engineering"
โ INSERT INTO Employees (Name, Department) VALUES ('John Smith', 'Engineering')
```
### Admin Features
- **User Management**: Create, update, delete users
- **Role Assignment**: Promote users to admin
- **Employee CRUD**: Full employee management via API
- **System Monitoring**: Health checks and diagnostics
## ๐ง Configuration
### Environment Variables
| Variable | Description | Default | Required |
|----------|-------------|---------|----------|
| `JWT_SECRET_KEY` | JWT signing key (32+ chars) | - | Yes (Production) |
| `ASPNETCORE_ENVIRONMENT` | Environment name | Development | No |
### Configuration Files
**appsettings.json**
```json
{
"Jwt": {
"SecretKey": "", // Leave empty for production
"Issuer": "LocalTextToSqlChat",
"Audience": "LocalTextToSqlChat.Client"
},
"OllamaSettings": {
"BaseUrl": "http://localhost:11434",
"ModelId": "phi3:3.8b"
}
}
```
## ๐ก๏ธ Security Features
### Multi-Layer Protection
- **SQL Injection Prevention**: Parameterized queries + validation
- **Query Intent Analysis**: Prevents unauthorized write operations
- **JWT Security**: Environment-based secrets + proper validation
- **CORS Protection**: Environment-specific allowed origins
- **Security Headers**: XSS, CSRF, content-type protection
### Best Practices Implemented
- โ
Secure password hashing (BCrypt)
- โ
Token expiration and validation
- โ
Role-based authorization
- โ
Input sanitization and validation
- โ
Environment-based configuration
- โ
Health monitoring endpoints
## ๐งช Testing
### API Testing
```bash
# Health check
curl https://localhost:7267/health
# Authentication
curl -X POST https://localhost:7267/api/auth/register \
-H "Content-Type: application/json" \
-d '{"username":"test","email":"test@example.com","password":"Test123!"}'
```
### Database Testing
The application includes comprehensive employee management with smart defaults:
- **HireDate**: Automatically set to current date
- **Department**: Defaults to "Unknown" if not specified
- **Salary**: Defaults to 0 if not provided
## ๐๏ธ Development
### Project Structure
```
LocalSQLRAGClientServer/
โโโ LocalTextToSqlChat.Client/ # Blazor WebAssembly
โ โโโ Pages/ # Razor pages
โ โโโ Services/ # Client services
โ โโโ Models/ # Client models
โโโ LocalTextToSqlChat.Server/ # ASP.NET Core API
โ โโโ Controllers/ # API controllers
โ โโโ Services/ # Business logic
โ โโโ Data/ # Database layer
โ โโโ Models/ # Data models
โโโ README.md
```
### Key Technologies
- **Frontend**: Blazor WebAssembly, Bootstrap 5, SignalR-ready
- **Backend**: ASP.NET Core 9, Entity Framework alternative
- **Database**: SQLite with migration support
- **AI/ML**: Semantic Kernel, Ollama integration
- **Authentication**: JWT Bearer tokens
- **Security**: BCrypt, CORS, Security Headers
## ๐ Deployment
### Production Checklist
- [ ] Set `JWT_SECRET_KEY` environment variable
- [ ] Configure production database connection
- [ ] Set up HTTPS certificates
- [ ] Configure CORS for production domains
- [ ] Enable application logging
- [ ] Set up monitoring and health checks
- [ ] Review and test security configurations
### Docker Support (Coming Soon)
```dockerfile
# Dockerfile example (future enhancement)
FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base
# ... Docker configuration
```
### Development Guidelines
- Follow C# coding standards
- Add unit tests for new features
- Update documentation
- Ensure security best practices
## ๐ Acknowledgments
- **Ollama Team** - Local AI model hosting
- **Microsoft** - Semantic Kernel framework
- **Blazor Community** - WebAssembly innovations
- **.NET Foundation** - Outstanding development platform
---
**Built with โค๏ธ using .NET 9 and Blazor WebAssembly**