mirror of
https://github.com/hwchase17/langchain.git
synced 2026-04-25 01:16:55 +00:00
chore(core,langchain,langchain_v1)!: remove globals from langchain-v1, update globals in langchain-classic, langchain-core (#33251)
* Remove globals.py from langchain_v1 * Adjust langchain-core to not inspect langchain namespace
This commit is contained in:
@@ -1,18 +1,10 @@
|
||||
"""Global values and configuration that apply to all of LangChain."""
|
||||
|
||||
import warnings
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from langchain_core.caches import BaseCache
|
||||
|
||||
try:
|
||||
import langchain # type: ignore[import-not-found]
|
||||
|
||||
_HAS_LANGCHAIN = True
|
||||
except ImportError:
|
||||
_HAS_LANGCHAIN = False
|
||||
|
||||
|
||||
# DO NOT USE THESE VALUES DIRECTLY!
|
||||
# Use them only via `get_<X>()` and `set_<X>()` below,
|
||||
@@ -29,26 +21,6 @@ def set_verbose(value: bool) -> None: # noqa: FBT001
|
||||
Args:
|
||||
value: The new value for the `verbose` global setting.
|
||||
"""
|
||||
if _HAS_LANGCHAIN:
|
||||
# We're about to run some deprecated code, don't report warnings from it.
|
||||
# The user called the correct (non-deprecated) code path and shouldn't get
|
||||
# warnings.
|
||||
with warnings.catch_warnings():
|
||||
warnings.filterwarnings(
|
||||
"ignore",
|
||||
message=(
|
||||
"Importing verbose from langchain root module "
|
||||
"is no longer supported"
|
||||
),
|
||||
)
|
||||
# N.B.: This is a workaround for an unfortunate quirk of Python's
|
||||
# module-level `__getattr__()` implementation:
|
||||
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
|
||||
#
|
||||
# Remove it once `langchain.verbose` is no longer supported, and once all
|
||||
# users have migrated to using `set_verbose()` here.
|
||||
langchain.verbose = value
|
||||
|
||||
global _verbose # noqa: PLW0603
|
||||
_verbose = value
|
||||
|
||||
@@ -59,35 +31,7 @@ def get_verbose() -> bool:
|
||||
Returns:
|
||||
The value of the `verbose` global setting.
|
||||
"""
|
||||
if _HAS_LANGCHAIN:
|
||||
# We're about to run some deprecated code, don't report warnings from it.
|
||||
# The user called the correct (non-deprecated) code path and shouldn't get
|
||||
# warnings.
|
||||
with warnings.catch_warnings():
|
||||
warnings.filterwarnings(
|
||||
"ignore",
|
||||
message=(
|
||||
".*Importing verbose from langchain root module "
|
||||
"is no longer supported"
|
||||
),
|
||||
)
|
||||
# N.B.: This is a workaround for an unfortunate quirk of Python's
|
||||
# module-level `__getattr__()` implementation:
|
||||
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
|
||||
#
|
||||
# Remove it once `langchain.verbose` is no longer supported, and once all
|
||||
# users have migrated to using `set_verbose()` here.
|
||||
#
|
||||
# In the meantime, the `verbose` setting is considered True if either the
|
||||
# old or the new value are True. This accommodates users who haven't
|
||||
# migrated to using `set_verbose()` yet. Those users are getting
|
||||
# deprecation warnings directing them to use `set_verbose()` when they
|
||||
# import `langchain.verbose`.
|
||||
old_verbose = langchain.verbose
|
||||
else:
|
||||
old_verbose = False
|
||||
|
||||
return _verbose or old_verbose
|
||||
return _verbose
|
||||
|
||||
|
||||
def set_debug(value: bool) -> None: # noqa: FBT001
|
||||
@@ -96,24 +40,6 @@ def set_debug(value: bool) -> None: # noqa: FBT001
|
||||
Args:
|
||||
value: The new value for the `debug` global setting.
|
||||
"""
|
||||
if _HAS_LANGCHAIN:
|
||||
# We're about to run some deprecated code, don't report warnings from it.
|
||||
# The user called the correct (non-deprecated) code path and shouldn't get
|
||||
# warnings.
|
||||
with warnings.catch_warnings():
|
||||
warnings.filterwarnings(
|
||||
"ignore",
|
||||
message="Importing debug from langchain root module "
|
||||
"is no longer supported",
|
||||
)
|
||||
# N.B.: This is a workaround for an unfortunate quirk of Python's
|
||||
# module-level `__getattr__()` implementation:
|
||||
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
|
||||
#
|
||||
# Remove it once `langchain.debug` is no longer supported, and once all
|
||||
# users have migrated to using `set_debug()` here.
|
||||
langchain.debug = value
|
||||
|
||||
global _debug # noqa: PLW0603
|
||||
_debug = value
|
||||
|
||||
@@ -124,32 +50,7 @@ def get_debug() -> bool:
|
||||
Returns:
|
||||
The value of the `debug` global setting.
|
||||
"""
|
||||
if _HAS_LANGCHAIN:
|
||||
# We're about to run some deprecated code, don't report warnings from it.
|
||||
# The user called the correct (non-deprecated) code path and shouldn't get
|
||||
# warnings.
|
||||
with warnings.catch_warnings():
|
||||
warnings.filterwarnings(
|
||||
"ignore",
|
||||
message="Importing debug from langchain root module "
|
||||
"is no longer supported",
|
||||
)
|
||||
# N.B.: This is a workaround for an unfortunate quirk of Python's
|
||||
# module-level `__getattr__()` implementation:
|
||||
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
|
||||
#
|
||||
# Remove it once `langchain.debug` is no longer supported, and once all
|
||||
# users have migrated to using `set_debug()` here.
|
||||
#
|
||||
# In the meantime, the `debug` setting is considered True if either the old
|
||||
# or the new value are True. This accommodates users who haven't migrated
|
||||
# to using `set_debug()` yet. Those users are getting deprecation warnings
|
||||
# directing them to use `set_debug()` when they import `langchain.debug`.
|
||||
old_debug = langchain.debug
|
||||
else:
|
||||
old_debug = False
|
||||
|
||||
return _debug or old_debug
|
||||
return _debug
|
||||
|
||||
|
||||
def set_llm_cache(value: Optional["BaseCache"]) -> None:
|
||||
@@ -158,26 +59,6 @@ def set_llm_cache(value: Optional["BaseCache"]) -> None:
|
||||
Args:
|
||||
value: The new LLM cache to use. If `None`, the LLM cache is disabled.
|
||||
"""
|
||||
if _HAS_LANGCHAIN:
|
||||
# We're about to run some deprecated code, don't report warnings from it.
|
||||
# The user called the correct (non-deprecated) code path and shouldn't get
|
||||
# warnings.
|
||||
with warnings.catch_warnings():
|
||||
warnings.filterwarnings(
|
||||
"ignore",
|
||||
message=(
|
||||
"Importing llm_cache from langchain root module "
|
||||
"is no longer supported"
|
||||
),
|
||||
)
|
||||
# N.B.: This is a workaround for an unfortunate quirk of Python's
|
||||
# module-level `__getattr__()` implementation:
|
||||
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
|
||||
#
|
||||
# Remove it once `langchain.llm_cache` is no longer supported, and
|
||||
# once all users have migrated to using `set_llm_cache()` here.
|
||||
langchain.llm_cache = value
|
||||
|
||||
global _llm_cache # noqa: PLW0603
|
||||
_llm_cache = value
|
||||
|
||||
@@ -188,33 +69,4 @@ def get_llm_cache() -> Optional["BaseCache"]:
|
||||
Returns:
|
||||
The value of the `llm_cache` global setting.
|
||||
"""
|
||||
if _HAS_LANGCHAIN:
|
||||
# We're about to run some deprecated code, don't report warnings from it.
|
||||
# The user called the correct (non-deprecated) code path and shouldn't get
|
||||
# warnings.
|
||||
with warnings.catch_warnings():
|
||||
warnings.filterwarnings(
|
||||
"ignore",
|
||||
message=(
|
||||
"Importing llm_cache from langchain root module "
|
||||
"is no longer supported"
|
||||
),
|
||||
)
|
||||
# N.B.: This is a workaround for an unfortunate quirk of Python's
|
||||
# module-level `__getattr__()` implementation:
|
||||
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
|
||||
#
|
||||
# Remove it once `langchain.llm_cache` is no longer supported, and
|
||||
# once all users have migrated to using `set_llm_cache()` here.
|
||||
#
|
||||
# In the meantime, the `llm_cache` setting returns whichever of
|
||||
# its two backing sources is truthy (not `None` and non-empty),
|
||||
# or the old value if both are falsy. This accommodates users
|
||||
# who haven't migrated to using `set_llm_cache()` yet.
|
||||
# Those users are getting deprecation warnings directing them
|
||||
# to use `set_llm_cache()` when they import `langchain.llm_cache`.
|
||||
old_llm_cache = langchain.llm_cache
|
||||
else:
|
||||
old_llm_cache = None
|
||||
|
||||
return _llm_cache or old_llm_cache
|
||||
return _llm_cache
|
||||
|
||||
@@ -366,39 +366,6 @@ def __getattr__(name: str) -> Any:
|
||||
)
|
||||
|
||||
return SerpAPIWrapper
|
||||
if name == "verbose":
|
||||
from langchain.globals import _verbose
|
||||
|
||||
_warn_on_import(
|
||||
name,
|
||||
replacement=(
|
||||
"langchain.globals.set_verbose() / langchain.globals.get_verbose()"
|
||||
),
|
||||
)
|
||||
|
||||
return _verbose
|
||||
if name == "debug":
|
||||
from langchain.globals import _debug
|
||||
|
||||
_warn_on_import(
|
||||
name,
|
||||
replacement=(
|
||||
"langchain.globals.set_debug() / langchain.globals.get_debug()"
|
||||
),
|
||||
)
|
||||
|
||||
return _debug
|
||||
if name == "llm_cache":
|
||||
from langchain.globals import _llm_cache
|
||||
|
||||
_warn_on_import(
|
||||
name,
|
||||
replacement=(
|
||||
"langchain.globals.set_llm_cache() / langchain.globals.get_llm_cache()"
|
||||
),
|
||||
)
|
||||
|
||||
return _llm_cache
|
||||
msg = f"Could not find: {name}"
|
||||
raise AttributeError(msg)
|
||||
|
||||
|
||||
@@ -1,180 +1,19 @@
|
||||
"""Global values and configuration that apply to all of LangChain."""
|
||||
|
||||
import warnings
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
from langchain_core.globals import (
|
||||
get_debug,
|
||||
get_llm_cache,
|
||||
get_verbose,
|
||||
set_debug,
|
||||
set_llm_cache,
|
||||
set_verbose,
|
||||
)
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from langchain_core.caches import BaseCache
|
||||
|
||||
|
||||
# DO NOT USE THESE VALUES DIRECTLY!
|
||||
# Use them only via `get_<X>()` and `set_<X>()` below,
|
||||
# or else your code may behave unexpectedly with other uses of these global settings:
|
||||
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
|
||||
_verbose: bool = False
|
||||
_debug: bool = False
|
||||
_llm_cache: Optional["BaseCache"] = None
|
||||
|
||||
|
||||
def set_verbose(
|
||||
value: bool, # noqa: FBT001
|
||||
) -> None:
|
||||
"""Set a new value for the `verbose` global setting."""
|
||||
import langchain
|
||||
|
||||
# We're about to run some deprecated code, don't report warnings from it.
|
||||
# The user called the correct (non-deprecated) code path and shouldn't get warnings.
|
||||
with warnings.catch_warnings():
|
||||
warnings.filterwarnings(
|
||||
"ignore",
|
||||
message=(
|
||||
"Importing verbose from langchain root module is no longer supported"
|
||||
),
|
||||
)
|
||||
# N.B.: This is a workaround for an unfortunate quirk of Python's
|
||||
# module-level `__getattr__()` implementation:
|
||||
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
|
||||
#
|
||||
# Remove it once `langchain.verbose` is no longer supported, and once all users
|
||||
# have migrated to using `set_verbose()` here.
|
||||
langchain.verbose = value
|
||||
|
||||
global _verbose # noqa: PLW0603
|
||||
_verbose = value
|
||||
|
||||
|
||||
def get_verbose() -> bool:
|
||||
"""Get the value of the `verbose` global setting."""
|
||||
import langchain
|
||||
|
||||
# We're about to run some deprecated code, don't report warnings from it.
|
||||
# The user called the correct (non-deprecated) code path and shouldn't get warnings.
|
||||
with warnings.catch_warnings():
|
||||
warnings.filterwarnings(
|
||||
"ignore",
|
||||
message=(
|
||||
"Importing verbose from langchain root module is no longer supported"
|
||||
),
|
||||
)
|
||||
# N.B.: This is a workaround for an unfortunate quirk of Python's
|
||||
# module-level `__getattr__()` implementation:
|
||||
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
|
||||
#
|
||||
# Remove it once `langchain.verbose` is no longer supported, and once all users
|
||||
# have migrated to using `set_verbose()` here.
|
||||
#
|
||||
# In the meantime, the `verbose` setting is considered True if either the old
|
||||
# or the new value are True. This accommodates users who haven't migrated
|
||||
# to using `set_verbose()` yet. Those users are getting deprecation warnings
|
||||
# directing them to use `set_verbose()` when they import `langchain.verbose`.
|
||||
old_verbose = langchain.verbose
|
||||
|
||||
return _verbose or old_verbose
|
||||
|
||||
|
||||
def set_debug(
|
||||
value: bool, # noqa: FBT001
|
||||
) -> None:
|
||||
"""Set a new value for the `debug` global setting."""
|
||||
import langchain
|
||||
|
||||
# We're about to run some deprecated code, don't report warnings from it.
|
||||
# The user called the correct (non-deprecated) code path and shouldn't get warnings.
|
||||
with warnings.catch_warnings():
|
||||
warnings.filterwarnings(
|
||||
"ignore",
|
||||
message="Importing debug from langchain root module is no longer supported",
|
||||
)
|
||||
# N.B.: This is a workaround for an unfortunate quirk of Python's
|
||||
# module-level `__getattr__()` implementation:
|
||||
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
|
||||
#
|
||||
# Remove it once `langchain.debug` is no longer supported, and once all users
|
||||
# have migrated to using `set_debug()` here.
|
||||
langchain.debug = value
|
||||
|
||||
global _debug # noqa: PLW0603
|
||||
_debug = value
|
||||
|
||||
|
||||
def get_debug() -> bool:
|
||||
"""Get the value of the `debug` global setting."""
|
||||
import langchain
|
||||
|
||||
# We're about to run some deprecated code, don't report warnings from it.
|
||||
# The user called the correct (non-deprecated) code path and shouldn't get warnings.
|
||||
with warnings.catch_warnings():
|
||||
warnings.filterwarnings(
|
||||
"ignore",
|
||||
message="Importing debug from langchain root module is no longer supported",
|
||||
)
|
||||
# N.B.: This is a workaround for an unfortunate quirk of Python's
|
||||
# module-level `__getattr__()` implementation:
|
||||
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
|
||||
#
|
||||
# Remove it once `langchain.debug` is no longer supported, and once all users
|
||||
# have migrated to using `set_debug()` here.
|
||||
#
|
||||
# In the meantime, the `debug` setting is considered True if either the old
|
||||
# or the new value are True. This accommodates users who haven't migrated
|
||||
# to using `set_debug()` yet. Those users are getting deprecation warnings
|
||||
# directing them to use `set_debug()` when they import `langchain.debug`.
|
||||
old_debug = langchain.debug
|
||||
|
||||
return _debug or old_debug
|
||||
|
||||
|
||||
def set_llm_cache(value: Optional["BaseCache"]) -> None:
|
||||
"""Set a new LLM cache, overwriting the previous value, if any."""
|
||||
import langchain
|
||||
|
||||
# We're about to run some deprecated code, don't report warnings from it.
|
||||
# The user called the correct (non-deprecated) code path and shouldn't get warnings.
|
||||
with warnings.catch_warnings():
|
||||
warnings.filterwarnings(
|
||||
"ignore",
|
||||
message=(
|
||||
"Importing llm_cache from langchain root module is no longer supported"
|
||||
),
|
||||
)
|
||||
# N.B.: This is a workaround for an unfortunate quirk of Python's
|
||||
# module-level `__getattr__()` implementation:
|
||||
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
|
||||
#
|
||||
# Remove it once `langchain.llm_cache` is no longer supported, and
|
||||
# once all users have migrated to using `set_llm_cache()` here.
|
||||
langchain.llm_cache = value
|
||||
|
||||
global _llm_cache # noqa: PLW0603
|
||||
_llm_cache = value
|
||||
|
||||
|
||||
def get_llm_cache() -> "BaseCache":
|
||||
"""Get the value of the `llm_cache` global setting."""
|
||||
import langchain
|
||||
|
||||
# We're about to run some deprecated code, don't report warnings from it.
|
||||
# The user called the correct (non-deprecated) code path and shouldn't get warnings.
|
||||
with warnings.catch_warnings():
|
||||
warnings.filterwarnings(
|
||||
"ignore",
|
||||
message=(
|
||||
"Importing llm_cache from langchain root module is no longer supported"
|
||||
),
|
||||
)
|
||||
# N.B.: This is a workaround for an unfortunate quirk of Python's
|
||||
# module-level `__getattr__()` implementation:
|
||||
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
|
||||
#
|
||||
# Remove it once `langchain.llm_cache` is no longer supported, and
|
||||
# once all users have migrated to using `set_llm_cache()` here.
|
||||
#
|
||||
# In the meantime, the `llm_cache` setting returns whichever of
|
||||
# its two backing sources is truthy (not `None` and non-empty),
|
||||
# or the old value if both are falsy. This accommodates users
|
||||
# who haven't migrated to using `set_llm_cache()` yet.
|
||||
# Those users are getting deprecation warnings directing them
|
||||
# to use `set_llm_cache()` when they import `langchain.llm_cache`.
|
||||
old_llm_cache = langchain.llm_cache
|
||||
|
||||
return _llm_cache or old_llm_cache
|
||||
__all__ = [
|
||||
"get_debug",
|
||||
"get_llm_cache",
|
||||
"get_verbose",
|
||||
"set_debug",
|
||||
"set_llm_cache",
|
||||
"set_verbose",
|
||||
]
|
||||
|
||||
@@ -25,10 +25,12 @@ def test_caching() -> None:
|
||||
params = llm.dict()
|
||||
params["stop"] = None
|
||||
llm_string = str(sorted([(k, v) for k, v in params.items()]))
|
||||
get_llm_cache().update("foo", llm_string, [Generation(text="fizz")])
|
||||
cache = get_llm_cache()
|
||||
assert cache is not None
|
||||
cache.update("foo", llm_string, [Generation(text="fizz")])
|
||||
output = llm.generate(["foo", "bar", "foo"])
|
||||
expected_cache_output = [Generation(text="foo")]
|
||||
cache_output = get_llm_cache().lookup("bar", llm_string)
|
||||
cache_output = cache.lookup("bar", llm_string)
|
||||
assert cache_output == expected_cache_output
|
||||
set_llm_cache(None)
|
||||
expected_generations = [
|
||||
|
||||
@@ -22,42 +22,10 @@ def test_no_warning() -> None:
|
||||
core_set_verbose(value=False)
|
||||
|
||||
|
||||
def test_debug_is_settable_directly() -> None:
|
||||
from langchain_core.callbacks.manager import _get_debug
|
||||
|
||||
import langchain
|
||||
|
||||
previous_value = langchain.debug
|
||||
previous_fn_reading = _get_debug()
|
||||
assert previous_value == previous_fn_reading
|
||||
|
||||
# Flip the value of the flag.
|
||||
langchain.debug = not previous_value
|
||||
|
||||
new_value = langchain.debug
|
||||
new_fn_reading = _get_debug()
|
||||
|
||||
try:
|
||||
# We successfully changed the value of `debug`.
|
||||
assert new_value != previous_value
|
||||
|
||||
# If we access `debug` via a function used elsewhere in langchain,
|
||||
# it also sees the same new value.
|
||||
assert new_value == new_fn_reading
|
||||
|
||||
# If we access `debug` via `get_debug()` we also get the same value.
|
||||
assert new_value == get_debug()
|
||||
finally:
|
||||
# Make sure we don't alter global state, even if the test fails.
|
||||
# Always reset `debug` to the value it had before.
|
||||
set_debug(previous_value)
|
||||
|
||||
|
||||
def test_debug_is_settable_via_setter() -> None:
|
||||
from langchain_core import globals as langchain_globals
|
||||
from langchain_core.callbacks.manager import _get_debug
|
||||
|
||||
from langchain import globals as langchain_globals
|
||||
|
||||
previous_value = langchain_globals._debug
|
||||
previous_fn_reading = _get_debug()
|
||||
assert previous_value == previous_fn_reading
|
||||
@@ -84,38 +52,9 @@ def test_debug_is_settable_via_setter() -> None:
|
||||
set_debug(previous_value)
|
||||
|
||||
|
||||
def test_verbose_is_settable_directly() -> None:
|
||||
import langchain
|
||||
from langchain.chains.base import _get_verbosity
|
||||
|
||||
previous_value = langchain.verbose
|
||||
previous_fn_reading = _get_verbosity()
|
||||
assert previous_value == previous_fn_reading
|
||||
|
||||
# Flip the value of the flag.
|
||||
langchain.verbose = not previous_value
|
||||
|
||||
new_value = langchain.verbose
|
||||
new_fn_reading = _get_verbosity()
|
||||
|
||||
try:
|
||||
# We successfully changed the value of `verbose`.
|
||||
assert new_value != previous_value
|
||||
|
||||
# If we access `verbose` via a function used elsewhere in langchain,
|
||||
# it also sees the same new value.
|
||||
assert new_value == new_fn_reading
|
||||
|
||||
# If we access `verbose` via `get_verbose()` we also get the same value.
|
||||
assert new_value == get_verbose()
|
||||
finally:
|
||||
# Make sure we don't alter global state, even if the test fails.
|
||||
# Always reset `verbose` to the value it had before.
|
||||
set_verbose(previous_value)
|
||||
|
||||
|
||||
def test_verbose_is_settable_via_setter() -> None:
|
||||
from langchain import globals as langchain_globals
|
||||
from langchain_core import globals as langchain_globals
|
||||
|
||||
from langchain.chains.base import _get_verbosity
|
||||
|
||||
previous_value = langchain_globals._verbose
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
"""Global values and configuration that apply to all of LangChain.
|
||||
|
||||
TODO: will be removed in a future alpha version.
|
||||
"""
|
||||
|
||||
from typing import TYPE_CHECKING, Optional
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from langchain_core.caches import BaseCache
|
||||
|
||||
|
||||
# DO NOT USE THESE VALUES DIRECTLY!
|
||||
# Use them only via `get_<X>()` and `set_<X>()` below,
|
||||
# or else your code may behave unexpectedly with other uses of these global settings:
|
||||
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
|
||||
_verbose: bool = False
|
||||
_debug: bool = False
|
||||
_llm_cache: Optional["BaseCache"] = None
|
||||
Reference in New Issue
Block a user