2023-05-02 02:07:26 +00:00
|
|
|
from typing import List, Type
|
|
|
|
|
2023-12-11 21:53:30 +00:00
|
|
|
from langchain_core.tools import BaseTool, StructuredTool
|
|
|
|
|
|
|
|
import langchain_community.tools
|
|
|
|
from langchain_community.tools import _DEPRECATED_TOOLS
|
|
|
|
from langchain_community.tools import __all__ as tools_all
|
2023-05-02 02:07:26 +00:00
|
|
|
|
|
|
|
_EXCLUDE = {
|
|
|
|
BaseTool,
|
|
|
|
StructuredTool,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def _get_tool_classes(skip_tools_without_default_names: bool) -> List[Type[BaseTool]]:
|
|
|
|
results = []
|
|
|
|
for tool_class_name in tools_all:
|
2023-10-27 18:16:42 +00:00
|
|
|
if tool_class_name in _DEPRECATED_TOOLS:
|
|
|
|
continue
|
2023-05-02 02:07:26 +00:00
|
|
|
# Resolve the str to the class
|
2023-12-11 21:53:30 +00:00
|
|
|
tool_class = getattr(langchain_community.tools, tool_class_name)
|
2023-05-02 02:07:26 +00:00
|
|
|
if isinstance(tool_class, type) and issubclass(tool_class, BaseTool):
|
|
|
|
if tool_class in _EXCLUDE:
|
|
|
|
continue
|
2023-12-20 19:51:33 +00:00
|
|
|
if skip_tools_without_default_names and tool_class.__fields__[
|
|
|
|
"name"
|
|
|
|
].default in [ # type: ignore
|
|
|
|
None,
|
|
|
|
"",
|
|
|
|
]:
|
2023-05-02 02:07:26 +00:00
|
|
|
continue
|
|
|
|
results.append(tool_class)
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
|
|
def test_tool_names_unique() -> None:
|
|
|
|
"""Test that the default names for our core tools are unique."""
|
|
|
|
tool_classes = _get_tool_classes(skip_tools_without_default_names=True)
|
|
|
|
names = sorted([tool_cls.__fields__["name"].default for tool_cls in tool_classes])
|
|
|
|
duplicated_names = [name for name in names if names.count(name) > 1]
|
|
|
|
assert not duplicated_names
|