RAGAS로 Graph RAG 평가

LangChain과 Neo4j로 배우는 Graph RAG

Adam Cowley

Manager, Developer Education at Neo4j

Text-to-Cypher

 

결과가 프롬프트에 주입되어 LLM으로 전송할 준비가 됩니다

벡터 검색

 

리뷰는 화면 이름 @emileifrem 인 Person이 게시합니다

LangChain과 Neo4j로 배우는 Graph RAG

Graph RAG 평가

 

  1. time으로 엔드 투 엔드 생성 평가
  2. tiktoken으로 토큰 사용량과 비용 평가
  3. ragas로 출력 품질 평가
    • 컨텍스트 정밀도
    • 노이즈 민감도

 

성능 상충관계

1 Image generated with GPT-4o
LangChain과 Neo4j로 배우는 Graph RAG

Noise Sensitivity

  • 검색 문서의 무관한 정보량을 측정합니다
  • 벡터 검색 단독일 때 더 높습니다
  • 관련 정보 기준으로 점수를 반환합니다
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/
LangChain과 Neo4j로 배우는 Graph RAG

Context Precision

  • 검색 문서에서 관련 청크 비율을 측정합니다
  • 점수가 높을수록 검색 정보의 관련성이 높습니다
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
LangChain과 Neo4j로 배우는 Graph RAG

Context Precision

  • 검색 문서에서 관련 청크 비율을 측정합니다
  • 점수가 높을수록 검색 정보의 관련성이 높습니다
from ragas.metrics import LLMContextPrecisionWithoutReference


metric = LLMContextPrecisionWithoutReference( llm=evaluator_llm, )
LangChain과 Neo4j로 배우는 Graph RAG

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 }, ]
}
LangChain과 Neo4j로 배우는 Graph RAG

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
        }),
    ]
}
LangChain과 Neo4j로 배우는 Graph RAG

벡터 전용 결과 구조

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!"
] }
LangChain과 Neo4j로 배우는 Graph RAG

하이브리드 결과 구조

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"
}
}),
# ...
] }
LangChain과 Neo4j로 배우는 Graph RAG

평가 데이터세트 생성

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])
LangChain과 Neo4j로 배우는 Graph RAG

평가용 LLM 선택

from langchain.chat_models import init_chat_model

# 평가에 사용할 LLM 선택
llm = init_chat_model(
    "gpt-4o-mini",
    model_provider="openai",
    api_key="...",
    temperature=0
)


# LangchainLLMWrapper로 래핑 from ragas.llms import LangchainLLMWrapper evaluator_llm = LangchainLLMWrapper(llm)
LangChain과 Neo4j로 배우는 Graph RAG

응답 평가

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}
LangChain과 Neo4j로 배우는 Graph RAG

Vamos praticar!

LangChain과 Neo4j로 배우는 Graph RAG

Preparing Video For Download...