fix: ALL_TOOLS_NAMES constant reduces reruns

This commit is contained in:
Gabriel Almeida 2023-03-28 10:16:12 -03:00
commit 9918e466d4
4 changed files with 9 additions and 38 deletions

View file

@ -1,5 +1,4 @@
from langchain import agents, chains, prompts from langchain import agents, chains, prompts
from langchain.agents.load_tools import get_all_tool_names
from langflow.custom import customs from langflow.custom import customs
from langflow.interface.custom_lists import ( from langflow.interface.custom_lists import (
@ -8,6 +7,7 @@ from langflow.interface.custom_lists import (
) )
from langflow.settings import settings from langflow.settings import settings
from langflow.utils import util from langflow.utils import util
from langflow.utils.constants import ALL_TOOLS_NAMES
def get_type_dict(): def get_type_dict():
@ -51,7 +51,7 @@ def list_tools():
tools = [] tools = []
for tool in get_all_tool_names(): for tool in list(ALL_TOOLS_NAMES):
tool_params = util.get_tool_params(util.get_tool_by_name(tool)) tool_params = util.get_tool_params(util.get_tool_by_name(tool))
if tool_params and tool_params["name"] in settings.tools or settings.dev: if tool_params and tool_params["name"] in settings.tools or settings.dev:
tools.append(tool_params["name"]) tools.append(tool_params["name"])

View file

@ -6,7 +6,6 @@ from langchain.agents.load_tools import (
_EXTRA_LLM_TOOLS, _EXTRA_LLM_TOOLS,
_EXTRA_OPTIONAL_TOOLS, _EXTRA_OPTIONAL_TOOLS,
_LLM_TOOLS, _LLM_TOOLS,
get_all_tool_names,
) )
from langflow.custom import customs from langflow.custom import customs
@ -16,7 +15,7 @@ from langflow.interface.custom_lists import (
) )
from langflow.node.template import Field, Template from langflow.node.template import Field, Template
from langflow.utils import util from langflow.utils import util
from langflow.utils.constants import CUSTOM_TOOLS from langflow.utils.constants import ALL_TOOLS_NAMES, CUSTOM_TOOLS
def get_signature(name: str, object_type: str): def get_signature(name: str, object_type: str):
@ -86,8 +85,7 @@ def get_tool_signature(name: str):
NODE_INPUTS = ["llm", "func"] NODE_INPUTS = ["llm", "func"]
base_classes = ["Tool"] base_classes = ["Tool"]
all_tools = {} all_tools = {}
all_tool_names: list[str] = get_all_tool_names() + list(CUSTOM_TOOLS.keys()) for tool in ALL_TOOLS_NAMES:
for tool in all_tool_names:
if tool_params := util.get_tool_params(util.get_tool_by_name(tool)): if tool_params := util.get_tool_params(util.get_tool_by_name(tool)):
tool_name = tool_params.get("name") or str(tool) tool_name = tool_params.get("name") or str(tool)
all_tools[tool_name] = {"type": tool, "params": tool_params} all_tools[tool_name] = {"type": tool, "params": tool_params}

View file

@ -1,5 +1,6 @@
from langchain.agents import Tool from langchain.agents import Tool
from langflow.interface.custom_types import PythonFunction from langflow.interface.custom_types import PythonFunction
from langchain.agents.load_tools import get_all_tool_names
OPENAI_MODELS = [ OPENAI_MODELS = [
"text-davinci-003", "text-davinci-003",
@ -16,3 +17,5 @@ DEFAULT_PYTHON_FUNCTION = """
def python_function(text: str) -> str: def python_function(text: str) -> str:
return text return text
""" """
ALL_TOOLS_NAMES = set(get_all_tool_names() + list(CUSTOM_TOOLS.keys()))

View file

@ -2,7 +2,6 @@
# Insights: # Insights:
# - Defer prompts building to the last moment or when they have all the tools # - Defer prompts building to the last moment or when they have all the tools
# - Build each inner agent first, then build the outer agent # - Build each inner agent first, then build the outer agent
from langchain.agents.load_tools import get_all_tool_names
from copy import deepcopy from copy import deepcopy
import types import types
@ -10,6 +9,7 @@ from typing import Any, Dict, List, Optional, Union
from langflow.interface import loading from langflow.interface import loading
from langflow.utils import payload, util from langflow.utils import payload, util
from langflow.interface.listing import ALL_TYPES_DICT from langflow.interface.listing import ALL_TYPES_DICT
from langflow.utils.constants import ALL_TOOLS_NAMES
class Node: class Node:
@ -351,7 +351,6 @@ class Graph:
def _build_nodes(self) -> List[Node]: def _build_nodes(self) -> List[Node]:
nodes = [] nodes = []
all_tool_names = set(get_all_tool_names())
for node in self._nodes: for node in self._nodes:
node_data = node["data"] node_data = node["data"]
node_type = node_data["type"] node_type = node_data["type"]
@ -363,7 +362,7 @@ class Graph:
nodes.append(AgentNode(node)) nodes.append(AgentNode(node))
elif "chain" in node_type.lower(): elif "chain" in node_type.lower():
nodes.append(ChainNode(node)) nodes.append(ChainNode(node))
elif "tool" in node_type.lower() or node_lc_type in all_tool_names: elif "tool" in node_type.lower() or node_lc_type in ALL_TOOLS_NAMES:
nodes.append(ToolNode(node)) nodes.append(ToolNode(node))
else: else:
nodes.append(Node(node)) nodes.append(Node(node))
@ -377,32 +376,3 @@ class Graph:
if node_type in node_types: if node_type in node_types:
children.append(node) children.append(node)
return children return children
def _build_agent(self, agent_node: Node) -> None:
# Identify the ZeroShotPrompt node and any inner ZeroShotAgent nodes
zero_shot_prompt_node = None
inner_agent_nodes = []
for edge in agent_node.edges:
if edge.source == agent_node:
source_node = edge.target
if (
isinstance(source_node, DeferredNode)
and source_node.node_type == "ZeroShotPrompt"
):
zero_shot_prompt_node = source_node
elif source_node.node_type == "ZeroShotAgent":
inner_agent_nodes.append(source_node)
# First, build any inner ZeroShotAgent nodes
for inner_agent_node in inner_agent_nodes:
self._build_agent(inner_agent_node)
# Build the ZeroShotAgent node itself
agent_built = agent_node.build()
if zero_shot_prompt_node:
# Set the tools parameter in the ZeroShotPrompt node
zero_shot_prompt_node.params["tools"] = agent_built.tools
# Build the ZeroShotPrompt node
zero_shot_prompt_node.build()