Files
espiritismo-rag-system/.superpowers/sdd/2025-11-01-espiritismo-rag-system/task-4-brief.md
T
lgcosta b9c0c66d21 feat: implementar sistema RAG Espiritismo completo
- Stack: GPT4All + Llama 3.2 3B, LangChain, Qdrant, FastAPI, Docker
- 11 tarefas implementadas com 100% de conclusão
- Funcionalidades: chat com IA, upload PDF, bot Telegram, agendamento
- 6 livros PDF indexados (A Gênese, Livro dos Espíritos, etc.)
- Documentação rica: README, BUILD, task reports, design specs
- Deploy com Docker Compose
- Production ready

Tech stack:
- AI: GPT4All 2.4.6, Llama 3.2 3B q4_0
- Backend: FastAPI 0.115.0, Celery 5.4.0, Redis 5.2.0
- RAG: LangChain 0.3.7, Qdrant 1.11.1
- Frontend: HTML5/CSS3/Vanilla JS
- Deploy: Docker Compose

Acceptance Criteria:
✅ CPU-only, <8GB RAM, ~5GB storage
✅ Suporta upload de PDFs
✅ Interface web rica e responsiva
✅ Bot Telegram funcional
✅ Agendamento de mensagens
✅ Código limpo e documentado
✅ Deploy via Docker

Status: PRODUCTION READY
Version: 1.0.0
Date: 2026-11-01
2026-11-01 10:56:17 -03:00

6.0 KiB

message: str
response: str

class UploadResponse(BaseModel): message: str filename: str

class ScheduleInfo(BaseModel): id: int time: str book: Optional[str] enabled: bool next_run: Optional[str]

class ScheduleRequest(BaseModel): time: str book: Optional[str] = None enabled: bool = True


- [ ] **Passo 2: Criar main.py**

```python
from fastapi import FastAPI, HTTPException
from fastapi.responses import HTMLResponse
from app.services.rag_service import QdrantService, RAGService
from app.models import ChatRequest, ChatResponse, BookInfo, ChatRequestResponse, UploadResponse, ScheduleInfo, ScheduleRequest
import os
from app.utils.schedule_manager import ScheduleManager

app = FastAPI(title="Sistema RAG Espiritismo")

# Initialize services
qdrant_service = QdrantService(os.getenv("QDRANT_URL", "http://qdrant:6333"))
rag_service = RAGService(qdrant_service, os.getenv("GPT4ALL_MODEL", "llama-3.2-3b-instruct-q4_0.gguf"))
schedule_manager = ScheduleManager("/app/schedules.json")

@app.get("/", response_class=HTMLResponse)
async def index():
    """Serve the frontend interface."""
    with open("app/templates/index.html", "r") as f:
        return f.read()

@app.post("/api/chat", response_model=ChatResponse)
async def chat(request: ChatRequest):
    """Chat endpoint - Query the RAG system."""
    try:
        result = rag_service.query(request.message, request.top_k)
        return ChatResponse(**result)
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/api/books", response_model=List[BookInfo])
async def get_books():
    """Get list of indexed books."""
    try:
        books = qdrant_service.get_books()
        return [BookInfo(title=book, source=f"Qdrant collection") for book in books]
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.get("/api/schedules", response_model=List[ScheduleInfo])
async def get_schedules():
    """Get list of schedules."""
    try:
        schedules = schedule_manager.get_all()
        return schedules
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/api/schedules", response_model=ScheduleInfo)
async def create_schedule(request: ScheduleRequest):
    """Create a new schedule."""
    try:
        schedule = schedule_manager.create(
            time=request.time,
            book=request.book,
            enabled=request.enabled
        )
        return schedule
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.delete("/api/schedules/{schedule_id}")
async def delete_schedule(schedule_id: int):
    """Delete a schedule."""
    try:
        schedule_manager.delete(schedule_id)
        return {"message": "Schedule deleted"}
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@app.post("/api/upload")
async def upload_pdf(file):
    """Upload a new PDF file."""
    try:
        from app.services.pdf_processor import PDFProcessor
        from app.utils.embeddings import EmbeddingService

        file_path = f"/app/uploads/{file.filename}"

        with open(file_path, "wb") as buffer:
            buffer.write(await file.read())

        # Process the PDF
        embedding_service = EmbeddingService()
        pdf_processor = PDFProcessor(embedding_service)

        book_metadata = {
            "title": file.filename.replace(".pdf", "").replace("-", " ").replace("_", " "),
            "author": "Uploaded",
            "type": "book",
        }

        chunks = pdf_processor.load_and_split(file_path, book_metadata)
        chunks_with_embeddings = pdf_processor.embed_chunks(chunks)

        # Add to Qdrant
        qdrant_service.add_chunks(chunks_with_embeddings)

        # Save chunks to JSON
        chunks_file = f"/app/index/{file.filename}.json"
        os.makedirs("/app/index", exist_ok=True)

        with open(chunks_file, 'w') as f:
            json.dump(chunks_with_embeddings, f, indent=2)

        return UploadResponse(
            message="PDF uploaded and indexed successfully",
            filename=file.filename
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

Tarefa 5: Frontend Interface

Arquivos:

  • Criar: backend/app/templates/index.html
  • Criar: frontend/static/css/style.css
  • Criar: frontend/static/js/script.js

Interfaces:

  • Consumes: API web endpoints

  • Produces: Interface web funcional para chat, upload e configuração

  • Passo 1: Criar style.css

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    padding: 20px;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
}

header {
    background: white;
    padding: 20px;
    border-radius: 10px;
    margin-bottom: 20px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

header h1 {
    color: #667eea;
    font-size: 2rem;
}

header p {
    color: #666;
    margin-top: 5px;
}

.grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(350px, 1fr));
    gap: 20px;
}

.card {
    background: white;
    padding: 20px;
    border-radius: 10px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.card h2 {
    color: #667eea;
    margin-bottom: 15px;
    font-size: 1.25rem;
}

.chat-container {
    height: 500px;
    display: flex;
    flex-direction: column;
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 15px;
    background: #f5f5f5;
    border-radius: 5px;
    margin-bottom: 15px;
}

.message {
    padding: 10px;
    margin-bottom: 10px;
    border-radius: 5px;
}

.message.user {
    background: #667eea;
    color: white;
}

.message.bot {
    background: #e3f2fd;
}

.message-content {
    line-height: 1.6;
}

.message-source {
    font-size: 0.8rem;
    color: #666;
    margin-top: 5px;
    font-style: italic;
}

input[type="text"],
input[type="datetime-local"],
input[type="number"] {
    width: 100%;