From 23c5d87311fe1e5d505171912563b61f1e545561 Mon Sep 17 00:00:00 2001 From: Eugene Yurtsev Date: Wed, 1 May 2024 15:24:02 -0400 Subject: [PATCH] langchain[patch]: Migrate utils to use optional langchain_community (#21163) Migrate utils to use optional imports from langchain community --- .../langchain/utils/ernie_functions.py | 35 +++++++++++++++---- libs/langchain/langchain/utils/openai.py | 24 +++++++++++-- 2 files changed, 51 insertions(+), 8 deletions(-) diff --git a/libs/langchain/langchain/utils/ernie_functions.py b/libs/langchain/langchain/utils/ernie_functions.py index e71152a5bf5..251d5fb239e 100644 --- a/libs/langchain/langchain/utils/ernie_functions.py +++ b/libs/langchain/langchain/utils/ernie_functions.py @@ -1,9 +1,32 @@ -from langchain_community.utils.ernie_functions import ( - FunctionDescription, - ToolDescription, - convert_pydantic_to_ernie_function, - convert_pydantic_to_ernie_tool, -) +from typing import TYPE_CHECKING, Any + +from langchain._api import create_importer + +if TYPE_CHECKING: + from langchain_community.utils.ernie_functions import ( + FunctionDescription, + ToolDescription, + convert_pydantic_to_ernie_function, + convert_pydantic_to_ernie_tool, + ) + +# Create a way to dynamically look up deprecated imports. +# Used to consolidate logic for raising deprecation warnings and +# handling optional imports. +DEPRECATED_LOOKUP = { + "FunctionDescription": "langchain_community.utils.ernie_functions", + "ToolDescription": "langchain_community.utils.ernie_functions", + "convert_pydantic_to_ernie_function": "langchain_community.utils.ernie_functions", + "convert_pydantic_to_ernie_tool": "langchain_community.utils.ernie_functions", +} + +_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP) + + +def __getattr__(name: str) -> Any: + """Look up attributes dynamically.""" + return _import_attribute(name) + __all__ = [ "FunctionDescription", diff --git a/libs/langchain/langchain/utils/openai.py b/libs/langchain/langchain/utils/openai.py index dfc00bd8ebc..2b159aec853 100644 --- a/libs/langchain/langchain/utils/openai.py +++ b/libs/langchain/langchain/utils/openai.py @@ -1,3 +1,23 @@ -from langchain_community.utils.openai import is_openai_v1 +from typing import TYPE_CHECKING, Any -__all__ = ["is_openai_v1"] +from langchain._api import create_importer + +if TYPE_CHECKING: + from langchain_community.utils.openai import is_openai_v1 + +# Create a way to dynamically look up deprecated imports. +# Used to consolidate logic for raising deprecation warnings and +# handling optional imports. +DEPRECATED_LOOKUP = {"is_openai_v1": "langchain_community.utils.openai"} + +_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP) + + +def __getattr__(name: str) -> Any: + """Look up attributes dynamically.""" + return _import_attribute(name) + + +__all__ = [ + "is_openai_v1", +]