mirror of
https://github.com/hwchase17/langchain
synced 2024-11-08 07:10:35 +00:00
ed58eeb9c5
Moved the following modules to new package langchain-community in a backwards compatible fashion: ``` mv langchain/langchain/adapters community/langchain_community mv langchain/langchain/callbacks community/langchain_community/callbacks mv langchain/langchain/chat_loaders community/langchain_community mv langchain/langchain/chat_models community/langchain_community mv langchain/langchain/document_loaders community/langchain_community mv langchain/langchain/docstore community/langchain_community mv langchain/langchain/document_transformers community/langchain_community mv langchain/langchain/embeddings community/langchain_community mv langchain/langchain/graphs community/langchain_community mv langchain/langchain/llms community/langchain_community mv langchain/langchain/memory/chat_message_histories community/langchain_community mv langchain/langchain/retrievers community/langchain_community mv langchain/langchain/storage community/langchain_community mv langchain/langchain/tools community/langchain_community mv langchain/langchain/utilities community/langchain_community mv langchain/langchain/vectorstores community/langchain_community mv langchain/langchain/agents/agent_toolkits community/langchain_community mv langchain/langchain/cache.py community/langchain_community mv langchain/langchain/adapters community/langchain_community mv langchain/langchain/callbacks community/langchain_community/callbacks mv langchain/langchain/chat_loaders community/langchain_community mv langchain/langchain/chat_models community/langchain_community mv langchain/langchain/document_loaders community/langchain_community mv langchain/langchain/docstore community/langchain_community mv langchain/langchain/document_transformers community/langchain_community mv langchain/langchain/embeddings community/langchain_community mv langchain/langchain/graphs community/langchain_community mv langchain/langchain/llms community/langchain_community mv langchain/langchain/memory/chat_message_histories community/langchain_community mv langchain/langchain/retrievers community/langchain_community mv langchain/langchain/storage community/langchain_community mv langchain/langchain/tools community/langchain_community mv langchain/langchain/utilities community/langchain_community mv langchain/langchain/vectorstores community/langchain_community mv langchain/langchain/agents/agent_toolkits community/langchain_community mv langchain/langchain/cache.py community/langchain_community ``` Moved the following to core ``` mv langchain/langchain/utils/json_schema.py core/langchain_core/utils mv langchain/langchain/utils/html.py core/langchain_core/utils mv langchain/langchain/utils/strings.py core/langchain_core/utils cat langchain/langchain/utils/env.py >> core/langchain_core/utils/env.py rm langchain/langchain/utils/env.py ``` See .scripts/community_split/script_integrations.sh for all changes
55 lines
1.9 KiB
Python
55 lines
1.9 KiB
Python
import logging
|
|
import os
|
|
import tempfile
|
|
from typing import Any, Iterator, List
|
|
|
|
from langchain_core.documents import Document
|
|
|
|
from langchain_community.document_loaders.base import BaseLoader
|
|
from langchain_community.document_loaders.unstructured import UnstructuredFileLoader
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class BaiduBOSFileLoader(BaseLoader):
|
|
"""Load from `Baidu Cloud BOS` file."""
|
|
|
|
def __init__(self, conf: Any, bucket: str, key: str):
|
|
"""Initialize with BOS config, bucket and key name.
|
|
:param conf(BceClientConfiguration): BOS config.
|
|
:param bucket(str): BOS bucket.
|
|
:param key(str): BOS file key.
|
|
"""
|
|
self.conf = conf
|
|
self.bucket = bucket
|
|
self.key = key
|
|
|
|
def load(self) -> List[Document]:
|
|
return list(self.lazy_load())
|
|
|
|
def lazy_load(self) -> Iterator[Document]:
|
|
"""Load documents."""
|
|
try:
|
|
from baidubce.services.bos.bos_client import BosClient
|
|
except ImportError:
|
|
raise ImportError(
|
|
"Please using `pip install bce-python-sdk`"
|
|
+ " before import bos related package."
|
|
)
|
|
|
|
# Initialize BOS Client
|
|
client = BosClient(self.conf)
|
|
with tempfile.TemporaryDirectory() as temp_dir:
|
|
file_path = f"{temp_dir}/{self.bucket}/{self.key}"
|
|
os.makedirs(os.path.dirname(file_path), exist_ok=True)
|
|
# Download the file to a destination
|
|
logger.debug(f"get object key {self.key} to file {file_path}")
|
|
client.get_object_to_file(self.bucket, self.key, file_path)
|
|
try:
|
|
loader = UnstructuredFileLoader(file_path)
|
|
documents = loader.load()
|
|
return iter(documents)
|
|
except Exception as ex:
|
|
logger.error(f"load document error = {ex}")
|
|
return iter([Document(page_content="")])
|