langchain[patch]: Migrate more code in utils to use optional langchain import (#21166)

Moving is interactive util to avoid circular deps
pull/20363/merge
Eugene Yurtsev 3 weeks ago committed by GitHub
parent 23fdd320bc
commit 94a838740e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -16,7 +16,7 @@ del metadata # optional, avoids polluting the results of dir(__package__)
def _warn_on_import(name: str, replacement: Optional[str] = None) -> None:
"""Warn on import of deprecated module."""
from langchain.utils.interactive_env import is_interactive_env
from langchain._api.interactive_env import is_interactive_env
if is_interactive_env():
# No warnings for interactive environments.

@ -4,7 +4,7 @@ from typing import Any, Callable, Dict, Optional
from langchain_core._api import LangChainDeprecationWarning
from langchain.utils.interactive_env import is_interactive_env
from langchain._api.interactive_env import is_interactive_env
ALLOWED_TOP_LEVEL_PKGS = {
"langchain_community",

@ -20,7 +20,7 @@ import warnings
from langchain_core._api import LangChainDeprecationWarning
from langchain.utils.interactive_env import is_interactive_env
from langchain._api.interactive_env import is_interactive_env
def __getattr__(name: str) -> None:

@ -23,7 +23,7 @@ from typing import Any, Callable, Dict, Type
from langchain_core._api import LangChainDeprecationWarning
from langchain_core.language_models.llms import BaseLLM
from langchain.utils.interactive_env import is_interactive_env
from langchain._api.interactive_env import is_interactive_env
def _import_ai21() -> Any:

@ -3,7 +3,7 @@ from typing import Any
from langchain_core._api import LangChainDeprecationWarning
from langchain.utils.interactive_env import is_interactive_env
from langchain._api.interactive_env import is_interactive_env
def __getattr__(name: str) -> Any:

@ -22,7 +22,7 @@ from typing import Any
from langchain_core._api import LangChainDeprecationWarning
from langchain_core.tools import BaseTool, StructuredTool, Tool, tool
from langchain.utils.interactive_env import is_interactive_env
from langchain._api.interactive_env import is_interactive_env
# Used for internal purposes
_DEPRECATED_TOOLS = {"PythonAstREPLTool", "PythonREPLTool"}

@ -4,6 +4,8 @@
These functions do not depend on any other LangChain module.
"""
from typing import TYPE_CHECKING, Any
from langchain_core.utils import (
comma_list,
get_from_dict_or_env,
@ -28,7 +30,28 @@ from langchain_core.utils.utils import (
xor_args,
)
from langchain.utils.math import cosine_similarity, cosine_similarity_top_k
from langchain._api import create_importer
if TYPE_CHECKING:
from langchain_community.utils.math import (
cosine_similarity,
cosine_similarity_top_k,
)
# Not deprecated right now because we will likely need to move these functions
# back into langchain (as long as we're OK with the dependency on numpy).
_MODULE_LOOKUP = {
"cosine_similarity": "langchain_community.utils.math",
"cosine_similarity_top_k": "langchain_community.utils.math",
}
_import_attribute = create_importer(__package__, module_lookup=_MODULE_LOOKUP)
def __getattr__(name: str) -> Any:
"""Look up attributes dynamically."""
return _import_attribute(name)
__all__ = [
"StrictFormatter",
@ -37,12 +60,12 @@ __all__ = [
"convert_to_secret_str",
"cosine_similarity",
"cosine_similarity_top_k",
"formatter",
"get_bolded_text",
"get_color_mapping",
"get_colored_text",
"get_from_dict_or_env",
"get_from_env",
"formatter",
"get_pydantic_field_names",
"guard_import",
"mock_now",

@ -1,7 +1,32 @@
from langchain_community.utils.math import (
Matrix,
cosine_similarity,
cosine_similarity_top_k,
)
from typing import TYPE_CHECKING, Any
__all__ = ["Matrix", "cosine_similarity", "cosine_similarity_top_k"]
from langchain._api import create_importer
if TYPE_CHECKING:
from langchain_community.utils.math import (
cosine_similarity,
cosine_similarity_top_k,
)
# Create a way to dynamically look up deprecated imports.
# Used to consolidate logic for raising deprecation warnings and
# handling optional imports.
# Not marked as deprecated since we may want to move the functionality
# into langchain as long as we're OK with numpy as the dependency.
_MODULE_LOOKUP = {
"cosine_similarity": "langchain_community.utils.math",
"cosine_similarity_top_k": "langchain_community.utils.math",
}
_import_attribute = create_importer(__package__, module_lookup=_MODULE_LOOKUP)
def __getattr__(name: str) -> Any:
"""Look up attributes dynamically."""
return _import_attribute(name)
__all__ = [
"cosine_similarity",
"cosine_similarity_top_k",
]

Loading…
Cancel
Save