RAG 评估简介

使用 LangChain 的 Retrieval Augmented Generation (RAG)

Meri Nova

Machine Learning Engineer

RAG 评估类型

一个 RAG 流程图,标出可评估的环节:检索过程、LLM 幻觉、答案与问题的相关性,以及将答案与参考答案对比。

1 图片来源:LangSmith
使用 LangChain 的 Retrieval Augmented Generation (RAG)

输出准确性:字符串评估

query = "What are the main components of RAG architecture?"
predicted_answer = "Training and encoding"
ref_answer = "Retrieval and Generation"
使用 LangChain 的 Retrieval Augmented Generation (RAG)

输出准确性:字符串评估

prompt_template = """You are an expert professor specialized in grading students' answers to questions.
You are grading the following question:{query}
Here is the real answer:{answer}
You are grading the following predicted answer:{result}
Respond with CORRECT or INCORRECT:
Grade:"""

prompt = PromptTemplate(
    input_variables=["query", "answer", "result"],
    template=prompt_template
)

eval_llm = ChatOpenAI(temperature=0, model="gpt-4o-mini", openai_api_key='...')
使用 LangChain 的 Retrieval Augmented Generation (RAG)

输出准确性:字符串评估

from langsmith.evaluation import LangChainStringEvaluator

qa_evaluator = LangChainStringEvaluator(
    "qa",
    config={
        "llm": eval_llm,
        "prompt": PROMPT
    }
)

score = qa_evaluator.evaluator.evaluate_strings( prediction=predicted_answer, reference=ref_answer, input=query )
使用 LangChain 的 Retrieval Augmented Generation (RAG)

输出准确性:字符串评估

print(f"Score: {score}")
Score: {'reasoning': 'INCORRECT', 'value': 'INCORRECT', 'score': 0}
query = "What are the main components of RAG architecture?"
predicted_answer = "Training and encoding"
ref_answer = "Retrieval and Generation"
使用 LangChain 的 Retrieval Augmented Generation (RAG)

Ragas 框架

对比生成指标与检索指标的表格。

1 图片来源:Ragas
使用 LangChain 的 Retrieval Augmented Generation (RAG)

忠实度(Faithfulness)

  • 生成的输出是否忠实于上下文?

 

$$ \text{Faithfulness} = \frac{\text{可由上下文推断的断言数}}{\text{断言总数}} $$

  • 归一化至 (0, 1)
使用 LangChain 的 Retrieval Augmented Generation (RAG)

评估忠实度

from langchain_openai import ChatOpenAI, OpenAIEmbeddings

from ragas.integrations.langchain import EvaluatorChain from ragas.metrics import faithfulness
llm = ChatOpenAI(model="gpt-4o-mini", api_key="...") embeddings = OpenAIEmbeddings(model="text-embedding-3-small", api_key="...")
faithfulness_chain = EvaluatorChain( metric=faithfulness, llm=llm, embeddings=embeddings )
使用 LangChain 的 Retrieval Augmented Generation (RAG)

评估忠实度

eval_result = faithfulness_chain({

"question": "How does the RAG model improve question answering with LLMs?",
"answer": "The RAG model improves question answering by combining the retrieval of documents...",
"contexts": [ "The RAG model integrates document retrieval with LLMs by first retrieving relevant passages...", "By incorporating retrieval mechanisms, RAG leverages external knowledge sources, allowing the...", ]
})
print(eval_result)
'faithfulness': 1.0
使用 LangChain 的 Retrieval Augmented Generation (RAG)

上下文查准率(Context precision)

  • 检索到的文档与查询的相关性如何?
  • 归一化至 (0, 1)1 = 高度相关
from ragas.metrics import context_precision

llm = ChatOpenAI(model="gpt-4o-mini", api_key="...")
embeddings = OpenAIEmbeddings(model="text-embedding-3-small", api_key="...")

context_precision_chain = EvaluatorChain(
    metric=context_precision,
    llm=llm,
    embeddings=embeddings
)
使用 LangChain 的 Retrieval Augmented Generation (RAG)

评估上下文查准率

eval_result = context_precision_chain({
  "question": "How does the RAG model improve question answering with large language models?",
  "ground_truth": "The RAG model improves question answering by combining the retrieval of...",
  "contexts": [
    "The RAG model integrates document retrieval with LLMs by first retrieving...",
    "By incorporating retrieval mechanisms, RAG leverages external knowledge sources...",
  ]
})

print(f"Context Precision: {eval_result['context_precision']}")
Context Precision: 0.99999999995
使用 LangChain 的 Retrieval Augmented Generation (RAG)

Passons à la pratique !

使用 LangChain 的 Retrieval Augmented Generation (RAG)

Preparing Video For Download...