LangChain / RAG / Vector DBs · Lekcja

Ocena wydajności systemu RAG

Poznaj metryki i techniki oceny dokładności, trafności oraz spójności odpowiedzi aplikacji RAG.

Lekcja 3 z 411 kroki

Ocena wydajności systemu RAG to bezpłatna lekcja LangChain / RAG / Vector DBs na CoddyKit. To lekcja 3 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej LangChain / RAG / Vector DBs, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs LangChain / RAG / Vector DBs zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

Why Evaluate RAG Systems?

When you build a Retrieval Augmented Generation (RAG) application, it's crucial to know if it's working as intended. Evaluation helps us understand if our system is providing accurate, relevant, and helpful answers.

Without proper evaluation, it's hard to tell if changes to your RAG pipeline (like new embedding models or chunking strategies) are actually making it better or worse.

Key Aspects of RAG Evaluation

Evaluating a RAG system involves looking at different aspects. We generally focus on:

  • Retrieval Quality: Are the right documents being found?
  • Generation Quality: Is the LLM producing good answers based on those documents?

These two parts are often evaluated separately and then together to get a full picture.

The Importance of Ground Truth

To evaluate effectively, especially for automated metrics, you need a 'ground truth' dataset. This means having:

  • A set of user queries.
  • The truly relevant documents for each query.
  • The ideal, correct answer for each query.

This reference data allows us to compare our RAG system's outputs against what's considered correct.

Retrieval Metrics: Precision & Recall

For the retrieval component, we often use metrics like Precision and Recall.

  • Precision: Out of all the documents retrieved, how many were actually relevant? High precision means fewer irrelevant documents.
  • Recall: Out of all the truly relevant documents, how many did our system manage to retrieve? High recall means fewer missed relevant documents.

These help us gauge how good our document search is.

Code: Calculating Retrieval Metrics

This Python snippet demonstrates how to calculate precision and recall for a mock retrieval scenario. relevant_docs are the 'ground truth' and retrieved_docs are what our system found.

def evaluate_retrieval(relevant_docs, retrieved_docs):
    true_positives = len(relevant_docs.intersection(retrieved_docs))

    precision = true_positives / len(retrieved_docs) if len(retrieved_docs) > 0 else 0
    recall = true_positives / len(relevant_docs) if len(relevant_docs) > 0 else 0

    print(f"Precision: {precision:.2f}")
    print(f"Recall: {recall:.2f}")

if __name__ == "__main__":
    # Example 1: Perfect retrieval
    relevant_set_1 = {"docA", "docB", "docC"}
    retrieved_set_1 = {"docA", "docB", "docC"}
    print("--- Example 1: Perfect Retrieval ---")
    evaluate_retrieval(relevant_set_1, retrieved_set_1)

    # Example 2: Some missing, some irrelevant
    relevant_set_2 = {"docX", "docY", "docZ"}
    retrieved_set_2 = {"docX", "docA", "docY"}
    print("\n--- Example 2: Mixed Retrieval ---")
    evaluate_retrieval(relevant_set_2, retrieved_set_2)

Generation Metrics: Faithfulness

For the generated answer, faithfulness (also called 'factuality') is key. It measures whether the LLM's answer is truly supported by the retrieved documents.

A RAG system should not 'hallucinate' or make up information. If a fact isn't in the retrieved context, the LLM shouldn't include it in its answer.

Generation Metrics: Answer Relevance

Answer relevance assesses if the generated answer directly addresses the user's original query. An answer might be faithful to the retrieved documents but still not relevant if the documents themselves were off-topic.

This metric helps ensure the RAG system stays focused on the user's actual information need.

Human vs. Automated Evaluation

While automated metrics are efficient, human evaluation is often essential for subjective qualities like fluency, tone, and nuanced relevance.

  • Automated: Fast, scalable, good for objective metrics (precision, recall, some faithfulness checks).
  • Human: Gold standard for subjective quality, crucial for complex queries, but expensive and slow.

A hybrid approach, using both, is often best for comprehensive RAG evaluation.

Tools for RAG Evaluation

Several libraries and frameworks help streamline RAG evaluation:

  • LangChain: Provides built-in evaluation modules, including reference-free metrics and integrations with LLM-as-a-judge.
  • Ragas: Specifically designed for RAG evaluation, offering metrics like faithfulness, answer relevance, context precision, and context recall.
  • TruLens: Offers observability and evaluation for LLM applications, including RAG, with deep insights into chain execution.

These tools automate much of the metric calculation and reporting.

Quick Check: RAG Metrics

Which of the following are key metrics used to evaluate the quality of the generated answer in a RAG system?

Recap: Evaluating RAG Performance

We've learned that evaluating RAG systems is vital for ensuring they deliver accurate and relevant information. Key takeaways include:

  • Evaluation covers both retrieval (finding relevant docs) and generation (creating good answers).
  • Ground truth data is essential for reliable evaluation.
  • Metrics like Precision and Recall measure retrieval quality.
  • Faithfulness and Answer Relevance assess the quality of the generated response.
  • A combination of automated and human evaluation provides the most comprehensive insights.
  • Tools like LangChain, Ragas, and TruLens can assist in setting up robust evaluation pipelines.
Bezpłatny start

Ucz się LangChain / RAG / Vector DBs dzięki korepetycjom AI — za darmo

Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.

Kursy
12
Lekcje
48

Często zadawane pytania

Czy lekcja „Ocena wydajności systemu RAG” jest bezpłatna?

Tak — pełny tekst „Ocena wydajności systemu RAG” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu LangChain / RAG / Vector DBs, przejdź na CoddyKit PRO. Kurs LangChain / RAG / Vector DBs zawiera 4 lekcji w sumie.

Co nauczysz się w „Ocena wydajności systemu RAG”?

Poznaj metryki i techniki oceny dokładności, trafności oraz spójności odpowiedzi aplikacji RAG. Ćwiczysz LangChain / RAG / Vector DBs z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć LangChain / RAG / Vector DBs?

Nie wymagamy żadnego doświadczenia. LangChain / RAG / Vector DBs w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 3 z 4.

Ile czasu zajmuje lekcja „Ocena wydajności systemu RAG”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji LangChain / RAG / Vector DBs?

Tak. Każda lekcja LangChain / RAG / Vector DBs zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Integracja wszystkich komponentów RAG
  2. Wykonywanie zapytań i generowanie odpowiedzi
  3. Ocena wydajności systemu RAG
  4. Tworzenie wzorcowego zbioru testowego dla RAG
← Powrót do LangChain / RAG / Vector DBs