Sistema de criacao de formularios visuais (drag-and-drop) que gera orcamentos automaticos usando Inteligencia Artificial. Construido para a Total Electrique (instalacao de bornes de recharge EV no Quebec, Canada).
┌──────────────────────────────────────────────────────┐
│ FRONTEND (SvelteKit) │
│ localhost:5173 │
│ │
│ /admin/flows/[id]/edit → Editor visual de flows │
│ /q/[slug] → Formulario do cliente │
│ /api/flows/* → CRUD flows (MongoDB) │
│ /api/generate-quote → Proxy → Backend Python │
└────────────────────────┬─────────────────────────────┘
│ HTTP POST
┌────────────────────────▼─────────────────────────────┐
│ BACKEND (Python/FastAPI) │
│ localhost:8001 │
│ │
│ POST /api/submissions → Gera orcamento com IA │
│ GET /api/submissions → Lista submissions │
│ │
│ PydanticAI Agent → Output estruturado (QuoteOutput)│
│ CSV pricing → Precos reais, sem alucinacao │
│ Output validator → Recalcula totais e taxes │
└──────────────────────────────────────────────────────┘
│
MongoDB Atlas
(flows, submissions)
| Camada | Tecnologia |
|---|---|
| Frontend | SvelteKit + Svelte 5 (runes) + Tailwind CSS |
| Flow Builder | @xyflow/svelte (SvelteFlow) |
| Backend | Python 3.12 + FastAPI + Motor (async MongoDB) |
| Agente IA | PydanticAI com output parsing estruturado |
| Banco | MongoDB Atlas |
| Testes E2E | Playwright |
git clone <repo-url>
cd frontendSvelteFlow
cd backend
# Criar e ativar virtual environment
python -m venv venv
# Windows:
venv\Scripts\activate
# Linux/Mac:
source venv/bin/activate
# Instalar dependencias
pip install -r requirements.txt
# Configurar variaveis de ambiente
cp .env.example .env
# Editar .env com suas credenciais (MongoDB URI, API keys)
# Rodar o backend
uvicorn main:app --host 0.0.0.0 --port 8001
O backend roda em http://localhost:8001.
cd frontend
# Instalar dependencias
npm install
# Configurar variaveis de ambiente
cp .env.example .env
# Editar .env com sua MongoDB URI e URL do backend
# Rodar em modo desenvolvimento
npm run dev
O frontend roda em http://localhost:5173.
http://localhost:5173/adminhttp://localhost:5173/q/<slug-do-flow>backend/.env)| Variavel | Descricao | Exemplo |
|---|---|---|
MONGODB_URI |
Connection string do MongoDB Atlas | mongodb+srv://user:[email protected]/ |
MONGODB_DATABASE |
Nome do banco | flowquote |
OPENAI_API_KEY |
Chave da API OpenAI | sk-proj-... |
OPENAI_MODEL |
Modelo OpenAI | gpt-4o-mini |
AI_PROVIDER |
Provedor de IA (openai ou anthropic) |
openai |
ANTHROPIC_API_KEY |
Chave Anthropic (se usar Claude) | sk-ant-... |
ANTHROPIC_MODEL |
Modelo Anthropic | claude-sonnet-4-20250514 |
CORS_ORIGINS |
Origens CORS permitidas | ["http://localhost:5173"] |
frontend/.env)| Variavel | Descricao | Exemplo |
|---|---|---|
MONGODB_URI |
Connection string do MongoDB Atlas | mongodb+srv://user:[email protected]/ |
MONGODB_DATABASE |
Nome do banco | flowquote |
BACKEND_URL |
URL do backend Python | http://localhost:8001 |
Importante: Frontend e Backend compartilham o MESMO MongoDB Atlas.
frontendSvelteFlow/
├── backend/ # API Python/FastAPI
│ ├── main.py # Entry point + CORS + lifespan
│ ├── requirements.txt # Dependencias Python
│ ├── config/
│ │ ├── settings.py # Pydantic Settings (.env)
│ │ └── database.py # Motor MongoDB client
│ ├── routers/
│ │ ├── flow.py # CRUD flows
│ │ └── submission.py # Submissions + quote generation
│ ├── services/
│ │ ├── submission_service.py # Orquestracao
│ │ └── quote_generator.py # Agente PydanticAI
│ ├── dtos/
│ │ └── submission/
│ │ └── create_submission/
│ │ ├── request.py # Pydantic request model
│ │ └── response.py # Pydantic response model (output parsing)
│ ├── factories/ # Criacao de documentos
│ ├── mappers/ # MongoDB <-> Response
│ └── data/repositories/ # CRUD MongoDB
│
├── frontend/ # SvelteKit App
│ ├── src/
│ │ ├── lib/
│ │ │ ├── dto/flows/ # Types + Request DTOs
│ │ │ ├── stores/ # flowBuilder store (Svelte 5 runes)
│ │ │ ├── services/ # FlowsService, SubmissionsService
│ │ │ └── data/repositories/ # API calls
│ │ └── routes/
│ │ ├── admin/flows/[id]/edit/ # Editor visual de flows
│ │ ├── q/[slug]/ # Formulario publico do cliente
│ │ └── api/
│ │ ├── flows/ # CRUD flows (SvelteKit → MongoDB)
│ │ ├── generate-quote/ # Proxy → Backend Python
│ │ └── submissions/ # CRUD submissions
│ ├── tests/
│ │ └── quote-e2e.spec.ts # Testes E2E Playwright
│ └── playwright.config.ts
│
└── README.md # Este arquivo
/admin → lista de flowsproduto,preco,unidade,categoria/q/<slug> (link compartilhavel)/api/generate-quotePOST /api/submissions)QuoteOutput (output parsing Pydantic)O admin faz upload de um CSV com os precos dos produtos. A IA usa SOMENTE esses precos.
produto,preco,unidade,categoria
Borne 16A Level 1,499,unidade,borne
Borne 32A Level 2,699,unidade,borne
Borne 48A Level 2,899,unidade,borne
Controller DCC-9,699,unidade,accessoire
Installation murale exterieure,490,unidade,installation
Installation sur poteau,690,unidade,installation
Cablage par pied,9,pied,cablage
Deplacement,69,unidade,deplacement
Um template CSV pode ser baixado direto no editor de flows.
cd frontend
# Instalar browsers Playwright (primeira vez)
npx playwright install chromium
# Rodar os testes (backend + frontend devem estar rodando)
npx playwright test tests/quote-e2e.spec.ts
O agente usa PydanticAI com output parsing estruturado:
QuoteOutput (Pydantic model com items, subtotal, taxes, total, recommandations)output_validator recalcula matematicamente todos os valoresProvedores suportados: OpenAI (GPT-4o-mini) e Anthropic (Claude Sonnet).
| Problema | Solucao |
|---|---|
| Backend nao inicia | Verificar .env (MongoDB URI, API keys) |
| Quote nao gera | Verificar se OPENAI_API_KEY ou ANTHROPIC_API_KEY esta no .env do backend |
| Frontend nao conecta no backend | Verificar BACKEND_URL=http://localhost:8001 no .env do frontend |
| Erro 422 ao gerar quote | Verificar que todos os answers[].value sao strings (nao numeros) |
__pycache__ impede reload |
Deletar backend/**/__pycache__/ e reiniciar uvicorn |