A Hands-On LangChain Tutorial: Building “Sherlock PDF” for Smarter Scientific Literature Search
From the publisher’s feed, collected in the ZeroLabs newsroom.
Because “I remember reading that somewhere” is not a reproducible research method.SHERLOCK PDFQuery: How does sleep affect hippocampal memory consolidation?Best evidence:0.91 | sleep_memory_study.pdf, page 5 | Relevant excerpt…0.87 | hippocampus_review.pdf, page 12 | Another relevant excerpt…That is what you’ll build in this tutorial: ask a question in plain language, and get back the best-matching passages with the filename and page number. Deerstalker hat optional. 😉What you’ll need: Python 3.10+, an OpenAI API key, a few text-based PDFs, and about 15 minutes. Embedding a few papers costs only cents.The problemScientists consume an extraordinary amount of information: papers, preprints, reviews, supplementary files, protocols, and the occasional 87-page PDF named final_version_REALLY_FINAL_2.4.pdf. Reading it all is hard. The real challenge comes weeks later:Which paper discussed that result? What page was it on? What was the exact paragraph?Our brains are great at forming ideas and less great at storing the coordinates of every useful sentence. Reopening ten papers and trying keyword combinations can take longer than the experiment that raised the question.Why LangChain?An LLM application is more than one prompt. Documents must be loaded, cleaned, split into pieces, converted into numerical representations, searched, and traced back to their sources. LangChain offers reusable components for each step (loaders, splitters, embeddings, vector stores) behind a consistent interface. Less plumbing, more questions.How Sherlock PDF works?Load every PDF from a local docs/ folder.Extract the text page by page.Split it into overlapping chunks, keeping metadata such as filename and page.Convert each chunk into an embedding, a numerical representation of its meaning.Store the embeddings in an in-memory vector store.Embed your question and retrieve the closest passages.A tiny mathematical interlude (no chalkboard panic)How does Sherlock decide that a passage resembles a question? A common measure is cosine similarity, which compares the direction of two vectors rather than their length:Here, A . B is the dot product, ||A|| and ||B|| are the lengths of the vectors, and theta is the angle between them. A score near 1 means that the vectors point in very similar directions; a score near 0 means little directional similarity.Picture two clue cards. One says “muddy footprints, tobacco ash, and a missing laboratory key.” The other says “soil on a boot, cigar residue, and unauthorized access to the lab.” The wording differs, but the clues point in nearly the same direction. A card about formatting a bibliography points somewhere else entirely.That is why semantic search can find a relevant paragraph even when it doesn’t repeat your words. And the page number is my favorite detail: a plausible answer is useful, but a passage you can locate, read in context, and verify is far more useful. Scientific retrieval should lead you back to evidence, not ask you to trust a mysterious oracle in the cloud.Hands-On LangChain TutorialThis tutorial reproduces Sherlock PDF, a small LangChain application that searches scientific PDFs and returns the most relevant excerpts with their filenames and page numbers.1. Get the projectClone or download the repository, then open a terminal inside it:git clone "https://github.com/suvrazastrovision/langchain_demo.git"cd path\to\langchain_demoThe project should look like this:2. Create a virtual environment in PowerShellpython -m venv .venv.\.venv\Scripts\Activate.ps1For macOS or Linux:python3 -m venv .venvsource .venv/bin/activate3. Install the dependenciespython -m pip install -r requirements.txtThe project uses:langchain-community for loading PDFslangchain-core for documents and the vector storelangchain-openai for OpenAI embeddingslangchain-text-splitters for creating searchable chunkspymupdf and pypdf for PDF processingpython-dotenv for loading environment variables4. Add your OpenAI API keyCreate a .env file in the project directory:OPENAI_API_KEY=your_api_key_hereDo not commit this file or share your API key publicly. The repository’s .gitignore already excludes .env.Sherlock PDF is a small demo and therefore I have chosen text-embedding-3-small. Embeddings convert text into numerical vectors so semantically related questions and passages can be found even when they use different wording. OpenAI bills embedding requests according to the number of input tokens. See the official OpenAI embeddings guide.5. Add scientific papersPlace one or more PDFs inside the docs directory:docs/├── nature_emm.pdf├── nn_ad.pdf└── nn_sleep.pdfI have used text-based PDFs for this demo. One can also search images and scanned documents. Scanned documents containing only images will require an OCR step before their text can be searched. Also remember that PDF text is sent to the OpenAI API to create embeddings. Do not process confidential or unpublished documents unless your organisation’s data policy permits it.6. Choose a research questionOpen sherlock_pdf.py and locate the main() function:def main(): query = ( "How do inhibitory glutamatergic synapses between dorsal fan-shaped " "body neurons generate slow-wave activity and regulate sleep pressure " "in Drosophila?")Replace the question with something relevant to the content of your papers or documents. I am using couple of scientific journal articles from Nature here:query = "How does sleep affect hippocampal memory consolidation?"You can also change how many passages are returned:results = hunt(query, store, top_k=3) For example, use top_k=5 to retrieve five excerpts.7. Run Sherlock PDFRun “python sherlock_pdf.py” or alternatively copy the following code:from pathlib import Pathfrom dotenv import load_dotenvfrom langchain_community.document_loaders import DirectoryLoader, PyMuPDFLoaderfrom langchain_core.vectorstores import InMemoryVectorStorefrom langchain_openai import OpenAIEmbeddingsfrom langchain_text_splitters import RecursiveCharacterTextSplitterDOCS_PATH = Path(__file__).resolve().parent / "docs"EMBEDDING_MODEL = "text-embedding-3-small"load_dotenv(Path(__file__).resolve().parent / ".env")def load_notes(folder): """Load PDF research notes as LangChain Document objects.""" loader = DirectoryLoader( str(folder), glob="*.pdf", loader_cls=PyMuPDFLoader, ) return loader.load()def split_notes(documents): """Split loaded notes while preserving document metadata.""" splitter = RecursiveCharacterTextSplitter( chunk_size=220, chunk_overlap=40, separators=["\n\n", "\n", " ", ""], ) return splitter.split_documents(documents)def build_store(chunks): """Build an in-memory LangChain vector store from document chunks.""" embeddings = OpenAIEmbeddings(model=EMBEDDING_MODEL) return InMemoryVectorStore.from_documents(chunks, embeddings)def hunt(query, store, top_k=3): """Return the strongest evidence chunks for a query.""" if not isinstance(top_k, int) or isinstance(top_k, bool) or top_k raise ValueError("top_k must be a positive integer") matches = store.similarity_search_with_score(query, k=top_k) return [ { "score": score, "source": Path(document.metadata["source"]).name, # PyMuPDFLoader stores a zero-based PDF page index. "page": document.metadata["page"] + 1, "text": document.page_content, } for document, score in matches ]def print_evidence(query, results): """Print a compact evidence report.""" print("SHERLOCK PDF") print(f"Query: {query}") print() if not results: print("Best evidence:") print("No evidence found.") return print("Best evidence:") for item in results: print( f"{item['score']:.2f} | {item['source']}, page {item['page']} | " f"{item['text']}" )def main(): query = ( "How do inhibitory glutamatergic synapses between dorsal fan-shaped " "body neurons generate slow-wave activity and regulate sleep pressure " "in Drosophila?") documents = load_notes(DOCS_PATH) chunks = split_notes(documents) store = build_store(chunks) results = hunt(query, store, top_k=3) print_evidence(query, results)if __name__ == "__main__": main()For my querry output looks like this:8. What happens behind the scenes?The script performs five main operations:PyMuPDFLoader extracts text and metadata from every PDF page.RecursiveCharacterTextSplitter divides the pages into small, overlapping chunks.OpenAIEmbeddings converts every chunk into a numerical vector.InMemoryVectorStore stores the vectors and compares them with the question.The best matches are returned with their original filename and page number.Conceptually:PDFs → pages → text chunks → embeddings → similarity search → evidenceThe page metadata survives the chunking process. Because PyMuPDF numbers pages starting from zero, Sherlock PDF adds one before displaying the page number.9. Try a semantic-search experimentAsk the same question using different terminology:query = "Does sleep make hippocampal connections weaker?"A keyword search might miss a paragraph that says “Synaptic downscaling occurs during slow-wave sleep in CA3.” Semantic search can still connect the two, because they discuss related concepts in different words.10. LimitationsThe vector store is rebuilt on every run. For thousands of papers, use a persistent store such as Chroma, FAISS, Pinecone, or PostgreSQL with pgvector. That’s the topic of my next tutorial.Retrieval does not establish scientific truth. Always open the cited page and check the methods, context, and limitations.Where this can goThe same pipeline scales from a few PDFs to thousands of documents once they’re indexed in a persistent vector database. Add filters (author, year, journal, topic) and a simple interface, and an entire lab could share one searchable research memory.Is this better than EndNote or Mendeley?For one job, yes: asking a natural-language question and getting a semantically related passage with filename and page number. But EndNote and Mendeley are mature tools for collecting references, annotating PDFs, collaborating, and building bibliographies, and this demo does none of that.So the real comparison isn’t replacement versus obsolete software. It’s semantic evidence retrieval versus reference management. Watson manages the case files; Sherlock follows the conceptual clues.Final thoughtWe may never memorize every paragraph we read, and perhaps we should stop trying. Sherlock PDF is a retrieval assistant, not a truth machine. It tells you where to start reading; interpretation and verification still belong to you. Even Sherlock needed Watson, and preferably peer review. 😄Elementary, my dear reviewer.Over to you!Thanks for reading! If you try Sherlock PDF on your own papers, I’d love to hear how it goes.Questions or ideas? Leave a comment, and tell me which features you’d like in the next tutorial (persistent database, a UI, filters?).Feedback and constructive criticism are very welcome. I’m still learning, and your suggestions make these tutorials better.Found it useful? A like or a share helps other researchers find it too.Thank you, and happy hunting!A Hands-On LangChain Tutorial: Building “Sherlock PDF” for Smarter Scientific Literature Search was originally published in Towards AI on Medium, where people are continuing the conversation by highlighting and responding to this story.