# sql_to_fastapi_LLM_generator
**Repository Path**: ncepu-bj/sql_to_fastapi_LLM_generator
## Basic Information
- **Project Name**: sql_to_fastapi_LLM_generator
- **Description**: 通过大模型来生成基于fastapi框架的标准RESTful风格的分层接口项目
- **Primary Language**: Python
- **License**: MIT
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 3
- **Forks**: 0
- **Created**: 2025-08-27
- **Last Updated**: 2026-01-10
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# SQL to FastAPI LLM Code Generator




**Intelligent SQL to FastAPI Code Generator powered by Chinese Large Language Models**
*Transform SQL table structures into high-quality FastAPI RESTful API projects through enhanced prompt engineering*
## 🎯 Overview
This project is an intelligent code generator powered by Large Language Models (LLMs) that transforms SQL database table structures into complete FastAPI RESTful API projects. The project adopts a core strategy of "**Enhanced Prompt Engineering + Lightweight Templates**", supports mainstream Chinese LLMs (DeepSeek, Qianwen, Kimi), and generates complete three-tier architecture code including model layer, service layer, and API layer.
### ✨ Key Features
- 🧠 **Intelligent Prompt System**: Deep analysis of SQL table structures to generate targeted professional prompts
- 📊 **Smart Complexity Assessment**: Multi-dimensional evaluation of table structure complexity with automatic optimal strategy selection
- 🎯 **Layered Code Generation**: Complete generation of SQLAlchemy models, business services, and FastAPI routes
- 🔍 **Code Quality Assurance**: Built-in syntax checking, quality scoring, and improvement suggestion system
- 🌏 **Chinese LLM Optimization**: Specially optimized for DeepSeek, Qianwen (Tongyi Qianwen), Kimi and other domestic models
- 🏗️ **Enterprise Architecture**: Standard three-tier architecture with complete exception handling and data validation
## 🏗️ System Architecture
```
SQL Structure → Context Analysis → Complexity Assessment → Smart Prompts → LLM Generation → Code Validation → FastAPI Project
↓ ↓ ↓ ↓ ↓ ↓ ↓
Parser Semantic Inference Strategy Selection Layer Prompts Model Call Quality Check Project Assembly
```
### Core Components
- **SQL Parser**: Parse SQL DDL and extract table structures and constraint information
- **Prompt System**: Intelligently analyze and generate high-quality code generation prompts
- **LLM Integration**: Unified interface supporting multiple Chinese large language models
- **Code Generation Engine**: Generate complete FastAPI project code based on analysis results
- **Quality Validator**: Ensure syntax correctness and best practices of generated code
## 🚀 Quick Start
### Requirements
- Python 3.9+
- Supported LLM Services: DeepSeek API, Qianwen API, Kimi API
### Installation
1. **Clone the repository**
```bash
git clone https://github.com/your-repo/sql_to_fastapi_LLM_generator.git
cd sql_to_fastapi_LLM_generator
```
2. **Create virtual environment**
```bash
# Using conda (recommended)
conda create -n sql_to_fastapi python=3.11
conda activate sql_to_fastapi
# Or using venv
python -m venv venv
source venv/bin/activate # Linux/Mac
# or venv\Scripts\activate # Windows
```
3. **Install dependencies**
```bash
pip install -r requirements.txt
```
4. **Configure LLM services**
```bash
# Copy configuration template
cp .env.example .env
# Edit .env file and add your LLM API keys
# DEEPSEEK_API_KEY=your_deepseek_api_key
# QIANWEN_API_KEY=your_qianwen_api_key
# KIMI_API_KEY=your_kimi_api_key
```
### Basic Usage
1. **Prepare SQL file**
```sql
-- Save your SQL DDL to input/ directory
-- Example: input/my_database.sql
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(100) NOT NULL,
password VARCHAR(255) NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME ON UPDATE CURRENT_TIMESTAMP
);
```
2. **Run generator**
```bash
python start.py --input input/my_database.sql --output output/my_project
```
3. **Check generated results**
```
output/my_project/
├── app/
│ ├── models/ # SQLAlchemy models
│ ├── services/ # Business logic layer
│ ├── routers/ # FastAPI routes
│ └── schemas/ # Pydantic data models
├── requirements.txt # Project dependencies
└── main.py # FastAPI application entry
```
## 📚 Documentation
### Core Documentation
- 📋 [Implementation Roadmap](./docs/IMPLEMENTATION_ROADMAP.md) - Overall project planning and progress
- 🏗️ [Prompt System Architecture](./docs/prompt_system_architecture.md) - Core prompt system detailed explanation
- 📖 [Prompt Engineering Guide](./docs/prompt_engineering_guide.md) - Prompt design and optimization
### Development Documentation
- 🎯 [Week 2 Summary](./docs/WEEK2_SUMMARY.md) - Latest development progress
- 🏢 [Enterprise Architecture Design](./docs/enterprise_architecture_design.md) - Overall architecture design
- 📝 [Documentation Index](./docs/README.md) - Complete documentation directory
## 🧪 Testing
Run complete test suite:
```bash
# Run all tests
python -m pytest tests/ -v
# Run specific tests
python -m pytest tests/test_prompt_system.py -v
python -m pytest tests/test_llm_clients.py -v
```
## 🎨 Project Highlights
### 🧠 Intelligent Prompt Engineering
- **Business Semantic Analysis**: Automatically infer field business meanings (email, phone, password, etc.)
- **Complexity Adaptation**: Intelligently select generation strategies based on table complexity
- **Layer Specialization**: Generate specialized prompts for model, service, and API layers
### 🎯 Code Quality Assurance
- **Syntax Validation**: Automatically check syntax correctness of generated code
- **Quality Scoring**: Multi-dimensional quality scoring of generated code (1-10 points)
- **Best Practices**: Built-in FastAPI and SQLAlchemy best practices
### 🌏 Chinese LLM Deep Optimization
- **DeepSeek**: Prompt strategies optimized for DeepSeek models
- **Qianwen (Tongyi Qianwen)**: Special handling adapted for Alibaba Cloud Qianwen models
- **Kimi**: Support for Moonshot Kimi model's long-text capabilities
## 📊 Development Progress
### ✅ Phase 1 (Completed)
- [x] Project basic architecture
- [x] SQL parser
- [x] Database schema analyzer
- [x] LLM client integration
- [x] Exception handling system
### ✅ Week 2 (Completed) - Prompt System Framework
- [x] Intelligent prompt builder (227 lines of code)
- [x] Deep context analyzer (398 lines of code)
- [x] Multi-dimensional complexity evaluator (414 lines of code)
- [x] Lightweight template system (699 lines of code)
- [x] LLM response parser (502 lines of code)
- [x] Complete test suite (2574 lines total code)
### 🔄 Week 3 (In Progress) - Generation Engine Core
- [ ] Generation engine master controller
- [ ] Simple CRUD generation strategy
- [ ] Basic code validator
- [ ] Project assembler
- [ ] Basic CLI commands
## 🤝 Contributing
We welcome all forms of contributions!
### Ways to Contribute
1. 🐛 **Report Bugs**: Describe issues in Issues
2. 💡 **Feature Suggestions**: Propose new feature ideas
3. 📖 **Improve Documentation**: Enhance project documentation
4. 💻 **Code Contributions**: Submit Pull Requests
### Development Workflow
```bash
# 1. Fork this repository
# 2. Create feature branch
git checkout -b feature/your-feature-name
# 3. Commit changes
git commit -m "Add: your feature description"
# 4. Push to branch
git push origin feature/your-feature-name
# 5. Create Pull Request
```
## 📜 License
This project is licensed under the [MIT License](LICENSE).
## 🙏 Acknowledgments
- Thanks to [DeepSeek](https://deepseek.com/) for providing excellent large language model services
- Thanks to [Alibaba Cloud Tongyi Qianwen](https://tongyi.aliyun.com/) for technical support
- Thanks to [Moonshot Kimi](https://kimi.moonshot.cn/) for long-text processing capabilities
- Thanks to the FastAPI and SQLAlchemy communities for their outstanding contributions
---
**🌟 If this project helps you, please give us a Star!**
Made with ❤️ by SQL to FastAPI LLM Generator Team
1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md
2. Gitee blog [blog.gitee.com](https://blog.gitee.com)
3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore)
4. The most valuable open source project [GVP](https://gitee.com/gvp)
5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help)
6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)