Valutare Graph RAG con RAGAS

Graph RAG con LangChain e Neo4j

Adam Cowley

Manager, Developer Education at Neo4j

Text-to-Cypher

 

I risultati sono inseriti nel prompt, pronti per l'LLM

Ricerca vettoriale

 

La recensione è pubblicata da una Persona con il nome @emileifrem

Graph RAG con LangChain e Neo4j

Valutare Graph RAG

 

  1. Valutare la generazione end-to-end con time
  2. Valutare token e costi con tiktoken
  3. Valutare la qualità dell’output con ragas
    • Context precision
    • Noise sensitivity

 

compromessi_di_prestazioni.jpg

1 Immagine generata con GPT-4o
Graph RAG con LangChain e Neo4j

Noise Sensitivity

  • Misura la quantità di informazioni irrilevanti nei documenti recuperati
  • Più alta con la sola ricerca vettoriale
  • Restituisce un punteggio in base alle informazioni rilevanti
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 con LangChain e Neo4j

Context Precision

  • Misura la quota di chunk rilevanti nei documenti recuperati
  • Un punteggio più alto indica informazioni più pertinenti recuperate
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 con LangChain e Neo4j

Context Precision

  • Misura la quota di chunk rilevanti nei documenti recuperati
  • Un punteggio più alto indica informazioni più pertinenti recuperate
from ragas.metrics import LLMContextPrecisionWithoutReference


metric = LLMContextPrecisionWithoutReference( llm=evaluator_llm, )
Graph RAG con LangChain e Neo4j

Struttura del risultato 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 con LangChain e Neo4j

Struttura del risultato 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 con LangChain e Neo4j

Struttura del risultato solo vettoriale

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 con LangChain e Neo4j

Struttura del risultato ibrido

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 con LangChain e Neo4j

Creare un dataset di valutazione

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 con LangChain e Neo4j

Scelta di un LLM per la valutazione

from langchain.chat_models import init_chat_model

# Scegli un LLM per la valutazione
llm = init_chat_model(
    "gpt-4o-mini",
    model_provider="openai",
    api_key="...",
    temperature=0
)


# Wrappa con LangchainLLMWrapper from ragas.llms import LangchainLLMWrapper evaluator_llm = LangchainLLMWrapper(llm)
Graph RAG con LangChain e Neo4j

Valutare le risposte

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 con LangChain e Neo4j

Ayo berlatih!

Graph RAG con LangChain e Neo4j

Preparing Video For Download...