Add LCAgentComponent and XMLAgentComponent
This commit is contained in:
parent
db4a4e52aa
commit
b437ca007e
3 changed files with 77 additions and 14 deletions
0
src/backend/langflow/base/agents/__init__.py
Normal file
0
src/backend/langflow/base/agents/__init__.py
Normal file
70
src/backend/langflow/base/agents/agent.py
Normal file
70
src/backend/langflow/base/agents/agent.py
Normal file
|
|
@ -0,0 +1,70 @@
|
||||||
|
from typing import List, Union
|
||||||
|
|
||||||
|
from langchain.agents import AgentExecutor, BaseMultiActionAgent, BaseSingleActionAgent
|
||||||
|
|
||||||
|
from langflow import CustomComponent
|
||||||
|
from langflow.field_typing import BaseMemory, Text, Tool
|
||||||
|
|
||||||
|
|
||||||
|
class LCAgentComponent(CustomComponent):
|
||||||
|
def build_config(self):
|
||||||
|
return {
|
||||||
|
"lc": {
|
||||||
|
"display_name": "LangChain",
|
||||||
|
"info": "The LangChain to interact with.",
|
||||||
|
},
|
||||||
|
"handle_parsing_errors": {
|
||||||
|
"display_name": "Handle Parsing Errors",
|
||||||
|
"info": "If True, the agent will handle parsing errors. If False, the agent will raise an error.",
|
||||||
|
"advanced": True,
|
||||||
|
},
|
||||||
|
"output_key": {
|
||||||
|
"display_name": "Output Key",
|
||||||
|
"info": "The key to use to get the output from the agent.",
|
||||||
|
"advanced": True,
|
||||||
|
},
|
||||||
|
"memory": {
|
||||||
|
"display_name": "Memory",
|
||||||
|
"info": "Memory to use for the agent.",
|
||||||
|
},
|
||||||
|
"tools": {
|
||||||
|
"display_name": "Tools",
|
||||||
|
"info": "Tools the agent can use.",
|
||||||
|
},
|
||||||
|
"input_value": {
|
||||||
|
"display_name": "Input",
|
||||||
|
"info": "Input text to pass to the agent.",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
async def run_agent(
|
||||||
|
self,
|
||||||
|
agent: Union[BaseSingleActionAgent, BaseMultiActionAgent, AgentExecutor],
|
||||||
|
inputs: str,
|
||||||
|
input_variables: list[str],
|
||||||
|
tools: List[Tool],
|
||||||
|
memory: BaseMemory = None,
|
||||||
|
handle_parsing_errors: bool = True,
|
||||||
|
output_key: str = "output",
|
||||||
|
) -> Text:
|
||||||
|
if isinstance(agent, AgentExecutor):
|
||||||
|
runnable = agent
|
||||||
|
else:
|
||||||
|
runnable = AgentExecutor.from_agent_and_tools(
|
||||||
|
agent=agent, tools=tools, verbose=True, memory=memory, handle_parsing_errors=handle_parsing_errors
|
||||||
|
)
|
||||||
|
input_dict = {"input": inputs}
|
||||||
|
for var in input_variables:
|
||||||
|
if var not in ["agent_scratchpad", "input"]:
|
||||||
|
input_dict[var] = ""
|
||||||
|
result = await runnable.ainvoke(input_dict)
|
||||||
|
self.status = result
|
||||||
|
if output_key in result:
|
||||||
|
return result.get(output_key)
|
||||||
|
elif "output" not in result:
|
||||||
|
if output_key != "output":
|
||||||
|
raise ValueError(f"Output key not found in result. Tried '{output_key}' and 'output'.")
|
||||||
|
else:
|
||||||
|
raise ValueError("Output key not found in result. Tried 'output'.")
|
||||||
|
|
||||||
|
return result.get("output")
|
||||||
|
|
@ -1,13 +1,13 @@
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from langchain.agents import AgentExecutor, create_xml_agent
|
from langchain.agents import create_xml_agent
|
||||||
from langchain_core.prompts import PromptTemplate
|
from langchain_core.prompts import PromptTemplate
|
||||||
|
|
||||||
from langflow import CustomComponent
|
from langflow.base.agents.agent import LCAgentComponent
|
||||||
from langflow.field_typing import BaseLLM, BaseMemory, Text, Tool
|
from langflow.field_typing import BaseLLM, BaseMemory, Text, Tool
|
||||||
|
|
||||||
|
|
||||||
class XMLAgentComponent(CustomComponent):
|
class XMLAgentComponent(LCAgentComponent):
|
||||||
display_name = "XMLAgent"
|
display_name = "XMLAgent"
|
||||||
description = "Construct an XML agent from an LLM and tools."
|
description = "Construct an XML agent from an LLM and tools."
|
||||||
|
|
||||||
|
|
@ -57,7 +57,7 @@ class XMLAgentComponent(CustomComponent):
|
||||||
"display_name": "Memory",
|
"display_name": "Memory",
|
||||||
"info": "Memory to use for the agent.",
|
"info": "Memory to use for the agent.",
|
||||||
},
|
},
|
||||||
"inputs": {
|
"input_value": {
|
||||||
"display_name": "Inputs",
|
"display_name": "Inputs",
|
||||||
"info": "Input text to pass to the agent.",
|
"info": "Input text to pass to the agent.",
|
||||||
},
|
},
|
||||||
|
|
@ -65,7 +65,7 @@ class XMLAgentComponent(CustomComponent):
|
||||||
|
|
||||||
async def build(
|
async def build(
|
||||||
self,
|
self,
|
||||||
inputs: str,
|
input_value: str,
|
||||||
llm: BaseLLM,
|
llm: BaseLLM,
|
||||||
tools: List[Tool],
|
tools: List[Tool],
|
||||||
prompt: str,
|
prompt: str,
|
||||||
|
|
@ -84,13 +84,6 @@ class XMLAgentComponent(CustomComponent):
|
||||||
prompt_template = PromptTemplate.from_template(prompt)
|
prompt_template = PromptTemplate.from_template(prompt)
|
||||||
input_variables = prompt_template.input_variables
|
input_variables = prompt_template.input_variables
|
||||||
agent = create_xml_agent(llm, tools, prompt_template, tools_renderer=render_tool_description)
|
agent = create_xml_agent(llm, tools, prompt_template, tools_renderer=render_tool_description)
|
||||||
runnable = AgentExecutor.from_agent_and_tools(
|
result = await self.run_agent(agent, input_value, input_variables, tools, memory, handle_parsing_errors)
|
||||||
agent=agent, tools=tools, verbose=True, memory=memory, handle_parsing_errors=handle_parsing_errors
|
|
||||||
)
|
|
||||||
input_dict = {"input": inputs}
|
|
||||||
for var in input_variables:
|
|
||||||
if var not in ["agent_scratchpad", "input"]:
|
|
||||||
input_dict[var] = ""
|
|
||||||
result = await runnable.ainvoke(input_dict)
|
|
||||||
self.status = result
|
self.status = result
|
||||||
return result.get("output")
|
return result
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue