scientific rag pipeline with hybrid retrieval, multi-style citations, and semantic scholar integration#2
Open
ykd007 wants to merge 1 commit into
Conversation
…and semantic scholar integration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds a full scientific RAG pipeline under a
rag/module. Here's what's in it:rag/pipeline.py— ScientificRAGPipelineThe main class that ties everything together. Accepts uploaded files or Semantic Scholar paper IDs, retrieves the most relevant chunks for a query using hybrid search, then generates a grounded answer via OpenAI. Supports both blocking and streaming (SSE) modes.
rag/hybrid_retriever.py— HybridRetrieverCombines BM25 keyword search (pure Python, no deps) with dense cosine similarity via any embedding function. The
alphaparameter lets you tune how much weight to give each signal. Falls back to BM25 only if embeddings aren't available. Per-user isolation is built in so one user can't see another's private documents.rag/document_manager.py— DocumentManagerHandles PDF (pdfminer or PyPDF2), DOCX (python-docx), TXT, MD, and HTML. Chunks with configurable size and overlap. Deduplicates by content hash so uploading the same file twice is a no-op.
rag/semantic_scholar.py— SemanticScholarClientThin wrapper around the Semantic Scholar Graph API. Supports search, single-paper fetch, references, and citations. Handles 429 rate limiting with automatic backoff. Works without an API key (1 req/s) or faster with one.
rag/citation_manager.py— CitationManagerFormats paper dicts into APA, MLA, Chicago, IEEE, Harvard, Vancouver, or BibTeX strings.
rag/api_routes.py— FastAPI routerMounts under
/rag— endpoints for document upload, document list/delete, paper search, paper ingest, blocking query, and SSE streaming query. Justapp.include_router(rag_router)to wire it in.tests/test_rag.py16 unit tests, all passing. Cover BM25 retrieval, user isolation, vector blending, all 7 citation styles, document deduplication, unsupported file rejection, and mocked S2 API calls.
Closes #45