1
1
"""Global values and configuration that apply to all of LangChain."""
2
2
3
- import warnings
4
3
from typing import TYPE_CHECKING , Optional
5
4
6
5
if TYPE_CHECKING :
7
6
from langchain_core .caches import BaseCache
8
7
9
- try :
10
- import langchain # type: ignore[import-not-found]
11
-
12
- _HAS_LANGCHAIN = True
13
- except ImportError :
14
- _HAS_LANGCHAIN = False
15
-
16
8
17
9
# DO NOT USE THESE VALUES DIRECTLY!
18
10
# Use them only via `get_<X>()` and `set_<X>()` below,
@@ -29,26 +21,6 @@ def set_verbose(value: bool) -> None: # noqa: FBT001
29
21
Args:
30
22
value: The new value for the `verbose` global setting.
31
23
"""
32
- if _HAS_LANGCHAIN :
33
- # We're about to run some deprecated code, don't report warnings from it.
34
- # The user called the correct (non-deprecated) code path and shouldn't get
35
- # warnings.
36
- with warnings .catch_warnings ():
37
- warnings .filterwarnings (
38
- "ignore" ,
39
- message = (
40
- "Importing verbose from langchain root module "
41
- "is no longer supported"
42
- ),
43
- )
44
- # N.B.: This is a workaround for an unfortunate quirk of Python's
45
- # module-level `__getattr__()` implementation:
46
- # https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
47
- #
48
- # Remove it once `langchain.verbose` is no longer supported, and once all
49
- # users have migrated to using `set_verbose()` here.
50
- langchain .verbose = value
51
-
52
24
global _verbose # noqa: PLW0603
53
25
_verbose = value
54
26
@@ -59,35 +31,7 @@ def get_verbose() -> bool:
59
31
Returns:
60
32
The value of the `verbose` global setting.
61
33
"""
62
- if _HAS_LANGCHAIN :
63
- # We're about to run some deprecated code, don't report warnings from it.
64
- # The user called the correct (non-deprecated) code path and shouldn't get
65
- # warnings.
66
- with warnings .catch_warnings ():
67
- warnings .filterwarnings (
68
- "ignore" ,
69
- message = (
70
- ".*Importing verbose from langchain root module "
71
- "is no longer supported"
72
- ),
73
- )
74
- # N.B.: This is a workaround for an unfortunate quirk of Python's
75
- # module-level `__getattr__()` implementation:
76
- # https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
77
- #
78
- # Remove it once `langchain.verbose` is no longer supported, and once all
79
- # users have migrated to using `set_verbose()` here.
80
- #
81
- # In the meantime, the `verbose` setting is considered True if either the
82
- # old or the new value are True. This accommodates users who haven't
83
- # migrated to using `set_verbose()` yet. Those users are getting
84
- # deprecation warnings directing them to use `set_verbose()` when they
85
- # import `langchain.verbose`.
86
- old_verbose = langchain .verbose
87
- else :
88
- old_verbose = False
89
-
90
- return _verbose or old_verbose
34
+ return _verbose
91
35
92
36
93
37
def set_debug (value : bool ) -> None : # noqa: FBT001
@@ -96,24 +40,6 @@ def set_debug(value: bool) -> None: # noqa: FBT001
96
40
Args:
97
41
value: The new value for the `debug` global setting.
98
42
"""
99
- if _HAS_LANGCHAIN :
100
- # We're about to run some deprecated code, don't report warnings from it.
101
- # The user called the correct (non-deprecated) code path and shouldn't get
102
- # warnings.
103
- with warnings .catch_warnings ():
104
- warnings .filterwarnings (
105
- "ignore" ,
106
- message = "Importing debug from langchain root module "
107
- "is no longer supported" ,
108
- )
109
- # N.B.: This is a workaround for an unfortunate quirk of Python's
110
- # module-level `__getattr__()` implementation:
111
- # https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
112
- #
113
- # Remove it once `langchain.debug` is no longer supported, and once all
114
- # users have migrated to using `set_debug()` here.
115
- langchain .debug = value
116
-
117
43
global _debug # noqa: PLW0603
118
44
_debug = value
119
45
@@ -124,32 +50,7 @@ def get_debug() -> bool:
124
50
Returns:
125
51
The value of the `debug` global setting.
126
52
"""
127
- if _HAS_LANGCHAIN :
128
- # We're about to run some deprecated code, don't report warnings from it.
129
- # The user called the correct (non-deprecated) code path and shouldn't get
130
- # warnings.
131
- with warnings .catch_warnings ():
132
- warnings .filterwarnings (
133
- "ignore" ,
134
- message = "Importing debug from langchain root module "
135
- "is no longer supported" ,
136
- )
137
- # N.B.: This is a workaround for an unfortunate quirk of Python's
138
- # module-level `__getattr__()` implementation:
139
- # https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
140
- #
141
- # Remove it once `langchain.debug` is no longer supported, and once all
142
- # users have migrated to using `set_debug()` here.
143
- #
144
- # In the meantime, the `debug` setting is considered True if either the old
145
- # or the new value are True. This accommodates users who haven't migrated
146
- # to using `set_debug()` yet. Those users are getting deprecation warnings
147
- # directing them to use `set_debug()` when they import `langchain.debug`.
148
- old_debug = langchain .debug
149
- else :
150
- old_debug = False
151
-
152
- return _debug or old_debug
53
+ return _debug
153
54
154
55
155
56
def set_llm_cache (value : Optional ["BaseCache" ]) -> None :
@@ -158,26 +59,6 @@ def set_llm_cache(value: Optional["BaseCache"]) -> None:
158
59
Args:
159
60
value: The new LLM cache to use. If `None`, the LLM cache is disabled.
160
61
"""
161
- if _HAS_LANGCHAIN :
162
- # We're about to run some deprecated code, don't report warnings from it.
163
- # The user called the correct (non-deprecated) code path and shouldn't get
164
- # warnings.
165
- with warnings .catch_warnings ():
166
- warnings .filterwarnings (
167
- "ignore" ,
168
- message = (
169
- "Importing llm_cache from langchain root module "
170
- "is no longer supported"
171
- ),
172
- )
173
- # N.B.: This is a workaround for an unfortunate quirk of Python's
174
- # module-level `__getattr__()` implementation:
175
- # https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
176
- #
177
- # Remove it once `langchain.llm_cache` is no longer supported, and
178
- # once all users have migrated to using `set_llm_cache()` here.
179
- langchain .llm_cache = value
180
-
181
62
global _llm_cache # noqa: PLW0603
182
63
_llm_cache = value
183
64
@@ -188,33 +69,4 @@ def get_llm_cache() -> Optional["BaseCache"]:
188
69
Returns:
189
70
The value of the `llm_cache` global setting.
190
71
"""
191
- if _HAS_LANGCHAIN :
192
- # We're about to run some deprecated code, don't report warnings from it.
193
- # The user called the correct (non-deprecated) code path and shouldn't get
194
- # warnings.
195
- with warnings .catch_warnings ():
196
- warnings .filterwarnings (
197
- "ignore" ,
198
- message = (
199
- "Importing llm_cache from langchain root module "
200
- "is no longer supported"
201
- ),
202
- )
203
- # N.B.: This is a workaround for an unfortunate quirk of Python's
204
- # module-level `__getattr__()` implementation:
205
- # https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
206
- #
207
- # Remove it once `langchain.llm_cache` is no longer supported, and
208
- # once all users have migrated to using `set_llm_cache()` here.
209
- #
210
- # In the meantime, the `llm_cache` setting returns whichever of
211
- # its two backing sources is truthy (not `None` and non-empty),
212
- # or the old value if both are falsy. This accommodates users
213
- # who haven't migrated to using `set_llm_cache()` yet.
214
- # Those users are getting deprecation warnings directing them
215
- # to use `set_llm_cache()` when they import `langchain.llm_cache`.
216
- old_llm_cache = langchain .llm_cache
217
- else :
218
- old_llm_cache = None
219
-
220
- return _llm_cache or old_llm_cache
72
+ return _llm_cache
0 commit comments