LCEL रिट्रीवल चेन बनाना

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

Meri Nova

Machine Learning Engineer

रिट्रीवल के लिए डेटा तैयार करना

डॉक्यूमेंट chunks स्टोर किए जाते हैं.

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

RAG के लिए LCEL परिचय

एक user input.

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

RAG के लिए LCEL परिचय

RunnablePassthrough से चेन में आता user input, जिसे "question" असाइन किया जाता है.

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

RAG के लिए LCEL परिचय

इनपुट से retriever को क्वेरी किया जाता है और "context" असाइन होता है.

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

RAG के लिए LCEL परिचय

"context" और "question" को prompt template में जोड़ा जाता है.

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

RAG के लिए LCEL परिचय

प्रॉम्प्ट LLM (मॉडल) में जाता है और आउटपुट जेनरेट होता है.

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

RAG के लिए LCEL परिचय

मॉडल आउटपुट एक parser को पास किया जाता है.

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

Retriever इंस्टैंशिएट करना

vector_store = Chroma.from_documents(
    documents=chunks, 
    embedding=embedding_model
)


retriever = vector_store.as_retriever( search_type="similarity", search_kwargs={"k": 2} )
LangChain के साथ Retrieval Augmented Generation (RAG)

Prompt template बनाना

from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_template(""" Use the following pieces of context to answer the question at the end. If you don't know the answer, say that you don't know. Context: {context} Question: {question} """)
llm = ChatOpenAI(model="gpt-4o-mini", api_key="...", temperature=0)
LangChain के साथ Retrieval Augmented Generation (RAG)

LCEL रिट्रीवल चेन बनाना

from langchain_core.runnables import RunnablePassthrough
from langchain_core.output_parsers import StrOutputParser

chain = (
{"context": retriever, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
LangChain के साथ Retrieval Augmented Generation (RAG)

रिट्रीवल चेन invoke करना

result = chain.invoke({"question": "What are the key findings or results presented in the paper?"})
print(result)
- Top Performance: RAG models set new records on open-domain question answering tasks...
- Better Generation: RAG models produce more specific, diverse, and factual language...
- Dynamic Knowledge Use: The non-parametric memory allows RAG models to access and ...
LangChain के साथ Retrieval Augmented Generation (RAG)

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

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

Preparing Video For Download...