RAG मूल्यांकन परिचय

LangChain के साथ Retrieval Augmented Generation (RAG)

Meri Nova

Machine Learning Engineer

RAG मूल्यांकन के प्रकार

एक RAG वर्कफ़्लो जिसमें वे प्रक्रियाएँ हाइलाइट हैं जिन्हें हम मूल्यांकित कर सकते हैं: रिट्रीवल प्रक्रिया, LLM hallucination, इनपुट प्रश्न से उत्तर की प्रासंगिकता, और उत्तर की संदर्भ उत्तर से तुलना.

1 Image Credit: LangSmith
LangChain के साथ Retrieval Augmented Generation (RAG)

आउटपुट शुद्धता: स्ट्रिंग मूल्यांकन

query = "What are the main components of RAG architecture?"
predicted_answer = "Training and encoding"
ref_answer = "Retrieval and Generation"
LangChain के साथ Retrieval Augmented Generation (RAG)

आउटपुट शुद्धता: स्ट्रिंग मूल्यांकन

prompt_template = """You are an expert professor specialized in grading students' answers to questions.
You are grading the following question:{query}
Here is the real answer:{answer}
You are grading the following predicted answer:{result}
Respond with CORRECT or INCORRECT:
Grade:"""

prompt = PromptTemplate(
    input_variables=["query", "answer", "result"],
    template=prompt_template
)

eval_llm = ChatOpenAI(temperature=0, model="gpt-4o-mini", openai_api_key='...')
LangChain के साथ Retrieval Augmented Generation (RAG)

आउटपुट शुद्धता: स्ट्रिंग मूल्यांकन

from langsmith.evaluation import LangChainStringEvaluator

qa_evaluator = LangChainStringEvaluator(
    "qa",
    config={
        "llm": eval_llm,
        "prompt": PROMPT
    }
)

score = qa_evaluator.evaluator.evaluate_strings( prediction=predicted_answer, reference=ref_answer, input=query )
LangChain के साथ Retrieval Augmented Generation (RAG)

आउटपुट शुद्धता: स्ट्रिंग मूल्यांकन

print(f"Score: {score}")
Score: {'reasoning': 'INCORRECT', 'value': 'INCORRECT', 'score': 0}
query = "What are the main components of RAG architecture?"
predicted_answer = "Training and encoding"
ref_answer = "Retrieval and Generation"
LangChain के साथ Retrieval Augmented Generation (RAG)

Ragas फ़्रेमवर्क

एक तालिका जो generation metrics और retrieval metrics की तुलना करती है.

1 Image Credit: Ragas
LangChain के साथ Retrieval Augmented Generation (RAG)

Faithfulness

  • क्या जनरेट किया गया आउटपुट संदर्भ को ईमानदारी से दर्शाता है?

 

$$ \text{Faithfulness} = \frac{\text{संदर्भ से निकाले जा सकने वाले दावों की संख्या}}{\text{कुल दावे}} $$

  • (0, 1) पर normalized
LangChain के साथ Retrieval Augmented Generation (RAG)

Faithfulness का मूल्यांकन

from langchain_openai import ChatOpenAI, OpenAIEmbeddings

from ragas.integrations.langchain import EvaluatorChain from ragas.metrics import faithfulness
llm = ChatOpenAI(model="gpt-4o-mini", api_key="...") embeddings = OpenAIEmbeddings(model="text-embedding-3-small", api_key="...")
faithfulness_chain = EvaluatorChain( metric=faithfulness, llm=llm, embeddings=embeddings )
LangChain के साथ Retrieval Augmented Generation (RAG)

Faithfulness का मूल्यांकन

eval_result = faithfulness_chain({

"question": "How does the RAG model improve question answering with LLMs?",
"answer": "The RAG model improves question answering by combining the retrieval of documents...",
"contexts": [ "The RAG model integrates document retrieval with LLMs by first retrieving relevant passages...", "By incorporating retrieval mechanisms, RAG leverages external knowledge sources, allowing the...", ]
})
print(eval_result)
'faithfulness': 1.0
LangChain के साथ Retrieval Augmented Generation (RAG)

Context precision

  • क्वेरी के लिए प्राप्त दस्तावेज़ कितने प्रासंगिक हैं?
  • (0, 1) पर normalized1 = अत्यधिक प्रासंगिक
from ragas.metrics import context_precision

llm = ChatOpenAI(model="gpt-4o-mini", api_key="...")
embeddings = OpenAIEmbeddings(model="text-embedding-3-small", api_key="...")

context_precision_chain = EvaluatorChain(
    metric=context_precision,
    llm=llm,
    embeddings=embeddings
)
LangChain के साथ Retrieval Augmented Generation (RAG)

Context precision का मूल्यांकन

eval_result = context_precision_chain({
  "question": "How does the RAG model improve question answering with large language models?",
  "ground_truth": "The RAG model improves question answering by combining the retrieval of...",
  "contexts": [
    "The RAG model integrates document retrieval with LLMs by first retrieving...",
    "By incorporating retrieval mechanisms, RAG leverages external knowledge sources...",
  ]
})

print(f"Context Precision: {eval_result['context_precision']}")
Context Precision: 0.99999999995
LangChain के साथ Retrieval Augmented Generation (RAG)

अभ्यास करते हैं!

LangChain के साथ Retrieval Augmented Generation (RAG)

Preparing Video For Download...