LangChain / RAG / Vector DBs · บทเรียน

การสร้างชุดทดสอบมาตรฐานสำหรับ RAG

สร้างชุดข้อมูลคำถามและคำตอบที่คัดสรรแล้ว เพื่อวัดและเปรียบเทียบคุณภาพของ RAG อย่างเป็นกลางเมื่อเวลาผ่านไป

บทเรียน 4 จาก 413 ขั้นตอน

การสร้างชุดทดสอบมาตรฐานสำหรับ RAG เป็นบทเรียน LangChain / RAG / Vector DBs ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน LangChain / RAG / Vector DBs และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส LangChain / RAG / Vector DBs มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

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
เริ่มต้นได้ฟรี

เรียนรู้ LangChain / RAG / Vector DBs ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การสร้างชุดทดสอบมาตรฐานสำหรับ RAG” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การสร้างชุดทดสอบมาตรฐานสำหรับ RAG” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส LangChain / RAG / Vector DBs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส LangChain / RAG / Vector DBs มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การสร้างชุดทดสอบมาตรฐานสำหรับ RAG”

สร้างชุดข้อมูลคำถามและคำตอบที่คัดสรรแล้ว เพื่อวัดและเปรียบเทียบคุณภาพของ RAG อย่างเป็นกลางเมื่อเวลาผ่านไป คุณปฏิบัติ LangChain / RAG / Vector DBs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน LangChain / RAG / Vector DBs หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน LangChain / RAG / Vector DBs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การสร้างชุดทดสอบมาตรฐานสำหรับ RAG” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน LangChain / RAG / Vector DBs นี้ได้ไหม

ได้ บทเรียน LangChain / RAG / Vector DBs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การผสานรวมองค์ประกอบ RAG ทั้งหมด
  2. การสืบค้นและสร้างคำตอบ
  3. การประเมินประสิทธิภาพระบบ RAG
  4. การสร้างชุดทดสอบมาตรฐานสำหรับ RAG
← กลับไปที่ LangChain / RAG / Vector DBs