पाठ 21 / 31
Query Engines और Response Synthesis
Nodes retrieve करके और उत्तर संश्लेषित करके सवालों के जवाब दें।
Retrieve करें, फिर संश्लेषित करें
index.as_query_engine() retrieval और response synthesizer को लपेटता है: वह शीर्ष nodes retrieve करता है, उनसे prompt बनाता है और LLM से उत्तर माँगता है। लौटे Response में source_nodes होते हैं ताकि आप citations दिखा सकें। Response modes तय करते हैं कि संदर्भ कैसे मिलाया जाए: compact जितना समाए ठूँसता है, refine node-दर-node उत्तर सुधारता है, tree_summarize पदानुक्रमित सार करता है (कई nodes के सार के लिए अच्छा)। Retrieval और synthesis के बीच node postprocessors (similarity cutoff, reranker, metadata filters) जोड़ सकते हैं, और memory सहित बहु-turn chat के लिए as_chat_engine() उपयोग कर सकते हैं।
Mock models के साथ query engine, चलाकर
मैंने यह Python virtual environment में langchain-core 1.6.6, langchain-text-splitters 1.1.2 और llama-index-core 0.14.25 के साथ ऑफ़लाइन चलाया। किसी API key या network call की ज़रूरत नहीं क्योंकि असली मॉडल की जगह fake मॉडल या खिलौना embedding है। MockLLM और MockEmbedding engine को ऑफ़लाइन चलने देते हैं, इसलिए उत्तर का पाठ placeholder है, पर ढाँचा असली है: नतीजा Response ऑब्जेक्ट है जिसके एक source node में नीति पाठ है।
from llama_index.core import Document, VectorStoreIndex, Settings
from llama_index.core.embeddings import MockEmbedding
from llama_index.core.llms import MockLLM
Settings.llm = MockLLM(max_tokens=20)
Settings.embed_model = MockEmbedding(embed_dim=8)
index = VectorStoreIndex.from_documents([Document(text="Employees get 24 days of paid leave per year.")])
engine = index.as_query_engine(similarity_top_k=1)
response = engine.query("How many leave days?")
print(type(response).__name__)
print("source nodes:", len(response.source_nodes))
print(response.source_nodes[0].node.get_content())
Output:
Response source nodes: 1 Employees get 24 days of paid leave per year.
त्वरित जाँच: Response में source_nodes क्यों शामिल हैं?
- LLM प्रशिक्षित करने के लिए
- ताकि आप दिखा सकें कि किन अंशों ने उत्तर का समर्थन किया
- Index संपीड़ित करने के लिए
- Citations हटाने के लिए
Answer
ताकि आप दिखा सकें कि किन अंशों ने उत्तर का समर्थन किया — Source nodes citations और debugging संभव करते हैं।