0
0
LangChainframework~10 mins

Why the RAG chain connects retrieval to generation in LangChain - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a RAG chain that connects retrieval to generation.

LangChain
from langchain.chains import RetrievalQA
from langchain.llms import OpenAI

retriever = get_retriever()
llm = OpenAI()
rag_chain = RetrievalQA(llm=llm, retriever=[1])
Drag options to blanks, or click blank then click option'
Aretriever
Bllm
Cquery
Ddocument
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the language model instead of the retriever to the chain.
Using an undefined variable instead of the retriever.
2fill in blank
medium

Complete the code to retrieve documents before generating an answer.

LangChain
query = "What is RAG?"
docs = [1].get_relevant_documents(query)
answer = llm.generate(docs)
Drag options to blanks, or click blank then click option'
Aretriever
Bllm
Cquery
Drag_chain
Attempts:
3 left
💡 Hint
Common Mistakes
Calling get_relevant_documents on the language model.
Using the query variable instead of the retriever.
3fill in blank
hard

Fix the error in connecting retrieval to generation in the RAG chain.

LangChain
from langchain.chains import RetrievalQA
from langchain.llms import OpenAI

retriever = get_retriever()
llm = OpenAI()
rag_chain = RetrievalQA(llm=[1], retriever=llm)
result = rag_chain.run("Explain RAG")
Drag options to blanks, or click blank then click option'
Aresult
BOpenAI
Cquery
Dretriever
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping llm and retriever arguments.
Passing the wrong variable to retriever.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps documents to their content length.

LangChain
doc_lengths = { [1]: len([2]) for doc in docs}
Drag options to blanks, or click blank then click option'
Adoc.content
Bdoc
Ddoc.text
Attempts:
3 left
💡 Hint
Common Mistakes
Using doc.text instead of doc.content.
Swapping key and value in the comprehension.
5fill in blank
hard

Fill all three blanks to filter documents with content longer than 100 characters and create a summary dictionary.

LangChain
summary = {doc.id: [1] for doc in docs if len([2]) [3] 100}
Drag options to blanks, or click blank then click option'
Adoc.content[:50]
Bdoc.content
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' instead of '>' in the condition.
Using doc.id as value instead of key.
Not slicing the content for summary.