用 RAGAS 評估 Graph RAG

使用 LangChain 與 Neo4j 的 Graph RAG

Adam Cowley

Manager, Developer Education at Neo4j

Text-to-Cypher

 

結果會注入提示詞,準備送往 LLM

向量搜尋

 

此評論由螢幕名稱為 @emileifrem 的 Person 張貼

使用 LangChain 與 Neo4j 的 Graph RAG

評估 Graph RAG

 

  1. time 評估端到端產生
  2. tiktoken 評估權杖用量與成本
  3. ragas 評估輸出品質
    • 脈絡精確度
    • 雜訊敏感度

 

performance_tradeoffs.jpg

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

雜訊敏感度(Noise Sensitivity)

  • 衡量擷取文件中不相關資訊的量
  • 僅用向量搜尋時通常較高
  • 依相關資訊回傳分數
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

脈絡精確度(Context Precision)

  • 衡量擷取文件中相關片段的比例
  • 分數越高表示取回的資訊越相關
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

脈絡精確度(Context Precision)

  • 衡量擷取文件中相關片段的比例
  • 分數越高表示取回的資訊越相關
from ragas.metrics import LLMContextPrecisionWithoutReference


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

Text-to-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

Text-to-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

# Choose an LLM to perform the evaluation
llm = init_chat_model(
    "gpt-4o-mini",
    model_provider="openai",
    api_key="...",
    temperature=0
)


# Wrap in 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...