मुख्य सामग्री पर जाएं

Anthropic का संदर्भात्मक रिट्रीवल: इम्प्लीमेंटेशन सहित एक गाइड

जानें कि Anthropic का संदर्भात्मक रिट्रीवल रिट्रीवल सिस्टम्स की शुद्धता कैसे बढ़ाता है और इसे चरण-दर-चरण कैसे लागू करें।
अपडेट किया गया 25 सित॰ 2026  · 8 मि॰ पढ़ें

AI के साथ खोजें

ChatGPTClaudePerplexity

रिट्रीवल-ऑगमेंटेड जेनरेशन (RAG) AI मॉडलों को बाहरी स्रोतों से अतिरिक्त ज्ञान शामिल करने में मदद करता है। हालांकि, ये सिस्टम अक्सर महत्वपूर्ण संदर्भ खो देते हैं, जिससे परिणाम कम सटीक हो सकते हैं।

Anthropic ने संदर्भात्मक रिट्रीवल विकसित किया है—जानकारी रिट्रीवल में सुधार करने का एक सरल, लेकिन प्रभावी तरीका। प्रत्येक chunk के लिए सही संदर्भ बनाए रखने से रिट्रीवल त्रुटियाँ 67% तक घटती हैं, जिससे डाउनस्ट्रीम कार्यों में प्रदर्शन काफी बेहतर होता है।

इस लेख में, मैं संदर्भात्मक रिट्रीवल समझाऊँगा और आप इसे अपनी एप्लिकेशन्स में कैसे उपयोग कर सकते हैं।

Contextual Retrieval क्या है?

पारंपरिक RAG सिस्टम प्रभावी होते हैं, लेकिन उनमें एक बड़ी कमी है: वे अक्सर दस्तावेज़ों को छोटे-छोटे chunks में बाँटते हैं ताकि रिट्रीवल आसान हो जाए, लेकिन इससे महत्वपूर्ण संदर्भ हट सकता है।

उदाहरण के लिए, कोई chunk यह कह सकता है “इसके 3.85 मिलियन से अधिक निवासी इसे यूरोपीय संघ का सबसे अधिक आबादी वाला शहर बनाते हैं” बिना यह बताए कि किस शहर या देश की बात हो रही है। संदर्भ की यह कमी, खासकर जब विशिष्ट विवरणों की आवश्यकता हो, तो अधूरे या अप्रासंगिक परिणाम दे सकती है।

standard RAG diagram

स्रोत: Introducing Contextual Retrieval

संदर्भात्मक रिट्रीवल इसे इस तरह ठीक करता है कि एम्बेडिंग से पहले हर chunk में एक छोटा, संदर्भ-विशिष्ट स्पष्टीकरण जनरेट कर जोड़ देता है। ऊपर दिए गए उदाहरण में, chunk इस प्रकार रूपांतरित होगा:

contextualized_chunk = """Berlin is the capital and largest city of Germany, known for being the EU's most populous city within its limits.
Its more than 3.85 million inhabitants make it the European Union's most populous city, as measured by population within city limits.
"""

contextual retrieval RAG diagram

स्रोत: Introducing Contextual Retrieval

कुल मिलाकर, कोडबेस, वैज्ञानिक पत्रों और फिक्शन सहित विभिन्न क्षेत्रों में Anthropic के आंतरिक परीक्षण दिखाते हैं कि संदर्भात्मक रिट्रीवल, संदर्भात्मक एम्बेडिंग मॉडलों और Contextual BM25 के साथ उपयोग करने पर रिट्रीवल त्रुटियों को 49% तक कम करता है।

contextual retrieval performance

स्रोत: Introducing Contextual Retrieval

Contextual Retrieval का इम्प्लीमेंटेशन

अब मैं चरण-दर-चरण समझाऊँगा कि संदर्भात्मक रिट्रीवल कैसे लागू करें। हम निम्नलिखित नमूना दस्तावेज़ को उदाहरण के रूप में उपयोग करेंगे:

# Input text for the knowledge base
input_text = """Berlin is the capital and largest city of Germany, both by area and by population.
Its more than 3.85 million inhabitants make it the European Union's most populous city, as measured by population within city limits.
The city is also one of the states of Germany and is the third smallest state in the country in terms of area.
Paris is the capital and most populous city of France.
It is situated along the Seine River in the north-central part of the country.
The city has a population of over 2.1 million residents within its administrative limits, making it one of Europe's major population centers."""

चरण 1: दस्तावेज़ को chunks में बाँटें

पहला कदम है नमूना दस्तावेज़ को छोटे, स्वतंत्र chunks में विभाजित करना। इस स्थिति में, हम इसे अलग-अलग वाक्यों में बाँटेंगे।

# Splitting the input text into smaller chunks
test_chunks = [
    'Berlin is the capital and largest city of Germany, both by area and by population.',
    "\\n\\nIts more than 3.85 million inhabitants make it the European Union's most populous city, as measured by population within city limits.",
    '\\n\\nThe city is also one of the states of Germany and is the third smallest state in the country in terms of area.',
    '\\n\\n# Paris is the capital and most populous city of France.',
    '\\n\\n# It is situated along the Seine River in the north-central part of the country.',
    "\\n\\n# The city has a population of over 2.1 million residents within its administrative limits, making it one of Europe's major population centers."
]

चरण 2: प्रॉम्प्ट टेम्पलेट परिभाषित करें

अगले चरण में, हम वह प्रॉम्प्ट सेट करेंगे जिसका उपयोग हमारा मॉडल प्रत्येक chunk के लिए संदर्भ जनरेट करने में करेगा। Anthropic ने इसके लिए उपयोगी टेम्पलेट दिया है। प्रॉम्प्ट दो इनपुट लेगा: पूरा दस्तावेज़ और वह विशिष्ट chunk जिसे हमें संदर्भ में स्थापित करना है।

from langchain.prompts import ChatPromptTemplate, PromptTemplate, HumanMessagePromptTemplate

# Define the prompt for generating contextual information
anthropic_contextual_retrieval_system_prompt = """<document>
{WHOLE_DOCUMENT}
</document>
Here is the chunk we want to situate within the whole document
<chunk>
{CHUNK_CONTENT}
</chunk>
Please give a short succinct context to situate this chunk within the overall document for the purposes of improving search retrieval of the chunk. Answer only with the succinct context and nothing else."""

# Create a PromptTemplate for WHOLE_DOCUMENT and CHUNK_CONTENT
anthropic_prompt_template = PromptTemplate(
    input_variables=['WHOLE_DOCUMENT', 'CHUNK_CONTENT'],
    template=anthropic_contextual_retrieval_system_prompt
)

# Wrap the prompt in a HumanMessagePromptTemplate
human_message_prompt = HumanMessagePromptTemplate(prompt=anthropic_prompt_template)
# Create the final ChatPromptTemplate
anthropic_contextual_retrieval_final_prompt = ChatPromptTemplate(
    input_variables=['WHOLE_DOCUMENT', 'CHUNK_CONTENT'],
    messages=[human_message_prompt]
)

चरण 3: अपनी पसंद का भाषा मॉडल प्रारंभ करें

यहाँ, आप संदर्भ जनरेट करने के लिए कोई भी LLM चुन सकते हैं। हमारे उदाहरण में, हम OpenAI के GPT-4o मॉडल का उपयोग करेंगे।

import os
from langchain_openai import ChatOpenAI

# Load environment variables
os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY"

# Initialize the model instance
llm_model_instance = ChatOpenAI(
    model="gpt-4o",
)

चरण 4: सब कुछ जोड़ें

हम प्रॉम्प्ट टेम्पलेट को भाषा मॉडल से जोड़ेंगे ताकि एक LLM चेन बने जो पहले बनाए गए chunks को प्रोसेस करने के लिए तैयार हो।

from langchain.output_parsers import StrOutputParser

# Chain the prompt with the model instance
contextual_chunk_creation = anthropic_contextual_retrieval_final_prompt | llm_model_instance | StrOutputParser()

चरण 5: प्रत्येक chunk प्रोसेस करें

अब हम अपने chunks पर लूप चलाएँगे, प्रत्येक के लिए संदर्भात्मक जानकारी जनरेट करेंगे, और contextual_chunk_creation LLM चेन का उपयोग करके परिणाम प्रिंट करेंगे।

# Process each chunk and generate contextual information
for test_chunk in test_chunks:
    res = contextual_chunk_creation.invoke({
        "WHOLE_DOCUMENT": input_text,
        "CHUNK_CONTENT": test_chunk
    })
    print(res)
    print('-----')
The document compares Berlin and Paris, highlighting Berlin as Germany's capital and largest city by area and population, and noting its significance within the European Union.
-----
Berlin is the capital and largest city of Germany, known for being the EU's most populous city within its limits.
-----
Berlin is the capital and largest city of Germany, both by area and by population.
-----
Paris, the capital of France, is a major population center in Europe with over 2.1 million residents.
-----
Paris is the capital and most populous city of France.
-----
Paris is the capital and most populous city of France, with a population of over 2.1 million residents within its administrative limits.
-----

Reranking का उपयोग करके सुधार

जहाँ संदर्भात्मक रिट्रीवल रिट्रीवल शुद्धता बढ़ाता है, वहीं आप इसे रिरैंकिंग के साथ और आगे ले जा सकते हैं। यह फ़िल्टरिंग तकनीक सुनिश्चित करती है कि केवल सबसे प्रासंगिक chunks ही मॉडल को भेजे जाएँ, जिससे उत्तरों की गुणवत्ता बेहतर होती है और लागत व प्रोसेसिंग समय घटता है।

रिरैंकिंग पहले चरण में प्राप्त chunks को स्कोर करती है और केवल सर्वोत्तम को रखती है। हमारे परीक्षणों में, रिरैंकिंग ने रिट्रीवल त्रुटियों को 5.7% से घटाकर मात्र 1.9% कर दिया, जो पारंपरिक तरीकों की तुलना में 67% शुद्धता सुधार है।

contextual retrieval with reranking performance

स्रोत: Introducing Contextual Retrieval

अतिरिक्त विचार

Anthropic के शोध में पाया गया है कि हर स्थिति में संदर्भात्मक रिट्रीवल जैसी जटिलता की आवश्यकता नहीं होती। 200,000 टोकन (लगभग 500 पेज) से छोटे नॉलेज बेस के लिए, बेहतर विकल्प हो सकता है कि पूरे नॉलेज बेस को सीधे मॉडल के प्रॉम्प्ट में शामिल कर दिया जाए, और रिट्रीवल सिस्टम की जरूरत को पूरी तरह छोड़ दिया जाए।

वे प्रॉम्प्ट कैशिंग का लाभ उठाने की भी सलाह देते हैं, जो Claude के लिए पेश की गई सुविधा है। API कॉल्स के बीच अक्सर उपयोग होने वाले प्रॉम्प्ट्स को कैश करके, डेवलपर्स लागत को 90% तक घटा सकते हैं और प्रतिक्रिया समय को दो गुने से अधिक तेज़ कर सकते हैं। यह prompt caching cookbook इसे लागू करने के लिए चरण-दर-चरण मार्गदर्शन प्रदान करता है।

निष्कर्ष

Anthropic का संदर्भात्मक रिट्रीवल रिट्रीवल सिस्टम्स को बेहतर बनाने की एक सरल लेकिन प्रभावी विधि है। संदर्भात्मक एम्बेडिंग्स, BM25 और रिरैंकिंग को मिलाने से इन सिस्टम्स की शुद्धता में उल्लेखनीय सुधार होता है।

विभिन्न रिट्रीवल तकनीकों के बारे में और जानने के लिए, मैं निम्नलिखित ब्लॉग्स की सिफारिश करता हूँ:

  1. Corrective RAG (CRAG) का इम्प्लीमेंटेशन LangGraph के साथ
  2. RAG के लिए Recursive Retrieval: LlamaIndex के साथ इम्प्लीमेंटेशन

Ryan Ong's photo
Author
Ryan Ong
LinkedIn
Twitter

रायन एक लीड डेटा साइंटिस्ट हैं जो LLMs का उपयोग करके एआई अनुप्रयोग बनाने में विशेषज्ञता रखते/रखती हैं। वे इम्पीरियल कॉलेज लंदन में नैचुरल लैंग्वेज प्रोसेसिंग और नॉलेज ग्राफ्स में पीएचडी उम्मीदवार हैं, जहाँ से उन्होंने कंप्यूटर साइंस में अपना मास्टर’स भी पूरा किया है। डेटा साइंस के अलावा, वे साप्ताहिक Substack न्यूज़लेटर The Limitless Playbook लिखते हैं, जिसमें वे दुनिया के अग्रणी विचारकों से एक क्रियान्वयन योग्य विचार साझा करते हैं और समय-समय पर मूलभूत एआई अवधारणाओं पर भी लिखते हैं।

विषय
कृत्रिम बुद्धिमत्ता
बड़े भाषा मॉडल

इन कोर्सेज़ के साथ AI सीखें!

कोर्स

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

3 घंटा
20.9K
LangChain के साथ Retrieval Augmented Generation (RAG) का उपयोग करके बाहरी डेटा को LLMs के साथ एकीकृत करने की अत्याधुनिक विधियाँ सीखें।
विवरण देखेंRight Arrow
पाठ्यक्रम शुरू करें
और देखेंRight Arrow