Refactor document loaders to support metadata
This commit is contained in:
parent
310886b0ba
commit
0881f0600d
2 changed files with 19 additions and 11 deletions
|
|
@ -2,6 +2,7 @@
|
||||||
from langflow import CustomComponent
|
from langflow import CustomComponent
|
||||||
from langchain.docstore.document import Document
|
from langchain.docstore.document import Document
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
from langchain_community.document_loaders.college_confidential import CollegeConfidentialLoader
|
||||||
|
|
||||||
class CollegeConfidentialLoaderComponent(CustomComponent):
|
class CollegeConfidentialLoaderComponent(CustomComponent):
|
||||||
display_name = "CollegeConfidentialLoader"
|
display_name = "CollegeConfidentialLoader"
|
||||||
|
|
@ -19,6 +20,11 @@ class CollegeConfidentialLoaderComponent(CustomComponent):
|
||||||
web_path: str,
|
web_path: str,
|
||||||
metadata: Optional[dict] = {}
|
metadata: Optional[dict] = {}
|
||||||
) -> Document:
|
) -> Document:
|
||||||
# Assuming there is a loader class `CollegeConfidentialLoader` that takes `metadata` and `web_path` as arguments
|
documents = CollegeConfidentialLoader(web_path=web_path).load()
|
||||||
# Replace `CollegeConfidentialLoader` with the actual class name if different
|
if(metadata):
|
||||||
return CollegeConfidentialLoader(web_path=web_path, metadata=metadata)
|
for document in documents:
|
||||||
|
if not document.metadata:
|
||||||
|
document.metadata = metadata
|
||||||
|
else:
|
||||||
|
document.metadata.update(metadata)
|
||||||
|
return documents
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
from langflow import CustomComponent
|
from langflow import CustomComponent
|
||||||
from langchain.docstore.document import Document
|
from langchain.docstore.document import Document
|
||||||
from typing import Optional, Dict
|
from typing import Optional, Dict
|
||||||
|
from langchain_community.document_loaders.facebook_chat import FacebookChatLoader
|
||||||
class FacebookChatLoaderComponent(CustomComponent):
|
class FacebookChatLoaderComponent(CustomComponent):
|
||||||
display_name = "FacebookChatLoader"
|
display_name = "FacebookChatLoader"
|
||||||
description = "Load `Facebook Chat` messages directory dump."
|
description = "Load `Facebook Chat` messages directory dump."
|
||||||
|
|
@ -13,8 +13,7 @@ class FacebookChatLoaderComponent(CustomComponent):
|
||||||
"file_path": {
|
"file_path": {
|
||||||
"display_name": "File Path",
|
"display_name": "File Path",
|
||||||
"required": True,
|
"required": True,
|
||||||
"suffixes": [".json"],
|
"file_types": [".json"],
|
||||||
"file_types": ["json"],
|
|
||||||
"field_type": "file",
|
"field_type": "file",
|
||||||
},
|
},
|
||||||
"metadata": {
|
"metadata": {
|
||||||
|
|
@ -25,8 +24,11 @@ class FacebookChatLoaderComponent(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 class named FacebookChatLoader that takes file_path and metadata as parameters
|
documents = FacebookChatLoader(file_path=file_path).load()
|
||||||
# and returns a Document object. Replace 'FacebookChatLoader' with the actual class name.
|
if(metadata):
|
||||||
# As per the JSON, the output type is 'Document', which is part of langchain.documents.
|
for document in documents:
|
||||||
# Therefore, the 'FacebookChatLoader' should be imported or defined elsewhere in the codebase.
|
if not document.metadata:
|
||||||
return FacebookChatLoader(file_path=file_path, metadata=metadata)
|
document.metadata = metadata
|
||||||
|
else:
|
||||||
|
document.metadata.update(metadata)
|
||||||
|
return documents
|
||||||
Loading…
Add table
Add a link
Reference in a new issue