Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tool calling and openai tools do not include chat history #3030

Merged
merged 4 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/backend/base/langflow/base/agents/agent.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import abstractmethod
from typing import List, Union, cast
from typing import List, Optional, Union, cast

from langchain.agents import AgentExecutor, BaseMultiActionAgent, BaseSingleActionAgent
from langchain.agents.agent import RunnableAgent
Expand All @@ -10,8 +10,9 @@
from langflow.base.agents.utils import data_to_messages
from langflow.custom import Component
from langflow.field_typing import Text
from langflow.inputs.inputs import DataInput, InputTypes
from langflow.inputs.inputs import InputTypes
from langflow.io import BoolInput, HandleInput, IntInput, MessageTextInput
from langflow.schema import Data
from langflow.schema.message import Message
from langflow.template import Output
from langflow.utils.constants import MESSAGE_SENDER_AI
Expand Down Expand Up @@ -39,7 +40,6 @@ class LCAgentComponent(Component):
value=15,
advanced=True,
),
DataInput(name="chat_history", display_name="Chat History", is_list=True, advanced=True),
]

outputs = [
Expand Down Expand Up @@ -89,8 +89,13 @@ def get_agent_kwargs(self, flatten: bool = False) -> dict:
}
return {**base, "agent_executor_kwargs": agent_kwargs}

def get_chat_history_data(self) -> Optional[List[Data]]:
# might be overridden in subclasses
return None

async def run_agent(self, agent: AgentExecutor) -> Text:
input_dict: dict[str, str | list[BaseMessage]] = {"input": self.input_value}
self.chat_history = self.get_chat_history_data()
if self.chat_history:
input_dict["chat_history"] = data_to_messages(self.chat_history)
result = await agent.ainvoke(input_dict, config={"callbacks": [AgentAsyncHandler(self.log)]})
Expand Down
10 changes: 9 additions & 1 deletion src/backend/base/langflow/components/agents/OpenAIToolsAgent.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from typing import Optional, List

from langchain.agents import create_openai_tools_agent
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate, HumanMessagePromptTemplate

from langflow.base.agents.agent import LCToolsAgentComponent
from langflow.inputs import MultilineInput
from langflow.inputs.inputs import HandleInput
from langflow.inputs.inputs import HandleInput, DataInput
from langflow.schema import Data


class OpenAIToolsAgentComponent(LCToolsAgentComponent):
Expand All @@ -29,13 +32,18 @@ class OpenAIToolsAgentComponent(LCToolsAgentComponent):
MultilineInput(
name="user_prompt", display_name="Prompt", info="This prompt must contain 'input' key.", value="{input}"
),
DataInput(name="chat_history", display_name="Chat History", is_list=True, advanced=True),
]

def get_chat_history_data(self) -> Optional[List[Data]]:
return self.chat_history

def create_agent_runnable(self):
if "input" not in self.user_prompt:
raise ValueError("Prompt must contain 'input' key.")
messages = [
("system", self.system_prompt),
("placeholder", "{chat_history}"),
HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=["input"], template=self.user_prompt)),
("placeholder", "{agent_scratchpad}"),
]
Expand Down
10 changes: 9 additions & 1 deletion src/backend/base/langflow/components/agents/ToolCallingAgent.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from typing import Optional, List

from langchain.agents import create_tool_calling_agent
from langchain_core.prompts import ChatPromptTemplate, PromptTemplate, HumanMessagePromptTemplate
from langflow.base.agents.agent import LCToolsAgentComponent
from langflow.inputs import MultilineInput
from langflow.inputs.inputs import HandleInput
from langflow.inputs.inputs import HandleInput, DataInput
from langflow.schema import Data


class ToolCallingAgentComponent(LCToolsAgentComponent):
Expand All @@ -23,13 +26,18 @@ class ToolCallingAgentComponent(LCToolsAgentComponent):
MultilineInput(
name="user_prompt", display_name="Prompt", info="This prompt must contain 'input' key.", value="{input}"
),
DataInput(name="chat_history", display_name="Chat History", is_list=True, advanced=True),
]

def get_chat_history_data(self) -> Optional[List[Data]]:
return self.chat_history

def create_agent_runnable(self):
if "input" not in self.user_prompt:
raise ValueError("Prompt must contain 'input' key.")
messages = [
("system", self.system_prompt),
("placeholder", "{chat_history}"),
HumanMessagePromptTemplate(prompt=PromptTemplate(input_variables=["input"], template=self.user_prompt)),
("placeholder", "{agent_scratchpad}"),
]
Expand Down