AI-Driven Quotation Microservice (FastAPI + Decimal Finance Engine + Bilingual AI Email Generator)
This project is a production-grade quotation computation and communication microservice designed for B2B sales, procurement, and CRM automation systems. It accepts structured quotation requests via a REST API, performs precise financial calculations using decimal-safe arithmetic, and generates professional bilingual (English & Arabic) quotation emails through a controlled AI abstraction layer.
The service is built using FastAPI and Pydantic, making it fully OpenAPI compliant, strongly typed, and enterprise-ready for integration with CRMs, ERP systems, and sales automation pipelines.
- ๐ท๏ธ Project Title
- ๐งพ Executive Summary
- ๐ Table of Contents
- ๐งฉ Project Overview
- ๐ฏ Objectives & Goals
- โ Acceptance Criteria
- ๐ป Prerequisites
- โ๏ธ Installation & Setup
- ๐ API Documentation
- ๐ฅ๏ธ UI / Frontend
- ๐ข Status Codes
- ๐ Features
- ๐งฑ Tech Stack & Architecture
- ๐ ๏ธ Workflow & Implementation
- ๐งช Testing & Validation
- ๐ Validation Summary
- ๐งฐ Verification Testing Tools
- ๐งฏ Troubleshooting & Debugging
- ๐ Security & Secrets
- โ๏ธ Deployment
- โก Quick-Start Cheat Sheet
- ๐งพ Usage Notes
- ๐ง Performance & Optimization
- ๐ Enhancements & Features
- ๐งฉ Maintenance & Future Work
- ๐ Key Achievements
- ๐งฎ High-Level Architecture
- ๐๏ธ Project Structure
- ๐งญ How to Demonstrate Live
- ๐ก Summary, Closure & Compliance
This system exposes a REST API endpoint POST /quote that accepts a structured quotation request containing customer details and line items. It performs decimal-safe pricing calculations, applies taxes, computes totals, and generates bilingual professional email drafts using a deterministic mock AI layer.
- Provide accurate financial calculations using decimal arithmetic
- Generate professional quotation emails in English and Arabic
- Expose a clean OpenAPI-compliant REST interface
- Ensure validation, testing, and containerized deployment
- Enable enterprise-ready extensibility for real LLM integration
| Criteria | Description |
|---|---|
| API Validity | POST /quote must accept and validate structured JSON |
| Accuracy | All totals must be decimal-safe and reproducible |
| Emails | Both English and Arabic drafts must be generated |
| Testing | All pytest tests must pass |
| Docs | Swagger docs must be available at /docs |
- Python 3.11+
- pip
- Docker (optional)
- Postman or curl
- Create virtual environment
- Install dependencies from requirements.txt
- Run uvicorn app.main:app
- Access http://127.0.0.1:8000/docs
| Method | Endpoint | Description |
|---|---|---|
| POST | /quote | Accepts quotation JSON, returns totals and bilingual email drafts |
This microservice is API-first and UI-agnostic. The interactive Swagger UI at /docs serves as the reference interface for clients and testers.
| Code | Meaning |
|---|---|
| 200 | Quotation generated successfully |
| 422 | Invalid input schema |
| 500 | Internal processing error |
- Decimal-Safe Financial Engine โ Eliminates floating-point rounding errors for real-world pricing and taxation.
- Multi-Item Quotation Processing โ Supports unlimited line items with quantity, unit price, and tax.
- Bilingual AI Communication Layer โ Generates English and Arabic quotation emails using a deterministic AI engine.
- Enterprise-Grade API Validation โ Uses Pydantic schemas to enforce strict data integrity.
- OpenAPI / Swagger Interface โ Automatically generated interactive API documentation.
- Mock LLM Abstraction โ Allows safe offline testing while keeping architecture ready for real OpenAI or Claude.
- Dockerized Microservice โ Portable deployment across cloud, on-premise, or CI/CD pipelines.
- Automated Testing Suite โ Financial correctness verified using Pytest.
| Layer | Technology | Purpose |
|---|---|---|
| API Layer | FastAPI | High-performance REST API and OpenAPI generation |
| Validation | Pydantic | Strong typing, schema enforcement, email validation |
| Finance Engine | Decimal | Accurate currency and tax calculations |
| AI Layer | Mock LLM | Bilingual email generation engine |
| Testing | Pytest | Business logic verification |
| Deployment | Docker | Containerized execution |
Client | | POST /quote v FastAPI Gateway | v Pydantic Validation | v Financial Engine (Decimal) | v AI Draft Generator (Mock LLM) | v Structured JSON Response
- Client submits a quotation request with customer data and line items.
- FastAPI validates all inputs using Pydantic models.
- Each line item is processed using Decimal arithmetic to compute taxed totals.
- The grand total is calculated from all validated line totals.
- The Mock LLM engine generates professional quotation emails in English and Arabic.
- The API returns a structured JSON response containing totals and both email drafts.
| ID | Area | Command | Expected Output | Explanation |
|---|---|---|---|---|
| T1 | Math | pytest | Pass | Verifies line and grand totals |
| T2 | API | POST /quote | 200 | Validates full pipeline |
All inputs are validated using Pydantic and all arithmetic uses Decimal to prevent rounding errors.
- pytest
- curl
- FastAPI Swagger UI
- 422 Unprocessable Entity โ Input JSON does not match Pydantic schema (email format, missing fields, wrong types).
- 500 Internal Server Error โ Logic or AI layer failure; check Uvicorn logs.
- Incorrect totals โ Indicates Decimal conversion issue or invalid numeric input.
- Swagger not loading โ Ensure FastAPI is running on port 8000 and no firewall is blocking it.
No API keys are required. The Mock LLM ensures zero data leakage and full offline execution.
The microservice is containerized using Docker and can be deployed to any cloud or on-premise infrastructure including AWS ECS, Azure Container Apps, GCP Cloud Run, or a private VPS.
- Stateless microservice โ horizontally scalable
- No external API dependencies in Mock LLM mode
- Ready for CI/CD pipelines
- Install โ Run โ Open /docs โ Send POST /quote
Clients must submit valid email addresses and numeric prices. Currency precision is preserved via Decimal.
- FastAPI provides asynchronous request handling.
- Decimal ensures correctness over raw float performance.
- Stateless design allows horizontal scaling.
- Docker image is lightweight (Python slim base).
- Replace Mock LLM with OpenAI or Claude
- Add PDF quotation generation
- CRM integration
- Add database persistence
- Add authentication
- Add async LLM support
- Built a finance-grade pricing engine using Decimal.
- Implemented bilingual AI-driven business communication.
- Delivered a fully documented OpenAPI microservice.
- Packaged the entire system for cloud deployment.
The Quotation Microservice is designed as a clean, stateless, API-driven financial and AI communication engine. It follows a layered microservice architecture that separates input validation, business logic, and AI-based content generation, ensuring scalability, security, and auditability.
| Layer | Responsibility | Key Components |
|---|---|---|
| Client Layer | Submits quotation requests | Postman, CRM, Web App, Swagger UI |
| API Gateway | Request routing & validation | FastAPI |
| Validation Layer | Schema & data integrity enforcement | Pydantic Models |
| Business Logic Layer | Financial computation engine | logic.py (Decimal pricing) |
| AI Communication Layer | Bilingual email generation | mock_llm.py |
| Response Layer | Structured JSON output | FastAPI Serializer |
โโโโโโโโโโโโโโโโโโโโโโโโ
โ Client Applications โ
โ (CRM, Swagger, API) โ
โโโโโโโโโโโโโฌโโโโโโโโโโโ
โ POST /quote (JSON)
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ FastAPI API โ
โ app.main : /quote โ
โโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Pydantic Validation โ
โ Email, Items, Price, Tax โ
โโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโ
โ Validated Data
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Financial Engine โ
โ logic.py (Decimal Math) โ
โ - Line Totals โ
โ - Tax Calculation โ
โ - Grand Total โ
โโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโ
โ Calculated Quote
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ AI Communication Layer โ
โ mock_llm.py โ
โ - English Email โ
โ - Arabic Email โ
โโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Structured API Response โ
โ Totals + Email Drafts โ
โโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโ
โ
โผ
โโโโโโโโโโโโโโโโโโโโโโโโ
โ Client Receives โ
โ Final Quotation โ
โโโโโโโโโโโโโโโโโโโโโโโโ
- Stateless microservice โ horizontally scalable
- Strict data validation before any computation
- Financial-grade decimal arithmetic
- AI layer isolated for safe testing and future LLM integration
- API-first design for CRM, ERP, and SaaS integrations
- Container-ready for cloud deployment
Quotation Microservice/ โ โโ app/ โ โโ main.py โ โโ models.py โ โโ logic.py โ โโ mock_llm.py โ โโ tests/ โ โโ test_quote.py โ โโ Dockerfile โโ requirements.txt โโ README.md โโ technical_project_detail.pdf
- Run uvicorn
- Open /docs
- Submit POST /quote
- Show totals + bilingual email output
This project demonstrates enterprise-grade API engineering, financial computation accuracy, and AI-assisted business communication in a single microservice. It is compliant with modern backend standards including OpenAPI, containerization, test automation, and clean architecture, making it suitable for production, SaaS platforms, and CRM-driven sales automation.