開始使用免費開始

Ragas faithfulness 評估

在這個練習中,你會評估自己在第 1 章結尾建立的 RAG 架構之「faithfulness」。此 chain 已替你重新定義,並以變數 chain 提供。

你會使用提供的 query、chain 的輸出,以及擷取到的內容,透過 ragas 框架評估 faithfulness。

所需的類別已為你匯入。

本練習屬於課程

使用 LangChain 的 Retrieval Augmented Generation(RAG)

檢視課程

練習說明

  • 使用提供的 query 查詢 retriever,並以串列生成式從每個擷取到的文件中抽取文件文字。
  • 定義一個 ragas 的 faithfulness chain。
  • 評估可用的 RAG chain 的 faithfulness;你需要呼叫該 chain 以產生答案。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

from ragas.metrics import faithfulness

# Query the retriever using the query and extract the document text
query = "How does RAG improve question answering with LLMs?"
retrieved_docs = [doc.____ for doc in retriever.____(____)]

# Define the faithfulness chain
faithfulness_chain = ____(____, llm=llm, embeddings=embeddings)

# Evaluate the faithfulness of the RAG chain
eval_result = ____({
  "question": ____,
  "answer": ____.____(query),
  "contexts": ____
})

print(eval_result)
編輯並執行程式碼