Bird
Raised Fist0
LangChainframework~10 mins

Basic RAG chain with LCEL in LangChain - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Basic RAG chain with LCEL
User Query Input
↓
Retrieve Relevant Docs
↓
Pass Docs + Query to LCEL
↓
LCEL Generates Answer
↓
Return Answer to User
The flow starts with a user query, retrieves documents, then uses LCEL to generate an answer based on those docs and the query.
Execution Sample
LangChain
from langchain.chains import RetrievalQA
from langchain.llms import OpenAI
from langchain.document_loaders import SimpleDirectoryLoader
from langchain.vectorstores import FAISS

# Setup retriever and LCEL chain
retriever = FAISS.load_local('faiss_index', OpenAI().embedding)
qa_chain = RetrievalQA.from_chain_type(llm=OpenAI(), retriever=retriever)

# Run query
answer = qa_chain.run('What is LangChain?')
This code sets up a retrieval-based QA chain using LCEL and runs a query to get an answer.
Execution Table
StepActionInputOutputNotes
1Receive user query'What is LangChain?'Query storedStart with user input
2Retrieve documentsQuery + FAISS indexTop relevant docsRetriever finds related info
3Pass to LCELDocs + QueryPrompt for LCELPrepare input for language model
4LCEL generates answerPromptAnswer textLanguage model creates response
5Return answerAnswer textDisplayed to userFinal output
6EndN/AProcess completeNo more steps
💡 Process ends after answer is returned to user.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
queryNone'What is LangChain?''What is LangChain?''What is LangChain?''What is LangChain?''What is LangChain?'
retrieved_docsNoneNone[Doc1, Doc2, ...][Doc1, Doc2, ...][Doc1, Doc2, ...][Doc1, Doc2, ...]
lc_el_promptNoneNoneNonePrompt with docs + queryPrompt with docs + queryPrompt with docs + query
answerNoneNoneNoneNone'LangChain is a framework...''LangChain is a framework...'
Key Moments - 3 Insights
Why do we need to retrieve documents before asking LCEL?
The execution_table row 2 shows retrieval of relevant documents. LCEL uses these docs to give accurate answers instead of guessing.
What exactly does LCEL receive as input?
Row 3 in execution_table shows LCEL gets a prompt combining the user query and retrieved documents to generate a precise answer.
When does the process stop?
Row 6 marks the end after the answer is returned, so no further steps happen beyond that.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output after Step 2?
ATop relevant documents
BUser query stored
CAnswer text
DProcess complete
💡 Hint
Check the 'Output' column for Step 2 in the execution_table.
At which step does LCEL generate the answer?
AStep 1
BStep 3
CStep 4
DStep 5
💡 Hint
Look for 'LCEL generates answer' in the 'Action' column.
If the retriever returns no documents, what changes in the execution_table?
AStep 2 output would be empty or None
BStep 4 would generate answer normally
CStep 1 would change
DProcess would skip Step 3
💡 Hint
Focus on the 'retrieved_docs' variable in variable_tracker after Step 2.
Concept Snapshot
Basic RAG chain with LCEL:
1. Input user query.
2. Retrieve relevant documents using vector search.
3. Combine docs + query into prompt.
4. LCEL (language model) generates answer.
5. Return answer to user.
This ensures answers are based on real info, not guesswork.
Full Transcript
This visual execution trace shows how a Basic RAG chain with LCEL works. First, the user inputs a query. Then the system retrieves relevant documents from a vector store. These documents plus the query form a prompt for the LCEL language model. LCEL generates an answer based on this prompt. Finally, the answer is returned to the user. Variables like 'query', 'retrieved_docs', 'lc_el_prompt', and 'answer' change step-by-step. Key moments clarify why retrieval is needed before LCEL and when the process ends. The quiz tests understanding of each step's output and flow.

Practice

(1/5)
1. What is the main purpose of a RAG chain in Langchain?
easy
A. To combine document search with AI to answer questions
B. To train a new AI model from scratch
C. To visualize data in charts
D. To clean and preprocess raw data

Solution

  1. Step 1: Understand RAG chain function

    A RAG (Retrieval-Augmented Generation) chain uses search to find relevant documents and AI to generate answers.
  2. Step 2: Match purpose with options

    Only To combine document search with AI to answer questions describes combining search and AI to answer questions, which is the core of RAG chains.
  3. Final Answer:

    To combine document search with AI to answer questions -> Option A
  4. Quick Check:

    RAG chain = combine search + AI [OK]
Hint: RAG means search plus AI for answers [OK]
Common Mistakes:
  • Thinking RAG trains new models
  • Confusing RAG with data visualization
  • Assuming RAG only cleans data
2. Which of the following is the correct way to create a RAG chain using LCEL in Langchain?
easy
A. rag_chain = LCEL.RAGChain.build(docs, llm)
B. rag_chain = LCEL.RAGChain.from_documents(docs, llm)
C. rag_chain = RAGChain(docs, llm).build()
D. rag_chain = LCEL.createRAG(docs, llm)

Solution

  1. Step 1: Recall LCEL syntax for RAG chain

    LCEL provides a method called from_documents to create a RAG chain from documents and an LLM.
  2. Step 2: Compare options with correct syntax

    Only rag_chain = LCEL.RAGChain.from_documents(docs, llm) uses LCEL.RAGChain.from_documents(docs, llm), which is the correct method.
  3. Final Answer:

    rag_chain = LCEL.RAGChain.from_documents(docs, llm) -> Option B
  4. Quick Check:

    LCEL.RAGChain.from_documents = correct syntax [OK]
Hint: Use from_documents method to create RAG chain [OK]
Common Mistakes:
  • Using non-existent createRAG method
  • Calling build() on RAGChain instance
  • Mixing method names or class names
3. Given the code snippet:
docs = ["Doc1 content", "Doc2 content"]
llm = SomeLLM()
rag_chain = LCEL.RAGChain.from_documents(docs, llm)
answer = rag_chain.run("What is in Doc1?")
print(answer)

What is the expected output?
medium
A. An error because docs should be a string, not a list
B. A list of documents including Doc1 and Doc2
C. An answer generated by the LLM based on Doc1 content
D. The string 'What is in Doc1?' printed as is

Solution

  1. Step 1: Understand RAG chain run method

    Running rag_chain.run(question) uses the LLM to answer based on retrieved documents.
  2. Step 2: Analyze input and output

    Docs are provided as a list, which is valid. The output is an AI-generated answer about Doc1 content.
  3. Final Answer:

    An answer generated by the LLM based on Doc1 content -> Option C
  4. Quick Check:

    rag_chain.run(question) = AI answer [OK]
Hint: RAG chain answers questions using docs + LLM [OK]
Common Mistakes:
  • Expecting raw docs list as output
  • Thinking docs must be a single string
  • Assuming run just echoes input question
4. You wrote this code:
rag_chain = LCEL.RAGChain.from_documents("doc.txt", llm)
answer = rag_chain.run("Explain the content.")

But it raises an error. What is the likely problem?
medium
A. The llm variable is not defined
B. LCEL.RAGChain does not have from_documents method
C. The run method requires two arguments
D. The documents argument should be a list, not a filename string

Solution

  1. Step 1: Check documents argument type

    from_documents expects a list of document strings, not a filename string.
  2. Step 2: Identify error cause

    Passing "doc.txt" as a string causes an error because it is not a list of documents.
  3. Final Answer:

    The documents argument should be a list, not a filename string -> Option D
  4. Quick Check:

    Documents must be list, not filename string [OK]
Hint: Pass list of docs, not filename string [OK]
Common Mistakes:
  • Passing filename instead of document list
  • Assuming run needs two arguments
  • Thinking from_documents method is missing
5. You want to build a RAG chain that answers questions from multiple document types (PDF, text, and web pages) using LCEL. Which approach correctly prepares the documents before creating the RAG chain?
hard
A. Load and convert each document type to text, combine into a list, then pass to LCEL.RAGChain.from_documents
B. Pass file paths directly to LCEL.RAGChain.from_documents without loading
C. Use only one document type to avoid errors
D. Create separate RAG chains for each document type and run them independently

Solution

  1. Step 1: Understand document preparation for RAG

    RAG chains require documents as text strings, so all document types must be converted to text first.
  2. Step 2: Combine and pass documents correctly

    After conversion, combine all texts into a list and pass to LCEL.RAGChain.from_documents for a single chain.
  3. Final Answer:

    Load and convert each document type to text, combine into a list, then pass to LCEL.RAGChain.from_documents -> Option A
  4. Quick Check:

    Convert all docs to text list before RAG chain [OK]
Hint: Convert all docs to text list before RAG chain [OK]
Common Mistakes:
  • Passing file paths instead of text
  • Using only one document type unnecessarily
  • Creating multiple chains instead of one combined
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard