Skip to content

Commit

Permalink
fix: update csvAgent to work as expected (#3790)
Browse files Browse the repository at this point in the history
* ✨ (CSVAgent.py): Add support for MessageTextInput input type and Output for building agent response
📝 (CSVAgent.py): Update input descriptions and add info for better understanding of inputs
📝 (CSVAgent.py): Update build_agent_response method to handle input and return response as a Message object
📝 (CSVAgent.py): Update build_agent method to return Union[AgentExecutor, Agent] for flexibility and handle parsing errors

* [autofix.ci] apply automated fixes

* fix: remove Union from func return type

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: italojohnny <[email protected]>
  • Loading branch information
3 people committed Sep 12, 2024
1 parent 5b51fe8 commit e44479f
Showing 1 changed file with 56 additions and 4 deletions.
60 changes: 56 additions & 4 deletions src/backend/base/langflow/components/agents/CSVAgent.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from langchain_experimental.agents.agent_toolkits.csv.base import create_csv_agent

from langflow.base.agents.agent import LCAgentComponent
from langflow.field_typing import AgentExecutor
from langflow.inputs import HandleInput, FileInput, DropdownInput
from langflow.inputs.inputs import MessageTextInput
from langflow.schema.message import Message

from langflow.template.field.base import Output


class CSVAgentComponent(LCAgentComponent):
Expand All @@ -12,16 +15,65 @@ class CSVAgentComponent(LCAgentComponent):
name = "CSVAgent"

inputs = LCAgentComponent._base_inputs + [
HandleInput(name="llm", display_name="Language Model", input_types=["LanguageModel"], required=True),
FileInput(name="path", display_name="File Path", file_types=["csv"], required=True),
HandleInput(
name="llm",
display_name="Language Model",
input_types=["LanguageModel"],
required=True,
info="An LLM Model Object (It can be found in any LLM Component).",
),
FileInput(
name="path",
display_name="File Path",
file_types=["csv"],
input_types=["str", "Message"],
required=True,
info="A CSV File or File Path.",
),
DropdownInput(
name="agent_type",
display_name="Agent Type",
advanced=True,
options=["zero-shot-react-description", "openai-functions", "openai-tools"],
value="openai-tools",
),
MessageTextInput(
name="input_value",
display_name="Text",
info="Text to be passed as input and extract info from the CSV File.",
),
]

outputs = [
Output(display_name="Response", name="response", method="build_agent_response"),
Output(display_name="Agent", name="agent", method="build_agent"),
]

def build_agent_response(self) -> Message:
agent_kwargs = {
"verbose": self.verbose,
"allow_dangerous_code": True,
}

agent_csv = create_csv_agent(
llm=self.llm, path=self.path, agent_type=self.agent_type, handle_parsing_errors=True, **agent_kwargs
)

print(f"self.inputs = {agent_csv}")

result = agent_csv.invoke({"input": self.input_value})
return Message(text=str(result["output"]))

def build_agent(self) -> AgentExecutor:
return create_csv_agent(llm=self.llm, path=self.path, agent_type=self.agent_type, **self.get_agent_kwargs())
agent_kwargs = {
"verbose": self.verbose,
"allow_dangerous_code": True,
}

agent_csv = create_csv_agent(
llm=self.llm, path=self.path, agent_type=self.agent_type, handle_parsing_errors=True, **agent_kwargs
)

self.status = Message(text=str(agent_csv))

return agent_csv

0 comments on commit e44479f

Please sign in to comment.