Évaluer Graph RAG avec RAGAS

Graph RAG avec LangChain et Neo4j

Adam Cowley

Manager, Developer Education at Neo4j

Texte vers 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 personne au pseudo @emileifrem

Graph RAG avec LangChain et Neo4j

Évaluer Graph RAG

 

  1. Évaluer la génération de bout en bout avec time
  2. Évaluer l'usage des jetons et les coûts avec tiktoken
  3. Évaluer la qualité de sortie avec ragas
    • Précision du contexte
    • Sensibilité au bruit

 

performance_tradeoffs.jpg

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

Sensibilité au bruit

  • Mesure la quantité d'information non pertinente 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 proportion de segments pertinents dans les documents récupérés
  • Un score plus élevé indique une information plus pertinente
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 proportion de segments pertinents dans les documents récupérés
  • Un score plus élevé indique une information plus pertinente
from ragas.metrics import LLMContextPrecisionWithoutReference


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

Structure du résultat Texte→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 Texte→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 (vecteur seulement)

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 ensemble 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

# Choisir un LLM pour effectuer l'évaluation
llm = init_chat_model(
    "gpt-4o-mini",
    model_provider="openai",
    api_key="...",
    temperature=0
)


# Envelopper avec 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...