Đánh giá Graph RAG với RAGAS

Graph RAG với LangChain và Neo4j

Adam Cowley

Manager, Developer Education at Neo4j

Text-to-Cypher

 

Kết quả được chèn vào prompt, sẵn sàng gửi tới LLM

Tìm kiếm vector

 

Bài đánh giá được đăng bởi một Person với tên màn hình @emileifrem

Graph RAG với LangChain và Neo4j

Đánh giá Graph RAG

 

  1. Đánh giá sinh end-to-end bằng time
  2. Đánh giá số token và chi phí với tiktoken
  3. Đánh giá chất lượng đầu ra với ragas
    • Context precision
    • Noise sensitivity

 

performance_tradeoffs.jpg

1 Hình tạo bằng GPT-4o
Graph RAG với LangChain và Neo4j

Noise Sensitivity

  • Đo lượng thông tin không liên quan trong tài liệu truy hồi
  • Cao hơn khi chỉ dùng tìm kiếm vector
  • Trả về điểm dựa trên thông tin liên quan
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 với LangChain và Neo4j

Context Precision

  • Đo tỷ lệ mảnh liên quan trong tài liệu truy hồi
  • Điểm cao hơn nghĩa là thông tin truy hồi liên quan hơn
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 với LangChain và Neo4j

Context Precision

  • Đo tỷ lệ mảnh liên quan trong tài liệu truy hồi
  • Điểm cao hơn nghĩa là thông tin truy hồi liên quan hơn
from ragas.metrics import LLMContextPrecisionWithoutReference


metric = LLMContextPrecisionWithoutReference( llm=evaluator_llm, )
Graph RAG với LangChain và Neo4j

Cấu trúc kết quả 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 với LangChain và Neo4j

Cấu trúc kết quả 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 với LangChain và Neo4j

Cấu trúc kết quả chỉ-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 với LangChain và Neo4j

Cấu trúc kết quả lai

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 với LangChain và Neo4j

Tạo bộ dữ liệu đánh giá

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 với LangChain và Neo4j

Chọn LLM để đánh giá

from langchain.chat_models import init_chat_model

# Chọn LLM để đánh giá
llm = init_chat_model(
    "gpt-4o-mini",
    model_provider="openai",
    api_key="...",
    temperature=0
)


# Bọc bằng LangchainLLMWrapper from ragas.llms import LangchainLLMWrapper evaluator_llm = LangchainLLMWrapper(llm)
Graph RAG với LangChain và Neo4j

Đánh giá phản hồi

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 với LangChain và Neo4j

Hãy luyện tập!

Graph RAG với LangChain và Neo4j

Preparing Video For Download...