MODULO 5.3

โš™๏ธ Setup do Projeto

Estrutura de pastas, configuracao e ambiente de desenvolvimento.

6
Topicos
~40
Minutos
Pratico
Nivel
Hands-on
Tipo
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