用 RAGAS 评估 Graph RAG

使用 LangChain 和 Neo4j 的 Graph RAG

Adam Cowley

Manager, Developer Education at Neo4j

文本转 Cypher

 

结果被注入到提示中,准备发送给 LLM

向量检索

 

这条点评由屏幕名为 @emileifrem 的 Person 发布

使用 LangChain 和 Neo4j 的 Graph RAG

评估 Graph RAG

 

  1. time 评估端到端生成
  2. tiktoken 评估 token 用量与成本
  3. ragas 评估输出质量
    • 上下文精确度
    • 噪声敏感度

 

performance_tradeoffs.jpg

1 Image generated with GPT-4o
使用 LangChain 和 Neo4j 的 Graph RAG

噪声敏感度

  • 衡量检索文档中无关信息的数量
  • 仅用向量检索时更高
  • 基于相关信息返回评分
from ragas.metrics import NoiseSensitivity


metric = NoiseSensitivity( llm=evaluator_llm, mode="irrelevant" )
1 https://docs.ragas.io/en/stable/concepts/metrics/available_metrics/noise_sensitivity/
使用 LangChain 和 Neo4j 的 Graph RAG

上下文精确度

  • 衡量检索文档中相关片段的占比
  • 分数越高表示检索信息越相关
from ragas.metrics import LLMContextPrecisionWithReference


metric = LLMContextPrecisionWithReference( llm=evaluator_llm, )
1 https://docs.ragas.io/en/stable/concepts/metrics/available_metrics/context_precision/#llm-based-context-precision
使用 LangChain 和 Neo4j 的 Graph RAG

上下文精确度

  • 衡量检索文档中相关片段的占比
  • 分数越高表示检索信息越相关。
from ragas.metrics import LLMContextPrecisionWithoutReference


metric = LLMContextPrecisionWithoutReference( llm=evaluator_llm, )
使用 LangChain 和 Neo4j 的 Graph RAG

文本转 Cypher 结果结构

cypher_result = {

"user_input": "Who is Romeo's love?",
"response": "Romeo loves Juliet",
"retrieved_contexts": [ { "source": "Romeo", "target": "Juliet", "relationship": "LOVES", "sentiment": 0.9837 }, ]
}
使用 LangChain 和 Neo4j 的 Graph RAG

文本转 Cypher 结果结构

cypher_result = {
    "user_input": "Who is Romeo's love?",
    "response": "Romeo loves Juliet",
    "retrieved_contexts": [
        json.dumps({
            "source": "Romeo",
            "target": "Juliet",
            "relationship": "LOVES",
            "sentiment": 0.9837
        }),
    ]
}
使用 LangChain 和 Neo4j 的 Graph RAG

仅向量结果结构

vector_result = { 
    "user_input": "Who is Romeo's love?",
    "response": "Romeo loves Juliet",
    "retrieved_contexts": [

"But, soft! what light through yonder window breaks?..." "O, she doth teach the forches to burn bright!"
] }
使用 LangChain 和 Neo4j 的 Graph RAG

混合结果结构

hybrid_result = {
    "user_input": "Who is Romeo's love?",
    "response": "Romeo loves Juliet",

"retrieved_contexts": [
json.dumps({ "page_content": "But, soft! what light through yonder window breaks? ...",
"metadata": {
"act": 2, "scene": 2, "spoken_to": "Juliet"
}
}),
# ...
] }
使用 LangChain 和 Neo4j 的 Graph RAG

创建评估数据集

cypher_result = {
    "user_input": "Who is Romeo's love?",
    "retrieved_contexts": [
        json.dumps({
            "source": "Romeo",
            "target": "Juliet",
            "relationship": "LOVES",
            "sentiment": 0.9837
        }),
    ]
}


cypher_dataset = EvaluationDataset.from_list([cypher_result])
使用 LangChain 和 Neo4j 的 Graph RAG

选择评估用 LLM

from langchain.chat_models import init_chat_model

# 选择用于评估的 LLM
llm = init_chat_model(
    "gpt-4o-mini",
    model_provider="openai",
    api_key="...",
    temperature=0
)


# 用 LangchainLLMWrapper 封装 from ragas.llms import LangchainLLMWrapper evaluator_llm = LangchainLLMWrapper(llm)
使用 LangChain 和 Neo4j 的 Graph RAG

评估响应

from ragas import evaluate, EvaluationDataset
from ragas.metrics import LLMContextPrecisionWithoutReference, NoiseSensitivity

cypher_scores = evaluate(

dataset=cypher_dataset,
metrics=[
LLMContextPrecisionWithoutReference(llm=evaluator_llm),
NoiseSensitivity(llm=evaluator_llm, mode="irrelevant")
]
)
{'llm_context_precision_without_reference': 1.0000,
'noise_sensitivity(mode=irrelevant)': 0.0000}
使用 LangChain 和 Neo4j 的 Graph RAG

让我们一起练习吧!

使用 LangChain 和 Neo4j 的 Graph RAG

Preparing Video For Download...