Skip to content

Commit

Permalink
langchain[patch]: Update some agent tool kits to handle community imp…
Browse files Browse the repository at this point in the history
…ort as optional (#21157)

A few things that were not caught by the migration script
  • Loading branch information
eyurtsev committed May 1, 2024
1 parent 59f10ab commit 43110da
Show file tree
Hide file tree
Showing 8 changed files with 251 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
from langchain_community.agent_toolkits.amadeus.toolkit import AmadeusToolkit
from typing import TYPE_CHECKING, Any

from langchain._api import create_importer

if TYPE_CHECKING:
from langchain_community.agent_toolkits.amadeus.toolkit import AmadeusToolkit

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

_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)


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


__all__ = ["AmadeusToolkit"]
23 changes: 22 additions & 1 deletion libs/langchain/langchain/agents/agent_toolkits/json/prompt.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
from langchain_community.agent_toolkits.json.prompt import JSON_PREFIX, JSON_SUFFIX
from typing import TYPE_CHECKING, Any

from langchain._api import create_importer

if TYPE_CHECKING:
from langchain_community.agent_toolkits.json.prompt import JSON_PREFIX, JSON_SUFFIX

# Create a way to dynamically look up deprecated imports.
# Used to consolidate logic for raising deprecation warnings and
# handling optional imports.
DEPRECATED_LOOKUP = {
"JSON_PREFIX": "langchain_community.agent_toolkits.json.prompt",
"JSON_SUFFIX": "langchain_community.agent_toolkits.json.prompt",
}

_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)


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


__all__ = ["JSON_PREFIX", "JSON_SUFFIX"]
Original file line number Diff line number Diff line change
@@ -1,22 +1,86 @@
from langchain_community.agent_toolkits.openapi.planner_prompt import (
API_CONTROLLER_PROMPT,
API_CONTROLLER_TOOL_DESCRIPTION,
API_CONTROLLER_TOOL_NAME,
API_ORCHESTRATOR_PROMPT,
API_PLANNER_PROMPT,
API_PLANNER_TOOL_DESCRIPTION,
API_PLANNER_TOOL_NAME,
PARSING_DELETE_PROMPT,
PARSING_GET_PROMPT,
PARSING_PATCH_PROMPT,
PARSING_POST_PROMPT,
PARSING_PUT_PROMPT,
REQUESTS_DELETE_TOOL_DESCRIPTION,
REQUESTS_GET_TOOL_DESCRIPTION,
REQUESTS_PATCH_TOOL_DESCRIPTION,
REQUESTS_POST_TOOL_DESCRIPTION,
REQUESTS_PUT_TOOL_DESCRIPTION,
)
from typing import TYPE_CHECKING, Any

from langchain._api import create_importer

if TYPE_CHECKING:
from langchain_community.agent_toolkits.openapi.planner_prompt import (
API_CONTROLLER_PROMPT,
API_CONTROLLER_TOOL_DESCRIPTION,
API_CONTROLLER_TOOL_NAME,
API_ORCHESTRATOR_PROMPT,
API_PLANNER_PROMPT,
API_PLANNER_TOOL_DESCRIPTION,
API_PLANNER_TOOL_NAME,
PARSING_DELETE_PROMPT,
PARSING_GET_PROMPT,
PARSING_PATCH_PROMPT,
PARSING_POST_PROMPT,
PARSING_PUT_PROMPT,
REQUESTS_DELETE_TOOL_DESCRIPTION,
REQUESTS_GET_TOOL_DESCRIPTION,
REQUESTS_PATCH_TOOL_DESCRIPTION,
REQUESTS_POST_TOOL_DESCRIPTION,
REQUESTS_PUT_TOOL_DESCRIPTION,
)

# Create a way to dynamically look up deprecated imports.
# Used to consolidate logic for raising deprecation warnings and
# handling optional imports.
DEPRECATED_LOOKUP = {
"API_CONTROLLER_PROMPT": (
"langchain_community.agent_toolkits.openapi.planner_prompt"
),
"API_CONTROLLER_TOOL_DESCRIPTION": (
"langchain_community.agent_toolkits.openapi.planner_prompt"
),
"API_CONTROLLER_TOOL_NAME": (
"langchain_community.agent_toolkits.openapi.planner_prompt"
),
"API_ORCHESTRATOR_PROMPT": (
"langchain_community.agent_toolkits.openapi.planner_prompt"
),
"API_PLANNER_PROMPT": ("langchain_community.agent_toolkits.openapi.planner_prompt"),
"API_PLANNER_TOOL_DESCRIPTION": (
"langchain_community.agent_toolkits.openapi.planner_prompt"
),
"API_PLANNER_TOOL_NAME": (
"langchain_community.agent_toolkits.openapi.planner_prompt"
),
"PARSING_DELETE_PROMPT": (
"langchain_community.agent_toolkits.openapi.planner_prompt"
),
"PARSING_GET_PROMPT": ("langchain_community.agent_toolkits.openapi.planner_prompt"),
"PARSING_PATCH_PROMPT": (
"langchain_community.agent_toolkits.openapi.planner_prompt"
),
"PARSING_POST_PROMPT": (
"langchain_community.agent_toolkits.openapi.planner_prompt"
),
"PARSING_PUT_PROMPT": ("langchain_community.agent_toolkits.openapi.planner_prompt"),
"REQUESTS_DELETE_TOOL_DESCRIPTION": (
"langchain_community.agent_toolkits.openapi.planner_prompt"
),
"REQUESTS_GET_TOOL_DESCRIPTION": (
"langchain_community.agent_toolkits.openapi.planner_prompt"
),
"REQUESTS_PATCH_TOOL_DESCRIPTION": (
"langchain_community.agent_toolkits.openapi.planner_prompt"
),
"REQUESTS_POST_TOOL_DESCRIPTION": (
"langchain_community.agent_toolkits.openapi.planner_prompt"
),
"REQUESTS_PUT_TOOL_DESCRIPTION": (
"langchain_community.agent_toolkits.openapi.planner_prompt"
),
}

_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)


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


__all__ = [
"API_PLANNER_PROMPT",
Expand Down
32 changes: 27 additions & 5 deletions libs/langchain/langchain/agents/agent_toolkits/openapi/prompt.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
from langchain_community.agent_toolkits.openapi.prompt import (
DESCRIPTION,
OPENAPI_PREFIX,
OPENAPI_SUFFIX,
)
from typing import TYPE_CHECKING, Any

from langchain._api import create_importer

if TYPE_CHECKING:
from langchain_community.agent_toolkits.openapi.prompt import (
DESCRIPTION,
OPENAPI_PREFIX,
OPENAPI_SUFFIX,
)

# Create a way to dynamically look up deprecated imports.
# Used to consolidate logic for raising deprecation warnings and
# handling optional imports.
DEPRECATED_LOOKUP = {
"DESCRIPTION": "langchain_community.agent_toolkits.openapi.prompt",
"OPENAPI_PREFIX": "langchain_community.agent_toolkits.openapi.prompt",
"OPENAPI_SUFFIX": "langchain_community.agent_toolkits.openapi.prompt",
}

_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)


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


__all__ = ["OPENAPI_PREFIX", "OPENAPI_SUFFIX", "DESCRIPTION"]
35 changes: 29 additions & 6 deletions libs/langchain/langchain/agents/agent_toolkits/powerbi/prompt.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,32 @@
from langchain_community.agent_toolkits.powerbi.prompt import (
POWERBI_CHAT_PREFIX,
POWERBI_CHAT_SUFFIX,
POWERBI_PREFIX,
POWERBI_SUFFIX,
)
from typing import TYPE_CHECKING, Any

from langchain._api import create_importer

if TYPE_CHECKING:
from langchain_community.agent_toolkits.powerbi.prompt import (
POWERBI_CHAT_PREFIX,
POWERBI_CHAT_SUFFIX,
POWERBI_PREFIX,
POWERBI_SUFFIX,
)

# Create a way to dynamically look up deprecated imports.
# Used to consolidate logic for raising deprecation warnings and
# handling optional imports.
DEPRECATED_LOOKUP = {
"POWERBI_CHAT_PREFIX": "langchain_community.agent_toolkits.powerbi.prompt",
"POWERBI_CHAT_SUFFIX": "langchain_community.agent_toolkits.powerbi.prompt",
"POWERBI_PREFIX": "langchain_community.agent_toolkits.powerbi.prompt",
"POWERBI_SUFFIX": "langchain_community.agent_toolkits.powerbi.prompt",
}

_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)


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


__all__ = [
"POWERBI_PREFIX",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
from langchain_community.agent_toolkits.spark_sql.prompt import SQL_PREFIX, SQL_SUFFIX
from typing import TYPE_CHECKING, Any

from langchain._api import create_importer

if TYPE_CHECKING:
from langchain_community.agent_toolkits.spark_sql.prompt import (
SQL_PREFIX,
SQL_SUFFIX,
)

# Create a way to dynamically look up deprecated imports.
# Used to consolidate logic for raising deprecation warnings and
# handling optional imports.
DEPRECATED_LOOKUP = {
"SQL_PREFIX": "langchain_community.agent_toolkits.spark_sql.prompt",
"SQL_SUFFIX": "langchain_community.agent_toolkits.spark_sql.prompt",
}

_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)


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


__all__ = ["SQL_PREFIX", "SQL_SUFFIX"]
32 changes: 27 additions & 5 deletions libs/langchain/langchain/agents/agent_toolkits/sql/prompt.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
from langchain_community.agent_toolkits.sql.prompt import (
SQL_FUNCTIONS_SUFFIX,
SQL_PREFIX,
SQL_SUFFIX,
)
from typing import TYPE_CHECKING, Any

from langchain._api import create_importer

if TYPE_CHECKING:
from langchain_community.agent_toolkits.sql.prompt import (
SQL_FUNCTIONS_SUFFIX,
SQL_PREFIX,
SQL_SUFFIX,
)

# Create a way to dynamically look up deprecated imports.
# Used to consolidate logic for raising deprecation warnings and
# handling optional imports.
DEPRECATED_LOOKUP = {
"SQL_PREFIX": "langchain_community.agent_toolkits.sql.prompt",
"SQL_SUFFIX": "langchain_community.agent_toolkits.sql.prompt",
"SQL_FUNCTIONS_SUFFIX": "langchain_community.agent_toolkits.sql.prompt",
}

_import_attribute = create_importer(__package__, deprecated_lookups=DEPRECATED_LOOKUP)


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


__all__ = ["SQL_PREFIX", "SQL_SUFFIX", "SQL_FUNCTIONS_SUFFIX"]
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
"""Toolkit for interacting with a vector store."""
from typing import List

from langchain_community.tools.vectorstore.tool import (
VectorStoreQATool,
VectorStoreQAWithSourcesTool,
)
from langchain_core.language_models import BaseLanguageModel
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_core.tools import BaseTool, BaseToolkit
Expand Down Expand Up @@ -37,6 +33,15 @@ class Config:

def get_tools(self) -> List[BaseTool]:
"""Get the tools in the toolkit."""
try:
from langchain_community.tools.vectorstore.tool import (
VectorStoreQATool,
VectorStoreQAWithSourcesTool,
)
except ImportError:
raise ImportError(
"You need to install langchain-community to use this toolkit."
)
description = VectorStoreQATool.get_description(
self.vectorstore_info.name, self.vectorstore_info.description
)
Expand Down Expand Up @@ -72,6 +77,14 @@ class Config:
def get_tools(self) -> List[BaseTool]:
"""Get the tools in the toolkit."""
tools: List[BaseTool] = []
try:
from langchain_community.tools.vectorstore.tool import (
VectorStoreQATool,
)
except ImportError:
raise ImportError(
"You need to install langchain-community to use this toolkit."
)
for vectorstore_info in self.vectorstores:
description = VectorStoreQATool.get_description(
vectorstore_info.name, vectorstore_info.description
Expand Down

0 comments on commit 43110da

Please sign in to comment.