Skip to main content

ContextWalker

ContextWalker is a local PDF question-answering system that combines contextual chunking, semantic retrieval, BM25 keyword retrieval, Reciprocal Rank Fusion, neural reranking, and agent-driven exploration of neighboring chunks.

ContextWalker agentic RAG architecture

This repository is a modular reorganization of agent_sample.py. Its retrieval and answering logic is intentionally unchanged.

How it works

PDF extraction
  -> document summary
  -> overlapping chunks
  -> LLM-generated context for every chunk
  -> FAISS vector search + BM25 keyword search
  -> Reciprocal Rank Fusion
  -> cross-encoder reranking
  -> top five starting chunks
  -> agent explores neighboring chunks
  -> supported answer

See the architecture guide for a module-by-module explanation.

Requirements

  • Python 3.9 or newer
  • Ollama running locally at http://localhost:11434
  • The Ollama model gpt-oss:20b
  • A PDF named data.pdf in the directory from which ContextWalker is run

Installation

Install the published package from PyPI:

python -m pip install contextwalker
ollama pull gpt-oss:20b

For local development from a cloned repository:

python -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
python -m pip install -e .
ollama pull gpt-oss:20b

Place the document at data.pdf, start Ollama, and run:

contextwalker

Alternatively:

python -m contextwalker

Type exit, quit, or q to stop the interactive prompt.

Python API

ContextWalker can also be embedded directly in another Python application. Pass the PDF and a dedicated cache directory, build once, and reuse the same indexes for multiple questions:

from contextwalker import ContextWalker

rag = ContextWalker(
    pdf_path="manual.pdf",
    cache_dir="./manual_cache",
)

rag.build()

answer = rag.ask("What does MHL mean?")
print(answer)

answer, results = rag.ask(
    "Which page explains the setup process?",
    return_results=True,
)

for result in results:
    print(result["chunk_id"], result["reranker_score"])

Calling ask() before build() automatically builds the system. For a single-question script, use the convenience function:

from contextwalker import ask_pdf

answer = ask_pdf(
    pdf_path="manual.pdf",
    question="Summarize the installation procedure.",
    cache_dir="./manual_cache",
)

Use a different cache directory for each PDF. ContextWalker deliberately preserves the original cache behavior and does not automatically invalidate a cache when the source PDF changes.

Runtime cache

The first run creates rag_cache/document_summary.txt and rag_cache/contextual_chunks.json. Later runs reuse the contextual chunks but rebuild the in-memory FAISS and BM25 indexes. Delete rag_cache/ when changing the PDF or chunk-generation settings.

Configuration

The original constants are preserved in src/contextwalker/config.py:

  • chunks: 900 characters with 150 characters of overlap
  • vector candidates: 60
  • BM25 candidates: 60
  • fused candidates: 100
  • final reranked candidates: 5
  • neighbor radius: at most 5 chunks
  • agent reasoning steps: at most 25
  • tool calls per question: at most 40

Development

Validate imports and syntax without starting the heavyweight models:

python -m compileall -q src

The original agent_sample.py remains outside this repository and has not been modified.

Release maintainers should follow the publishing guide for the PyPI release process.

Metadata

Release files for contextwalker 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for contextwalker 0.2.0
File Size Uploaded
contextwalker-0.2.0.tar.gz 17.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for contextwalker 0.2.0
File Interpreter ABI Platform
contextwalker-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 38.9 kB

Release files / contextwalker-0.2.0.tar.gz

Download URL contextwalker-0.2.0.tar.gz
Size 17.7 kB
Tags Source
SHA-256 checksum
How to use checksums
7309ea66d0a3586322d3c12347e0c3e2f83636c546d1f20a312df4a145f0f187
BLAKE2b-256 checksum
How to use checksums
70717d7c1ef6cea6f23d2b46d05c5b250495a918f350295ceb5b97323ceaa74d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.

Transparency log

Release files / contextwalker-0.2.0-py3-none-any.whl

Download URL contextwalker-0.2.0-py3-none-any.whl
Size 21.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
062048305b15cd7e4d2f6a04d541b511f5a77d7864ae8181c8af2234faaea153
BLAKE2b-256 checksum
How to use checksums
ea2d4d8cd0c47153fbf66cc42720fb0e81d4eb1a604e01268781b64ee9040ae4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 29, 2026.

Transparency log

Release history Release notifications | RSS feed

0.2.1

2 release files

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page