Custom Component build error: error build node data : "input"
yuanzhiwei opened this issue · comments
Describe the bug
A clear and concise description of what the bug is.
Browser and Version
- Browser [chrome]
- Version [0.6]
To Reproduce
Steps to reproduce the behavior:
- I created an Agent using a custom component and passed in a template
- code show as below
Please help to find out what the problem is, thanks
From what I see, the code you provided doesn't seem to be the correct way to write an Agent for Langchain. An Agent should be a type that returns an Agent executor object, and it must be declared as a class derived from Base Agent to be usable as a Langchain Agent.
Like
from typing import Callable, List, Optional, Union
from langchain.agents import AgentExecutor, AgentType, initialize_agent, types
from langflow.field_typing import BaseChatMemory, BaseLanguageModel, Tool
from langflow.interface.custom.custom_component import CustomComponent
class AgentInitializerComponent(CustomComponent):
display_name: str = "Agent Initializer"
description: str = "Initialize a Langchain Agent."
documentation: str = "https://python.langchain.com/docs/modules/agents/agent_types/"
def build_config(self):
agents = list(types.AGENT_TO_CLASS.keys())
# field_type and required are optional
return {
"agent": {"options": agents, "value": agents[0], "display_name": "Agent Type"},
"max_iterations": {"display_name": "Max Iterations", "value": 10},
"memory": {"display_name": "Memory"},
"tools": {"display_name": "Tools"},
"llm": {"display_name": "Language Model"},
"code": {"advanced": True},
}
def build(
self,
agent: str,
llm: BaseLanguageModel,
tools: List[Tool],
max_iterations: int,
memory: Optional[BaseChatMemory] = None,
) -> Union[AgentExecutor, Callable]:
agent = AgentType(agent)
if memory:
return initialize_agent(
tools=tools,
llm=llm,
agent=agent,
memory=memory,
return_intermediate_steps=True,
handle_parsing_errors=True,
max_iterations=max_iterations,
)
return initialize_agent(
tools=tools,
llm=llm,
agent=agent,
return_intermediate_steps=True,
handle_parsing_errors=True,
max_iterations=max_iterations,
)
Like 喜欢
from typing import Callable, List, Optional, Union from langchain.agents import AgentExecutor, AgentType, initialize_agent, types from langflow.field_typing import BaseChatMemory, BaseLanguageModel, Tool from langflow.interface.custom.custom_component import CustomComponent class AgentInitializerComponent(CustomComponent): display_name: str = "Agent Initializer" description: str = "Initialize a Langchain Agent." documentation: str = "https://python.langchain.com/docs/modules/agents/agent_types/" def build_config(self): agents = list(types.AGENT_TO_CLASS.keys()) # field_type and required are optional return { "agent": {"options": agents, "value": agents[0], "display_name": "Agent Type"}, "max_iterations": {"display_name": "Max Iterations", "value": 10}, "memory": {"display_name": "Memory"}, "tools": {"display_name": "Tools"}, "llm": {"display_name": "Language Model"}, "code": {"advanced": True}, } def build( self, agent: str, llm: BaseLanguageModel, tools: List[Tool], max_iterations: int, memory: Optional[BaseChatMemory] = None, ) -> Union[AgentExecutor, Callable]: agent = AgentType(agent) if memory: return initialize_agent( tools=tools, llm=llm, agent=agent, memory=memory, return_intermediate_steps=True, handle_parsing_errors=True, max_iterations=max_iterations, ) return initialize_agent( tools=tools, llm=llm, agent=agent, return_intermediate_steps=True, handle_parsing_errors=True, max_iterations=max_iterations, )
Thank you for your reply. What if this custom component is a chain? You can see that this is actually a chain, but I named it as Agent. Is there any example?
Like 喜欢
from typing import Callable, List, Optional, Union from langchain.agents import AgentExecutor, AgentType, initialize_agent, types from langflow.field_typing import BaseChatMemory, BaseLanguageModel, Tool from langflow.interface.custom.custom_component import CustomComponent class AgentInitializerComponent(CustomComponent): display_name: str = "Agent Initializer" description: str = "Initialize a Langchain Agent." documentation: str = "https://python.langchain.com/docs/modules/agents/agent_types/" def build_config(self): agents = list(types.AGENT_TO_CLASS.keys()) # field_type and required are optional return { "agent": {"options": agents, "value": agents[0], "display_name": "Agent Type"}, "max_iterations": {"display_name": "Max Iterations", "value": 10}, "memory": {"display_name": "Memory"}, "tools": {"display_name": "Tools"}, "llm": {"display_name": "Language Model"}, "code": {"advanced": True}, } def build( self, agent: str, llm: BaseLanguageModel, tools: List[Tool], max_iterations: int, memory: Optional[BaseChatMemory] = None, ) -> Union[AgentExecutor, Callable]: agent = AgentType(agent) if memory: return initialize_agent( tools=tools, llm=llm, agent=agent, memory=memory, return_intermediate_steps=True, handle_parsing_errors=True, max_iterations=max_iterations, ) return initialize_agent( tools=tools, llm=llm, agent=agent, return_intermediate_steps=True, handle_parsing_errors=True, max_iterations=max_iterations, )
Thank you for your reply. What if this custom component is a chain? You can see that this is actually a chain, but I named it as Agent. Is there any example?
I think you are considering using custom chains or classes, and currently, this seems to be the best approach.
As you mentioned in your question, it is difficult to structure each chain as a pipeline and pass it on like in the official Langchain examples. In Langflow, since all components can be easily connected using the CustomComponent method, I do not recommend defining chains for serialization at the code level.
Your component should be sufficiently covered by the llmchain type.