Skip to content

Commit

Permalink
langchain[patch]: Migrate top level files to use optional langchain c…
Browse files Browse the repository at this point in the history
…ommunity (#21152)

Migrate a few top level files to treat langchain community as an optional dependency
  • Loading branch information
eyurtsev authored May 1, 2024
1 parent daab978 commit 7230e43
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 23 deletions.
71 changes: 53 additions & 18 deletions libs/langchain/langchain/cache.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,56 @@
from langchain_community.cache import (
AstraDBCache,
AstraDBSemanticCache,
AzureCosmosDBSemanticCache,
CassandraCache,
CassandraSemanticCache,
FullLLMCache,
FullMd5LLMCache,
GPTCache,
InMemoryCache,
MomentoCache,
RedisCache,
RedisSemanticCache,
SQLAlchemyCache,
SQLAlchemyMd5Cache,
SQLiteCache,
UpstashRedisCache,
)
from typing import TYPE_CHECKING, Any

from langchain._api import create_importer

if TYPE_CHECKING:
from langchain_community.cache import (
AstraDBCache,
AstraDBSemanticCache,
AzureCosmosDBSemanticCache,
CassandraCache,
CassandraSemanticCache,
FullLLMCache,
FullMd5LLMCache,
GPTCache,
InMemoryCache,
MomentoCache,
RedisCache,
RedisSemanticCache,
SQLAlchemyCache,
SQLAlchemyMd5Cache,
SQLiteCache,
UpstashRedisCache,
)

# Create a way to dynamically look up deprecated imports.
# Used to consolidate logic for raising deprecation warnings and
# handling optional imports.
DEPRECATED_LOOKUP = {
"FullLLMCache": "langchain_community.cache",
"SQLAlchemyCache": "langchain_community.cache",
"SQLiteCache": "langchain_community.cache",
"UpstashRedisCache": "langchain_community.cache",
"RedisCache": "langchain_community.cache",
"RedisSemanticCache": "langchain_community.cache",
"GPTCache": "langchain_community.cache",
"MomentoCache": "langchain_community.cache",
"InMemoryCache": "langchain_community.cache",
"CassandraCache": "langchain_community.cache",
"CassandraSemanticCache": "langchain_community.cache",
"FullMd5LLMCache": "langchain_community.cache",
"SQLAlchemyMd5Cache": "langchain_community.cache",
"AstraDBCache": "langchain_community.cache",
"AstraDBSemanticCache": "langchain_community.cache",
"AzureCosmosDBSemanticCache": "langchain_community.cache",
}

_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)


def __getattr__(name: str) -> Any:
"""Look up attributes dynamically."""
return _import_attribute(name)


__all__ = [
"FullLLMCache",
Expand Down
28 changes: 27 additions & 1 deletion libs/langchain/langchain/requests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
"""DEPRECATED: Kept for backwards compatibility."""
from langchain_community.utilities import Requests, RequestsWrapper, TextRequestsWrapper
from typing import TYPE_CHECKING, Any

from langchain._api import create_importer

if TYPE_CHECKING:
from langchain_community.utilities import (
Requests,
RequestsWrapper,
TextRequestsWrapper,
)

# Create a way to dynamically look up deprecated imports.
# Used to consolidate logic for raising deprecation warnings and
# handling optional imports.
DEPRECATED_LOOKUP = {
"Requests": "langchain_community.utilities",
"RequestsWrapper": "langchain_community.utilities",
"TextRequestsWrapper": "langchain_community.utilities",
}

_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)


def __getattr__(name: str) -> Any:
"""Look up attributes dynamically."""
return _import_attribute(name)


__all__ = [
"Requests",
Expand Down
24 changes: 22 additions & 2 deletions libs/langchain/langchain/serpapi.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
"""For backwards compatibility."""
from langchain_community.utilities.serpapi import SerpAPIWrapper
from typing import TYPE_CHECKING, Any

__all__ = ["SerpAPIWrapper"]
from langchain._api import create_importer

if TYPE_CHECKING:
from langchain_community.utilities import SerpAPIWrapper

# Create a way to dynamically look up deprecated imports.
# Used to consolidate logic for raising deprecation warnings and
# handling optional imports.
DEPRECATED_LOOKUP = {"SerpAPIWrapper": "langchain_community.utilities"}

_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)


def __getattr__(name: str) -> Any:
"""Look up attributes dynamically."""
return _import_attribute(name)


__all__ = [
"SerpAPIWrapper",
]
24 changes: 22 additions & 2 deletions libs/langchain/langchain/sql_database.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
"""Keep here for backwards compatibility."""
from langchain_community.utilities.sql_database import SQLDatabase
from typing import TYPE_CHECKING, Any

__all__ = ["SQLDatabase"]
from langchain._api import create_importer

if TYPE_CHECKING:
from langchain_community.utilities import SQLDatabase

# Create a way to dynamically look up deprecated imports.
# Used to consolidate logic for raising deprecation warnings and
# handling optional imports.
DEPRECATED_LOOKUP = {"SQLDatabase": "langchain_community.utilities"}

_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)


def __getattr__(name: str) -> Any:
"""Look up attributes dynamically."""
return _import_attribute(name)


__all__ = [
"SQLDatabase",
]

0 comments on commit 7230e43

Please sign in to comment.