โ Final Challenge
โบ Ask Your Documents
50 ptsYour app is complete: it loads and chunks your files, embeds them, indexes
them in a real vector database, retrieves by meaning, and answers with
citations โ or refuses. The final challenge is that app's query path as one
pure function. Everything you built is in it: cosine scoring, top-k
selection, the score gate, prompt assembly. The two impure edges โ the
embedding model and the LLM โ arrive as injected callables, exactly as
embed_texts and llm_client do in rag.py; the tests stub them the same
way the CLI chatbot course stubbed its client.
Implement
retrieve_and_answer(question, entries, embed, client, k=3, min_score=0.25):
entriesis a list of(chunk_id, text, vector)tuples โ your indexed corpus.- Call
embed(question)exactly once to get the query vector. - Score every entry with cosine similarity (a working
cosine_similarityis in the starter), and select up tokbest entries โ highest score first, ties keeping input order, exactly like yourtop_k. - Drop selected entries scoring below
min_score. If nothing survives (orentriesis empty), returnNonewithout callingclientโ that's the refusal gate. - Otherwise build the prompt with your
build_promptformat from the previous lesson, using the surviving(chunk_id, text)pairs in score order, callclient(prompt)exactly once, and return its reply.
Log in to submit a solution and earn points.