Mesa de Ayuda IA is structured as a layered, production-style system. A request originates in the browser, travels to the Next.js frontend, crosses to the FastAPI backend over a single HTTP endpoint (Documentation Index
Fetch the complete documentation index at: https://mintlify.com/geremyjampiersalasgarcia-eng/Caso_Practico_Semillero_IA/llms.txt
Use this file to discover all available pages before exploring further.
POST /api/v1/chat), and enters a LangGraph StateGraph orchestrator that classifies intent, routes the query to one or more specialized agents, and consolidates their responses before returning a structured JSON reply. All persistent state lives in two complementary stores: ChromaDB holds the vectorized knowledge bases used for semantic retrieval, while PostgreSQL records conversation history, CRM opportunities, and audit telemetry.
High-level flow
The following diagram shows the complete data path from browser to response:Request lifecycle
Each query passes through the following ten inference steps:- User submits a question (optionally with an attached image) via the Next.js frontend.
- The frontend sends an
HTTP POSTto/api/v1/chatwith the question, an optional Base64-encoded image, and an optional confirmation flag (used by the Action Agent for multi-turn registration flows). - FastAPI validates the request payload (input length, regex pattern check, JSON format) before the message reaches the orchestrator.
- The classifier node — a Gemini LLM call at temperature
0.0for full determinism — reads the question and assigns one of six intent labels:catalogo_precios,politicas_comerciales,proceso_ventas,multimodal,accion_registro, ormixta. - A conditional router in the LangGraph graph reads the intent label and directs execution to one or more agent nodes. If an image was attached, the router overrides the label and always routes to the multimodal agent.
- One or more specialized agents are invoked. For
mixtaqueries, the Catalog, Policies, and Sales Process agents run to cover all knowledge domains. - Each RAG agent queries its ChromaDB collection using cosine-similarity search (top-k = 4 fragments), constructs a prompt from the retrieved context and the user question, and calls Gemini at temperature
0.1to generate a grounded, source-cited response. - The Action Agent uses LangChain Function Calling (
@tooldecorator +bind_tools()) to invokeregistrar_oportunidad_crmwhen all required CRM fields are present and the user has confirmed. - The consolidate node integrates partial responses from all invoked agents into a single, coherent final answer.
- FastAPI returns a JSON response containing the final answer, the list of agents invoked, the source documents referenced, and any warnings (for example, if a discount exceeds the auto-approval threshold).
Security guardrails
The system enforces security at four independent layers, so a breach of one layer does not compromise the others.Layer 1 — FastAPI input validation
The first gate is a FastAPI dependency (validar_input) injected on the /api/v1/chat endpoint. It enforces a 2,000-character length limit on the user question and scans it against a set of regex patterns for known malicious phrases. Requests that exceed the length limit or match a prohibited pattern are rejected with HTTP 400 before they touch any AI component. Requests whose body does not conform to the Pydantic ChatRequest schema are automatically rejected with HTTP 422 by FastAPI’s built-in validation.
Layer 2 — Prompt hardening
Every agent receives an immutable system prompt loaded from a dedicated Markdown file inapp/prompts/. These prompts contain strict role instructions that tell the LLM exactly who it is, what it may and may not do, and that it must ignore any user instruction that tries to change its behavior. Because the system prompt is injected programmatically rather than concatenated from user input, it cannot be overwritten by message content.
Layer 3 — Server-side sandboxing (security.py)
Before a message reaches the LangGraph orchestrator, it passes through app/core/security.py. This module applies regex pattern matching to detect common prompt-injection attack signatures, including:
- Instructions to ignore or forget rules (
ignora instrucciones,olvida instrucciones,ignora reglas,ignora prompt,olvida prompt) - Role-change and impersonation attempts (
actúa como,compórtate como,ahora eres,modo desarrollador) - DAN-style jailbreaks (the literal token
DAN) - Prompt extraction attempts (
repite prompt,revela prompt,dime reglas,cuáles instrucciones,imprime prompt)
False and the service short-circuits the pipeline, returning a fixed corporate response — the malicious message never reaches the LLM.
Layer 4 — Output validation
After the consolidate node produces a final answer, a validation step checks the response for signs of hallucination — for example, a response that invents product names, prices, or policies not present in the retrieved documents. Responses that fail this check are replaced with a conservative fallback that informs the user the requested information was not found in official documents.The four guardrail layers correspond to the Seguridad pillar of the E-O-C-S (Evaluación, Observabilidad, Costos, Seguridad) framework implemented throughout the system.
Data stores
ChromaDB — vector knowledge base
ChromaDB is used exclusively for the RAG pipeline. Three isolated collections are populated by runningpython scripts/ingest.py from the backend/ directory:
| Collection | Source document | Used by |
|---|---|---|
col_catalogo | 01_Catalogo_Productos_Precios.txt | Catalog Agent, Multimodal Agent |
col_politicas | 02_Politicas_Comerciales_Descuentos_Credito.txt | Policies Agent |
col_proceso_ventas | 03_Proceso_Ventas_CRM.txt | Sales Process Agent |
RecursiveCharacterTextSplitter (chunk size 1,000 characters, overlap 200 characters) and embedded using GoogleGenerativeAIEmbeddings with the models/gemini-embedding-001 model. At query time, each agent performs a cosine-similarity search and retrieves the top 4 most relevant fragments (RETRIEVAL_TOP_K=4).
ChromaDB persists its data to the local filesystem at backend/data/chroma_db/. No external vector database service is required.
PostgreSQL — relational persistence
PostgreSQL (running as a Docker container on port5433) stores all non-vector application state. Tables are created automatically by SQLAlchemy on first backend startup:
| Table | Description |
|---|---|
conversations | Chat sessions — IDs and auto-generated titles |
messages | Individual turns — question, response, RAG sources, Base64 image attachments |
oportunidades | CRM opportunity records created by the Action Agent via Function Calling |
audit_logs | Per-request telemetry — detected intent, agents invoked, latency in milliseconds |
SQLite fallback: If the PostgreSQL container is not running when the backend starts, SQLAlchemy automatically falls back to a local SQLite database. This ensures the backend starts and serves requests without manual intervention, at the cost of losing multi-process durability.
Technology decisions
The table below summarizes the key architectural choices and their rationale, drawn directly from the project design documentation:| Component | Choice | Reason |
|---|---|---|
| Language | Python 3.11 | Industry standard for AI/NLP. Robust ecosystem with LangChain and Google AI. |
| Web framework | FastAPI + Uvicorn | Strict typing, automatic Swagger docs, high async throughput. |
| Agent framework | LangChain | Standard framework for agents, tools, and chains. |
| Orchestration | LangGraph (StateGraph) | Models the agent workflow as an explicit directed graph with conditional routing — deterministic, loop-safe, and easy to extend with new nodes. |
| Vector store | ChromaDB (local) | Persistent, fast, no external services required. One collection per agent for knowledge isolation. |
| LLM & Embeddings | Google Gemini via langchain-google-genai | ChatGoogleGenerativeAI for agents, GoogleGenerativeAIEmbeddings for vectors. Native multimodal vision support. |
| Vision | Gemini Vision (multimodal) | Native multimodal capability for product image analysis — no separate vision service needed. |
| Database | PostgreSQL + Docker | Robust conversation history and audit trail with volume-backed persistence. |
| Frontend | Next.js 14 + Tailwind CSS + Shadcn UI | Modern, responsive chat interface with TypeScript type safety. |
| Observability | Arize Phoenix + OpenTelemetry | Full distributed traces across all LangGraph nodes, token consumption, and cost estimation per query. |