From 2fcab9acd936fd1b6d2045b09977e1ae2c21ae1b Mon Sep 17 00:00:00 2001 From: Eugene Yurtsev Date: Wed, 1 May 2024 09:33:31 -0400 Subject: [PATCH] langchain[patch]: Upgrade storage to treat langchain community as optional (#21105) --- libs/langchain/langchain/storage/__init__.py | 50 ++++++++++--------- libs/langchain/langchain/storage/redis.py | 24 ++++++++- .../langchain/storage/upstash_redis.py | 31 ++++++++++-- 3 files changed, 75 insertions(+), 30 deletions(-) diff --git a/libs/langchain/langchain/storage/__init__.py b/libs/langchain/langchain/storage/__init__.py index 0ad76ba1e06..fb4134e6e02 100644 --- a/libs/langchain/langchain/storage/__init__.py +++ b/libs/langchain/langchain/storage/__init__.py @@ -5,48 +5,52 @@ to a simple key-value interface. The primary goal of these storages is to support implementation of caching. """ -import warnings -from typing import Any +from typing import TYPE_CHECKING, Any -from langchain_core._api import LangChainDeprecationWarning from langchain_core.stores import ( InMemoryByteStore, InMemoryStore, InvalidKeyException, ) +from langchain._api import create_importer from langchain.storage._lc_store import create_kv_docstore, create_lc_store from langchain.storage.encoder_backed import EncoderBackedStore 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: - from langchain_community import storage - - # 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) + """Look up attributes dynamically.""" + return _import_attribute(name) __all__ = [ - "EncoderBackedStore", - "RedisStore", - "create_lc_store", "create_kv_docstore", - "LocalFileStore", + "create_lc_store", + "EncoderBackedStore", + "InMemoryByteStore", "InMemoryStore", "InvalidKeyException", - "InMemoryByteStore", + "LocalFileStore", + "RedisStore", "UpstashRedisByteStore", "UpstashRedisStore", ] diff --git a/libs/langchain/langchain/storage/redis.py b/libs/langchain/langchain/storage/redis.py index bea04c92b78..80a6234c054 100644 --- a/libs/langchain/langchain/storage/redis.py +++ b/libs/langchain/langchain/storage/redis.py @@ -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", +] diff --git a/libs/langchain/langchain/storage/upstash_redis.py b/libs/langchain/langchain/storage/upstash_redis.py index 590389394e5..59f3b10699d 100644 --- a/libs/langchain/langchain/storage/upstash_redis.py +++ b/libs/langchain/langchain/storage/upstash_redis.py @@ -1,6 +1,27 @@ -from langchain_community.storage.upstash_redis import ( - UpstashRedisByteStore, - UpstashRedisStore, -) +from typing import TYPE_CHECKING, Any -__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", +]