Refactor document loaders to update metadata

This commit is contained in:
anovazzi1 2024-01-15 19:34:47 -03:00
commit 69f2f51161
2 changed files with 18 additions and 9 deletions

View file

@ -1,7 +1,7 @@
from langflow import CustomComponent from langflow import CustomComponent
from langflow.field_typing import Document from langflow.field_typing import Document
from typing import Optional, Dict from typing import Optional, Dict
from langchain_community.document_loaders.evernote import EverNoteLoader
class EverNoteLoaderComponent(CustomComponent): class EverNoteLoaderComponent(CustomComponent):
display_name = "EverNoteLoader" display_name = "EverNoteLoader"
@ -13,10 +13,9 @@ class EverNoteLoaderComponent(CustomComponent):
"file_path": { "file_path": {
"display_name": "File Path", "display_name": "File Path",
"required": True, "required": True,
"suffixes": [".xml"],
"show": True, "show": True,
"type": "file", "type": "file",
"file_types": ["xml"], "file_types": [".xml"],
"field_type": "file", "field_type": "file",
}, },
"metadata": { "metadata": {
@ -28,6 +27,11 @@ class EverNoteLoaderComponent(CustomComponent):
} }
def build(self, file_path: str, metadata: Optional[Dict] = None) -> Document: def build(self, file_path: str, metadata: Optional[Dict] = None) -> Document:
# Assuming there is a function or class named `EverNoteLoader` that takes these parameters documents = EverNoteLoader(file_path=file_path).load()
# and returns a `Document` object. Replace `EverNoteLoader` with the actual implementation. if(metadata):
return EverNoteLoader(file_path=file_path, metadata=metadata) for document in documents:
if not document.metadata:
document.metadata = metadata
else:
document.metadata.update(metadata)
return documents

View file

@ -22,6 +22,11 @@ class GitbookLoaderComponent(CustomComponent):
} }
def build(self, metadata: Optional[Dict] = None, web_page: str = "") -> Document: def build(self, metadata: Optional[Dict] = None, web_page: str = "") -> Document:
# Assuming there is a GitbookLoader class that takes metadata and web_page as parameters documents = GitbookLoader(web_page=web_page).load()
# Replace 'GitbookLoader' with the actual class name if different if(metadata):
return GitbookLoader(metadata=metadata, web_page=web_page) for document in documents:
if not document.metadata:
document.metadata = metadata
else:
document.metadata.update(metadata)
return documents