SAAO arXiv RAG: Hybrid-Search Literature Assistant

Overview

Astrophysics literature grows faster than any researcher can read it, and keyword search over abstracts only gets you so far. This project is a retrieval-augmented generation system built for the South African Astronomical Observatory community: it ingests arXiv papers, parses the full PDFs, indexes them for both keyword and semantic search, and answers natural-language questions with citations back to the source papers.

It is a complete service rather than a notebook demo — a monthly ingestion pipeline orchestrated with Airflow, a FastAPI backend, a Gradio interface, and a local model stack (Ollama, OpenSearch, PostgreSQL, Redis, MinIO, Langfuse) running through Docker Compose. The design goal throughout was citation-grounded answers: the model is instructed to answer only from retrieved excerpts and to say when the excerpts are insufficient.

The Ingestion Pipeline

The saao_monthly_ingestion DAG runs on a monthly schedule and chains six tasks: environment setup, fetching the papers listed in airflow/papers.txt through the arXiv API, parsing each PDF with Docling for structured full text, chunking (default 600 words with 100-word overlap), embedding, and indexing into OpenSearch. Verification and a monthly summary report follow, then a cleanup task prunes temporary PDFs. PDFs themselves are stored in MinIO and paper metadata lives in PostgreSQL, so the search index can always be rebuilt from first principles.

Chunks are indexed with deterministic document IDs (arxiv_id + chunk index) and upserted, which makes re-ingestion idempotent: re-running the pipeline updates existing chunks instead of duplicating them. Embeddings come from a local Ollama embedding model (768-dimensional vectors, nomic-embed-text by default).

Hybrid Retrieval

Keyword search alone misses paraphrases; vector search alone misses exact terminology and identifiers, which matter a lot in scientific text. The index therefore supports both in a single OpenSearch index: BM25 over analysed text and approximate nearest-neighbour search over the embeddings using HNSW (cosine similarity, m=16, ef_construction=512). Results are fused with Reciprocal Rank Fusion (k=60), which combines the two rankings without hand-tuned weights.

The API exposes hybrid search as a toggle. If embedding generation fails at query time, the request degrades gracefully to pure BM25 instead of erroring — a small piece of operational maturity that matters when the embedding service is a local container. Category filters (e.g. astro-ph.GA, astro-ph.SR) and a configurable top_k are supported on both the API and the UI.

Answer Generation

Retrieved chunks are assembled into a prompt for a local model served by Ollama (llama3.2:1b by default). The system prompt constrains the model to the supplied excerpts, requires citations by paper title or arXiv ID, and tells it to acknowledge uncertainty when the excerpts are incomplete — the classic failure mode of RAG systems is a model that answers confidently from its own priors.

Responses return the answer together with the source papers (direct PDF links), the number of chunks used, and the search mode, so users can check the evidence themselves. The endpoint streams tokens for long answers, and exact-match query results are cached in Redis. Every request is traced end to end in Langfuse — separate spans cover embedding, retrieval, prompt construction, and generation — which makes it possible to see whether a bad answer came from retrieval or from the model.

Engineering

The whole stack runs locally with Docker Compose: FastAPI, the Gradio UI, Airflow, OpenSearch and Dashboards, PostgreSQL, Redis, MinIO, Ollama, and the Langfuse/ClickHouse observability pair. Makefile profiles (up-core, up-api, up-pipeline, ingest, rag-app) let you bring up only the services a task needs instead of the full eleven-container system. The project uses Python 3.12 with uv, ruff for formatting and linting, mypy for type checking, and a pytest suite covering the API routers (ask, hybrid search, health), the arXiv client, metadata fetcher, OpenSearch query builder, PDF parser, and response schemas.

What I’d Do Next

The biggest gap is evaluation. The system has tests for its components, but no retrieval-quality benchmark: there is no curated question set with gold passages, so recall@k, citation precision, and answer faithfulness are not measured. Building that harness is the first priority — it converts every later change (reranking, chunking, model choice) from a guess into a measurement.

After that, the obvious levers are a cross-encoder reranker on top of the fused results, query rewriting for vague or multi-part questions, chunking experiments (section-aware rather than fixed-size), and a larger local model than the 1B parameter default for answer synthesis. Wiring linting, type checks, and tests into CI, and publishing the benchmark alongside the repo, would make the project easy for others to reproduce and extend.

Links