Evaluar Graph RAG con RAGAS

Graph RAG con LangChain y Neo4j

Adam Cowley

Manager, Developer Education at Neo4j

Text-to-Cypher

 

Los resultados se inyectan en el prompt listos para enviarse al LLM

Búsqueda vectorial

 

La reseña la publica una Persona con el alias @emileifrem

Graph RAG con LangChain y Neo4j

Evaluar Graph RAG

 

  1. Evaluar la generación de extremo a extremo con time
  2. Evaluar uso de tokens y costos con tiktoken
  3. Evaluar calidad de salida con ragas
    • Context precision
    • Noise sensitivity

 

compensaciones_de_rendimiento.jpg

1 Imagen generada con GPT-4o
Graph RAG con LangChain y Neo4j

Noise Sensitivity

  • Mide la cantidad de información irrelevante en los documentos recuperados
  • Suele ser más alta solo con búsqueda vectorial
  • Devuelve una puntuación basada en la información relevante
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 y Neo4j

Context Precision

  • Mide la proporción de fragmentos relevantes en los documentos recuperados
  • Una puntuación más alta indica información más relevante
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 y Neo4j

Context Precision

  • Mide la proporción de fragmentos relevantes en los documentos recuperados
  • Una puntuación más alta indica información más relevante
from ragas.metrics import LLMContextPrecisionWithoutReference


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

Estructura de resultados 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 y Neo4j

Estructura de resultados 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 y Neo4j

Estructura de resultados solo vector

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 y Neo4j

Estructura de resultados híbrida

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 y Neo4j

Crear un dataset de evaluación

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 y Neo4j

Elegir un LLM para evaluar

from langchain.chat_models import init_chat_model

# Elige un LLM para la evaluación
llm = init_chat_model(
    "gpt-4o-mini",
    model_provider="openai",
    api_key="...",
    temperature=0
)


# Envuélvelo con LangchainLLMWrapper from ragas.llms import LangchainLLMWrapper evaluator_llm = LangchainLLMWrapper(llm)
Graph RAG con LangChain y Neo4j

Evaluar respuestas

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 y Neo4j

¡Vamos a practicar!

Graph RAG con LangChain y Neo4j

Preparing Video For Download...