1
๐ Estrutura de Pastas
meu-projeto-gipm/
โโโ app/
โ โโโ __init__.py
โ โโโ main.py # FastAPI app
โ โโโ config.py # Configuracoes
โ โโโ database.py # SQLAlchemy setup
โ โโโ routers/
โ โ โโโ __init__.py
โ โ โโโ api.py # Endpoints
โ โโโ services/
โ โ โโโ __init__.py
โ โ โโโ llm.py # Integracao LLM
โ โ โโโ pipeline.py # Pipeline GIPM
โ โโโ models/
โ โ โโโ schemas.py # Pydantic models
โ โโโ governance/
โ โโโ personas.py # Definicao de personas
โโโ docs/
โ โโโ METHOD_MAPPING.md
โ โโโ COGNITIVE_GOVERNANCE.md
โโโ tests/
โ โโโ test_pipeline.py
โโโ .env.example
โโโ requirements.txt
โโโ Dockerfile
โโโ docker-compose.yml
2
๐ง Configuracao Inicial
# app/config.py
from pydantic_settings import BaseSettings
class Settings(BaseSettings):
# Projeto
PROJECT_NAME: str = "Meu Projeto GIPM"
VERSION: str = "1.0.0"
# Database
DATABASE_URL: str = "sqlite:///./data/app.db"
# LLM
LLM_PROVIDER: str = "gemini" # ou "openai"
GEMINI_API_KEY: str = ""
OPENAI_API_KEY: str = ""
# Governanca GIPM
MAX_TOKENS_PER_REQUEST: int = 30000
MAX_COST_PER_DAY_USD: float = 10.0
DEFAULT_PERSONA: str = "professional"
# Logging
LOG_LEVEL: str = "INFO"
class Config:
env_file = ".env"
settings = Settings()
3
๐ฆ Dependencias
# requirements.txt
# Framework
fastapi==0.109.0
uvicorn[standard]==0.27.0
# Database
sqlalchemy==2.0.25
aiosqlite==0.19.0
# LLM
google-generativeai==0.3.2
openai==1.10.0
# Validacao
pydantic==2.5.3
pydantic-settings==2.1.0
# Utilitarios
python-dotenv==1.0.0
structlog==24.1.0
# Testes
pytest==7.4.4
pytest-asyncio==0.23.3
httpx==0.26.0
4
๐ณ Docker Setup
# Dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY app/ ./app/
ENV PYTHONUNBUFFERED=1
EXPOSE 8000
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
---
# docker-compose.yml
version: '3.8'
services:
app:
build: .
ports:
- "8000:8000"
env_file:
- .env
volumes:
- ./data:/app/data
5
๐๏ธ Banco de Dados
# app/database.py
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from app.config import settings
engine = create_engine(
settings.DATABASE_URL,
connect_args={"check_same_thread": False}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base()
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
# Criar tabelas
def init_db():
Base.metadata.create_all(bind=engine)
6
๐งช Ambiente de Testes
# tests/conftest.py
import pytest
from fastapi.testclient import TestClient
from app.main import app
@pytest.fixture
def client():
return TestClient(app)
@pytest.fixture
def mock_llm_response():
return {"response": "Mock response", "tokens": 100}
# tests/test_pipeline.py
def test_health_check(client):
response = client.get("/health")
assert response.status_code == 200
assert response.json()["status"] == "healthy"
def test_pipeline_validation(client):
response = client.post("/api/v1/process", json={"input": ""})
assert response.status_code == 422 # Validacao falha
๐ Resumo do Modulo
โEstrutura - Pastas organizadas por funcao
โConfig - Variaveis de ambiente seguras
โDocker - Ambiente reproduzivel
โTestes - Infraestrutura pronta