The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
community[patch]: `cross_encoders` flatten namespaces (#20183)
Leonid Ganeline2f8dd1a1619f
diff --git a/libs/community/langchain_community/cross_encoders/__init__.py b/libs/community/langchain_community/cross_encoders/__init__.pyindex be68809d19..f6b64a86e8 100644--- a/libs/community/langchain_community/cross_encoders/__init__.py+++ b/libs/community/langchain_community/cross_encoders/__init__.py@@ -1,4 +1,4 @@-"""**Cross encoders** are wrappers around cross encoder models from different APIs and+"""**Cross encoders** are wrappers around cross encoder models from different APIs andservices.**Cross encoder models** can be LLMs or not.@@ -9,18 +9,22 @@BaseCrossEncoder --> <name>CrossEncoder # Examples: SagemakerEndpointCrossEncoder"""---import logging--from langchain_community.cross_encoders.base import BaseCrossEncoder-from langchain_community.cross_encoders.fake import FakeCrossEncoder-from langchain_community.cross_encoders.huggingface import HuggingFaceCrossEncoder-from langchain_community.cross_encoders.sagemaker_endpoint import (- SagemakerEndpointCrossEncoder,-)--logger = logging.getLogger(__name__)+import importlib+from typing import TYPE_CHECKING, Any++if TYPE_CHECKING:+ from langchain_community.cross_encoders.base import (+ BaseCrossEncoder, # noqa: F401+ )+ from langchain_community.cross_encoders.fake import (+ FakeCrossEncoder, # noqa: F401+ )+ from langchain_community.cross_encoders.huggingface import (+ HuggingFaceCrossEncoder, # noqa: F401+ )+ from langchain_community.cross_encoders.sagemaker_endpoint import (+ SagemakerEndpointCrossEncoder, # noqa: F401+ )__all__ = ["BaseCrossEncoder",@@ -28,3 +32,17 @@ __all__ = ["HuggingFaceCrossEncoder","SagemakerEndpointCrossEncoder",]++_module_lookup = {+ "BaseCrossEncoder": "langchain_community.cross_encoders.base",+ "FakeCrossEncoder": "langchain_community.cross_encoders.fake",+ "HuggingFaceCrossEncoder": "langchain_community.cross_encoders.huggingface",+ "SagemakerEndpointCrossEncoder": "langchain_community.cross_encoders.sagemaker_endpoint", # noqa: E501+}+++def __getattr__(name: str) -> Any:+ if name in _module_lookup:+ module = importlib.import_module(_module_lookup[name])+ return getattr(module, name)+ raise AttributeError(f"module {__name__} has no attribute {name}")diff --git a/libs/community/tests/unit_tests/chat_loaders/test_imports.py b/libs/community/tests/unit_tests/chat_loaders/test_imports.pynew file mode 100644index 0000000000..501fbf6656--- /dev/null+++ b/libs/community/tests/unit_tests/chat_loaders/test_imports.py@@ -0,0 +1,18 @@+from langchain_community.chat_loaders import _module_lookup++EXPECTED_ALL = [+ "BaseChatLoader",+ "FolderFacebookMessengerChatLoader",+ "GMailLoader",+ "IMessageChatLoader",+ "LangSmithDatasetChatLoader",+ "LangSmithRunChatLoader",+ "SingleFileFacebookMessengerChatLoader",+ "SlackChatLoader",+ "TelegramChatLoader",+ "WhatsAppChatLoader",+]+++def test_all_imports() -> None:+ assert set(_module_lookup.keys()) == set(EXPECTED_ALL)diff --git a/libs/community/tests/unit_tests/chat_message_histories/test_imports.py b/libs/community/tests/unit_tests/chat_message_histories/test_imports.pynew file mode 100644index 0000000000..ad55c10236--- /dev/null+++ b/libs/community/tests/unit_tests/chat_message_histories/test_imports.py@@ -0,0 +1,29 @@+from langchain_community.chat_message_histories import _module_lookup++EXPECTED_ALL = [+ "AstraDBChatMessageHistory",+ "CassandraChatMessageHistory",+ "ChatMessageHistory",+ "CosmosDBChatMessageHistory",+ "DynamoDBChatMessageHistory",+ "ElasticsearchChatMessageHistory",+ "FileChatMessageHistory",+ "FirestoreChatMessageHistory",+ "MomentoChatMessageHistory",+ "MongoDBChatMessageHistory",+ "Neo4jChatMessageHistory",+ "PostgresChatMessageHistory",+ "RedisChatMessageHistory",+ "RocksetChatMessageHistory",+ "SQLChatMessageHistory",+ "SingleStoreDBChatMessageHistory",+ "StreamlitChatMessageHistory",+ "TiDBChatMessageHistory",+ "UpstashRedisChatMessageHistory",+ "XataChatMessageHistory",+ "ZepChatMessageHistory",+]+++def test_all_imports() -> None:+ assert set(_module_lookup.keys()) == set(EXPECTED_ALL)diff --git a/libs/community/tests/unit_tests/cross_encoders/__init__.py b/libs/community/tests/unit_tests/cross_encoders/__init__.pynew file mode 100644index 0000000000..e69de29bb2diff --git a/libs/community/tests/unit_tests/cross_encoders/test_imports.py b/libs/community/tests/unit_tests/cross_encoders/test_imports.pynew file mode 100644index 0000000000..5de7395f17--- /dev/null+++ b/libs/community/tests/unit_tests/cross_encoders/test_imports.py@@ -0,0 +1,14 @@+from langchain_community.cross_encoders import __all__, _module_lookup++EXPECTED_ALL = [+ "BaseCrossEncoder",+ "FakeCrossEncoder",+ "HuggingFaceCrossEncoder",+ "SagemakerEndpointCrossEncoder",+]+++def test_all_imports() -> None:+ """Test that __all__ is correctly set."""+ assert set(__all__) == set(EXPECTED_ALL)+ assert set(__all__) == set(_module_lookup.keys())