Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The RAG chain connects the retriever to the language model by passing the retriever object to the RetrievalQA chain.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Calling get_relevant_documents on the language model.
Using the query variable instead of the retriever.
✗ Incorrect
The retriever fetches relevant documents for the query before the language model generates an answer.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping llm and retriever arguments.
Passing the wrong variable to retriever.
✗ Incorrect
The retriever must be passed as the retriever argument, not the language model.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using doc.text instead of doc.content.
Swapping key and value in the comprehension.
✗ Incorrect
The dictionary comprehension uses doc as key and length of doc.content as value.
5fill in blank
hardFill 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'
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.
✗ Incorrect
We take the first 50 characters of doc.content for docs where content length is greater than 100.