LangChain / RAG / Vector DBs · Lezione

Creare un set di test di riferimento per il RAG

Crei un dataset curato di domande e risposte che Le permetta di misurare e confrontare oggettivamente la qualità del RAG nel tempo.

Lezione 4 di 413 passaggi

Creare un set di test di riferimento per il RAG è una lezione LangChain / RAG / Vector DBs gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento LangChain / RAG / Vector DBs, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso LangChain / RAG / Vector DBs include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

Why You Need a Test Set

Eyeballing a few answers does not tell you if a change helped or hurt. A golden test set of question-answer pairs gives you repeatable, comparable measurements.

Anatomy of a Test Case

Each case captures what to ask, what is correct, and where the answer lives.

  • question
  • ground_truth answer
  • relevant_sources (expected docs)

A Sample Dataset

Store cases as simple records you can load and iterate over.

testset = [
    {"question": "What is the refund window?",
     "ground_truth": "30 days from purchase.",
     "sources": ["policy.pdf#p2"]},
    {"question": "Who approves expenses?",
     "ground_truth": "The department manager.",
     "sources": ["handbook.pdf#p7"]},
]

Manual vs. Synthetic

You can write cases by hand for accuracy, or generate them by prompting an LLM over your documents for scale. A hybrid approach is common: generate, then review.

Generating Questions with an LLM

Feed a chunk to the model and ask it to produce a question whose answer is contained in that chunk, plus the answer itself.

prompt = (
  "Read the passage and write one question a user might ask, "
  "plus the exact answer.\n\nPassage: " + chunk.page_content
)
qa = llm.invoke(prompt)

Reviewing Synthetic Cases

LLM-generated pairs can be ambiguous or unanswerable. Human review filters out weak cases before they pollute your metrics.

Retrieval vs. Generation Metrics

Separate two questions: did we fetch the right docs (retrieval), and did we write the right answer (generation)? Each is measured differently.

Context Recall

Context recall checks whether the expected source appears among the retrieved chunks. It isolates retrieval quality from the LLM.

def context_recall(retrieved_ids, expected_ids):
    hits = sum(1 for e in expected_ids if e in retrieved_ids)
    return hits / len(expected_ids)

Answer Correctness

Compare the generated answer to the ground truth. Exact match is brittle, so use an LLM judge or semantic similarity for fuzzy correctness.

judge_prompt = (
  "Is the ANSWER correct given the REFERENCE? Reply yes or no.\n"
  "REFERENCE: " + truth + "\nANSWER: " + answer
)
verdict = llm.invoke(judge_prompt)

Running the Suite

Loop over every case, run your pipeline, and aggregate scores so one number summarizes the whole system.

scores = []
for case in testset:
    docs = retriever.invoke(case["question"])
    ans = rag_chain.invoke(case["question"])
    scores.append(evaluate(case, docs, ans))
print(sum(scores) / len(scores))

Track Results Over Time

Save each run with a timestamp and the config used. When a metric drops, you can pinpoint the change that caused the regression.

Quick Check

Test your understanding of RAG evaluation.

Recap

You built an evaluation foundation:

  • A golden test set of question, ground truth, and sources
  • Generate then review synthetic cases
  • Measure retrieval (context recall) and generation (answer correctness) separately
  • Track scores across runs
Gratis per iniziare

Impara LangChain / RAG / Vector DBs con un tutor IA — gratis

Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.

Corsi
12
Lezioni
48

Domande Frequenti

La lezione «Creare un set di test di riferimento per il RAG» è gratuita?

Sì — il testo completo di «Creare un set di test di riferimento per il RAG» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso LangChain / RAG / Vector DBs, passa a CoddyKit PRO. Il corso LangChain / RAG / Vector DBs include 4 lezioni in totale.

Cosa imparerò in «Creare un set di test di riferimento per il RAG»?

Crei un dataset curato di domande e risposte che Le permetta di misurare e confrontare oggettivamente la qualità del RAG nel tempo. Eserciti LangChain / RAG / Vector DBs con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare LangChain / RAG / Vector DBs?

Non è richiesta alcuna esperienza precedente. LangChain / RAG / Vector DBs su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.

Quanto tempo richiede la lezione «Creare un set di test di riferimento per il RAG»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione LangChain / RAG / Vector DBs?

Sì. Ogni lezione LangChain / RAG / Vector DBs include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Integrare tutti i componenti RAG
  2. Eseguire query e generare risposte
  3. Valutare le prestazioni di un sistema RAG
  4. Creare un set di test di riferimento per il RAG
← Torna a LangChain / RAG / Vector DBs