Get the value of the `llm_cache` global setting.
()
| 168 | |
| 169 | |
| 170 | def get_llm_cache() -> "BaseCache": |
| 171 | """Get the value of the `llm_cache` global setting.""" |
| 172 | try: |
| 173 | import langchain # type: ignore[import] |
| 174 | |
| 175 | # We're about to run some deprecated code, don't report warnings from it. |
| 176 | # The user called the correct (non-deprecated) code path and shouldn't get warnings. |
| 177 | with warnings.catch_warnings(): |
| 178 | warnings.filterwarnings( |
| 179 | "ignore", |
| 180 | message=( |
| 181 | "Importing llm_cache from langchain root module is no longer supported" |
| 182 | ), |
| 183 | ) |
| 184 | # N.B.: This is a workaround for an unfortunate quirk of Python's |
| 185 | # module-level `__getattr__()` implementation: |
| 186 | # https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004 |
| 187 | # |
| 188 | # Remove it once `langchain.llm_cache` is no longer supported, and |
| 189 | # once all users have migrated to using `set_llm_cache()` here. |
| 190 | # |
| 191 | # In the meantime, the `llm_cache` setting returns whichever of |
| 192 | # its two backing sources is truthy (not `None` and non-empty), |
| 193 | # or the old value if both are falsy. This accommodates users |
| 194 | # who haven't migrated to using `set_llm_cache()` yet. |
| 195 | # Those users are getting deprecation warnings directing them |
| 196 | # to use `set_llm_cache()` when they import `langhchain.llm_cache`. |
| 197 | old_llm_cache = langchain.llm_cache |
| 198 | except ImportError: |
| 199 | old_llm_cache = None |
| 200 | |
| 201 | global _llm_cache |
| 202 | return _llm_cache or old_llm_cache |
no outgoing calls