0
0
LangChainframework~10 mins

Custom document loaders in LangChain - Interactive Code Practice

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

Complete the code to import the base class for creating a custom document loader.

LangChain
from langchain.document_loaders import [1]
Drag options to blanks, or click blank then click option'
ABaseDocumentLoader
BCustomLoaderBase
CDocumentLoaderBase
DLoaderBase
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class name that does not exist in the module.
Confusing the order of words in the class name.
2fill in blank
medium

Complete the method name to override for loading documents in a custom loader class.

LangChain
class MyLoader(BaseDocumentLoader):
    def [1](self):
        # Your code to load documents
        pass
Drag options to blanks, or click blank then click option'
Aload_documents
Bfetch_documents
Cload
Dget_docs
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name that does not match the base class signature.
Using a generic name like 'load' instead of 'load_documents'.
3fill in blank
hard

Fix the error in the custom loader by completing the return statement to return a list of Document objects.

LangChain
from langchain.schema import Document

class MyLoader(BaseDocumentLoader):
    def load_documents(self):
        text = 'Sample text content'
        return [[1](page_content=text)]
Drag options to blanks, or click blank then click option'
ADoc
BDocumentLoader
CDocument
DDocObject
Attempts:
3 left
💡 Hint
Common Mistakes
Returning a list of strings instead of Document objects.
Using a wrong class name for the document wrapper.
4fill in blank
hard

Fill both blanks to create a custom loader that reads text from a file path and returns a Document.

LangChain
class FileLoader(BaseDocumentLoader):
    def __init__(self, file_path):
        self.file_path = file_path

    def load_documents(self):
        with open(self.file_path, [1]) as f:
            text = f.read()
        return [Document(page_content=[2])]
Drag options to blanks, or click blank then click option'
Ar
Btext
Cw
Dfile_path
Attempts:
3 left
💡 Hint
Common Mistakes
Using write mode 'w' instead of read mode 'r'.
Returning the file path string instead of the file content.
5fill in blank
hard

Fill all three blanks to create a custom loader that filters lines containing a keyword and returns Documents for each matching line.

LangChain
class KeywordLoader(BaseDocumentLoader):
    def __init__(self, file_path, keyword):
        self.file_path = file_path
        self.keyword = keyword

    def load_documents(self):
        docs = []
        with open(self.file_path, [1]) as f:
            for line in f:
                if [2] in line:
                    docs.append(Document(page_content=[3]))
        return docs
Drag options to blanks, or click blank then click option'
Ar
Bself.keyword
Cline
Dw
Attempts:
3 left
💡 Hint
Common Mistakes
Opening the file in write mode instead of read mode.
Checking the wrong variable for the keyword.
Appending the keyword instead of the line content.