Compare commits

...

4 Commits

Author SHA1 Message Date
Eugene Yurtsev
df32037497 x 2024-03-07 14:39:29 -05:00
Eugene Yurtsev
f6cfc68bcc x 2024-03-06 21:52:14 -05:00
Eugene Yurtsev
b766fb8681 x 2024-03-06 21:48:17 -05:00
Eugene Yurtsev
c581e7172a x 2024-03-06 21:42:53 -05:00
20 changed files with 286 additions and 249 deletions

View File

@@ -16,6 +16,7 @@ from .beta_decorator import (
)
from .deprecation import (
LangChainDeprecationWarning,
caller_aware_warn,
deprecated,
suppress_langchain_deprecation_warning,
surface_langchain_deprecation_warnings,
@@ -35,4 +36,5 @@ __all__ = [
"suppress_langchain_deprecation_warning",
"surface_langchain_deprecation_warnings",
"warn_deprecated",
"caller_aware_warn",
]

View File

@@ -13,6 +13,7 @@ https://github.com/matplotlib/matplotlib/blob/main/lib/matplotlib/_api/deprecati
import contextlib
import functools
import inspect
import os
import warnings
from typing import Any, Callable, Generator, Type, TypeVar
@@ -26,6 +27,15 @@ class LangChainDeprecationWarning(DeprecationWarning):
class LangChainPendingDeprecationWarning(PendingDeprecationWarning):
"""A class for issuing deprecation warnings for LangChain users."""
SURFACE_INTERNAL_WARNINGS = (
os.environ.get("SURFACE_INTERNAL_WARNINGS", "false").lower() == "true"
)
def _get_surface_internal_warnings() -> bool:
"""Mocked for testing."""
return SURFACE_INTERNAL_WARNINGS
# PUBLIC API
@@ -379,8 +389,11 @@ def warn_deprecated(
warning_cls = (
LangChainPendingDeprecationWarning if pending else LangChainDeprecationWarning
)
warning = warning_cls(message)
warnings.warn(warning, category=LangChainDeprecationWarning, stacklevel=2)
caller_aware_warn(
message,
category=warning_cls,
surface_internal_warnings=_get_surface_internal_warnings(),
)
def surface_langchain_deprecation_warnings() -> None:
@@ -394,3 +407,18 @@ def surface_langchain_deprecation_warnings() -> None:
"default",
category=LangChainDeprecationWarning,
)
def caller_aware_warn(
message: str,
*,
category: Type[Warning] = LangChainDeprecationWarning,
surface_internal_warnings: bool = False,
) -> None:
"""Warn deprecated"""
if surface_internal_warnings:
warnings.warn(message, category=category, stacklevel=2)
else:
if is_caller_internal(depth=2):
return
warnings.warn(message, category=category, stacklevel=2)

View File

@@ -1,13 +1,24 @@
import inspect
import warnings
from contextlib import contextmanager
from typing import Any, Dict
from unittest.mock import patch
import pytest
from langchain_core._api import deprecation
from langchain_core._api.deprecation import deprecated, warn_deprecated
from langchain_core.pydantic_v1 import BaseModel
@contextmanager
def surface_internal_warnings() -> None:
"""Mock surface_langchain_deprecation_warnings."""
with patch.object(deprecation, "_get_surface_internal_warnings") as func:
func.return_value = True
yield func
@pytest.mark.parametrize(
"kwargs, expected_message",
[
@@ -53,14 +64,26 @@ from langchain_core.pydantic_v1 import BaseModel
)
def test_warn_deprecated(kwargs: Dict[str, Any], expected_message: str) -> None:
"""Test warn deprecated."""
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
with surface_internal_warnings():
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
warn_deprecated(**kwargs)
warn_deprecated(**kwargs)
assert len(warning_list) == 1
warning = warning_list[0].message
assert str(warning) == expected_message
def test_internal_warnings_disabled() -> None:
"""Test that internal warnings are disabled properly"""
with patch.object(deprecation, "_get_surface_internal_warnings") as func:
func.return_value = False
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
warn_deprecated("1.0.0", pending=True)
assert len(warning_list) == 0
assert len(warning_list) == 1
warning = warning_list[0].message
assert str(warning) == expected_message
def test_undefined_deprecation_schedule() -> None:
@@ -117,143 +140,152 @@ class ClassWithDeprecatedMethods:
def test_deprecated_function() -> None:
"""Test deprecated function."""
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
assert deprecated_function() == "This is a deprecated function."
assert len(warning_list) == 1
warning = warning_list[0].message
assert str(warning) == (
"The function `deprecated_function` was deprecated in LangChain 2.0.0 "
"and will be removed in 3.0.0"
)
with surface_internal_warnings():
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
assert deprecated_function() == "This is a deprecated function."
assert len(warning_list) == 1
warning = warning_list[0].message
assert str(warning) == (
"The function `deprecated_function` was deprecated in LangChain 2.0.0 "
"and will be removed in 3.0.0"
)
doc = deprecated_function.__doc__
assert isinstance(doc, str)
assert doc.startswith("[*Deprecated*] original doc")
doc = deprecated_function.__doc__
assert isinstance(doc, str)
assert doc.startswith("[*Deprecated*] original doc")
assert not inspect.iscoroutinefunction(deprecated_function)
assert not inspect.iscoroutinefunction(deprecated_function)
@pytest.mark.asyncio
async def test_deprecated_async_function() -> None:
"""Test deprecated async function."""
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
assert (
await deprecated_async_function() == "This is a deprecated async function."
)
assert len(warning_list) == 1
warning = warning_list[0].message
assert str(warning) == (
"The function `deprecated_async_function` was deprecated "
"in LangChain 2.0.0 and will be removed in 3.0.0"
)
with surface_internal_warnings():
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
assert (
await deprecated_async_function()
== "This is a deprecated async function."
)
assert len(warning_list) == 1
warning = warning_list[0].message
assert str(warning) == (
"The function `deprecated_async_function` was deprecated "
"in LangChain 2.0.0 and will be removed in 3.0.0"
)
doc = deprecated_function.__doc__
assert isinstance(doc, str)
assert doc.startswith("[*Deprecated*] original doc")
doc = deprecated_function.__doc__
assert isinstance(doc, str)
assert doc.startswith("[*Deprecated*] original doc")
assert inspect.iscoroutinefunction(deprecated_async_function)
assert inspect.iscoroutinefunction(deprecated_async_function)
def test_deprecated_method() -> None:
"""Test deprecated method."""
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
obj = ClassWithDeprecatedMethods()
assert obj.deprecated_method() == "This is a deprecated method."
assert len(warning_list) == 1
warning = warning_list[0].message
assert str(warning) == (
"The function `deprecated_method` was deprecated in "
"LangChain 2.0.0 and will be removed in 3.0.0"
)
with surface_internal_warnings():
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
obj = ClassWithDeprecatedMethods()
assert obj.deprecated_method() == "This is a deprecated method."
assert len(warning_list) == 1
warning = warning_list[0].message
assert str(warning) == (
"The function `deprecated_method` was deprecated in "
"LangChain 2.0.0 and will be removed in 3.0.0"
)
doc = obj.deprecated_method.__doc__
assert isinstance(doc, str)
assert doc.startswith("[*Deprecated*] original doc")
doc = obj.deprecated_method.__doc__
assert isinstance(doc, str)
assert doc.startswith("[*Deprecated*] original doc")
assert not inspect.iscoroutinefunction(obj.deprecated_method)
assert not inspect.iscoroutinefunction(obj.deprecated_method)
@pytest.mark.asyncio
async def test_deprecated_async_method() -> None:
"""Test deprecated async method."""
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
obj = ClassWithDeprecatedMethods()
assert (
await obj.deprecated_async_method() == "This is a deprecated async method."
)
assert len(warning_list) == 1
warning = warning_list[0].message
assert str(warning) == (
"The function `deprecated_async_method` was deprecated in "
"LangChain 2.0.0 and will be removed in 3.0.0"
)
with surface_internal_warnings():
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
obj = ClassWithDeprecatedMethods()
assert (
await obj.deprecated_async_method()
== "This is a deprecated async method."
)
assert len(warning_list) == 1
warning = warning_list[0].message
assert str(warning) == (
"The function `deprecated_async_method` was deprecated in "
"LangChain 2.0.0 and will be removed in 3.0.0"
)
doc = obj.deprecated_method.__doc__
assert isinstance(doc, str)
assert doc.startswith("[*Deprecated*] original doc")
doc = obj.deprecated_method.__doc__
assert isinstance(doc, str)
assert doc.startswith("[*Deprecated*] original doc")
assert inspect.iscoroutinefunction(obj.deprecated_async_method)
assert inspect.iscoroutinefunction(obj.deprecated_async_method)
def test_deprecated_classmethod() -> None:
"""Test deprecated classmethod."""
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
ClassWithDeprecatedMethods.deprecated_classmethod()
assert len(warning_list) == 1
warning = warning_list[0].message
assert str(warning) == (
"The function `deprecated_classmethod` was deprecated in "
"LangChain 2.0.0 and will be removed in 3.0.0"
)
with surface_internal_warnings():
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
ClassWithDeprecatedMethods.deprecated_classmethod()
assert len(warning_list) == 1
warning = warning_list[0].message
assert str(warning) == (
"The function `deprecated_classmethod` was deprecated in "
"LangChain 2.0.0 and will be removed in 3.0.0"
)
doc = ClassWithDeprecatedMethods.deprecated_classmethod.__doc__
assert isinstance(doc, str)
assert doc.startswith("[*Deprecated*] original doc")
doc = ClassWithDeprecatedMethods.deprecated_classmethod.__doc__
assert isinstance(doc, str)
assert doc.startswith("[*Deprecated*] original doc")
def test_deprecated_staticmethod() -> None:
"""Test deprecated staticmethod."""
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
assert (
ClassWithDeprecatedMethods.deprecated_staticmethod()
== "This is a deprecated staticmethod."
)
assert len(warning_list) == 1
warning = warning_list[0].message
with surface_internal_warnings():
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
assert (
ClassWithDeprecatedMethods.deprecated_staticmethod()
== "This is a deprecated staticmethod."
)
assert len(warning_list) == 1
warning = warning_list[0].message
assert str(warning) == (
"The function `deprecated_staticmethod` was deprecated in "
"LangChain 2.0.0 and will be removed in 3.0.0"
)
doc = ClassWithDeprecatedMethods.deprecated_staticmethod.__doc__
assert isinstance(doc, str)
assert doc.startswith("[*Deprecated*] original doc")
assert str(warning) == (
"The function `deprecated_staticmethod` was deprecated in "
"LangChain 2.0.0 and will be removed in 3.0.0"
)
doc = ClassWithDeprecatedMethods.deprecated_staticmethod.__doc__
assert isinstance(doc, str)
assert doc.startswith("[*Deprecated*] original doc")
def test_deprecated_property() -> None:
"""Test deprecated staticmethod."""
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
with surface_internal_warnings():
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
obj = ClassWithDeprecatedMethods()
assert obj.deprecated_property == "This is a deprecated property."
obj = ClassWithDeprecatedMethods()
assert obj.deprecated_property == "This is a deprecated property."
assert len(warning_list) == 1
warning = warning_list[0].message
assert len(warning_list) == 1
warning = warning_list[0].message
assert str(warning) == (
"The function `deprecated_property` was deprecated in "
"LangChain 2.0.0 and will be removed in 3.0.0"
)
doc = ClassWithDeprecatedMethods.deprecated_property.__doc__
assert isinstance(doc, str)
assert doc.startswith("[*Deprecated*] original doc")
assert str(warning) == (
"The function `deprecated_property` was deprecated in "
"LangChain 2.0.0 and will be removed in 3.0.0"
)
doc = ClassWithDeprecatedMethods.deprecated_property.__doc__
assert isinstance(doc, str)
assert doc.startswith("[*Deprecated*] original doc")
def test_whole_class_deprecation() -> None:
@@ -271,27 +303,28 @@ def test_whole_class_deprecation() -> None:
"""original doc"""
return "This is a deprecated method."
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
with surface_internal_warnings():
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
obj = DeprecatedClass()
assert obj.deprecated_method() == "This is a deprecated method."
obj = DeprecatedClass()
assert obj.deprecated_method() == "This is a deprecated method."
assert len(warning_list) == 2
warning = warning_list[0].message
assert str(warning) == (
"The class `tests.unit_tests._api.test_deprecation.DeprecatedClass` was "
"deprecated in tests 2.0.0 and will be removed in 3.0.0"
)
assert len(warning_list) == 2
warning = warning_list[0].message
assert str(warning) == (
"The class `tests.unit_tests._api.test_deprecation.DeprecatedClass` was "
"deprecated in tests 2.0.0 and will be removed in 3.0.0"
)
warning = warning_list[1].message
assert str(warning) == (
"The function `deprecated_method` was deprecated in "
"LangChain 2.0.0 and will be removed in 3.0.0"
)
# [*Deprecated*] should be inserted only once:
if obj.__doc__ is not None:
assert obj.__doc__.count("[*Deprecated*]") == 1
warning = warning_list[1].message
assert str(warning) == (
"The function `deprecated_method` was deprecated in "
"LangChain 2.0.0 and will be removed in 3.0.0"
)
# [*Deprecated*] should be inserted only once:
if obj.__doc__ is not None:
assert obj.__doc__.count("[*Deprecated*]") == 1
def test_whole_class_inherited_deprecation() -> None:
@@ -326,51 +359,52 @@ def test_whole_class_inherited_deprecation() -> None:
"""original doc"""
return "This is a deprecated method."
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
with surface_internal_warnings():
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
obj = DeprecatedClass()
assert obj.deprecated_method() == "This is a deprecated method."
obj = DeprecatedClass()
assert obj.deprecated_method() == "This is a deprecated method."
assert len(warning_list) == 2
warning = warning_list[0].message
assert str(warning) == (
"The class `tests.unit_tests._api.test_deprecation.DeprecatedClass` was "
"deprecated in tests 2.0.0 and will be removed in 3.0.0"
)
assert len(warning_list) == 2
warning = warning_list[0].message
assert str(warning) == (
"The class `tests.unit_tests._api.test_deprecation.DeprecatedClass` was "
"deprecated in tests 2.0.0 and will be removed in 3.0.0"
)
warning = warning_list[1].message
assert str(warning) == (
"The function `deprecated_method` was deprecated in "
"LangChain 2.0.0 and will be removed in 3.0.0"
)
# if [*Deprecated*] was inserted only once:
if obj.__doc__ is not None:
assert obj.__doc__.count("[*Deprecated*]") == 1
warning = warning_list[1].message
assert str(warning) == (
"The function `deprecated_method` was deprecated in "
"LangChain 2.0.0 and will be removed in 3.0.0"
)
# if [*Deprecated*] was inserted only once:
if obj.__doc__ is not None:
assert obj.__doc__.count("[*Deprecated*]") == 1
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
obj = InheritedDeprecatedClass()
assert obj.deprecated_method() == "This is a deprecated method."
obj = InheritedDeprecatedClass()
assert obj.deprecated_method() == "This is a deprecated method."
assert len(warning_list) == 2
warning = warning_list[0].message
assert str(warning) == (
"The class "
"`tests.unit_tests._api.test_deprecation.InheritedDeprecatedClass` "
"was deprecated in tests 2.2.0 and will be removed in 3.2.0"
)
assert len(warning_list) == 2
warning = warning_list[0].message
assert str(warning) == (
"The class "
"`tests.unit_tests._api.test_deprecation.InheritedDeprecatedClass` "
"was deprecated in tests 2.2.0 and will be removed in 3.2.0"
)
warning = warning_list[1].message
assert str(warning) == (
"The function `deprecated_method` was deprecated in "
"LangChain 2.2.0 and will be removed in 3.2.0"
)
# if [*Deprecated*] was inserted only once:
if obj.__doc__ is not None:
assert obj.__doc__.count("[*Deprecated*]") == 1
assert "[*Deprecated*] Inherited deprecated class." in obj.__doc__
warning = warning_list[1].message
assert str(warning) == (
"The function `deprecated_method` was deprecated in "
"LangChain 2.2.0 and will be removed in 3.2.0"
)
# if [*Deprecated*] was inserted only once:
if obj.__doc__ is not None:
assert obj.__doc__.count("[*Deprecated*]") == 1
assert "[*Deprecated*] Inherited deprecated class." in obj.__doc__
# Tests with pydantic models
@@ -383,17 +417,18 @@ class MyModel(BaseModel):
def test_deprecated_method_pydantic() -> None:
"""Test deprecated method."""
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
obj = MyModel()
assert obj.deprecated_method() == "This is a deprecated method."
assert len(warning_list) == 1
warning = warning_list[0].message
assert str(warning) == (
"The function `deprecated_method` was deprecated in "
"LangChain 2.0.0 and will be removed in 3.0.0"
)
with surface_internal_warnings():
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
obj = MyModel()
assert obj.deprecated_method() == "This is a deprecated method."
assert len(warning_list) == 1
warning = warning_list[0].message
assert str(warning) == (
"The function `deprecated_method` was deprecated in "
"LangChain 2.0.0 and will be removed in 3.0.0"
)
doc = obj.deprecated_method.__doc__
assert isinstance(doc, str)
assert doc.startswith("[*Deprecated*] original doc")
doc = obj.deprecated_method.__doc__
assert isinstance(doc, str)
assert doc.startswith("[*Deprecated*] original doc")

View File

@@ -12,6 +12,7 @@ EXPECTED_ALL = [
"warn_deprecated",
"as_import_path",
"get_relative_path",
"caller_aware_warn",
]

View File

@@ -246,15 +246,14 @@ def test_chat_valid_infer_variables() -> None:
def test_chat_from_role_strings() -> None:
"""Test instantiation of chat template from role strings."""
with pytest.warns(LangChainPendingDeprecationWarning):
template = ChatPromptTemplate.from_role_strings(
[
("system", "You are a bot."),
("assistant", "hello!"),
("human", "{question}"),
("other", "{quack}"),
]
)
template = ChatPromptTemplate.from_role_strings(
[
("system", "You are a bot."),
("assistant", "hello!"),
("human", "{question}"),
("other", "{quack}"),
]
)
messages = template.format_messages(question="How are you?", quack="duck")
assert messages == [

View File

@@ -1,10 +1,12 @@
# ruff: noqa: E402
"""Main entrypoint into package."""
import warnings
from importlib import metadata
from typing import Any, Optional
from langchain_core._api.deprecation import surface_langchain_deprecation_warnings
from langchain_core._api.deprecation import (
caller_aware_warn,
surface_langchain_deprecation_warnings,
)
try:
__version__ = metadata.version(__package__)
@@ -26,13 +28,13 @@ def _warn_on_import(name: str, replacement: Optional[str] = None) -> None:
return
if replacement:
warnings.warn(
caller_aware_warn(
f"Importing {name} from langchain root module is no longer supported. "
f"Please use {replacement} instead."
f"Please use {replacement} instead.",
)
else:
warnings.warn(
f"Importing {name} from langchain root module is no longer supported."
caller_aware_warn(
f"Importing {name} from langchain root module is no longer supported.",
)

View File

@@ -13,11 +13,10 @@ whether permissions of the given toolkit are appropriate for the application.
See [Security](https://python.langchain.com/docs/security) for more information.
"""
import warnings
from pathlib import Path
from typing import Any
from langchain_core._api import LangChainDeprecationWarning
from langchain_core._api import caller_aware_warn
from langchain_core._api.path import as_import_path
from langchain.agents.agent_toolkits.conversational_retrieval.openai_functions import (
@@ -61,13 +60,12 @@ def __getattr__(name: str) -> Any:
# If not in interactive env, raise warning.
if not is_interactive_env():
warnings.warn(
caller_aware_warn(
"Importing this agent toolkit from langchain is deprecated. Importing it "
"from langchain will no longer be supported as of langchain==0.2.0. "
"Please import from langchain-community instead:\n\n"
f"`from langchain_community.agent_toolkits import {name}`.\n\n"
"To install langchain-community run `pip install -U langchain-community`.",
category=LangChainDeprecationWarning,
)
return getattr(agent_toolkits, name)

View File

@@ -6,10 +6,9 @@
BaseCallbackHandler --> <name>CallbackHandler # Example: AimCallbackHandler
"""
import warnings
from typing import Any
from langchain_core._api import LangChainDeprecationWarning
from langchain_core._api import caller_aware_warn
from langchain_core.callbacks import (
StdOutCallbackHandler,
StreamingStdOutCallbackHandler,
@@ -34,15 +33,13 @@ def __getattr__(name: str) -> Any:
# If not in interactive env, raise warning.
if not is_interactive_env():
warnings.warn(
caller_aware_warn(
"Importing this callback from langchain is deprecated. Importing it from "
"langchain will no longer be supported as of langchain==0.2.0. "
"Please import from langchain-community instead:\n\n"
f"`from langchain_community.callbacks import {name}`.\n\n"
"To install langchain-community run `pip install -U langchain-community`.",
category=LangChainDeprecationWarning,
)
return getattr(callbacks, name)

View File

@@ -16,9 +16,8 @@ an interface where "chat messages" are the inputs and outputs.
AIMessage, BaseMessage, HumanMessage
""" # noqa: E501
import warnings
from langchain_core._api import LangChainDeprecationWarning
from langchain_core._api import caller_aware_warn
from langchain.utils.interactive_env import is_interactive_env
@@ -28,13 +27,12 @@ def __getattr__(name: str) -> None:
# If not in interactive env, raise warning.
if not is_interactive_env():
warnings.warn(
caller_aware_warn(
"Importing chat models from langchain is deprecated. Importing from "
"langchain will no longer be supported as of langchain==0.2.0. "
"Please import from langchain-community instead:\n\n"
f"`from langchain_community.chat_models import {name}`.\n\n"
"To install langchain-community run `pip install -U langchain-community`.",
category=LangChainDeprecationWarning,
)
return getattr(chat_models, name)

View File

@@ -14,10 +14,9 @@ The **Docstore** is a simplified version of the Document Loader.
Document, AddableMixin
"""
import warnings
from typing import Any
from langchain_core._api import LangChainDeprecationWarning
from langchain_core._api import caller_aware_warn
from langchain.utils.interactive_env import is_interactive_env
@@ -27,13 +26,12 @@ def __getattr__(name: str) -> Any:
# If not in interactive env, raise warning.
if not is_interactive_env():
warnings.warn(
caller_aware_warn(
"Importing docstores from langchain is deprecated. Importing from "
"langchain will no longer be supported as of langchain==0.2.0. "
"Please import from langchain-community instead:\n\n"
f"`from langchain_community.docstore import {name}`.\n\n"
"To install langchain-community run `pip install -U langchain-community`.",
category=LangChainDeprecationWarning,
)
return getattr(docstore, name)

View File

@@ -14,10 +14,9 @@
Document, <name>TextSplitter
"""
import warnings
from typing import Any
from langchain_core._api import LangChainDeprecationWarning
from langchain_core._api import caller_aware_warn
from langchain.utils.interactive_env import is_interactive_env
@@ -33,17 +32,16 @@ def __getattr__(name: str) -> Any:
# If not in interactive env, raise warning.
if not is_interactive_env():
warnings.warn(
caller_aware_warn(
"Importing document loaders from langchain is deprecated. Importing from "
"langchain will no longer be supported as of langchain==0.2.0. "
"Please import from langchain-community instead:\n\n"
f"`from langchain_community.document_loaders import {name}`.\n\n"
"To install langchain-community run `pip install -U langchain-community`.",
category=LangChainDeprecationWarning,
)
if name in _old_to_new_name:
warnings.warn(
caller_aware_warn(
f"Using legacy class name {name}, use {_old_to_new_name[name]} instead."
)
name = _old_to_new_name[name]

View File

@@ -14,10 +14,9 @@
Document
""" # noqa: E501
import warnings
from typing import Any
from langchain_core._api import LangChainDeprecationWarning
from langchain_core._api import caller_aware_warn
from langchain.utils.interactive_env import is_interactive_env
@@ -27,13 +26,12 @@ def __getattr__(name: str) -> Any:
# If not in interactive env, raise warning.
if not is_interactive_env():
warnings.warn(
caller_aware_warn(
"Importing document transformers from langchain is deprecated. Importing "
"from langchain will no longer be supported as of langchain==0.2.0. "
"Please import from langchain-community instead:\n\n"
f"`from langchain_community.document_transformers import {name}`.\n\n"
"To install langchain-community run `pip install -U langchain-community`.",
category=LangChainDeprecationWarning,
)
return getattr(document_transformers, name)

View File

@@ -12,10 +12,9 @@ from different APIs and services.
import logging
import warnings
from typing import Any
from langchain_core._api import LangChainDeprecationWarning
from langchain_core._api import caller_aware_warn
from langchain.embeddings.cache import CacheBackedEmbeddings
from langchain.utils.interactive_env import is_interactive_env
@@ -26,13 +25,12 @@ def __getattr__(name: str) -> Any:
# If not in interactive env, raise warning.
if not is_interactive_env():
warnings.warn(
caller_aware_warn(
"Importing embeddings from langchain is deprecated. Importing from "
"langchain will no longer be supported as of langchain==0.2.0. "
"Please import from langchain-community instead:\n\n"
f"`from langchain_community.embeddings import {name}`.\n\n"
"To install langchain-community run `pip install -U langchain-community`.",
category=LangChainDeprecationWarning,
)
return getattr(embeddings, name)

View File

@@ -17,10 +17,9 @@ access to the large language model (**LLM**) APIs and services.
CallbackManager, AsyncCallbackManager,
AIMessage, BaseMessage
""" # noqa: E501
import warnings
from typing import Any, Callable, Dict, Type
from langchain_core._api import LangChainDeprecationWarning
from langchain_core._api import caller_aware_warn
from langchain_core.language_models.llms import BaseLLM
from langchain.utils.interactive_env import is_interactive_env
@@ -545,13 +544,12 @@ def __getattr__(name: str) -> Any:
# If not in interactive env, raise warning.
if not is_interactive_env():
warnings.warn(
caller_aware_warn(
"Importing LLMs from langchain is deprecated. Importing from "
"langchain will no longer be supported as of langchain==0.2.0. "
"Please import from langchain-community instead:\n\n"
f"`from langchain_community.llms import {name}`.\n\n"
"To install langchain-community run `pip install -U langchain-community`.",
category=LangChainDeprecationWarning,
)
if name == "type_to_cls_dict":

View File

@@ -1,7 +1,6 @@
import warnings
from typing import Any
from langchain_core._api import LangChainDeprecationWarning
from langchain_core._api import caller_aware_warn
from langchain.utils.interactive_env import is_interactive_env
@@ -11,15 +10,13 @@ def __getattr__(name: str) -> Any:
# If not in interactive env, raise warning.
if not is_interactive_env():
warnings.warn(
caller_aware_warn(
"Importing chat message histories from langchain is deprecated. Importing "
"from langchain will no longer be supported as of langchain==0.2.0. "
"Please import from langchain-community instead:\n\n"
f"`from langchain_community.chat_message_histories import {name}`.\n\n"
"To install langchain-community run `pip install -U langchain-community`.",
category=LangChainDeprecationWarning,
)
return getattr(chat_message_histories, name)

View File

@@ -17,10 +17,9 @@ the backbone of a retriever, but there are other types of retrievers as well.
Document, Serializable, Callbacks,
CallbackManagerForRetrieverRun, AsyncCallbackManagerForRetrieverRun
"""
import warnings
from typing import Any
from langchain_core._api import LangChainDeprecationWarning
from langchain_core._api import caller_aware_warn
from langchain.retrievers.contextual_compression import ContextualCompressionRetriever
from langchain.retrievers.ensemble import EnsembleRetriever
@@ -43,13 +42,12 @@ def __getattr__(name: str) -> Any:
# If not in interactive env, raise warning.
if not is_interactive_env():
warnings.warn(
caller_aware_warn(
"Importing this retriever from langchain is deprecated. Importing it from "
"langchain will no longer be supported as of langchain==0.2.0. "
"Please import from langchain-community instead:\n\n"
f"`from langchain_community.retrievers import {name}`.\n\n"
"To install langchain-community run `pip install -U langchain-community`.",
category=LangChainDeprecationWarning,
)
return getattr(retrievers, name)

View File

@@ -5,10 +5,9 @@ to a simple key-value interface.
The primary goal of these storages is to support implementation of caching.
"""
import warnings
from typing import Any
from langchain_core._api import LangChainDeprecationWarning
from langchain_core._api import caller_aware_warn
from langchain.storage._lc_store import create_kv_docstore, create_lc_store
from langchain.storage.encoder_backed import EncoderBackedStore
@@ -22,13 +21,12 @@ def __getattr__(name: str) -> Any:
# If not in interactive env, raise warning.
if not is_interactive_env():
warnings.warn(
caller_aware_warn(
"Importing stores from langchain is deprecated. Importing from "
"langchain will no longer be supported as of langchain==0.2.0. "
"Please import from langchain-community instead:\n\n"
f"`from langchain_community.storage import {name}`.\n\n"
"To install langchain-community run `pip install -U langchain-community`.",
category=LangChainDeprecationWarning,
)
return getattr(storage, name)

View File

@@ -16,10 +16,9 @@ tool for the job.
CallbackManagerForToolRun, AsyncCallbackManagerForToolRun
"""
import warnings
from typing import Any
from langchain_core._api import LangChainDeprecationWarning
from langchain_core._api import caller_aware_warn
from langchain_core.tools import BaseTool, StructuredTool, Tool, tool
from langchain.utils.interactive_env import is_interactive_env
@@ -60,14 +59,13 @@ def __getattr__(name: str) -> Any:
# If not in interactive env, raise warning.
if not is_interactive_env():
warnings.warn(
caller_aware_warn(
"Importing tools from langchain is deprecated. Importing from "
"langchain will no longer be supported as of langchain==0.2.0. "
"Please import from langchain-community instead:\n\n"
f"`from langchain_community.tools import {name}`.\n\n"
"To install langchain-community run "
"`pip install -U langchain-community`.",
category=LangChainDeprecationWarning,
)
return getattr(tools, name)

View File

@@ -3,7 +3,6 @@
Other LangChain classes use **Utilities** to interact with third-part systems
and packages.
"""
import warnings
from typing import Any
from langchain_community.utilities.requests import (
@@ -11,7 +10,7 @@ from langchain_community.utilities.requests import (
RequestsWrapper,
TextRequestsWrapper,
)
from langchain_core._api import LangChainDeprecationWarning
from langchain_core._api import caller_aware_warn
from langchain.utils.interactive_env import is_interactive_env
@@ -21,13 +20,12 @@ def __getattr__(name: str) -> Any:
# If not in interactive env, raise warning.
if not is_interactive_env():
warnings.warn(
caller_aware_warn(
"Importing this utility from langchain is deprecated. Importing it from "
"langchain will no longer be supported as of langchain==0.2.0. "
"Please import from langchain-community instead:\n\n"
f"`from langchain_community.utilities import {name}`.\n\n"
"To install langchain-community run `pip install -U langchain-community`.",
category=LangChainDeprecationWarning,
)
return getattr(utilities, name)

View File

@@ -18,10 +18,9 @@ and retrieve the data that are 'most similar' to the embedded query.
Embeddings, Document
""" # noqa: E501
import warnings
from typing import Any
from langchain_core._api import LangChainDeprecationWarning
from langchain_core._api import caller_aware_warn
from langchain_core.vectorstores import VectorStore
from langchain.utils.interactive_env import is_interactive_env
@@ -32,13 +31,12 @@ def __getattr__(name: str) -> Any:
# If not in interactive env, raise warning.
if not is_interactive_env():
warnings.warn(
caller_aware_warn(
"Importing vector stores from langchain is deprecated. Importing from "
"langchain will no longer be supported as of langchain==0.2.0. "
"Please import from langchain-community instead:\n\n"
f"`from langchain_community.vectorstores import {name}`.\n\n"
"To install langchain-community run `pip install -U langchain-community`.",
category=LangChainDeprecationWarning,
)
return getattr(vectorstores, name)