RAG — Retrieval-Augmented GenerationpgvectorMulti-agent

How it works

chinnamAI writes articles the way a careful researcher would: it looks things up before it writes, and it only claims what it can cite. Under the hood that’s a pattern called RAG — Retrieval-Augmented Generation. Here’s the whole pipeline, from raw research papers to the cited article you download.

Part 1 — Building the library (done once, offline)

Before anyone asks a question, ~200 AI research papers from arXiv are turned into a searchable library.

1Download the papers

~200 arXiv papers on RAG, agents, transformers, embeddings, prompting, and evaluation — downloaded as PDFs with their titles, authors, and abstracts.

2Cut each paper into focused chunks

A paper is too big to search as one unit — so each is cut into index-card-sized chunks of ~800 tokens, split along the paper’s own section boundaries, with a 150-token overlap between neighbors so no sentence is ever lost on a cut line. One chunk ≈ one idea — that focus is what makes a chunk findable later.

3Figures and tables get a special trick

You can’t search pixels. So every extracted figure is shown to a vision model that writes a text description of it, and every table becomes markdown plus a summary line. The description is what gets searched — the original figure or table is what gets returned. Search the label, deliver the source.

4Turn every chunk into a meaning-fingerprint (embedding)

Each chunk — text, figure description, table description — is converted into an embedding: a list of 1,536 numbers that encodes its meaning. Chunks about similar ideas end up with similar numbers, even when they share no keywords. That’s the property everything else is built on.

5Store everything in a vector database

All chunks + their embeddings land in PostgreSQL with the pgvector extension — three indexes: paper text, figures, tables, all in one shared embedding space, so a single question can search all three at once. Ingestion is incremental: re-running it only embeds what’s new.

Part 2 — Picking & ranking chunks (every question)

When you type a topic, here’s exactly how the right chunks get found.

1Your question becomes a fingerprint too

The search query is embedded with the same model as the library — meaning can now be compared to meaning, as geometry: similar meaning = nearby points.

2Rank by cosine similarity, keep the top k

pgvector computes the cosine similarity between the question’s fingerprint and every chunk’s, then returns the closest matches: top 8 text chunks, top 2 figures, top 2 tables. This is classic nearest-neighbor search — the ranking is the geometry.

3The honesty floor: below 0.30, say so

Top-k has a trap: it always returns k results, relevant or not. So there’s a similarity floor of 0.30 — chunks scoring below it are treated as “no real match.” When the writing agent checks a specific claim and nothing clears the floor, it must drop or soften the claim rather than invent support. Honest “not found” beats confident nonsense.

Part 3 — Agents write from what retrieval returns

Retrieval feeds a small team of AI agents — each with one job, passing shared state.

1Researcher — decides its own searches

The Researcher doesn’t search your topic verbatim — it writes its own focused queries (often several), runs them against all three indexes, and distills the findings into working notes.

2Drafter — verifies claims while writing

The Drafter writes the article — and before stating any specific fact, it calls a verify_claim tool that re-searches the library for support (with that same 0.30 floor). Supported → written with a citation. Unsupported → omitted or generalized. Figures are referenced by ID, so image links can’t be invented.

3Critic — structured verdict, bounded retries

A Critic reviews the draft against the sources and returns a structured accept-or-rewrite verdict. A rewrite carries the critique back to the Drafter — at most 3 times — then the best version ships. A per-run budget cap is checked before every model call.

The result: every article you generate is grounded in real papers, cites what it uses, and admits what it couldn’t verify. The model does the reading and writing — deterministic code does the ranking, the thresholds, the budgets, and the final say.

See it do all of this in about a minute

Generate an article

Watch the live progress panel — research, drafting, critique — as it happens.