DocuStore.io
Architecture

Chat Pipeline

Agentic RAG chat system with three operating modes, grounding verification, and SSE streaming.

The DocuStore chat system is an agentic RAG (Retrieval-Augmented Generation) pipeline that answers questions grounded in the user's document corpus. It supports three operating modes, inline citation verification, and real-time token streaming via Server-Sent Events.

Operating Modes

ModePipelineBest For
Quick5-step single-shot retrievalFast factual lookups
Thinking6-stage agentic retrieval with LLM tool-calling loopComplex multi-faceted questions
Deep ThinkingThinking + page image analysisQuestions about figures and visual content

The mode is selected per-message via the mode field. When omitted, the server default (CHAT_DEFAULT_MODE, default thinking) is used.

Thinking Mode Pipeline (6 Stages)

Stage 1: Query Planning

Three analysis tracks run in parallel:

  • structflo-ner -- Extracts compound names, targets, and gene names from the query.
  • GLiNER2 -- Extracts author names for tag-based filtering.
  • LLM Planning -- Classifies query type (factual, comparative, exploratory, compound, follow-up), generates sub-queries, and selects a retrieval strategy.

NER entities are accumulated across conversation turns. Follow-up questions inherit entities from prior grounded turns, enabling multi-turn topic continuity.

Stage 2: Agentic Retrieval

An LLM tool-calling loop iteratively searches the document corpus:

  1. Seed -- Carry forward citations from the previous turn; detect [N] references in the question.
  2. Auto-seed -- Vector search with NER-derived filters, plus an unfiltered search for broad recall.
  3. Bioactivity pre-fetch -- Deterministic MongoDB lookup for compound bioactivity data.
  4. LLM loop -- Up to 5 iterations where the LLM evaluates accumulated results and decides whether to call additional tools or finish.

Available tools in the retrieval loop:

ToolDescription
search_documentsChunk + summary search via Qdrant
search_summariesSummary-only search
get_page_contentFull page text from MongoDB
search_structured_bioactivityDirect MongoDB bioactivity lookup
finish_retrievalSignal that retrieval is complete

The RetrievalAccumulator handles cross-iteration deduplication, budget tracking (max chars), and query deduplication to prevent repeated searches.

Stage 3: Context Assembly

Retrieved results are deduplicated (chunks take priority over summaries for the same page), tiered by relevance, and formatted with [N] citation indices:

TierCriteriaBudget
HIGHRerank > 0.7 or similarity > 0.85Full text
MEDIUMRerank > 0.4 or similarity > 0.61000 chars
LOWBelow thresholds200 chars

Stage 4: Adaptive Synthesis

A system prompt is selected based on query type (factual, comparative, exploratory, compound, follow-up). The LLM generates a draft answer with [N] citations. Tokens are not streamed to the client at this stage.

Stage 5: Inline Verification

An algorithmic citation coverage check runs first. If coverage is low and the query is factual, a selective LLM verification follows. If the answer is not grounded:

  1. Strategy A (Broaden) -- Re-run retrieval with unfiltered search enabled.
  2. Strategy B (Augment) -- Add unsupported claims to the query and re-run.

Stage 6: Answer Formatting

The draft is reformatted for coherence. This is where tokens stream to the client as SSE events. Citation indices are re-extracted to produce the final used_citations list.

SSE Event Schema

All modes emit the same event types:

SSE EventContent
agent_stepStep progress with optional thinking_content
retrieval_resultsCitation sources for the frontend sidebar
query_contextExtracted entities, authors, query type
tokenStreaming answer delta (only during formatting)
structured_blockRich content blocks (tables, molecules)
grounding_resultis_grounded boolean + confidence score
doneFinal metadata: message_id, token counts, duration, sources

Follow-up Handling

Multi-turn conversations use several continuity mechanisms:

  1. History window -- Last 10 messages loaded from MongoDB, compressed to a 4000-character budget.
  2. NER accumulation -- Entities from prior grounded turns are inherited or merged into the current query.
  3. Citation continuity -- Previous turn's citations are seeded into the retrieval accumulator at score 0.5.
  4. Explicit references -- [N] references in the question trigger a full page content fetch.

Configuration

VariableDefaultDescription
CHAT_DEFAULT_MODEthinkingDefault pipeline mode
CHAT_MAX_RETRIES1Grounding verification retries
CHAT_AGENT_MAX_ITERATIONS5Max tool-calling iterations
CHAT_AGENT_ITERATION_TIMEOUT_S30Per-iteration timeout (seconds)
CHAT_AGENT_TOTAL_TIMEOUT_S120Total retrieval loop timeout
CHAT_CONTEXT_BUDGET_CHARS12000Max chars for assembled context
CHAT_DEEP_THINKING_MAX_IMAGES5Max page images in deep thinking
CHAT_FOLLOW_UP_CONTEXT_BUDGET4000Char budget for conversation history
CHAT_LLM_PROVIDERFalls back to LLM_PROVIDERLLM provider for chat (can differ from batch)
CHAT_LLM_MODEL_NAMEFalls back to LLM_MODEL_NAMEModel for chat responses
CHAT_LLM_TEMPERATURE0.3Higher temperature for conversational style