Skip to content

SRCarlo/sql-generator-ai

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

3 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ SQL Generator AI (Spring AI + Groq)

Generate SQL queries from natural language using LLMs
Built with Spring Boot β€’ Powered by Groq β€’ Production-ready architecture


πŸ“Œ Overview

SQL Generator AI is a backend service that transforms natural language queries into SQL statements using Large Language Models (LLMs) via Spring AI.

This project reflects real-world backend engineering practices including:

  • Integration of modern LLM infrastructure in Spring Boot
  • Clean and scalable layered architecture
  • Effective prompt engineering with schema grounding
  • Production-oriented API design

🧠 How It Works

Client Request β†’ REST Controller β†’ Service Layer β†’ Spring AI β†’ Groq (LLaMA 3) β†’ SQL Response
  1. The client sends a natural language query
  2. The backend enriches the prompt with a predefined schema
  3. The request is processed via Spring AI's ChatClient
  4. Groq LLM generates an optimized SQL query
  5. The API returns the SQL response

πŸ€– AI Generated SQL Result

SQL Generator AI Project Preview


SQL Generation Demo


✨ Key Features

  • Natural Language β†’ SQL query generation
  • Integration with Groq (LLaMA 3.3 70B model)
  • Built using Spring AI ChatClient
  • Clean controller-service architecture
  • Schema-aware prompt design for higher accuracy
  • Lightweight, extensible, and production-ready

πŸ—οΈ Project Structure

sql-generator-ai/
β”‚
β”œβ”€β”€ src/main/java/com/example/sqlgeneratorai/
β”‚   β”œβ”€β”€ controller/
β”‚   β”‚   └── SqlController.java
β”‚   β”œβ”€β”€ service/
β”‚   β”‚   └── SqlGeneratorService.java
β”‚   β”œβ”€β”€ dto/
β”‚   β”‚   └── SqlRequest.java
β”‚   └── SqlGeneratorAiApplication.java
β”‚
β”œβ”€β”€ src/main/resources/
β”‚   └── application.properties
β”‚
β”œβ”€β”€ src/test/
β”œβ”€β”€ pom.xml
└── mvnw

βš™οΈ Tech Stack

  • Language: Java 21
  • Framework: Spring Boot
  • AI Integration: Spring AI
  • LLM Provider: Groq
  • Model Used: LLaMA 3.3 (70B Versatile)
  • API Style: RESTful APIs
  • Build Tool: Maven
  • Development Environment: VS Code

πŸ“‘ API Endpoint

πŸ”Ή Generate SQL

POST /api/sql/generate

πŸ“₯ Request

{
  "question": "Get all employees with salary greater than 50000"
}

πŸ“€ Response

SELECT * FROM employees WHERE salary > 50000;

🧠 Prompt Engineering

The application uses a predefined schema to guide the model:

TABLE employees(
    id BIGINT,
    name VARCHAR(100),
    salary DECIMAL,
    department_id BIGINT
);

Benefits

  • Improves SQL accuracy
  • Reduces hallucinated queries
  • Ensures schema consistency

βš™οΈ Configuration

Configure your application in application.properties:

spring.application.name=sql-generator-ai

spring.ai.openai.api-key=YOUR_API_KEY
spring.ai.openai.base-url=https://api.groq.com/openai
spring.ai.openai.chat.options.model=llama-3.3-70b-versatile

πŸš€ Getting Started

πŸ“₯ Clone Repository

git clone https://github.com/SRCarlo/sql-generator-ai.git
cd sql-generator-ai

πŸ—οΈ Build Project

mvn clean install

▢️ Run Application

mvn spring-boot:run

🌐 Test API

POST http://localhost:8080/api/sql/generate

πŸ§ͺ Testing

mvn test

πŸ” Code Highlights

Controller Layer

  • Exposes REST endpoint /api/sql/generate
  • Handles incoming HTTP requests

Service Layer

  • Uses Spring AI ChatClient
  • Injects schema into prompt
  • Processes LLM response

DTO Layer

public record SqlRequest(String question) {}

πŸ“ˆ Future Enhancements

  • Dynamic schema input support
  • SQL validation and optimization layer
  • Multi-database support (MySQL, PostgreSQL, etc.)
  • Swagger / OpenAPI documentation
  • Authentication & rate limiting
  • Docker containerization

⚠️ Security Best Practices

  • Never expose API keys in source code
  • Use environment variables or secret managers
  • Implement request validation and rate limiting

🀝 Contributing

Contributions are welcome!

git checkout -b feature/your-feature
git commit -m "feat: add your feature"
git push origin feature/your-feature

πŸ›‘οΈ License

This project is licensed under the MIT License.

You are free to use, modify, and distribute this software with proper attribution.


πŸ‘¨β€πŸ’» Author

Shubham Raut
Backend Developer | Java | Spring Boot | AI Integrations


⭐ Support

If you found this project helpful:

  • ⭐ Star the repository
  • 🍴 Fork the project
  • πŸ“’ Share it with others

Built with ❀️ using Spring AI and LLMs

About

Generate SQL queries from natural language using Spring AI and Groq (LLaMA 3). Built with Spring Boot using clean architecture and prompt engineering.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages