Skip to content

Commit c91dfaa

Browse files
Initial commit with SpringAI SQL Assistant project
0 parents  commit c91dfaa

35 files changed

+2071
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/mvnw text eol=lf
2+
*.cmd text eol=crlf

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
target/
2+
3+
.idea/
4+
*.iml
5+
.classpath
6+
.project
7+
.settings/
8+
9+
.DS_Store
10+
Thumbs.db
11+
*.log
12+
*.swp
13+
14+
application.yaml
15+
.env
16+
17+
HELP.md
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
wrapperVersion=3.3.2
18+
distributionType=only-script
19+
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/3.9.9/apache-maven-3.9.9-bin.zip

Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
FROM eclipse-temurin:21-jdk-alpine
2+
3+
WORKDIR /app
4+
5+
COPY target/SpringAI-0.0.1-SNAPSHOT.jar app.jar
6+
7+
EXPOSE 8080
8+
9+
ENTRYPOINT ["java", "-jar", "app.jar"]

README.md

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# Spring AI SQL Assistant
2+
![System Architecture](media_examples/architecture_schema.png)
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+
![UI Example](media_examples/Screenshot_1.png)
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!

docker-compose.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
services:
2+
app:
3+
build:
4+
context: .
5+
dockerfile: Dockerfile
6+
container_name: spring-ai-app
7+
ports:
8+
- "8080:8080"
9+
environment:
10+
- SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/your_database
11+
- SPRING_DATASOURCE_USERNAME=username
12+
- SPRING_DATASOURCE_PASSWORD=password
13+
- SPRING_AI_OPENAI_API-KEY=sk...
14+
- SPRING_AI_OPENAI_CHAT_OPTIONS_MODEL=gpt-3.5-turbo
15+
restart: unless-stopped
16+
depends_on:
17+
db:
18+
condition: service_healthy
19+
networks:
20+
- app-network
21+
22+
db:
23+
image: postgres:14
24+
container_name: spring-ai-db
25+
ports:
26+
- "5432:5432"
27+
environment:
28+
- POSTGRES_DB=your_database
29+
- POSTGRES_USER=username
30+
- POSTGRES_PASSWORD=password
31+
volumes:
32+
- postgres_data:/var/lib/postgresql/data
33+
- ./spring_ai_init.sql:/docker-entrypoint-initdb.d/init.sql
34+
healthcheck:
35+
test: ["CMD-SHELL", "pg_isready -U postgres"]
36+
interval: 5s
37+
timeout: 5s
38+
retries: 5
39+
restart: unless-stopped
40+
networks:
41+
- app-network
42+
43+
networks:
44+
app-network:
45+
driver: bridge
46+
47+
volumes:
48+
postgres_data:
49+

media_examples/Screenshot_1.png

85.8 KB
Loading

media_examples/Screenshot_2.png

75.2 KB
Loading

media_examples/Screenshot_3.png

54.5 KB
Loading

media_examples/Screenshot_4.png

81.1 KB
Loading

0 commit comments

Comments
 (0)