|
| 1 | +# Spring AI SQL Assistant |
| 2 | + |
| 3 | +Ask a question in natural language, and AI will generate and execute a SQL query. The application is connected to PostgreSQL, where the `ai_service` table containing the parameters of 18 AI models (prices, SQL support, providers, etc.) is stored. |
| 4 | + |
| 5 | +## 💡 Tech Stack |
| 6 | + |
| 7 | +- Java |
| 8 | +- Spring Boot 3.4.5 |
| 9 | +- Spring AI 1.0.0-M8 |
| 10 | +- PostgreSQL |
| 11 | +- OpenAI GPT (gpt-3.5-turbo) |
| 12 | +- 🐳 Docker + Docker Compose |
| 13 | + |
| 14 | +## Project Showcase |
| 15 | + |
| 16 | + |
| 17 | +- [UI walkthrough video](./media_examples/app_video.mp4) _(short demo, downloadable if not previewable)_ |
| 18 | +- 📂 Example screenshots and results are located in the `media_example/` folder |
| 19 | + |
| 20 | +## ⚙️ Key Engineering Challenges |
| 21 | + |
| 22 | +- **Natural language to SQL translation using LLMs** |
| 23 | + Integrated Spring AI with OpenAI (GPT-3.5) to translate user questions into SQL queries based on the structure of the `ai_services` table. |
| 24 | + |
| 25 | +- **Safe dynamic SQL execution** |
| 26 | + Designed a secure way to execute dynamically generated SQL without injection risks, using validation and controlled execution via `JdbcTemplate`. |
| 27 | + |
| 28 | +- **Database bootstrapping with production-ready data** |
| 29 | + On first startup, the PostgreSQL container automatically initializes the `ai_services` table with 18 real AI model entries via `spring_ai_init.sql`. |
| 30 | + |
| 31 | +- **Dockerized multi-container setup with persistent storage** |
| 32 | + Used Docker Compose to orchestrate the Spring Boot app and PostgreSQL with named volumes and health checks to ensure reliable startup and data persistence. |
| 33 | + |
| 34 | +- **Flexible, language-agnostic prompting pipeline** |
| 35 | + The LLM prompting system is designed to handle both SQL and natural questions with domain-specific context, and can be extended to other database schemas. |
| 36 | + |
| 37 | + |
| 38 | +## ✅ Testing |
| 39 | + |
| 40 | +The Java backend is covered with both unit and integration-style tests for core logic components: |
| 41 | + |
| 42 | +- Text-to-SQL conversion logic: Validates that natural language questions are correctly translated into SQL using mocked ChatClient (LLM). |
| 43 | +- SQL execution layer: Verifies safety of query execution, correct result formatting, null-handling, column ordering, and exception handling using JdbcTemplate. |
| 44 | +- Safety filters: Prevent unsafe queries (DELETE, DROP, etc.) from being executed — covered by negative test cases. |
| 45 | + |
| 46 | +## 🐳 How to Run |
| 47 | + |
| 48 | +1. Open docker-compose.yml and update these environment variables: |
| 49 | +- POSTGRES_PASSWORD=your_password |
| 50 | +- SPRING_AI_OPENAI_API-KEY=your_openai_api_key |
| 51 | +- |
| 52 | +2. Run everything with Docker Compose: |
| 53 | + |
| 54 | +```bash |
| 55 | +docker-compose up --build |
| 56 | +``` |
| 57 | + |
| 58 | +3. Access service: |
| 59 | + - `http://localhost:8080/ask` |
| 60 | + |
| 61 | +## ⚠️ Tech Notes |
| 62 | + |
| 63 | +- This project uses the OpenAI API, but the architecture allows for easy integration with other LLM providers (e.g. Anthropic, Google, Cohere, Mistral) depending on business needs and cost model. |
| 64 | + |
| 65 | +## 🤝 Thanks for your interest! |
| 66 | + |
| 67 | +- I'm always open to feedback, collaboration, or professional connections. |
| 68 | +- Feel free to reach out! |
0 commit comments