Retrieval Augmented Generation (RAG) कैसे काम करता है?

LlamaIndex प्रलेखन से छवि
Retrieval Augmented Generation (RAG) पाइपलाइनों में तीन चरण होते हैं: Indexing, Retrieval, और Generation।
Indexing
LLMs के साथ सटीक और संदर्भ-सम्मत उत्तर प्राप्त करने के लिए Indexing बुनियादी है। पहले, यह विभिन्न फ़ाइल फ़ॉर्मैट्स, जैसे Word Documents, PDF फ़ाइलें या HTML फ़ाइलें, से डेटा निकालने और साफ़ करने से शुरू होता है। डेटा साफ़ करने के बाद, इसे मानकीकृत सादा पाठ में बदला जाता है। LLMs के भीतर संदर्भ सीमाओं से बचने के लिए, पाठ को छोटे-छोटे हिस्सों में बाँटा जाता है। इस प्रक्रिया को Chunking कहते हैं। इसके बाद, प्रत्येक chunk को एक संख्यात्मक वेक्टर या embedding में बदला जाता है, जिसमें एक embedding मॉडल का उपयोग होता है। अंततः, एक इंडेक्स बनाया जाता है जो chunks और उनके संबंधित embeddings को key-value जोड़ों के रूप में स्टोर करता है।
Retrieval
Retrieval चरण के दौरान, उपयोगकर्ता क्वेरी को भी उसी embedding मॉडल का उपयोग करके एक वेक्टर प्रतिनिधित्व में बदला जाता है। फिर, क्वेरी वेक्टर और वेक्टराइज्ड chunks के बीच समानता स्कोर निकाले जाते हैं। सिस्टम उपयोगकर्ता क्वेरी से सर्वाधिक समानता रखने वाले शीर्ष K chunks को प्राप्त करता है।
Generation
उपयोगकर्ता क्वेरी और प्राप्त किए गए chunks को एक प्रॉम्प्ट टेम्पलेट में दिया जाता है। पिछले चरणों से प्राप्त संवर्धित प्रॉम्प्ट को अंत में LLM में इनपुट के रूप में दिया जाता है।
RAG की सीमाएँ
RAG सिस्टम बनाते समय, ऊपर बताए गए तीनों चरणों में आपको कुछ उल्लेखनीय चुनौतियाँ मिल सकती हैं, जैसे:
- Indexing: RAG की प्रभावशीलता डेटा की गुणवत्ता पर बहुत निर्भर करती है। यदि बाहरी डेटा स्रोत सूचनात्मक होने के बजाय अधिक शोरपूर्ण है, तो LLM द्वारा उत्पन्न उत्तर उपयोगी नहीं होंगे।
- Retrieval: आपकी फ़ाइल प्रकारों और RAG सिस्टम की सेटिंग्स पर निर्भर करते हुए, सिस्टम हमेशा ऐसे chunks नहीं लाता जो उपयोगकर्ता क्वेरी का उत्तर देने में सहायक हों।
- Generation: भले ही RAG सिस्टम प्रभावी ढंग से सेट किया गया हो, LLM अब भी कल्पित तथ्यों के आधार पर गलत उत्तर जनरेट कर सकता है।
आगे, हम इन सीमाओं को दरकिनार करने की रणनीतियों का पता लगाएंगे। हम तीन व्यापक रणनीतियों पर चर्चा करेंगे: Chunking, Re-Ranking, और Query Transformations। साथ ही, हम दिखाएँगे कि कैसे ये रणनीतियाँ Llamaindex और OpenAI API का उपयोग करके Wikipedia प्रविष्टियों से संबंधित प्रश्नों के उत्तर देने वाला एक RAG सिस्टम बनाकर RAG प्रदर्शन में सुधार करने में मदद कर सकती हैं। आइए गहराई में जाएँ!
बेसलाइन RAG सिस्टम सेट करना
RAG सिस्टम के प्रदर्शन में सुधार करने की तरकीबों का पता लगाने से पहले, हमें एक सरल RAG पाइपलाइन बनाकर बेसलाइन प्रदर्शन स्थापित करना होगा। आइए llama-index और openai पैकेज इंस्टॉल करने से शुरू करें।
!pip install llama-index
!pip install openai
Llamaindex Large Language Models (LLMs) पर आधारित अनुप्रयोगों के लिए एक डेटा फ्रेमवर्क है। यह विभिन्न प्रकार के बाहरी डेटा स्रोतों को इन्गेस्ट करने, Retrieval Augmentation Generation (RAG) सिस्टम बनाने, और अन्य LLMs के साथ इंटीग्रेशन को कुछ पंक्तियों के कोड में एब्स्ट्रैक्ट करने में सक्षम बनाता है। साथ ही, यह LLMs के साथ अधिक सटीक आउटपुट जनरेट करने के लिए कई तकनीकें प्रदान करता है।
डिफ़ॉल्ट रूप से, यह टेक्स्ट जनरेशन के लिए OpenAI के gpt-3.5-turbo मॉडल और retrieval व embeddings के लिए text-embedding-ada-002 मॉडल का उपयोग करता है। इन मॉडलों का उपयोग करने के लिए, OpenAI प्लेटफ़ॉर्म पर एक निःशुल्क खाता बनाना और OpenAI API कुंजी प्राप्त करना आवश्यक है। कुंजी प्राप्त करने के लिए, कृपया OpenAI API प्रलेखन देखें।
import os
os.environ["OPENAI_API_KEY"] = "YOUR_OPENAI_API_KEY"
अब जब हमारे पैकेज इंस्टॉल हो गए हैं, हम एक RAG सिस्टम बनाएँगे जो Emma Stone, Ryan Gosling और La La Land की Wikipedia पेजों के आधार पर प्रश्नों के उत्तर देता है। सबसे पहले, हमें Wikipedia पेज निकालने के लिए wikipedia लाइब्रेरी इंस्टॉल करनी होगी:
!pip install wikipedia
इसके बाद, हम Wikipedia से डेटा आसानी से डाउनलोड कर सकते हैं:
# Import packages
from llama_index.core import (VectorStoreIndex,ServiceContext, download_loader)
# Download the documents from Wikipedia and load them
WikipediaReader = download_loader("WikipediaReader")
loader = WikipediaReader()
pages = ['Emma_Stone', 'La_La_Land', 'Ryan_Gosling']
documents = loader.load_data(pages=pages, auto_suggest=False, redirect = False)
डेटा निकाल लेने के बाद, हम दस्तावेज़ों को बिना किसी ओवरलैप के 256 अक्षरों के chunks में बाँट सकते हैं। बाद में, इन chunks को embedding मॉडल का उपयोग करके संख्यात्मक वेक्टरों में बदला जाता है और एक Vector Store में इंडेक्स किया जाता है।
# Initialize the gpt3.5 model
gpt3 = OpenAI(temperature=0, model="gpt-3.5-turbo-instruct", api_key=OPENAI_API_KEY)
# Initialize the embedding model
embed_model = OpenAIEmbedding(model= OpenAIEmbeddingModelType.TEXT_EMBED_ADA_002, api_key=OPENAI_API_KEY)
# Transform chunks into numerical vectors using the embedding model
service_context_gpt3 = ServiceContext.from_defaults(llm=gpt3, chunk_size = 256, chunk_overlap=0, embed_model=embed_model)
index = VectorStoreIndex.from_documents(documents, service_context=service_context_gpt3)
retriever = index.as_retriever(similarity_top_k=3)
हैलुसिनेशन का जोखिम कम करने के लिए, हम PromptTemplate मॉड्यूल का उपयोग करते हैं ताकि LLM के उत्तर केवल दिए गए संदर्भ पर आधारित हों।
from llama_index.core.prompts import PromptTemplate
# Build a prompt template to only provide answers based on the loaded documents
template = (
"We have provided context information below. \n"
"---------------------\n"
"{context_str}"
"\n---------------------\n"
"Given this information, please answer the question: {query_str}\n"
"Don't give an answer unless it is supported by the context above.\n"
)
qa_template = PromptTemplate(template)
अब जब हमने अपना RAG सिस्टम सेट कर लिया है, तो हम प्राप्त दस्तावेज़ों पर आधारित प्रश्नों के साथ इसका परीक्षण कर सकते हैं। आइए इसे आज़माएँ!
क्वेरी 1: “उस फ़िल्म की कहानी क्या है जिसने Emma Stone को उनका पहला Academy Award जिताया?”
यह पहली क्वेरी चुनौतीपूर्ण है क्योंकि इसमें मॉडल को अलग-अलग सूचनाएँ देखनी पड़ती हैं:
- उस फ़िल्म का नाम जिसने Emma Stone को पहला Academy Award जिताया (जो इस मामले में La La Land है)
- उस विशेष फ़िल्म की कहानी।
# Create a prompt for the model
question = "What is the plot of the film that led Emma Stone to win her first Academy Award?"
# Retrieve the context from the model
contexts = retriever.retrieve(question)
context_list = [n.get_content() for n in contexts]
prompt = qa_template.format(context_str="\n\n".join(context_list), query_str=question)
# Generate the response
response = gpt3.complete(prompt)
print(str(response))
आउटपुट:
The plot of the film that made Emma Stone win her first Academy Award is not explicitly mentioned in the provided context.
क्वेरी 2: "Emma Stone और Ryan Gosling के परिवारों की तुलना करें”
यह दूसरी क्वेरी पिछली क्वेरी से भी अधिक चुनौतीपूर्ण है क्योंकि इसमें दोनों अभिनेताओं के परिवारों से संबंधित प्रासंगिक chunks चुनने की माँग है।
# Create a prompt for the model
question = "Compare the families of Emma Stone and Ryan Gosling"
# Retrieve the context from the model
contexts = retriever.retrieve(question)
context_list = [n.get_content() for n in contexts]
prompt = qa_template.format(context_str="\n\n".join(context_list), query_str=question)
# Generate the response
response = gpt3.complete(prompt)
print(str(response))
हमें निम्नलिखित आउटपुट प्राप्त होता है:
Based on the context provided, it is not possible to compare the families of Emma Stone and Ryan Gosling as the information focuses on their professional collaboration and experiences while working on the film "La La Land." There is no mention of their personal family backgrounds or relationships in the context provided.
जैसा कि आप देख सकते हैं, दोनों ही मामलों में हमें औसत से कम उत्तर मिले हैं। अगली धाराओं में, आइए इस RAG सिस्टम के प्रदर्शन को बेहतर बनाने के तरीके खोजें!
Chunk Size अपडेट करके RAG प्रदर्शन में सुधार
हम chunk size और chunk overlap को कस्टमाइज़ करके शुरू कर सकते हैं। जैसा कि ऊपर कहा गया, दस्तावेज़ों को एक विशिष्ट ओवरलैप के साथ chunks में बाँटा जाता है। डिफ़ॉल्ट रूप से, LlamaIndex 1024 को डिफ़ॉल्ट chunk size और 20 को डिफ़ॉल्ट chunk overlap के रूप में उपयोग करता है। इन हाइपरपैरामीटर्स के अलावा, सिस्टम डिफ़ॉल्ट रूप से शीर्ष 2 chunks को रिट्रीव करता है।
उदाहरण के लिए, हम chunk size को 512, chunk overlap को 50 तय कर सकते हैं और शीर्ष रिट्रीव किए गए chunks की संख्या बढ़ा सकते हैं:
# modify default values of chunk size and chunk overlap
service_context_gpt3 = ServiceContext.from_defaults(llm=gpt3, chunk_size = 512, chunk_overlap=50, embed_model=embed_model)
# build index
index = VectorStoreIndex.from_documents(
documents, service_context=service_context_gpt3
)
# returns the engine for the index
query_engine = index.as_query_engine(similarity_top_k=4)
क्वेरी 1: “उस फ़िल्म की कहानी क्या है जिसने Emma Stone को उनका पहला Academy Award जिताया?”
# generate the response
response = query_engine.query("What is the plot of the film that led Emma Stone to win her first Academy Award?")
print(response)
आउटपुट:
The film that made Emma Stone win her first Academy Award is a romantic musical called La La Land.
पिछले उत्तर की तुलना में, यह थोड़ा बेहतर है। इसने सफलतापूर्वक La La Land को वह फ़िल्म पहचाना, जिसने Emma Stone को पहला Academy Award जिताया, लेकिन यह फ़िल्म की कहानी का वर्णन नहीं कर पाया।
क्वेरी 2: "Emma Stone और Ryan Gosling के परिवारों की तुलना करें”
# generate the response
response = query_engine.query("Compare the families of Emma Stone and Ryan Gosling")
print(response)
आउटपुट:
Emma Stone has expressed her close relationship with her family and mentioned being blessed with great family and people around her. She has also shared about her mother's battle with breast cancer and their celebration by getting matching tattoos. On the other hand, there is no specific information provided about Ryan Gosling's family or his personal relationships in the context.
फिर से, RAG पाइपलाइन का आउटपुट बेहतर हुआ, लेकिन यह अब भी Ryan Gosling के परिवार के बारे में जानकारी नहीं पकड़ता।
Re-Ranking के साथ RAG प्रदर्शन में सुधार
जैसे-जैसे डेटा सेट आकार और जटिलता में बढ़ता है, जटिल क्वेरियों के अनुरूप उत्तर लौटाने के लिए प्रासंगिक जानकारी का चयन करना महत्वपूर्ण हो जाता है। इस उद्देश्य के लिए, Re-Ranking नामक तकनीकों का एक परिवार आपको पाठ के भीतर कौन से chunks महत्वपूर्ण हैं, यह समझने में सक्षम बनाता है। वे दस्तावेज़ों को पुन: क्रमित और फ़िल्टर करते हैं, सबसे प्रासंगिक दस्तावेज़ों को पहले रैंक करते हैं।
Re-Ranking के दो मुख्य दृष्टिकोण हैं:
- Re-Ranking मॉडल्स का उपयोग, जो embedding मॉडलों के वैकल्पिक रूप में काम करते हैं। ये क्वेरी और संदर्भ को इनपुट लेते हैं और embeddings के बजाय समानता स्कोर लौटाते हैं
- LLMs का उपयोग करके दस्तावेज़ों के भीतर निहित सार्थक जानकारी को अधिक दक्षता से कैप्चर करना।
इन Re-Ranking दृष्टिकोणों को लागू करने से पहले, आइए मूल्यांकन करें कि हमारी दूसरी क्वेरी के लिए बेसलाइन RAG सिस्टम शीर्ष तीन chunks के रूप में क्या लौटाता है:
# Retrieve the top three chunks for the second query
retriever = index.as_retriever(similarity_top_k=3)
query = "Compare the families of Emma Stone and Ryan Gosling"
nodes = retriever.retrieve(query)
# Print the chunks
for node in nodes:
print('----------------------------------------------------')
display_source_node(node, source_length = 500)
यह Re-Ranking से पहले का आउटपुट है; प्रत्येक chunk के पास एक Node ID और एक समानता स्कोर है।
Node ID: 9b3817fe-3a3f-4417-83d2-2e2996c8b468
Similarity: 0.8415899563985404
Text: Emily Jean "Emma" Stone (born November 6, 1988) is an American actress and producer. She is the recipient of various accolades, including two Academy Awards, two British Academy Film Awards, and two Golden Globe Awards. In 2017, she was the world's highest-paid actress and named by Time magazine as one of the 100 most influential people in the world. Born and raised in Scottsdale, Arizona, Stone began acting as a child in a theater production of The Wind in the Willows in 2000. As a teenager,...
----------------------------------------------------
Node ID: 1bef0308-8b0f-4f7e-9cd6-92ce5acf811f
Similarity: 0.831147173341674
Text: Coincidentally, Gosling turned down the Beast role in Beauty and the Beast in favor of La La Land. Chazelle subsequently decided to make his characters somewhat older, with experience in struggling to make their dreams, rather than younger newcomers just arriving in Los Angeles. Emma Stone plays Mia, an aspiring actress in Los Angeles. Stone has loved musicals since she saw Les Misérables when she was eight years old. She said "bursting into song has always been a real dream of mine", and her ...
----------------------------------------------------
Node ID: 576ae445-b12e-4d20-99b7-5e5a91ee7d74
Similarity: 0.8289486590392277
Text: Stone was named the best-dressed woman of 2012 by Vogue and was included on similar listings by Glamour in 2013 and 2015, and People in 2014.
== Personal life == Stone moved from Los Angeles to Greenwich Village, New York, in 2009. In 2016, she moved back to Los Angeles. Despite significant media attention, she refuses to publicly discuss her personal life. Concerned with living a normal life, Stone has said she dislikes receiving paparazzi attention outside her home. She has expressed her ...
FlagEmbeddingReranker के साथ Re-Ranking
प्रासंगिक chunks प्राप्त करने के लिए, हम Hugging Face का एक ओपन-सोर्स Re-Ranking मॉडल, bge-ranker-base मॉडल, का उपयोग कर सकते हैं।
OpenAI API की तरह ही, Hugging Face का उपयोग करने के लिए आपको एक user access token प्राप्त करना होगा। आप इस प्रलेखन का पालन करके Hugging Face से user access token बना सकते हैं।
HF_TOKEN = userdata.get('HF_TOKEN')
os.environ['HF_TOKEN'] = HF_TOKEN
आगे बढ़ने से पहले, Re-Ranking मॉडल का उपयोग करने के लिए आवश्यक लाइब्रेरियाँ इंस्टॉल करनी होंगी:
%pip install llama-index-postprocessor-flag-embedding-reranker
!pip install git+https://github.com/FlagOpen/FlagEmbedding.git
अंत में, हम सबसे प्रासंगिक chunks लौटाने के लिए bge-ranker-base मॉडल का उपयोग करते हैं।
# Import packages
from llama_index.postprocessor.flag_embedding_reranker import FlagEmbeddingReranker
from llama_index.core.schema import QueryBundle
# Re-Rank chunks based on the bge-reranker-base-model
reranker = FlagEmbeddingReranker(
top_n = 3,
model = "BAAI/bge-reranker-base",
)
# Return the updated chunks
query_bundle = QueryBundle(query_str=query)
ranked_nodes = reranker._postprocess_nodes(nodes, query_bundle = query_bundle)
for ranked_node in ranked_nodes:
print('----------------------------------------------------')
display_source_node(ranked_node, source_length = 500)
यह Re-Ranking के बाद का परिणाम है:
Node ID: 9b3817fe-3a3f-4417-83d2-2e2996c8b468
Similarity: 3.0143558979034424
Text: Emily Jean "Emma" Stone (born November 6, 1988) is an American actress and producer. She is the recipient of various accolades, including two Academy Awards, two British Academy Film Awards, and two Golden Globe Awards. In 2017, she was the world's highest-paid actress and named by Time magazine as one of the 100 most influential people in the world. Born and raised in Scottsdale, Arizona, Stone began acting as a child in a theater production of The Wind in the Willows in 2000. As a teenager,...
----------------------------------------------------
Node ID: 576ae445-b12e-4d20-99b7-5e5a91ee7d74
Similarity: 2.2117154598236084
Text: Stone was named the best-dressed woman of 2012 by Vogue and was included on similar listings by Glamour in 2013 and 2015, and People in 2014.
== Personal life == Stone moved from Los Angeles to Greenwich Village, New York, in 2009. In 2016, she moved back to Los Angeles. Despite significant media attention, she refuses to publicly discuss her personal life. Concerned with living a normal life, Stone has said she dislikes receiving paparazzi attention outside her home. She has expressed her ...
----------------------------------------------------
Node ID: 1bef0308-8b0f-4f7e-9cd6-92ce5acf811f
Similarity: 1.6185210943222046
Text: Coincidentally, Gosling turned down the Beast role in Beauty and the Beast in favor of La La Land. Chazelle subsequently decided to make his characters somewhat older, with experience in struggling to make their dreams, rather than younger newcomers just arriving in Los Angeles.Emma Stone plays Mia, an aspiring actress in Los Angeles. Stone has loved musicals since she saw Les Misérables when she was eight years old. She said "bursting into song has always been a real dream of mine", and her ...
आउटपुट से स्पष्ट है कि 1bef0308-8b0f-4f7e-9cd6-92ce5acf811f आईडी वाला नोड दूसरी स्थिति से तीसरी पर आ गया है। साथ ही, यह भी ध्यान देने योग्य है कि समानता स्कोरों में अधिक विविधता है।
अब जब हमने Re-Ranking का उपयोग कर लिया है, तो देखें कि मूल क्वेरी के लिए RAG का उत्तर अब कैसा दिखता है:
# Initialize the query engine with Re-Ranking
query_engine = index.as_query_engine(
similarity_top_k = 3,
node_postprocessors=[reranker]
)
# Print the response from the model
response = query_engine.query("Compare the families of Emma Stone and Ryan Gosling")
print(response)
Re-Ranking मॉडल लागू करने के बाद यह उत्तर मिला:
Both Emma Stone and Ryan Gosling have close relationships with their families. Stone has expressed her gratitude for having a great family and people around her who keep her grounded. Gosling, on the other hand, has drawn from his own experiences as an aspiring artist, indicating a connection to his personal background.
पहले के उत्तरों की तुलना में यहाँ उल्लेखनीय सुधार है, लेकिन यह अभी भी अपूर्ण है। आइए देखें कि LLM-आधारित Re-Ranking दृष्टिकोण RAG प्रदर्शन को कैसे और सुधार सकता है।
RankGPTRerank के साथ Re-Ranking
इस बार, हम एक LLM पर Re-Ranker के रूप में भरोसा कर सकते हैं। यहाँ, हम RankGPT मॉड्यूल का उपयोग करते हैं, जो RAG सिस्टम के भीतर दस्तावेज़ों को रैंक करने के लिए GPT मॉडल की क्षमताओं का लाभ उठाता है। शुरुआत करने के लिए, आवश्यक पैकेज इंस्टॉल करें:
%pip install llama-index-postprocessor-rankgpt-rerank
हम RankGPTRerank फ़ंक्शन का उपयोग करके gpt-3.5-turbo-0125 मॉडल से chunks को Re-Rank करते हैं।
# Import packages
from llama_index.postprocessor.rankgpt_rerank import RankGPTRerank
# Re-Rank the top 3 chunks based on the gpt-3.5-turbo-0125 model
reranker = RankGPTRerank(
top_n = 3,
llm = OpenAI(model="gpt-3.5-turbo-0125"),
)
# Display the top 3 chunks based on RankGPT
query_bundle = QueryBundle(query_str=query)
ranked_nodes = reranker._postprocess_nodes(nodes, query_bundle = query_bundle)
for ranked_node in ranked_nodes:
print('----------------------------------------------------')
display_source_node(ranked_node, source_length = 500)
RankGPT का उपयोग करके, हमें निम्नलिखित chunks मिलते हैं:
----------------------------------------------------
Node ID: 1bef0308-8b0f-4f7e-9cd6-92ce5acf811f
Similarity: 1.6185210943222046
Text: Coincidentally, Gosling turned down the Beast role in Beauty and the Beast in favor of La La Land. Chazelle subsequently decided to make his characters somewhat older, with experience in struggling to make their dreams, rather than younger newcomers just arriving in Los Angeles.Emma Stone plays Mia, an aspiring actress in Los Angeles. Stone has loved musicals since she saw Les Misérables when she was eight years old. She said "bursting into song has always been a real dream of mine", and her ...
----------------------------------------------------
Node ID: 9b3817fe-3a3f-4417-83d2-2e2996c8b468
Similarity: 3.0143558979034424
Text: Emily Jean "Emma" Stone (born November 6, 1988) is an American actress and producer. She is the recipient of various accolades, including two Academy Awards, two British Academy Film Awards, and two Golden Globe Awards. In 2017, she was the world's highest-paid actress and named by Time magazine as one of the 100 most influential people in the world. Born and raised in Scottsdale, Arizona, Stone began acting as a child in a theater production of The Wind in the Willows in 2000. As a teenager,...
----------------------------------------------------
Node ID: 576ae445-b12e-4d20-99b7-5e5a91ee7d74
Similarity: 2.2117154598236084
Text: Stone was named the best-dressed woman of 2012 by Vogue and was included on similar listings by Glamour in 2013 and 2015, and People in 2014.
== Personal life == Stone moved from Los Angeles to Greenwich Village, New York, in 2009. In 2016, she moved back to Los Angeles. Despite significant media attention, she refuses to publicly discuss her personal life. Concerned with living a normal life, Stone has said she dislikes receiving paparazzi attention outside her home. She has expressed her ...
चूँकि RankGPT पॅसेजों को समानता स्कोर के आधार पर क्रमबद्ध नहीं करता, इसलिए आउटपुट में प्रस्तुत स्कोर अपडेट नहीं होते। आउटपुट से हम देख सकते हैं कि पहला नोड, जिसे सबसे प्रासंगिक माना गया है, Emma Stone और Ryan Gosling दोनों का उल्लेख करता है, जबकि शेष नोड्स में Emma Stone के जीवन के बारे में अधिक विवरण हैं।
अब, यदि हम इस Re-Ranking पद्धति के साथ मॉडल का उत्तर प्रिंट करें, तो हमें निम्नलिखित आउटपुट मिलता है:
Both Emma Stone and Ryan Gosling have close relationships with their families. Stone has expressed gratitude for having a great family and supportive people around her, while Gosling has drawn from his own experiences as an aspiring artist, indicating a connection to his family as well. Stone's family has played a significant role in her life, supporting her career pursuits and celebrating important milestones with her. Similarly, Gosling's experiences as an actor have likely been influenced by his family dynamics and relationships.
यह उत्तर पहले वाले Re-Ranking तकनीक की तुलना में लंबा है और अधिक विवरण देता है।
Query Transformations के साथ RAG प्रदर्शन में सुधार
Query transformation, जिसे query rewriting भी कहते हैं, एक ऐसा तरीका है जिसमें कच्ची क्वेरी का उपयोग करके शीर्ष k chunks रिट्रीव करने के बजाय क्वेरी को किसी अन्य क्वेरी में बदला जाता है। हम निम्नलिखित तकनीकों का अन्वेषण करेंगे:
- HyDE: जहाँ हम उपयोगकर्ता क्वेरियों और उनके काल्पनिक उत्तरों को embeddings में बदलते हैं ताकि वेक्टर स्पेस में निकटता से मेल खाते दस्तावेज़ों को प्राप्त किया जा सके।
- Multi-Step Query Transformations: जहाँ हम जटिल उपयोगकर्ता क्वेरी को क्रमिक उप-प्रश्नों में विभाजित करते हैं ताकि प्रभावी प्रोसेसिंग हो सके।
HyDE के साथ RAG प्रदर्शन में सुधार
HyDE का अर्थ है Hypothetical Document Embeddings। यह दो चरणों में होता है। पहले, यह उपयोगकर्ता क्वेरी का एक काल्पनिक उत्तर बनाता है। एक बार काल्पनिक उत्तर/दस्तावेज़ तय हो जाने के बाद, उत्तर और क्वेरी को embeddings में बदला जाता है। फिर, सिस्टम वे दस्तावेज़ रिट्रीव करता है जो वेक्टर स्पेस में इन embeddings के सबसे निकट होते हैं। आइए इसे क्रियान्वयन में देखें!
# Import packages
from llama_index.core.indices.query.query_transform import HyDEQueryTransform
from llama_index.core.query_engine.transform_query_engine import (
TransformQueryEngine,
)
# build index and query engine for the index
index = VectorStoreIndex.from_documents(documents, service_context=service_context_gpt3)
query_engine = index.as_query_engine(similarity_top_k=4)
# HyDE setup
hyde = HyDEQueryTransform(include_original=True)
# Transform the query engine using HyDE
hyde_query_engine = TransformQueryEngine(query_engine, hyde)
# Print the response from the model
response = hyde_query_engine.query("Compare the families of Emma Stone and Ryan Gosling")
print(response)
यह आउटपुट है:
Both Emma Stone and Ryan Gosling come from diverse family backgrounds. Ryan Gosling's parents were of part French Canadian descent, along with some German, English, Scottish, and Irish. He grew up in a family that practiced the Church of Jesus Christ of Latter-day Saints. On the other hand, Emma Stone was born and raised in Scottsdale, Arizona, without specific details provided about her family's ethnic background or religious affiliation.
Re-Ranking विधियों की तुलना में, हम आंशिक सुधार देखते हैं। पहले, आउटपुट अधिकतर अभिनेताओं के अपने परिवारों से संबंधों पर केंद्रित थे। अब, वे उत्पत्ति पर अधिक केंद्रित हैं।
Multi-Step Query Transformations के साथ RAG प्रदर्शन में सुधार
Multi-step query transformation दृष्टिकोण उपयोगकर्ता की क्वेरी को क्रमिक उप-प्रश्नों में बाँटता है। जटिल प्रश्नों पर काम करते समय यह तरीका मददगार हो सकता है।
जैसा कि हमने देखा है, दो तथ्यों की तुलना करते समय LLMs अक्सर संघर्ष करते हैं, भले ही वे केवल किसी एक विशेष विषय पर पूछे गए प्रश्न का सही उत्तर दे सकें। यहाँ, हम multi-step query transformations को क्रियान्वयन में देखते हैं।
# Multi-step query setup
step_decompose_transform_gpt3 = StepDecomposeQueryTransform(gpt3, verbose=True)
index_summary = "Breaks down the initial query"
# Return query engine for the index
multi_step_query_engine = MultiStepQueryEngine(
query_engine=query_engine,
query_transform=step_decompose_transform_gpt3,
index_summary=index_summary
)
# print the response from the model
response = multi_step_query_engine.query("Compare the families of Emma Stone and Ryan Gosling")
हम मध्यवर्ती प्रश्न देख सकते हैं जो हमें अंतिम आउटपुट तक पहुँचने में सक्षम बनाते हैं:
> Current query: Compare the families of Emma Stone and Ryan Gosling
> New query: What is the family background of Emma Stone and Ryan Gosling?
> Current query: Compare the families of Emma Stone and Ryan Gosling
> New query: What is the specific family background of Emma Stone?
> Current query: Compare the families of Emma Stone and Ryan Gosling
> New query: What is Emma Stone's relationship with her family like?
उपयोगकर्ता क्वेरी के लिए निम्नलिखित उत्तर है:
Both Ryan Gosling and Emma Stone have strong family connections, with Emma Stone emphasizing the importance of her family in keeping her grounded. Ryan Gosling's family background includes a mix of French Canadian, German, English, Scottish, and Irish heritage.
इस मामले में, Emma Stone और Ryan Gosling के परिवारों की तुलना करने के लिए multi-step query transformation का उपयोग सहायक रहा।
निष्कर्ष
इस लेख में, आपने RAG प्रदर्शन में सुधार करने की विभिन्न तकनीकें सीखी हैं। चाहे वह Chunking हो, Re-Ranking हो, या Query Transformations, सर्वोत्तम दृष्टिकोण प्रायः उपयोग केस और किसी दी गई क्वेरी के लिए आपके वांछित आउटपुट पर निर्भर करता है।
यदि आप RAG में गहराई तक जाना चाहते हैं, तो हम हमारे कोड-अलॉन्ग देखें: Retrieval Augmented Generation with LlamaIndex और Retrieval Augmented Generation with GPT and Milvus। ये वीडियो दिखाते हैं कि विभिन्न उपयोग मामलों में retrieval augmented generation का उपयोग करके LLMs को वेक्टर डेटाबेस के साथ कैसे जोड़ा जाए।

डेटा वैज्ञानिक - CRIF
Retrieval Augmented Generation (RAG) बड़े भाषा मॉडल्स (LLMs) की सटीकता और विश्वसनीयता बढ़ाने की सबसे लोकप्रिय तकनीकों में से एक है। यह बाहरी डेटा स्रोतों से अतिरिक्त जानकारी प्रदान करके संभव होता है। इस तरह, उत्तरों को विशिष्ट संदर्भों के अनुरूप बनाया जा सकता है और बिना फाइन-ट्यूनिंग व री-ट्रेनिंग के अपडेट किया जा सकता है।
RAG की लोकप्रियता के बावजूद, यह हर संदर्भ में अच्छी तरह काम नहीं करता। इस लेख में, हम चरण-दर-चरण समझाएँगे कि RAG पाइपलाइनों का प्रभावी निर्माण कैसे करें, RAG की सीमाएँ क्या हैं, और इन सीमाओं को कैसे दूर किया जा सकता है। आइए शुरू करें!