fix: fixes tool metadata order and update issue, resolves agent tool metadata update failure (#5248)

This pull request includes several changes to the langflow project, focusing on deprecating the agent_description feature and enhancing the tools' metadata handling. The most important changes include marking the agent_description as deprecated, updating the tools' metadata management, and adding tags to tools.
This commit is contained in:
Edwin Jose 2024-12-16 13:33:35 -05:00 committed by GitHub
commit 4fc7b187c4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 80 additions and 41 deletions

View file

@ -57,10 +57,11 @@ class LCAgentComponent(Component):
),
MultilineInput(
name="agent_description",
display_name="Agent Description",
display_name="Agent Description [Deprecated]",
info=(
"The description of the agent. This is only used when in Tool Mode. "
f"Defaults to '{DEFAULT_TOOLS_DESCRIPTION}' and tools are added dynamically."
f"Defaults to '{DEFAULT_TOOLS_DESCRIPTION}' and tools are added dynamically. "
"This feature is deprecated and will be removed in future versions."
),
advanced=True,
value=DEFAULT_TOOLS_DESCRIPTION,
@ -236,11 +237,11 @@ class LCToolsAgentComponent(LCAgentComponent):
component_toolkit = _get_component_toolkit()
tools_names = self._build_tools_names()
agent_description = self.get_tool_description()
# Check if tools_description is the default value
if agent_description == DEFAULT_TOOLS_DESCRIPTION:
description = f"{agent_description}{tools_names}"
else:
description = agent_description
return component_toolkit(component=self).get_tools(
# TODO: Agent Description Depreciated Feature to be removed
description = f"{agent_description}{tools_names}"
tools = component_toolkit(component=self).get_tools(
tool_name=self.get_tool_name(), tool_description=description, callbacks=self.get_langchain_callbacks()
)
if hasattr(self, "tools_metadata"):
tools = component_toolkit(component=self, metadata=self.tools_metadata).update_tools_metadata(tools=tools)
return tools

View file

@ -218,6 +218,7 @@ class ComponentToolkit:
args_schema=args_schema,
handle_tool_error=True,
callbacks=callbacks,
tags=[formatted_name],
)
)
else:
@ -229,6 +230,7 @@ class ComponentToolkit:
args_schema=args_schema,
handle_tool_error=True,
callbacks=callbacks,
tags=[formatted_name],
)
)
if len(tools) == 1 and (tool_name or tool_description):
@ -243,17 +245,37 @@ class ComponentToolkit:
raise ValueError(msg)
return tools
def get_tools_metadata_dictionary(self) -> dict:
if isinstance(self.metadata, pd.DataFrame):
try:
return {
record["tags"][0]: record
for record in self.metadata.to_dict(orient="records")
if record.get("tags")
}
except (KeyError, IndexError) as e:
msg = "Error processing metadata records: " + str(e)
raise ValueError(msg) from e
return {}
def update_tools_metadata(
self,
tools: list[BaseTool | StructuredTool],
) -> list[BaseTool]:
# update the tool_name and description according to the name and secriotion mentioned in the list
if isinstance(self.metadata, pd.DataFrame):
metadata_dict = self.metadata.to_dict(orient="records")
for tool, metadata in zip(tools, metadata_dict, strict=False):
if isinstance(tool, StructuredTool | BaseTool):
tool.name = metadata.get("name", tool.name)
tool.description = metadata.get("description", tool.description)
metadata_dict = self.get_tools_metadata_dictionary()
for tool in tools:
if isinstance(tool, StructuredTool | BaseTool) and tool.tags:
try:
tag = tool.tags[0]
except IndexError:
msg = "Tool tags cannot be empty."
raise ValueError(msg) from None
if tag in metadata_dict:
tool_metadata = metadata_dict[tag]
tool.name = tool_metadata.get("name", tool.name)
tool.description = tool_metadata.get("description", tool.description)
else:
msg = f"Expected a StructuredTool or BaseTool, got {type(tool)}"
raise TypeError(msg)

View file

@ -6,20 +6,35 @@ TOOLS_METADATA_INPUT_NAME = "tools_metadata"
TOOL_TABLE_SCHEMA = [
{
"name": "name",
"display_name": "Name",
"display_name": "Tool Name",
"type": "str",
"description": "Specify the name of the output field.",
"description": "Specify the name of the tool.",
"sortable": False,
"filterable": False,
"edit_mode": EditMode.INLINE,
},
{
"name": "description",
"display_name": "Description",
"display_name": "Tool Description",
"type": "str",
"description": "Describe the purpose of the output field.",
"description": "Describe the purpose of the tool.",
"sortable": False,
"filterable": False,
"edit_mode": EditMode.INLINE,
},
{
"name": "tags",
"display_name": "Tool Identifiers",
"type": "str",
"description": (
"These are the default identifiers for the tools and cannot be changed. "
"Tool Name and Tool Description are the only editable fields."
),
"disable_edit": True,
"sortable": False,
"filterable": False,
"edit_mode": EditMode.INLINE,
},
]
TOOLS_METADATA_INFO = "Use the table to configure the tools."

View file

@ -18,6 +18,7 @@ from langflow.base.tools.constants import (
TOOL_OUTPUT_DISPLAY_NAME,
TOOL_OUTPUT_NAME,
TOOL_TABLE_SCHEMA,
TOOLS_METADATA_INFO,
TOOLS_METADATA_INPUT_NAME,
)
from langflow.custom.tree_visitor import RequiredInputsVisitor
@ -1174,7 +1175,7 @@ class Component(CustomComponent):
tool_data = (
self.tools_metadata
if hasattr(self, TOOLS_METADATA_INPUT_NAME)
else [{"name": tool.name, "description": tool.description} for tool in tools]
else [{"name": tool.name, "description": tool.description, "tags": tool.tags} for tool in tools]
)
try:
from langflow.io import TableInput
@ -1184,7 +1185,7 @@ class Component(CustomComponent):
return TableInput(
name=TOOLS_METADATA_INPUT_NAME,
info="Use the table to configure the tools.",
info=TOOLS_METADATA_INFO,
display_name="Toolset configuration",
real_time_refresh=True,
table_schema=TOOL_TABLE_SCHEMA,

View file

@ -1656,9 +1656,9 @@
"agent_description": {
"_input_type": "MultilineInput",
"advanced": true,
"display_name": "Agent Description",
"display_name": "Agent Description [Deprecated]",
"dynamic": false,
"info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically.",
"info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically. This feature is deprecated and will be removed in future versions.",
"input_types": [
"Message"
],

View file

@ -1900,9 +1900,9 @@
"agent_description": {
"_input_type": "MultilineInput",
"advanced": true,
"display_name": "Agent Description",
"display_name": "Agent Description [Deprecated]",
"dynamic": false,
"info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically.",
"info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically. This feature is deprecated and will be removed in future versions.",
"input_types": [
"Message"
],

View file

@ -2147,9 +2147,9 @@
"agent_description": {
"_input_type": "MultilineInput",
"advanced": true,
"display_name": "Agent Description",
"display_name": "Agent Description [Deprecated]",
"dynamic": false,
"info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically.",
"info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically. This feature is deprecated and will be removed in future versions.",
"input_types": [
"Message"
],

View file

@ -721,9 +721,9 @@
"agent_description": {
"_input_type": "MultilineInput",
"advanced": true,
"display_name": "Agent Description",
"display_name": "Agent Description [Deprecated]",
"dynamic": false,
"info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically.",
"info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically. This feature is deprecated and will be removed in future versions.",
"input_types": [
"Message"
],

View file

@ -670,9 +670,9 @@
"agent_description": {
"_input_type": "MultilineInput",
"advanced": true,
"display_name": "Agent Description",
"display_name": "Agent Description [Deprecated]",
"dynamic": false,
"info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically.",
"info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically. This feature is deprecated and will be removed in future versions.",
"input_types": [
"Message"
],
@ -1248,9 +1248,9 @@
"agent_description": {
"_input_type": "MultilineInput",
"advanced": true,
"display_name": "Agent Description",
"display_name": "Agent Description [Deprecated]",
"dynamic": false,
"info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically.",
"info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically. This feature is deprecated and will be removed in future versions.",
"input_types": [
"Message"
],
@ -3141,9 +3141,9 @@
"agent_description": {
"_input_type": "MultilineInput",
"advanced": true,
"display_name": "Agent Description",
"display_name": "Agent Description [Deprecated]",
"dynamic": false,
"info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically.",
"info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically. This feature is deprecated and will be removed in future versions.",
"input_types": [
"Message"
],

View file

@ -204,9 +204,9 @@
"agent_description": {
"_input_type": "MultilineInput",
"advanced": true,
"display_name": "Agent Description",
"display_name": "Agent Description [Deprecated]",
"dynamic": false,
"info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically.",
"info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically. This feature is deprecated and will be removed in future versions.",
"input_types": [
"Message"
],

View file

@ -1299,9 +1299,9 @@
"agent_description": {
"_input_type": "MultilineInput",
"advanced": true,
"display_name": "Agent Description",
"display_name": "Agent Description [Deprecated]",
"dynamic": false,
"info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically.",
"info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically. This feature is deprecated and will be removed in future versions.",
"input_types": [
"Message"
],
@ -1877,9 +1877,9 @@
"agent_description": {
"_input_type": "MultilineInput",
"advanced": true,
"display_name": "Agent Description",
"display_name": "Agent Description [Deprecated]",
"dynamic": false,
"info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically.",
"info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically. This feature is deprecated and will be removed in future versions.",
"input_types": [
"Message"
],
@ -2455,9 +2455,9 @@
"agent_description": {
"_input_type": "MultilineInput",
"advanced": true,
"display_name": "Agent Description",
"display_name": "Agent Description [Deprecated]",
"dynamic": false,
"info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically.",
"info": "The description of the agent. This is only used when in Tool Mode. Defaults to 'A helpful assistant with access to the following tools:' and tools are added dynamically. This feature is deprecated and will be removed in future versions.",
"input_types": [
"Message"
],