Johns Hopkins case study

TextWave

Question answering over a document store, from a plain-language question to a cited answer, on a laptop.

Program
MS in Artificial Intelligence, Johns Hopkins, fall 2026
Domain
Retrieval-augmented generation
Stack
Python, sentence-transformers, FAISS, Ollama, Flask, Docker
01

The problem

TextWave is the third case study in the AI program I am taking at Johns Hopkins in fall 2026, after TechTrack and IronClad. The scenario is a company that wants a system that takes a question in plain language, finds the passages in a customer's document store that answer it, and returns an answer a person can read, with the source it came from. Answers have to be relevant on domain-specific and institution-specific material, documents have to come and go as the collection changes, the store has to scale to billions of documents, and every answer has to cite its source.

The pipeline is the standard one. A chunker cuts each document into pieces, a sentence embedding model turns chunks and questions into vectors, a FAISS index returns the nearest chunks, an optional re-ranker reorders them, and a small language model running locally writes the answer from the chunks it is handed. The analysis settles five things in order: the chunking, the index, the generator, the re-ranker, and how many chunks go into the prompt. Each decision reuses the one before it, and where a later result bears on an earlier choice I went back and said so.

02

What I built

I built the retrieval and generation service in Python, then the evaluation harness around it.

  • Two chunkers, fixed-length character windows and runs of sentences, both with configurable overlap, and a text preprocessing stage that tolerates files that are not UTF-8
  • Bag-of-words and TF-IDF vectorizers written from scratch, with the term weighting the course specifies
  • Five re-rankers over the retrieved candidates: a cross-encoder, TF-IDF, bag of words, a weighted hybrid of the first two, and a TF-IDF-then-cross-encoder sequence
  • The brute-force and HNSW index wrappers carried over from IronClad, plus an LSH wrapper for the analysis, every chunk keeping its source file in the metadata so answers can cite it
  • A Flask service whose answer endpoint builds the dense index in a background thread at startup and answers from TF-IDF until it is ready, packaged in Docker
  • An evaluation harness that caches every generated answer with its scores as it arrives, so every table is a re-read of the cache, with paired bootstrap intervals on every comparison
03

The design analysis

The corpus is 150 encyclopedia-style articles, about 696,000 words, and 21 of the files are byte-identical copies of another file. The question file collapses to 513 unique questions with a reference answer, 223 of them yes or no, labelled easy, medium or hard by the person who wrote the answer. I used the same 513 questions for every task so that every comparison is paired, and I ran the generators through a local Ollama server (phi3:mini and qwen2.5:1.5b) at a fixed temperature. The course scores an answer as a true or false match against the reference, by exact match and by a transformer-based equivalence classifier. I report both, and after reading 48 of their disagreements by hand I added a third rule that reads the polarity of a yes or no answer directly and never credits a refusal. Every headline number carries a 95 percent bootstrap interval, and I ran the configurations a decision rests on twice, because generation at this temperature moves the score by about two points between runs.

  1. Chunking and the index

    I measured retrieval on its own first, scoring a chunk as relevant when its text occurs in the article the question was written against. Shorter chunks retrieve the article more often in both families, sentence chunks beat fixed-length chunks of the same length because a fixed window cuts sentences mid-word, and overlap lifts precision in the sentence family only. The service default of 1,500 characters sits 8.8 points of hit@3 below the setting I chose, two sentences per chunk with one sentence of overlap. Among the indexes, HNSW reproduced the exact ranking at a tenth of the cost, and LSH at 256 bits returned only 54 percent of the exact top ten while still finding the article for 93 percent of questions, because an article cut into 255 chunks forgives an approximate neighbour. On synthetic indexes up to 500,000 vectors brute force grew linearly to 26 ms per query and HNSW stayed at a quarter of a millisecond with 99 percent of the exact neighbours.

    Three charts of hit@3, precision at 3, and MRR against median chunk length for fixed-length and sentence chunking, marker shape showing the overlap setting. Both families fall as chunks grow, sentence chunks sit above fixed-length chunks of the same length, and overlap lifts precision only in the sentence family.
    Thirty chunkings on a brute-force cosine index. A question is one sentence, and the embedding of a single sentence sits closer to it than the embedding of a paragraph that also covers four other things, so every retrieval number falls as chunks grow. I picked the configuration by keeping those whose hit@3 sat inside the best one's interval and then taking the highest precision at 3, which is the share of the three slots that go to the right article.
    Show the numbers
    chunkinghit@3p@3hit@3 gap to chosen (95% interval)p@3 gap to chosen
    2 sentences, overlap 1 (chosen)94.583.6
    1 sentence95.579.4-1.0 (-2.5 to 0.8)4.2 (2.5 to 5.7)
    3 sentences, overlap 292.483.72.1 (0.6 to 3.7)-0.1 (-1.4 to 1.1)
    fixed 250 characters, no overlap92.476.52.1 (0.4 to 3.9)7.0 (5.4 to 8.6)
    fixed 1,500 characters, overlap 150 (service default)85.873.78.8 (6.2 to 11.5)9.8 (7.7 to 11.8)
    Two charts against index size on log axes: query time per question and hit@3 on the real questions, one line per index family. Brute force rises linearly, HNSW stays flat near a quarter of a millisecond, and LSH loses hit rate as the index grows.
    I grew the index with noise-perturbed copies of real chunk vectors, which can only crowd the real ones, and scored the real questions as before. The crossover where HNSW earns its build time is an extrapolation from these four sizes, about a million chunks, and I say so in the report rather than claim a measurement past 500,000. Memory is the open problem at a billion: the raw vectors are about 1.5 TB, so the index has to be sharded or compressed, and I tested neither.
    Show the numbers
    indexvectorsbuild squery msoverlap with exact top 10hit@3
    brute force30,7720.011.301.00094.5
    HNSW M=32, efSearch 6430,7724.650.100.99594.5
    LSH 256 bits30,7720.010.060.54093.0
    brute force500,0000.5325.861.00094.5
    HNSW M=32, efSearch 64500,000196.980.250.99094.2
    LSH 256 bits500,0000.671.090.48491.0
  2. The generator with no retrieval

    The baseline is each model answering the 513 questions from its own training alone, twice each. The two models were level on exact match and on my checked rule, and phi3:mini won the transformer metric by 5 to 7 points in both runs. Reading the disagreements showed where that margin came from. The transformer classifier judges whether an answer is about the right thing rather than whether it is correct, so on yes or no questions it credited 95 percent of phi3's answers, 78 of them opening with the wrong polarity, and phi3 simply answers those questions with a confident "Yes" or "No" more often than qwen does. Exact match has the opposite failure, a reference of "no" matching inside "not" and, later, inside the refusal "No context". That hand check is where the third scorer came from, and it is the one I read first whenever a comparison turns on refusals.

    Three charts of exact match, transformer match, and checked match by difficulty for phi3:mini and qwen2.5:1.5b with bootstrap intervals. The transformer match gap between the models is wide on easy questions and closes on hard ones; the other two scorers put the models level everywhere.
    Both runs pooled. The run-to-run movement was one to three points overall and up to six inside a difficulty stratum, and only 23 percent of phi3's answers were identical across the two runs, so that is the noise floor I held every later comparison against. I took phi3:mini forward on the course metric and on answer form (half the words, a third as many runaway answers), and re-ran the comparison on the full pipeline before I let it stand.
    Show the numbers
    model and runexact matchtransformer matchcheckedchecked easychecked mediumchecked hard
    phi3:mini, run 136.564.334.752.922.317.9
    phi3:mini, run 237.863.235.953.823.918.9
    qwen2.5:1.5b, run 137.457.533.150.720.118.9
    qwen2.5:1.5b, run 240.258.535.956.521.717.0
  3. Retrieval in front of the generator

    Handing phi3:mini the top three chunks with the course's context-only prompt added 20 to 26 points on every scorer, ten times the run-to-run movement. The gain was largest on medium questions and smallest on hard ones, where the article was retrieved more often than for any other stratum and the answer still was not in the three chunks. On the 485 questions where a chunk from the right article reached the prompt, the checked score went from 36 to 61 percent; on the 28 where none did, the model obeyed its prompt and scored zero, refusing or guessing from the wrong article. The 45 refusals on questions whose article was retrieved are the more interesting failure: the chunk is from the right article and the fact is not in it, which is a retrieval depth problem and what the re-ranker and the prompt size are for. With retrieval in front of it the smaller model gained as much as phi3 and landed within a point of it, so the generator choice stood but the ground under it moved. Retrieval sets this system's answer quality, not the generator.

    Three charts of the stand-alone model against RAG by difficulty on exact match, transformer match, and checked match, both runs pooled, with bootstrap intervals. The gain is largest on medium questions and smallest, with the widest interval, on hard ones.
    The stand-alone model already answered half the easy questions from memory, because the corpus is encyclopedia articles about Lincoln, kangaroos and Finland and a 3.8 billion parameter model has read about all of them. Nineteen of the 58 refusals were scored correct by exact match and 40 by the transformer, because "No context" contains "no" and the classifier reads a refusal about the right subject as equivalent, so the course scorers reward exactly the refusal the prompt asks for. I report the refusal count beside every configuration from here on.
    Show the numbers
    comparisonexact matchtransformer matchcheckedchecked easychecked mediumchecked hard
    RAG minus stand-alone, run 126.3 (21.6 to 30.8)15.4 (11.3 to 19.7)23.4 (18.7 to 27.9)22.4 (15.2 to 30.5)29.9 (21.7 to 38.6)14.2 (3.8 to 23.6)
    RAG minus stand-alone, run 222.8 (18.1 to 27.3)15.0 (10.5 to 18.9)20.1 (15.2 to 24.6)18.4 (10.3 to 26.0)27.2 (18.5 to 35.9)11.3 (1.9 to 21.7)
    RAG run 2 minus run 1-2.1 (-4.5 to 0.2)-1.6 (-3.7 to 0.6)-2.1 (-4.5 to 0.4)-3.1 (-6.7 to 0.4)-1.1 (-5.4 to 3.3)-1.9 (-6.6 to 2.8)
  4. Re-rankers with retrieval held fixed

    The index returned ten candidates, a re-ranker reordered them, and the first three went to the model. On the article-level retrieval metrics every re-ranker landed within a point or two of the plain index order, because a re-ranker can only reorder ten candidates that already hold the right article 95 percent of the time. On the answers, the cross-encoder was the best setting on all three scorers, 3.9 points of checked match over the index order, and the gain held in a second run on every scorer. I traced the gap between the two layers to the relevance rule: a chunk about Lincoln's childhood counts as relevant to a question about his assassination, but the generator needs the sentence that holds the answer. Measuring that directly, the cross-encoder put the answer in the prompt for 63 percent of the free-text questions against 57 for the index order, while its article recall moved 0.4 points. It costs 8 ms per question, and that cost does not grow with the corpus.

    Two scatter charts, one point per re-ranker setting: checked match on free-text questions against article recall at 3, and against the share of questions whose reference answer appears in the three chunks. Article recall spans 1.2 points and does not order the settings; the answer-in-context rate spans 7.2 points and does.
    Across the nine settings I generated answers for, checked match on free-text questions correlated at 0.90 with the answer-in-context rate and at 0.39 with article recall. The settings share most of their pipeline, so I read that as a description of the chart and not as a test. It is also the retrieval metric I would tune on if I repeated the first task: it needs only the reference answers the question file already has, it sees the sentence and not just the article, and it is sensitive to chunk length, the blind spot of the file-level rule.
    Show the numbers
    re-rankerexact matchtransformer matchcheckedrefusalschecked minus index order (95% interval)
    none (index order)62.879.758.158
    cross-encoder66.783.262.0493.9 (0.8 to 7.4)
    hybrid, TF-IDF weight 0.165.382.161.8543.7 (0.8 to 7.4)
    hybrid, weight 0.365.182.861.2563.1 (-0.2 to 6.8)
    hybrid, weight 0.563.582.160.2542.1 (-0.8 to 5.5)
    TF-IDF62.481.557.766-0.4 (-3.7 to 3.1)
    bag of words61.881.157.761-0.4 (-3.3 to 2.9)
    sequential, TF-IDF top 3 then cross-encoder62.881.158.5660.4 (-2.9 to 4.3)
    sequential, top 5 then cross-encoder62.482.858.7530.6 (-2.5 to 3.7)
  5. How many chunks in the prompt

    With the cross-encoder ordering the ten candidates, I handed the model the first m of them for m from 1 to 10. One chunk is not enough: the model refused 92 times and scored 9 points below three chunks. A second chunk recovered almost all of it, and from two to ten no scorer moved by more than 1.6 points, apart from a dip at eight that two more chunks undid. Each extra chunk added about 60 prompt tokens, about 0.3 s at the median, and one more source for a user to check. My rule took the smallest m the scores could not separate from the best, and a second run of two and three chunks confirmed the tie: the sign of every difference flipped between runs, and with both runs averaged no difference was larger than 0.4 points. The eight-chunk dip taught me something about the output rather than the retrieval. As the prompt grows, phi3 starts to open answers with "Answer:" as if continuing a transcript, 36 times at eight chunks, and a client that parses the first word of the answer would have to strip a label the model adds only on long prompts.

    Three charts against the number of chunks in the prompt: the three answer scores with intervals and dotted lines at the stand-alone model, checked match by difficulty, and median prompt tokens with median latency. Scores rise from one chunk to two and stay level to ten apart from a dip at eight, while tokens and latency grow by about the same step per chunk.
    More chunks do what they are meant to do: the share of prompts with nothing from the right article falls from 8.8 to 3.5 percent and the share with the answer somewhere in the prompt rises from 51 to 68. But the gain per chunk shrinks fast, distractors grow faster, and by eight chunks 7 percent of prompts hold the same passage twice, because the corpus repeats paragraphs across files. The best m moves with the question mix, the chunk length, and how many documents cover the same subject, so I would re-run this sweep on a customer's own questions before fixing it in production.
    Show the numbers
    chunks in promptexact matchtransformer matchcheckedrefusalsmedian prompt tokensmedian schecked minus m=3 (95% interval)
    157.979.153.0921541.04-9.0 (-12.5 to -5.5)
    265.781.761.8492161.29-0.2 (-3.1 to 2.7)
    366.783.262.0492821.55
    566.181.761.2544062.23-0.8 (-3.7 to 2.3)
    864.782.758.3525922.95-3.7 (-6.8 to -0.6)
    1066.181.961.6437043.72-0.4 (-3.3 to 2.7)
04

What I found

+27pts
Of checked match from retrieval, against about 1 for the choice of generator
61.8%
Checked match for the final configuration, the same in both runs; 65.7 exact, 81.7 transformer
8ms
Cross-encoder cost per question for 3.9 points, and it does not grow with the corpus
2
Chunks in the prompt: tied with three over two runs at 0.83 of the latency and one fewer source to check

Retrieval sets the answer quality, not the generator

Two generators that differ by a factor of two in size land within a point of each other once the right chunks are in front of them, and the 20 to 26 point gain from retrieval is ten times the noise between runs. The settings worth tuning are the ones that decide what reaches the prompt: the chunking, the re-ranker, and the number of chunks.

Relevance by file cannot see the answer sentence

Every re-ranker looked the same on article-level recall and the cross-encoder was four points better on the answers, because it put the sentence that holds the answer in the prompt 6.6 points more often. A retrieval metric that needs only the reference answers, and that sees the sentence rather than the article, is the one I would tune on next time.

The scorers have to be checked before they can decide

The transformer metric rewards a confident wrong polarity, exact match rewards the refusal the prompt asks for, and the model's own output format drifts on long prompts. Reading 48 disagreements by hand produced a third rule that arbitrates where the shipped scorers split, and I say which scorer each decision rests on.

The design, one row per decision

Every decision in the report, the measurement it rests on, the requirement it serves, and the alternative I rejected.

chunking
two sentences, one sentence of overlap
evidencehit@3 94.5 and p@3 83.6, 8.8 points of hit@3 over the 1,500-character default; the one-sentence chunking retrieves slightly better and answers 2.5 points worse
servesrelevant answers; the generator reads the pronoun's referent
rejectedfixed-length windows, 2 to 9 points of hit@3 behind at every length
index
brute force now, HNSW past about a million chunks
evidenceexact search at 1.3 ms on 30,772 vectors and 26 ms at 500,000; HNSW (M 32, efSearch 64) at 0.25 ms with 99 percent of the exact neighbours
servesscale to billions, sharded
rejectedLSH, half the exact neighbours at 256 bits and 4,096 bits to come within a point
generator
phi3:mini
evidencelevel with qwen2.5:1.5b on correctness, 5 to 7 points ahead on the course metric, half the words, a direct yes or no 95 percent of the time
servesanswers a person reads
rejectedqwen2.5:1.5b, the right pick where 0.15 s per answer or a gigabyte of memory binds
re-ranker
cross-encoder
evidence3.9 points of checked match over the index order, held in a second run; the answer in the prompt 6.6 points more often
servesrelevance; the first cited source from the right article 91 percent of the time
rejectedthe hybrid, tied but with a weight to tune; TF-IDF, bag of words and the sequence, within a point of no re-ranker
chunks in the prompt
m = 2
evidence8.8 points over one chunk; level with three over two runs on every scorer; 216 prompt tokens against 282
servesrelevance at the lowest cost per question; two sources to check
rejectedone chunk, 92 refusals; five or more, nothing the scores can see for twice the latency
scoring
three scorers, read in a stated order
evidence48 disagreements read by hand; the transformer credits wrong polarity, exact match credits refusals
servesdecisions that rest on correctness rather than on a scorer's blind spot
rejectedany single shipped scorer deciding alone
05

Limits and notes

The evaluation set is 513 questions about 21 articles, and the model had read about all of them before it saw a single chunk, so the numbers say less about institution-specific documents than the requirement asks. The scaling past 30,772 vectors is synthetic, and the billion-vector memory figures are arithmetic, not measurements. Two of the scorers are the course's and the third is my own rule from a hand check, and I did no manual review or LLM judge of the final configuration. I ran the configurations a decision rests on twice and everything else once, and the run-to-run spread is about two points, so single-run differences smaller than that are not evidence. Latencies are from one laptop with the generator on its GPU and FAISS on one CPU thread. Removal of documents is designed and not built, and whether a cited chunk actually supports the answer it is attached to is untested. Once the system is live there is no answer key, so I would track the refusal rate, the share of answer words that occur in the cited passage, and the re-ranker's top score, calibrated first against a hand-graded sample. The failure I worry about most never arises in this benchmark: two editions of the same policy side by side, identical except for one figure, where the model would quote whichever ranked higher.

Course repositories stay private under the university's rules, so this page summarizes my results and does not link the code.