mirror of
https://github.com/hwchase17/langchain
synced 2024-11-18 09:25:54 +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
47 lines
1.5 KiB
Python
47 lines
1.5 KiB
Python
"""Document loader helpers."""
|
|
|
|
import concurrent.futures
|
|
from typing import List, NamedTuple, Optional, cast
|
|
|
|
|
|
class FileEncoding(NamedTuple):
|
|
"""File encoding as the NamedTuple."""
|
|
|
|
encoding: Optional[str]
|
|
"""The encoding of the file."""
|
|
confidence: float
|
|
"""The confidence of the encoding."""
|
|
language: Optional[str]
|
|
"""The language of the file."""
|
|
|
|
|
|
def detect_file_encodings(file_path: str, timeout: int = 5) -> List[FileEncoding]:
|
|
"""Try to detect the file encoding.
|
|
|
|
Returns a list of `FileEncoding` tuples with the detected encodings ordered
|
|
by confidence.
|
|
|
|
Args:
|
|
file_path: The path to the file to detect the encoding for.
|
|
timeout: The timeout in seconds for the encoding detection.
|
|
"""
|
|
import chardet
|
|
|
|
def read_and_detect(file_path: str) -> List[dict]:
|
|
with open(file_path, "rb") as f:
|
|
rawdata = f.read()
|
|
return cast(List[dict], chardet.detect_all(rawdata))
|
|
|
|
with concurrent.futures.ThreadPoolExecutor() as executor:
|
|
future = executor.submit(read_and_detect, file_path)
|
|
try:
|
|
encodings = future.result(timeout=timeout)
|
|
except concurrent.futures.TimeoutError:
|
|
raise TimeoutError(
|
|
f"Timeout reached while detecting encoding for {file_path}"
|
|
)
|
|
|
|
if all(encoding["encoding"] is None for encoding in encodings):
|
|
raise RuntimeError(f"Could not detect encoding for {file_path}")
|
|
return [FileEncoding(**enc) for enc in encodings if enc["encoding"] is not None]
|