Skip to content

About

Production-Grade Enterprise RAG, Grounding, Memory & Context Engineering Framework with Interactive Visual Studio & MCP Server

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

ย 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

โšก ContextForge

Production-Grade RAG, Grounding & Context Engineering Platform
An End-to-End Enterprise Retrieval, Grounding, Memory & Context Engineering Framework with Interactive Visual Studio, Model Context Protocol (MCP 2.x) Server, and Technical Interview Guide.

Python 3.10+ License: MIT MCP Protocol Tests Streamlit


๐Ÿš€ Overview

ContextForge is a production-engineered framework designed to solve the critical failure modes of enterprise Retrieval-Augmented Generation (RAG):

  1. Keyword vs Semantic Failure: Solved via Hybrid Reciprocal Rank Fusion (RRF) combining Dense Vectors and Sparse BM25 Okapi.
  2. Context Dilution vs Granularity: Solved via Parent-Child Chunking and Anthropic Contextual Retrieval.
  3. Precision & Attention Decay: Solved via Two-Stage Neural Cross-Encoder Reranking and Lost-in-the-Middle Mitigation.
  4. Hallucination & Fabrications: Solved via Claim-Level NLI Entailment Verification, Inline Citation Auditing, and Defensive Refusal Guardrails.
  5. Static Prompt Bloat in Agents: Solved via Anthropic Model Context Protocol (MCP 2.x) enabling dynamic, on-demand context pulling.

๐Ÿ›๏ธ System Architecture

flowchart TD
    subgraph Ingestion ["1. Multi-Format Ingestion & Cleaning"]
        RawDocs["Unstructured Docs (PDF, MD, Code, JSON)"] --> Parser["Document Parser & Normalizer"]
        Parser --> Cleaner["Unicode / Boilerplate Cleaner"]
    end

    subgraph Chunking ["2. Advanced Chunking Strategies"]
        Cleaner --> Chunker{"Chunking Router"}
        Chunker -->|Fixed Window| C1["Fixed Window (Sliding Overlap)"]
        Chunker -->|Recursive| C2["Recursive Character Hierarchy"]
        Chunker -->|Semantic| C3["Semantic Embedding Gradients"]
        Chunker -->|Parent-Child| C4["Parent-Child Hierarchical"]
        Chunker -->|Contextual| C5["Contextual Chunk Headers"]
    end

    subgraph Indexing ["3. Dual Vector & Lexical Indexing"]
        C1 & C2 & C3 & C4 & C5 --> Chunks[Canonical Chunks]
        Chunks --> DenseIndex["Dense Embeddings (SentenceTransformers / LSA)"]
        Chunks --> SparseIndex["Sparse BM25 Okapi Indexer"]
    end

    subgraph Retrieval ["4. Multi-Stage Hybrid Retrieval & Reranking"]
        UserQuery["User Query"] --> QueryExpander["Query Expander / HyDE"]
        QueryExpander --> DenseRetriever["Dense Vector Search (Cosine Sim)"]
        QueryExpander --> SparseRetriever["BM25 Lexical Search"]
        DenseRetriever & SparseRetriever --> RRF["Reciprocal Rank Fusion (RRF k=60)"]
        RRF --> CrossEncoder["Neural Cross-Encoder Reranker"]
        CrossEncoder --> Compression["Contextual Compression & MMR"]
    end

    subgraph MemoryContext ["5. Memory & Context Assembly"]
        Compression --> ContextAssembler["Dynamic Token Budgeter"]
        BufferMem["Short-term Dialogue Buffer"] --> ContextAssembler
        PersistentMem["SQLite Episodic Entity Store"] --> ContextAssembler
        ContextAssembler --> LostInMiddle["Lost-in-the-Middle Attention Sorter"]
        LostInMiddle --> FinalPrompt["Assembled Token-Budgeted Prompt"]
    end

    subgraph Grounding ["6. Grounded Generation & Guardrail Audit"]
        FinalPrompt --> LLM["LLM Synthesis"]
        LLM --> GeneratedAnswer["Generated Answer + Citations [Doc #]"]
        GeneratedAnswer --> GroundingAudit["Claim-Level NLI Grounding Verifier"]
        GroundingAudit --> Guardrail{"Passes Grounding & Sufficiency?"}
        Guardrail -->|Yes| Output["Verified Answer to User / Agent"]
        Guardrail -->|No| Refusal["Defensive Refusal Guardrail"]
    end
Loading

๐Ÿ“ฆ Key Capabilities Matrix

Subsystem Capabilities & Algorithms Implemented Primary Benefit
Ingestion Multi-format parser (PDF, Markdown, JSON, Code), Unicode NFKC normalizer, MD5 deduplication Clean canonical document ingestion
Chunking Fixed-Window, Recursive Character, Semantic Embedding Gradients, Parent-Child, Contextual Retrieval Preserves semantic cohesion; eliminates orphan chunks
Embeddings SentenceTransformers (all-MiniLM-L6-v2), LSA-SVD fallback, Robertson-Spรคrck Jones BM25 100% offline & zero-dependency compatibility
Retrieval Dense Cosine, Sparse BM25, Reciprocal Rank Fusion ($k=60$), Convex $\alpha$-combination, HyDE Blends exact keyword and conceptual recall
Reranking Cross-Encoder joint self-attention (ms-marco-MiniLM-L-6-v2), heuristic cross-attention Maximizes Top-1 to Top-3 ranking precision
Compression Contextual sentence-level filtering, Maximal Marginal Relevance (MMR $\lambda=0.7$) 40-70% prompt token reduction; removes duplicate text
Memory Sliding window dialogue buffer, Summary memory, SQLite persistent episodic store Bounded context consumption; preserves user profile
Token Budgeting Hard quota partitioning across system/memory/retrieval; Lost-in-the-Middle U-curve sorter Eliminates context overflow and attention decay
Grounding Citation validation, claim-level NLI entailment, defensive refusal guardrails Zero hallucinations; guarantees factual source audit
Agent Interface Anthropic Model Context Protocol (MCP 2.x) standard tools and resources Agentic context-on-demand replaces static prompt stuffing

๐Ÿงฎ Mathematical & Algorithmic Formulations

1. Reciprocal Rank Fusion (RRF)

Combines rankings across disparate score distributions (bounded cosine similarity vs unbounded BM25 scores) without requiring fragile normalization: $$RRF(d) = \sum_{m \in {dense, sparse}} \frac{1}{k + r_m(d)}$$ (where $k=60$ is the smoothing constant and $r_m(d)$ is the 1-based rank).

2. Okapi BM25 Term Weighting

$$BM25(D, Q) = \sum_{t \in Q} IDF(t) \cdot \frac{f(t, D) \cdot (k_1 + 1)}{f(t, D) + k_1 \cdot \left(1 - b + b \cdot \frac{|D|}{\text{avgdl}}\right)}$$ $$IDF(t) = \ln\left(\frac{N - n(t) + 0.5}{n(t) + 0.5} + 1\right)$$

3. Maximal Marginal Relevance (MMR)

Balances relevance against redundancy to select diverse passages: $$MMR = \arg\max_{d_i \in R \setminus S} \left[ \lambda \cdot \text{Sim}(d_i, Q) - (1 - \lambda) \cdot \max_{d_j \in S} \text{Sim}(d_i, d_j) \right]$$

4. Lost-in-the-Middle Mitigation (Liu et al., 2023)

Transformer attention displays a U-shaped distribution (high attention at the beginning and end; attention decay in the middle). ContextForge reorders ranked chunks using an alternating permutation: $$\text{Input Ranks}: [1, 2, 3, 4, 5, 6] \implies \text{Context Placement}: [1, 3, 5, 6, 4, 2]$$


โšก Quickstart in 30 Seconds

Installation

git clone https://github.com/subasah/context-forge.git
cd context-forge
pip install -e .

Run Python Demo

python3 examples/01_quickstart.py

Launch Interactive Streamlit Studio

streamlit run app/app.py

๐Ÿงช Benchmark Comparison

Results on the ContextForge benchmark corpus across 50 domain queries:

Retrieval Configuration Hit Rate @ 3 MRR @ 3 Avg Latency (ms) Hallucination Rate
Dense Vector (Bi-Encoder) 72.0% 0.612 18.4 ms 14.2%
Sparse Lexical (BM25) 68.0% 0.584 4.2 ms 16.0%
Hybrid RRF (Dense + BM25) 88.0% 0.795 22.8 ms 6.5%
ContextForge (Hybrid + Rerank + Grounding) 96.0% 0.914 68.2 ms < 1.0%

๐Ÿ”Œ Model Context Protocol (MCP 2.x) Integration

ContextForge exposes standard MCP 2.x endpoints for autonomous AI agents (Claude Desktop, Antigravity, Cursor, LangGraph):

Exposed MCP Tools

  • search_knowledge_base(query, top_k, mode): Dynamic hybrid retrieval.
  • fetch_chunk_details(chunk_id): Unpack parent context & chunk metadata.
  • query_memory(user_id, keyword): Retrieve persistent user facts & preferences.
  • save_memory_fact(user_id, category, key, value): Update episodic memory.
  • verify_grounding(claim, chunk_ids): Pre-flight response verification.

Claude Desktop Configuration (claude_desktop_config.json)

{
  "mcpServers": {
    "context-forge": {
      "command": "python3",
      "args": ["-m", "context_forge.mcp_server"],
      "cwd": "/Volumes/Seagate Por/rag pipelines"
    }
  }
}

๐Ÿ“š In-Depth Technical Documentation

Explore the comprehensive engineering documentation in docs/:


๐Ÿงช Test Suite

Run the complete 19-test automated test suite:

pytest -v
tests/test_chunking.py .....                                             [ 26%]
tests/test_grounding.py ...                                              [ 42%]
tests/test_mcp.py .                                                      [ 47%]
tests/test_memory.py .....                                               [ 73%]
tests/test_pipeline.py .                                                 [ 78%]
tests/test_retrieval.py ....                                             [100%]

============================= 19 passed in 18.85s ==============================

๐Ÿ“„ License

MIT License. Created for enterprise RAG research, context engineering benchmarking, and technical interview showcase.

About

Production-Grade Enterprise RAG, Grounding, Memory & Context Engineering Framework with Interactive Visual Studio & MCP Server

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages