langchain[patch]: Upgrade storage to treat langchain community as optional (#21105)

This commit is contained in:
Eugene Yurtsev 2024-05-01 09:33:31 -04:00 committed by GitHub
parent ab55f6996d
commit 2fcab9acd9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 75 additions and 30 deletions

View File

@ -5,48 +5,52 @@ to a simple key-value interface.
The primary goal of these storages is to support implementation of caching. The primary goal of these storages is to support implementation of caching.
""" """
import warnings from typing import TYPE_CHECKING, Any
from typing import Any
from langchain_core._api import LangChainDeprecationWarning
from langchain_core.stores import ( from langchain_core.stores import (
InMemoryByteStore, InMemoryByteStore,
InMemoryStore, InMemoryStore,
InvalidKeyException, InvalidKeyException,
) )
from langchain._api import create_importer
from langchain.storage._lc_store import create_kv_docstore, create_lc_store from langchain.storage._lc_store import create_kv_docstore, create_lc_store
from langchain.storage.encoder_backed import EncoderBackedStore from langchain.storage.encoder_backed import EncoderBackedStore
from langchain.storage.file_system import LocalFileStore from langchain.storage.file_system import LocalFileStore
from langchain.utils.interactive_env import is_interactive_env
if TYPE_CHECKING:
from langchain_community.storage import (
RedisStore,
UpstashRedisByteStore,
UpstashRedisStore,
)
# Create a way to dynamically look up deprecated imports.
# Used to consolidate logic for raising deprecation warnings and
# handling optional imports.
DEPRECATED_LOOKUP = {
"RedisStore": "langchain_community.storage",
"UpstashRedisByteStore": "langchain_community.storage",
"UpstashRedisStore": "langchain_community.storage",
}
_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)
def __getattr__(name: str) -> Any: def __getattr__(name: str) -> Any:
from langchain_community import storage """Look up attributes dynamically."""
return _import_attribute(name)
# If not in interactive env, raise warning.
if not is_interactive_env():
warnings.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)
__all__ = [ __all__ = [
"EncoderBackedStore",
"RedisStore",
"create_lc_store",
"create_kv_docstore", "create_kv_docstore",
"LocalFileStore", "create_lc_store",
"EncoderBackedStore",
"InMemoryByteStore",
"InMemoryStore", "InMemoryStore",
"InvalidKeyException", "InvalidKeyException",
"InMemoryByteStore", "LocalFileStore",
"RedisStore",
"UpstashRedisByteStore", "UpstashRedisByteStore",
"UpstashRedisStore", "UpstashRedisStore",
] ]

View File

@ -1,3 +1,23 @@
from langchain_community.storage.redis import RedisStore from typing import TYPE_CHECKING, Any
__all__ = ["RedisStore"] from langchain._api import create_importer
if TYPE_CHECKING:
from langchain_community.storage import RedisStore
# Create a way to dynamically look up deprecated imports.
# Used to consolidate logic for raising deprecation warnings and
# handling optional imports.
DEPRECATED_LOOKUP = {"RedisStore": "langchain_community.storage"}
_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)
def __getattr__(name: str) -> Any:
"""Look up attributes dynamically."""
return _import_attribute(name)
__all__ = [
"RedisStore",
]

View File

@ -1,6 +1,27 @@
from langchain_community.storage.upstash_redis import ( from typing import TYPE_CHECKING, Any
UpstashRedisByteStore,
UpstashRedisStore,
)
__all__ = ["UpstashRedisStore", "UpstashRedisByteStore"] from langchain._api import create_importer
if TYPE_CHECKING:
from langchain_community.storage import UpstashRedisByteStore, UpstashRedisStore
# Create a way to dynamically look up deprecated imports.
# Used to consolidate logic for raising deprecation warnings and
# handling optional imports.
DEPRECATED_LOOKUP = {
"UpstashRedisStore": "langchain_community.storage",
"UpstashRedisByteStore": "langchain_community.storage",
}
_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)
def __getattr__(name: str) -> Any:
"""Look up attributes dynamically."""
return _import_attribute(name)
__all__ = [
"UpstashRedisStore",
"UpstashRedisByteStore",
]