Merge pull request #18674

* Implement lazy_load() for TextLoader
This commit is contained in:
Christophe Bornet 2024-03-06 19:23:42 +01:00 committed by GitHub
parent 5b92f962f1
commit 68fc0cf909
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,5 +1,5 @@
import logging
from typing import List, Optional
from typing import Iterator, Optional
from langchain_core.documents import Document
@ -34,7 +34,7 @@ class TextLoader(BaseLoader):
self.encoding = encoding
self.autodetect_encoding = autodetect_encoding
def load(self) -> List[Document]:
def lazy_load(self) -> Iterator[Document]:
"""Load from file path."""
text = ""
try:
@ -57,4 +57,4 @@ class TextLoader(BaseLoader):
raise RuntimeError(f"Error loading {self.file_path}") from e
metadata = {"source": self.file_path}
return [Document(page_content=text, metadata=metadata)]
yield Document(page_content=text, metadata=metadata)