Skip to content

Commit

Permalink
[autofix.ci] apply automated fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
autofix-ci[bot] authored Jul 8, 2024
1 parent 2dfc611 commit 3385e74
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 160 deletions.
22 changes: 5 additions & 17 deletions src/backend/base/langflow/base/agents/agent.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
from abc import abstractmethod
from langchain.agents.agent import RunnableAgent

from langchain.agents import AgentExecutor, BaseMultiActionAgent, BaseSingleActionAgent
from langchain_core.messages import BaseMessage
from langchain.agents import AgentExecutor
from langchain_core.runnables import Runnable

from langflow.base.agents.utils import data_to_messages, get_agents_list
from langflow.custom import CustomComponent, Component
from langflow.field_typing import Text, Tool
from langflow.custom import Component
from langflow.inputs import BoolInput, IntInput, HandleInput
from langflow.schema import Data
from langflow.template import Output


Expand Down Expand Up @@ -64,10 +60,7 @@ def get_agent_kwargs(self, flatten: bool = False) -> dict:
**base,
**agent_kwargs,
}
return {
**base,
"agent_executor_kwargs": agent_kwargs
}
return {**base, "agent_executor_kwargs": agent_kwargs}


class LCToolsAgentComponent(LCAgentComponent):
Expand All @@ -78,20 +71,15 @@ class LCToolsAgentComponent(LCAgentComponent):
input_types=["Tool"],
is_list=True,
),
HandleInput(
name="llm",
display_name="Language Model",
input_types=["LanguageModel"],
required=True
),
HandleInput(name="llm", display_name="Language Model", input_types=["LanguageModel"], required=True),
]

def build_agent(self) -> AgentExecutor:
agent = self.creat_agent_runnable()
return AgentExecutor.from_agent_and_tools(
agent=RunnableAgent(runnable=agent, input_keys_arg=["input"], output_keys_arg=["output"]),
tools=self.tools,
**self.get_agent_kwargs(flatten=True)
**self.get_agent_kwargs(flatten=True),
)

@abstractmethod
Expand Down
3 changes: 1 addition & 2 deletions src/backend/base/langflow/base/vectorstores/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from langflow.helpers.data import docs_to_data
from langflow.io import Output
from langflow.schema import Data
from langchain.agents.agent_toolkits.vectorstore.toolkit import VectorStoreInfo


class LCVectorStoreComponent(Component):
Expand All @@ -28,7 +27,7 @@ class LCVectorStoreComponent(Component):
display_name="Vector Store",
name="vector_store",
method="cast_vector_store",
)
),
]

def _validate_outputs(self):
Expand Down
23 changes: 3 additions & 20 deletions src/backend/base/langflow/components/agents/CSVAgent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from langflow.base.agents.agent import LCAgentComponent
from langflow.field_typing import AgentExecutor
from langflow.inputs import HandleInput, FileInput, DropdownInput
from langflow.template import Output


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

inputs = LCAgentComponent._base_inputs + [
FileInput(
name="path",
display_name="File Path",
file_types=["csv"],
required=True
),
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),
DropdownInput(
name="agent_type",
display_name="Agent Type",
advanced=True,
options=["zero-shot-react-description", "openai-functions", "openai-tools"],
value="openai-tools",
),

]

def build_agent(self) -> AgentExecutor:
return create_csv_agent(
llm=self.llm,
path=self.path,
agent_type=self.agent_type,
**self.get_agent_kwargs()
)
return create_csv_agent(llm=self.llm, path=self.path, agent_type=self.agent_type, **self.get_agent_kwargs())
25 changes: 5 additions & 20 deletions src/backend/base/langflow/components/agents/JsonAgent.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
from langchain_community.tools.json.tool import JsonSpec

from langflow.base.agents.agent import LCAgentComponent
from langflow.custom import Component
from langflow.inputs import BoolInput, HandleInput, FileInput
from langflow.template import Output
from langflow.inputs import HandleInput, FileInput


class JsonAgentComponent(LCAgentComponent):
Expand All @@ -18,19 +16,10 @@ class JsonAgentComponent(LCAgentComponent):
name = "JsonAgent"

inputs = LCAgentComponent._base_inputs + [
FileInput(
name="path",
display_name="File Path",
file_types=["json", "yaml", "yml"],
required=True
),
HandleInput(
name="llm",
display_name="Language Model",
input_types=["LanguageModel"],
required=True
)
FileInput(name="path", display_name="File Path", file_types=["json", "yaml", "yml"], required=True),
HandleInput(name="llm", display_name="Language Model", input_types=["LanguageModel"], required=True),
]

def build_agent(self) -> AgentExecutor:
if self.path.endswith("yaml") or self.path.endswith("yml"):
yaml_dict = yaml.load(open(self.path, "r"), Loader=yaml.FullLoader)
Expand All @@ -39,8 +28,4 @@ def build_agent(self) -> AgentExecutor:
spec = JsonSpec.from_file(Path(self.path))
toolkit = JsonToolkit(spec=spec)

return create_json_agent(
llm=self.llm,
toolkit=toolkit,
**self.get_agent_kwargs()
)
return create_json_agent(llm=self.llm, toolkit=toolkit, **self.get_agent_kwargs())
13 changes: 5 additions & 8 deletions src/backend/base/langflow/components/agents/OpenAIToolsAgent.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from langchain.agents import create_openai_tools_agent
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate, HumanMessagePromptTemplate

from langflow.base.agents.agent import LCAgentComponent, LCToolsAgentComponent
from langflow.base.agents.agent import LCToolsAgentComponent
from langflow.inputs import MultilineInput


Expand All @@ -20,10 +20,7 @@ class OpenAIToolsAgentComponent(LCToolsAgentComponent):
value="You are a helpful assistant",
),
MultilineInput(
name="user_prompt",
display_name="Prompt",
info="This prompt must contain 'input' key.",
value="{input}"
name="user_prompt", display_name="Prompt", info="This prompt must contain 'input' key.", value="{input}"
),
]

Expand All @@ -32,8 +29,8 @@ def creat_agent_runnable(self):
raise ValueError("Prompt must contain 'input' key.")
messages = [
("system", self.system_prompt),
HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['input'], template=self.user_prompt)),
("placeholder", "{agent_scratchpad}")
HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=["input"], template=self.user_prompt)),
("placeholder", "{agent_scratchpad}"),
]
prompt = ChatPromptTemplate.from_messages(messages)
return create_openai_tools_agent(self.llm, self.tools, prompt)
return create_openai_tools_agent(self.llm, self.tools, prompt)
32 changes: 6 additions & 26 deletions src/backend/base/langflow/components/agents/OpenAPIAgent.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
import yaml
from langchain.agents import AgentExecutor
from langchain_community.agent_toolkits import create_openapi_agent
from langchain_community.agent_toolkits.json.toolkit import JsonToolkit
from langchain_community.tools.json.tool import JsonSpec
from langchain_community.agent_toolkits.openapi.toolkit import BaseToolkit, OpenAPIToolkit
from langchain_community.agent_toolkits.openapi.toolkit import OpenAPIToolkit

from langflow.base.agents.agent import LCAgentComponent
from langflow.custom import Component
from langflow.inputs import BoolInput, HandleInput, FileInput
from langchain_community.utilities.requests import TextRequestsWrapper

Expand All @@ -19,25 +17,11 @@ class OpenAPIAgentComponent(LCAgentComponent):
name = "OpenAPIAgent"

inputs = LCAgentComponent._base_inputs + [
FileInput(
name="path",
display_name="File Path",
file_types=["json", "yaml", "yml"],
required=True
),
HandleInput(
name="llm",
display_name="Language Model",
input_types=["LanguageModel"],
required=True
),
BoolInput(
name="allow_dangerous_requests",
display_name="Allow Dangerous Requests",
value=False,
required=True
)
FileInput(name="path", display_name="File Path", file_types=["json", "yaml", "yml"], required=True),
HandleInput(name="llm", display_name="Language Model", input_types=["LanguageModel"], required=True),
BoolInput(name="allow_dangerous_requests", display_name="Allow Dangerous Requests", value=False, required=True),
]

def build_agent(self) -> AgentExecutor:
if self.path.endswith("yaml") or self.path.endswith("yml"):
yaml_dict = yaml.load(open(self.path, "r"), Loader=yaml.FullLoader)
Expand All @@ -55,8 +39,4 @@ def build_agent(self) -> AgentExecutor:
agent_args = self.get_agent_kwargs()
agent_args["max_iterations"] = agent_args["agent_executor_kwargs"]["max_iterations"]
del agent_args["agent_executor_kwargs"]["max_iterations"]
return create_openapi_agent(
llm=self.llm,
toolkit=toolkit,
**agent_args
)
return create_openapi_agent(llm=self.llm, toolkit=toolkit, **agent_args)
19 changes: 3 additions & 16 deletions src/backend/base/langflow/components/agents/SQLAgent.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,8 @@ class SQLAgentComponent(LCAgentComponent):
name = "SQLAgent"

inputs = LCAgentComponent._base_inputs + [
MessageTextInput(
name="database_uri",
display_name="Database URI",
required=True
),
HandleInput(
name="llm",
display_name="Language Model",
input_types=["LanguageModel"],
required=True
)
MessageTextInput(name="database_uri", display_name="Database URI", required=True),
HandleInput(name="llm", display_name="Language Model", input_types=["LanguageModel"], required=True),
]

def build_agent(self) -> AgentExecutor:
Expand All @@ -32,8 +23,4 @@ def build_agent(self) -> AgentExecutor:
agent_args = self.get_agent_kwargs()
agent_args["max_iterations"] = agent_args["agent_executor_kwargs"]["max_iterations"]
del agent_args["agent_executor_kwargs"]["max_iterations"]
return create_sql_agent(
llm=self.llm,
toolkit=toolkit,
**agent_args
)
return create_sql_agent(llm=self.llm, toolkit=toolkit, **agent_args)
14 changes: 5 additions & 9 deletions src/backend/base/langflow/components/agents/ToolCallingAgent.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from langchain.agents import create_tool_calling_agent
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate, HumanMessagePromptTemplate
from langflow.base.agents.agent import LCAgentComponent, LCToolsAgentComponent
from langflow.base.agents.agent import LCToolsAgentComponent
from langflow.inputs import MultilineInput


Expand All @@ -19,21 +19,17 @@ class ToolCallingAgentComponent(LCToolsAgentComponent):
value="You are a helpful assistant",
),
MultilineInput(
name="user_prompt",
display_name="Prompt",
info="This prompt must contain 'input' key.",
value="{input}"
name="user_prompt", display_name="Prompt", info="This prompt must contain 'input' key.", value="{input}"
),
]


def creat_agent_runnable(self):
if "input" not in self.user_prompt:
raise ValueError("Prompt must contain 'input' key.")
messages = [
("system", self.system_prompt),
HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=['input'], template=self.user_prompt)),
("placeholder", "{agent_scratchpad}")
HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=["input"], template=self.user_prompt)),
("placeholder", "{agent_scratchpad}"),
]
prompt = ChatPromptTemplate.from_messages(messages)
return create_tool_calling_agent(self.llm, self.tools, prompt)
return create_tool_calling_agent(self.llm, self.tools, prompt)
19 changes: 3 additions & 16 deletions src/backend/base/langflow/components/agents/VectorStoreAgent.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from typing import Callable, Union

from langchain.agents import AgentExecutor, create_vectorstore_agent
from langchain.agents.agent_toolkits.vectorstore.toolkit import VectorStoreToolkit
from langflow.base.agents.agent import LCAgentComponent
Expand All @@ -12,21 +10,10 @@ class VectorStoreAgentComponent(LCAgentComponent):
name = "VectorStoreAgent"

inputs = LCAgentComponent._base_inputs + [
HandleInput(
name="llm",
display_name="Language Model",
input_types=["LanguageModel"],
required=True
),
HandleInput(
name="vectorstore",
display_name="Vector Store",
input_types=["VectorStoreInfo"],
required=True
)
HandleInput(name="llm", display_name="Language Model", input_types=["LanguageModel"], required=True),
HandleInput(name="vectorstore", display_name="Vector Store", input_types=["VectorStoreInfo"], required=True),
]

def build_agent(self) -> AgentExecutor:
toolkit = VectorStoreToolkit(vectorstore_info=self.vectorstore, llm=self.llm)
return create_vectorstore_agent(llm=self.llm, toolkit=toolkit,
**self.get_agent_kwargs())
return create_vectorstore_agent(llm=self.llm, toolkit=toolkit, **self.get_agent_kwargs())
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from langchain.agents import create_vectorstore_router_agent
from langchain.agents.agent_toolkits.vectorstore.toolkit import VectorStoreRouterToolkit, VectorStoreInfo
from langchain.agents.agent_toolkits.vectorstore.toolkit import VectorStoreRouterToolkit

from langflow.base.agents.agent import LCAgentComponent
from langchain.agents import AgentExecutor, BaseMultiActionAgent, BaseSingleActionAgent
from langchain.agents import AgentExecutor
from langflow.inputs import HandleInput


Expand All @@ -12,22 +12,16 @@ class VectorStoreRouterAgentComponent(LCAgentComponent):
name = "VectorStoreRouterAgent"

inputs = LCAgentComponent._base_inputs + [
HandleInput(
name="llm",
display_name="Language Model",
input_types=["LanguageModel"],
required=True
),
HandleInput(name="llm", display_name="Language Model", input_types=["LanguageModel"], required=True),
HandleInput(
name="vectorstores",
display_name="Vector Stores",
input_types=["VectorStoreInfo"],
is_list=True,
required=True
)
required=True,
),
]

def build_agent(self) -> AgentExecutor:
toolkit = VectorStoreRouterToolkit(vectorstores=self.vectorstores, llm=self.llm)
return create_vectorstore_router_agent(llm=self.llm, toolkit=toolkit,
**self.get_agent_kwargs())
return create_vectorstore_router_agent(llm=self.llm, toolkit=toolkit, **self.get_agent_kwargs())
Loading

0 comments on commit 3385e74

Please sign in to comment.