Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pebblo 0.1.18 snowflake v2 #88

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 53 additions & 1 deletion libs/community/langchain_community/document_loaders/pebblo.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""Pebblo's safe dataloader is a wrapper for document loaders"""

import ast
import logging
import os
import uuid
from importlib.metadata import version
from typing import Dict, Iterator, List, Optional
from typing import Any, Dict, Iterator, List, Optional

from langchain_core.documents import Document

Expand Down Expand Up @@ -45,6 +46,7 @@ def __init__(
classifier_url: Optional[str] = None,
*,
classifier_location: str = "local",
**kwargs: Any,
dristysrivastava marked this conversation as resolved.
Show resolved Hide resolved
):
if not name or not isinstance(name, str):
raise NameError("Must specify a valid name.")
Expand All @@ -57,6 +59,7 @@ def __init__(
self.source_path = get_loader_full_path(self.loader)
self.docs: List[Document] = []
self.docs_with_id: List[IndexedDocument] = []
self.kwargs = kwargs.get("kwargs")
loader_name = str(type(self.loader)).split(".")[-1].split("'")[0]
self.source_type = get_loader_type(loader_name)
self.source_path_size = get_source_size(self.source_path)
Expand Down Expand Up @@ -162,6 +165,41 @@ def lazy_load(self) -> Iterator[Document]:
def set_discover_sent(cls) -> None:
cls._discover_sent = True

def _get_authfield_from_md(self, doc: Document, auth_field_name: str) -> None:
"""
Extracts the AUTH_FIELD from the metadata and adds it to the document.

Args:
doc (Document): Document object.
auth_field_name (str): Name of the AUTH_FIELD.
"""
auth_field_list: List[str] = []
# Extract the AUTH_FIELD block
auth_field_str = doc.metadata.pop(auth_field_name, "")
if auth_field_str:
# Convert the string representation of the list to an actual list
# using ast.literal_eval
auth_field_list = ast.literal_eval(auth_field_str)
doc.metadata["authorized_identities"] = auth_field_list
else:
# logger.info("AUTH_FIELD not found")
pass

def _get_sourcefield_from_md(self, doc: Document, source_field_name: str) -> None:
"""
Extracts the SOURCE_FIELD from the metadata and adds it to the document.

Args:
doc (Document): Document object.
source_field_name (str): Name of the SOURCE_FIELD.
"""
auth_field_str = doc.metadata.pop(source_field_name, "")
if auth_field_str:
doc.metadata["full_path"] = auth_field_str
else:
# logger.info("SOURCE_FIELD not found")
pass

def _get_app_details(self) -> App:
"""Fetch app details. Internal method.

Expand Down Expand Up @@ -258,6 +296,20 @@ def _add_pebblo_specific_metadata(self, classified_docs: dict) -> None:
"""Add Pebblo specific metadata to documents."""
for doc in self.docs_with_id:
doc_metadata = doc.metadata
if (
self.loader.__class__.__name__ == "SnowflakeLoader"
and self.kwargs is not None
):
if self.kwargs.get("auth_identities") is not None:
# Snowflake Table column name for authorized_identities
# e.g. values [[email protected], [email protected]]
column_name = self.kwargs.get("auth_identities")
self._get_authfield_from_md(doc, column_name)
if self.kwargs.get("source") is not None:
# Snowflake Table column name for authorized_identities
# e.g. values [[email protected], [email protected]]
column_name = self.kwargs.get("source")
self._get_sourcefield_from_md(doc, column_name)
if self.loader.__class__.__name__ == "SharePointLoader":
doc_metadata["full_path"] = get_full_path(
doc_metadata.get("source", self.source_path)
Expand Down
1 change: 1 addition & 0 deletions libs/community/langchain_community/utilities/pebblo.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"NotionDBLoader",
"GoogleDriveLoader",
"SharePointLoader",
"SnowflakeLoader",
]

LOADER_TYPE_MAPPING = {
Expand Down
Loading