Skip to content

maxogod/student-rag-api

Repository files navigation

Student RAG API

A FastAPI RAG pipeline that ingests PDF files, makes embeddings and stores them in a Vector Store (Postgres + pgvector database), then when a query is made performs a similarity search and builds the context to pass to the LLM to generate accurate bibliography-backed answers to the questions using langchain.

Usage

  • POST /data: Ingest PDF documents into the system.
  • POST /query: Send a query to the system and receive a response based on the ingested documents.

Run

Build and run the application using Docker Compose:

docker compose up --build # or build and then run separately

# Remove containers and volumes later
docker compose down -v

Examples

curl -X POST localhost:8000/data -F "file=@./data/Distribuidos_Clase_07_MOM_Distribuido_ZeroMQ.pdf"

# Response
{
    "chunks_ingested":11
}
curl -X POST -H "Content-Type: application/json" -d '{ "query": "para que sirven los grupos de comunicacion?" }' localhost:8000/query

# Response
{
    "query": "para que sirven los grupos de comunicacion?",
    "answer": "Para ese query, te puedo decir que según el contexto proporcionado, los grupos de comunicación (o patrones de mensajería) en ZeroMQ sirven para:\n\n*   Comunicar tareas entre productores y consumidores (patrón Producer-Consumer o Push-Pull)\n*   Implementar patrones de mensajería como:\n    *   Request-Reply\n    *   Publisher-Subscriber\n    *   Parallel Pipeline\n    *   Patrones avanzados\n\nEstos patrones permiten la comunicación entre múltiples productores y consumidores, garantizando la entrega justa de mensajes (fairness) y utilizando sockets específicos para cada rol (PUSH/PULL, ROUTER/DEALER, etc.). \n\nZeroMQ es una herramienta útil para crear brokerless middlewares y es altamente performante."
}

example

Architecture

Data ingestion step

+--------+     +----------+     +-----------+     +--------+
|  PDFs  | --> |   Data   | --> | Embedding | --> | Vector |
|        |     | Chunking |     |   Model   |     |   DB   |
+--------+     +----------+     +-----------+     +--------+
                     |                |
                     | x chunk_size   |
                     +----------------+

Retrieval step

+---------+     +-----------+     +--------+
|  Query  | --> | Embedding | --> | Vector |
|         |     |   Model   |     |   DB   |
+---------+     +-----------+     +--------+
                                      ^
                                      |
                               similarity search

Augmented Generation step

+-----------+     +-----------+     +-------+
| Retrieval | --> |  Context  | --> |  LLM  | (llama-4-scout)
|   Step    |     | Documents |     +-------+
+-----------+     +-----------+         ^
                                        |
                                    +-------+
                                    | Query |
                                    +-------+

Package by Layer Structure

This project follows a layered architecture to separate responsibilities clearly:

api/

Handles the HTTP layer (built with FastAPI).

  • controllers/: Define the /data and /query endpoints.
  • router.py: Registers routes.
  • dependencies.py: Dependency injection configuration.

schemas/

Defines request and response DTOs (validation using Pydantic).

services/

Contains the business logic:

  • PDF ingestion and text chunking
  • Embedding generation
  • Vector search
  • LLM interaction
  • Query orchestration (RAG flow)

repositories/

Implements persistence and similarity search using PostgreSQL + pgvector.

models/

Domain models representing core entities (e.g., documents).

About

A FastAPI RAG pipeline that ingests PDF files, makes embeddings and stores them in a Vector Store (Postgres + pgvector database), then when a query is made performs a similarity search and builds the context to pass to the LLM to generate accurate bibliography-backed answers to the questions using langchain.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors