Spaces:
Sleeping
Sleeping
File size: 1,839 Bytes
67dc3b9 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
from langchain_community.tools.tavily_search import TavilySearchResults
from langgraph.prebuilt import ToolNode
from typing import List, Any
class ToolManager:
"""Manages tools for the chatbot, allowing dynamic addition of multiple tools."""
def __init__(self, tools: List[Any] = None):
"""
Initialize the ToolManager with an optional list of tools.
Args:
tools (List[Any], optional): A list of initial tools. Defaults to None.
"""
self.tools: List[Any] = tools if tools else []
def add_tool(self, tool: Any) -> None:
"""
Add a single tool to the tool list.
Args:
tool (Any): A tool instance that implements the required API.
"""
if tool is not None:
self.tools.append(tool)
else:
raise ValueError("Tool cannot be None.")
def add_tools(self, *new_tools: Any) -> None:
"""
Add multiple tools at once.
Args:
*new_tools (Any): Variable-length argument list of tools.
"""
if not new_tools:
raise ValueError("No tools provided. Please add at least one tool.")
self.tools.extend(new_tools)
def get_tools(self) -> List[Any]:
"""
Retrieve the list of registered tools.
Returns:
List[Any]: A list of initialized tool instances.
"""
return self.tools
def create_tool_node(self) -> ToolNode:
"""
Creates and returns a ToolNode for the graph using registered tools.
Returns:
ToolNode: A LangGraph ToolNode containing all registered tools.
"""
if not self.tools:
raise ValueError("No tools registered. Please add tools before creating a ToolNode.")
return ToolNode(tools=self.tools)
|