Graph RAG with LangChain and Neo4j
Adam Cowley
Manager, Developer Education at Neo4j
time
tiktoken
ragas
from ragas.metrics import NoiseSensitivity
metric = NoiseSensitivity( llm=evaluator_llm, mode="irrelevant" )
from ragas.metrics import LLMContextPrecisionWithReference
metric = LLMContextPrecisionWithReference( llm=evaluator_llm, )
from ragas.metrics import LLMContextPrecisionWithoutReference
metric = LLMContextPrecisionWithoutReference( llm=evaluator_llm, )
cypher_result = {
"user_input": "Who is Romeo's love?",
"response": "Romeo loves Juliet",
"retrieved_contexts": [ { "source": "Romeo", "target": "Juliet", "relationship": "LOVES", "sentiment": 0.9837 }, ]
}
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
}),
]
}
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!"
] }
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"
}
}),
# ...
] }
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])
from langchain_openai import ChatOpenAI # Choose an LLM to perform the evaluation llm = ChatOpenAI( model="gpt-4o-mini", temperature=0 )
# Wrap in LangchainLLMWrapper from ragas.llms import LangchainLLMWrapper evaluator_llm = LangchainLLMWrapper(llm)
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 with LangChain and Neo4j