Learn AI/Projects
LESSON 34 / 36Applied 75 min with practice

Project: search your own study notes

Build a local passage retriever with ranking, source IDs, and a no-match state.

WHAT YOU WILL LEARN
  • Index a small original corpus
  • Rank passages with TF–IDF
  • Test relevant and unsupported questions

Own the retrieval path

A useful note-search tool needs a collection, a stable identifier for each passage, a representation, and a ranking function. This project keeps everything in memory and uses original synthetic notes so it can run immediately.

The query and documents share one vocabulary and weighting scheme. A zero query vector means the index recognizes none of the terms. Return no match instead of presenting a random document as relevant.

Build toward grounded assistance

The output includes the actual passage and its ID. That makes the evidence inspectable and provides a foundation for a later RAG application. This project deliberately does not generate an answer: it retrieves text.

Create a relevance dataset with ordinary questions, synonyms, misleading keyword overlap, and questions outside the collection. Evaluate top-k recall separately from answer correctness. Before indexing private documents, add access filtering before ranking and preserve those permissions in every cache.

PUT THE IDEA INTO CODE

A small experiment you can run.

A positive overlap score is only a basic no-match rule. It is not a validated confidence threshold or proof that the passage answers every part of the question.

project-search-your-notes.py
import math, re
from collections import Counter
notes = {
    "python": "A Python function accepts inputs and returns a result.",
    "evaluation": "Keep test data separate when selecting a model.",
    "neuron": "A neuron applies weights and a bias before an activation.",
    "privacy": "Collect only the data required for a stated purpose.",
}
def tokens(text):
    return re.findall(r"[a-z]+", text.lower())
corpus = {key: tokens(text) for key, text in notes.items()}
vocabulary = sorted(set(word for words in corpus.values() for word in words))
idf = {w: math.log((1+len(notes))/(1+sum(w in d for d in corpus.values())))+1 for w in vocabulary}
def vector(text):
    counts = Counter(tokens(text))
    return [counts[w]*idf[w] for w in vocabulary]
def cosine(a, b):
    denominator = math.sqrt(sum(x*x for x in a)*sum(x*x for x in b))
    return sum(x*y for x, y in zip(a, b))/denominator if denominator else 0.
index = {key: vector(text) for key, text in notes.items()}
def search(question, limit=2):
    query = vector(question)
    ranked = sorted(((cosine(query, v), key) for key, v in index.items()), reverse=True)
    return [{"id": key, "score": round(score, 3), "passage": notes[key]}
            for score, key in ranked[:limit] if score > 0]
for question in ["Python function", "test data model", "galactic weather"]:
    print(question, "->", search(question) or "No matching passage")
Copy code

Save the file, open your terminal in that folder, and run python project-search-your-notes.py. Use python3 or py if required by your installation. Setup guide

What to expect

The Python and test-data queries retrieve relevant passages; galactic weather returns no match.

YOUR TURN

Build a five-question retrieval evaluation.

  1. Write questions before looking at ranked results and assign relevant passage IDs.
  2. Calculate the fraction with a relevant ID in the first two results.
  3. Add a synonym query and document how the lexical method fails.
Compare with a suggested solution

For each supported question, count success if the relevant ID appears in the top two. Report unsupported questions separately because they test abstention. A semantic retriever could be a later comparison, not an assumed improvement.

CHECK YOUR UNDERSTANDING

One idea to take with you.

Is this project a complete generative RAG assistant?

Make it part of your progress.

Finish the practice and answer the knowledge check to mark this lesson complete.

Go deeper with primary documentation

Optional references for further study. This lesson and its examples were written for Artificials.

Python SQLite modulePython HTTP server: limitations