Évaluer Graph RAG avec RAGAS

Graph RAG avec LangChain et Neo4j

Adam Cowley

Manager, Developer Education at Neo4j

Text-to-Cypher

 

Les résultats sont injectés dans l’invite, prêts à être envoyés au LLM

Recherche vectorielle

 

L’avis est publié par une Person avec le pseudo @emileifrem

Graph RAG avec LangChain et Neo4j

Évaluer Graph RAG

 

  1. Évaluer de bout en bout avec time
  2. Évaluer les tokens et coûts avec tiktoken
  3. Évaluer la qualité avec ragas
    • Précision du contexte
    • Sensibilité au bruit

 

compromis_de_performance.jpg

1 Image générée avec GPT-4o
Graph RAG avec LangChain et Neo4j

Sensibilité au bruit

  • Mesure la quantité d’informations non pertinentes dans les documents récupérés
  • Plus élevée en recherche vectorielle seule
  • Retourne un score basé sur l’information pertinente
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/
Graph RAG avec LangChain et Neo4j

Précision du contexte

  • Mesure la part de segments pertinents dans les documents récupérés
  • Un score plus élevé = informations plus pertinentes
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
Graph RAG avec LangChain et Neo4j

Précision du contexte

  • Mesure la part de segments pertinents dans les documents récupérés
  • Un score plus élevé = informations plus pertinentes
from ragas.metrics import LLMContextPrecisionWithoutReference


metric = LLMContextPrecisionWithoutReference( llm=evaluator_llm, )
Graph RAG avec LangChain et Neo4j

Structure du résultat 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 }, ]
}
Graph RAG avec LangChain et Neo4j

Structure du résultat 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
        }),
    ]
}
Graph RAG avec LangChain et Neo4j

Structure du résultat vectoriel seul

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!"
] }
Graph RAG avec LangChain et Neo4j

Structure du résultat hybride

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"
}
}),
# ...
] }
Graph RAG avec LangChain et Neo4j

Créer un jeu de données d’évaluation

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])
Graph RAG avec LangChain et Neo4j

Choisir un LLM pour l’évaluation

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)
Graph RAG avec LangChain et Neo4j

Évaluer les réponses

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}
Graph RAG avec LangChain et Neo4j

Passons à la pratique !

Graph RAG avec LangChain et Neo4j

Preparing Video For Download...