Deprecate direct access to globals like debug and verbose. (#11311)

Instead of accessing `langchain.debug`, `langchain.verbose`, or
`langchain.llm_cache`, please use the new getter/setter functions in
`langchain.globals`:
- `langchain.globals.set_debug()` and `langchain.globals.get_debug()`
- `langchain.globals.set_verbose()` and
`langchain.globals.get_verbose()`
- `langchain.globals.set_llm_cache()` and
`langchain.globals.get_llm_cache()`

Using the old globals directly will now raise a warning.

---------

Co-authored-by: Harrison Chase <hw.chase.17@gmail.com>
This commit is contained in:
Predrag Gruevski
2023-10-12 18:48:04 -04:00
committed by GitHub
parent 01b7b46908
commit 9e32120cbb
22 changed files with 479 additions and 119 deletions

View File

@@ -1,5 +1,5 @@
```python
import langchain
from langchain.globals import set_llm_cache
from langchain.chat_models import ChatOpenAI
llm = ChatOpenAI()
@@ -10,7 +10,7 @@ llm = ChatOpenAI()
```python
from langchain.cache import InMemoryCache
langchain.llm_cache = InMemoryCache()
set_llm_cache(InMemoryCache())
# The first time, it is not yet in cache, so it should take longer
llm.predict("Tell me a joke")
@@ -57,7 +57,7 @@ rm .langchain.db
```python
# We can do the same thing with a SQLite cache
from langchain.cache import SQLiteCache
langchain.llm_cache = SQLiteCache(database_path=".langchain.db")
set_llm_cache(SQLiteCache(database_path=".langchain.db"))
```

View File

@@ -1,5 +1,5 @@
```python
import langchain
from langchain.globals import set_llm_cache
from langchain.llms import OpenAI
# To make the caching really obvious, lets use a slower model.
@@ -11,7 +11,7 @@ llm = OpenAI(model_name="text-davinci-002", n=2, best_of=2)
```python
from langchain.cache import InMemoryCache
langchain.llm_cache = InMemoryCache()
set_llm_cache(InMemoryCache())
# The first time, it is not yet in cache, so it should take longer
llm.predict("Tell me a joke")
@@ -58,7 +58,7 @@ rm .langchain.db
```python
# We can do the same thing with a SQLite cache
from langchain.cache import SQLiteCache
langchain.llm_cache = SQLiteCache(database_path=".langchain.db")
set_llm_cache(SQLiteCache(database_path=".langchain.db"))
```