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.agents.load_tools import get_all_tool_names
from langflow.custom import customs
from langflow.interface.custom_lists import (
@ -8,6 +7,7 @@ from langflow.interface.custom_lists import (
)
from langflow.settings import settings
from langflow.utils import util
from langflow.utils.constants import ALL_TOOLS_NAMES
def get_type_dict():
@ -51,7 +51,7 @@ def list_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))
if tool_params and tool_params["name"] in settings.tools or settings.dev:
tools.append(tool_params["name"])

View file

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

View file

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

View file

@ -2,7 +2,6 @@
# Insights:
# - Defer prompts building to the last moment or when they have all the tools
# - Build each inner agent first, then build the outer agent
from langchain.agents.load_tools import get_all_tool_names
from copy import deepcopy
import types
@ -10,6 +9,7 @@ from typing import Any, Dict, List, Optional, Union
from langflow.interface import loading
from langflow.utils import payload, util
from langflow.interface.listing import ALL_TYPES_DICT
from langflow.utils.constants import ALL_TOOLS_NAMES
class Node:
@ -351,7 +351,6 @@ class Graph:
def _build_nodes(self) -> List[Node]:
nodes = []
all_tool_names = set(get_all_tool_names())
for node in self._nodes:
node_data = node["data"]
node_type = node_data["type"]
@ -363,7 +362,7 @@ class Graph:
nodes.append(AgentNode(node))
elif "chain" in node_type.lower():
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))
else:
nodes.append(Node(node))
@ -377,32 +376,3 @@ class Graph:
if node_type in node_types:
children.append(node)
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()