Agentic RAG System

A document question-answering pipeline built from scratch in Python — raw file in, grounded cited answer out, with charts when the data supports one.

python openai flask rag graph agents embeddings fastapi

Most RAG demos do three things: embed some text, run a similarity search, and paste the top chunks into a prompt. That works fine until the answer requires reasoning across multiple documents, the relevant content is spread across different source types, or the question is quantitative and a chart would actually be more useful than prose.

This system adds a graph reasoning layer between retrieval and the LLM. Retrieved passages become typed nodes — facts, data points, definitions, procedures — connected by weighted edges based on keyword overlap, source proximity, and relevance score. A union-find algorithm clusters them into topic threads, which become the labeled sections in the final prompt. The model only sees its cluster's context and is explicitly blocked from going off-document.

The result is an answer that's traceable to source, structured around the actual topic shape of the documents, and accompanied by a chart whenever the retrieved data supports one.

Grounding as a hard constraint. The prompt explicitly tells the model to use only the retrieved context and to say so when it can't answer. This isn't a guardrail bolted on after the fact — it's the core discipline the whole pipeline is built around.

Graph over straight retrieval. A flat top-k retrieval collapses everything into one blob. The graph layer preserves the topic structure of the source documents, so the LLM gets organized sections rather than a random pile of passages.

One API key, two stages. The same OpenAI key covers embedding and completion, but they're kept separate in code so swapping providers for either stage stays a small, isolated change.

Zero heavy dependencies where possible. The cosine similarity math is written by hand. The knowledge base is JSON. The server is plain Flask. The goal was a system you can read, understand, and extend without a library archaeology expedition.

Coming soon
Next project in progress.
Coming soon
Next project in progress.