Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 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
6 changes: 5 additions & 1 deletion src/backend/base/langflow/base/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ async def run_agent(
if hasattr(self, "system_prompt"):
input_dict["system_prompt"] = self.system_prompt
if hasattr(self, "chat_history") and self.chat_history:
input_dict["chat_history"] = data_to_messages(self.chat_history)
logger.info(f"Chat history: {self.chat_history}")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cleanup logs

if isinstance(self.chat_history, Data):
input_dict["chat_history"] = data_to_messages(self.chat_history)
if all(isinstance(m, Message) for m in self.chat_history):
input_dict["chat_history"] = data_to_messages([m.to_data() for m in self.chat_history])

if hasattr(self, "graph"):
session_id = self.graph.session_id
Expand Down
34 changes: 23 additions & 11 deletions src/backend/base/langflow/components/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from langflow.custom.custom_component.component import _get_component_toolkit
from langflow.custom.utils import update_component_build_config
from langflow.field_typing import Tool
from langflow.io import BoolInput, DropdownInput, MultilineInput, Output
from langflow.io import BoolInput, DropdownInput, IntInput, MultilineInput, Output
from langflow.logging import logger
from langflow.schema.dotdict import dotdict
from langflow.schema.message import Message
Expand All @@ -27,6 +27,9 @@ def set_advanced_true(component_input):
return component_input


MODEL_PROVIDERS_LIST = ["Anthropic", "Google Generative AI", "Groq", "OpenAI"]


class AgentComponent(ToolCallingAgentComponent):
display_name: str = "Agent"
description: str = "Define the agent's instructions, then enter a task to complete using tools."
Expand All @@ -41,11 +44,11 @@ class AgentComponent(ToolCallingAgentComponent):
name="agent_llm",
display_name="Model Provider",
info="The provider of the language model that the agent will use to generate responses.",
options=[*sorted(MODEL_PROVIDERS), "Custom"],
options=[*MODEL_PROVIDERS_LIST, "Custom"],
value="OpenAI",
real_time_refresh=True,
input_types=[],
options_metadata=[MODELS_METADATA[key] for key in sorted(MODEL_PROVIDERS)] + [{"icon": "brain"}],
options_metadata=[MODELS_METADATA[key] for key in MODEL_PROVIDERS_LIST] + [{"icon": "brain"}],
),
*MODEL_PROVIDERS_DICT["OpenAI"]["inputs"],
MultilineInput(
Expand All @@ -55,8 +58,17 @@ class AgentComponent(ToolCallingAgentComponent):
value="You are a helpful assistant that can use tools to answer questions and perform tasks.",
advanced=False,
),
IntInput(
name="n_messages",
display_name="Number of Chat History Messages",
value=100,
info="Number of chat history messages to retrieve.",
advanced=True,
show=True,
),
*LCToolsAgentComponent._base_inputs,
*memory_inputs,
# removed memory inputs from agent component
# *memory_inputs,
Comment on lines +70 to +71
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are these needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to keep track since it is a major change. We can remove the comments if required.

BoolInput(
name="add_current_date_tool",
display_name="Current Date",
Expand All @@ -78,6 +90,8 @@ async def message_response(self) -> Message:

# Get memory data
self.chat_history = await self.get_memory_data()
if isinstance(self.chat_history, Message):
self.chat_history = [self.chat_history]

# Add current date tool if enabled
if self.add_current_date_tool:
Expand Down Expand Up @@ -112,13 +126,11 @@ async def message_response(self) -> Message:
raise

async def get_memory_data(self):
memory_kwargs = {
component_input.name: getattr(self, f"{component_input.name}") for component_input in self.memory_inputs
}
# filter out empty values
memory_kwargs = {k: v for k, v in memory_kwargs.items() if v is not None}

return await MemoryComponent(**self.get_base_args()).set(**memory_kwargs).retrieve_messages()
return (
await MemoryComponent(**self.get_base_args())
.set(session_id=self.graph.session_id, order="Ascending", n_messages=self.n_messages)
.retrieve_messages()
)

def get_llm(self):
if not isinstance(self.agent_llm, str):
Expand Down
15 changes: 10 additions & 5 deletions src/backend/base/langflow/components/helpers/memory.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class MemoryComponent(Component):
value=100,
info="Number of messages to retrieve.",
advanced=True,
show=False,
show=True,
),
MessageTextInput(
name="session_id",
Expand Down Expand Up @@ -197,28 +197,33 @@ async def retrieve_messages(self) -> Data:

stored = await self.memory.aget_messages()
# langchain memories are supposed to return messages in ascending order

if order == "DESC":
stored = stored[::-1]
if n_messages:
stored = stored[:n_messages]
stored = stored[-n_messages:] if order == "ASC" else stored[:n_messages]
stored = [Message.from_lc_message(m) for m in stored]
if sender_type:
expected_type = MESSAGE_SENDER_AI if sender_type == MESSAGE_SENDER_AI else MESSAGE_SENDER_USER
stored = [m for m in stored if m.type == expected_type]
else:
# For internal memory, we always fetch the last N messages by ordering by DESC
stored = await aget_messages(
sender=sender_type,
sender_name=sender_name,
session_id=session_id,
limit=n_messages,
limit=10000,
order=order,
)
self.status = stored
if n_messages:
stored = stored[-n_messages:] if order == "ASC" else stored[:n_messages]

# self.status = stored
return cast(Data, stored)

async def retrieve_messages_as_text(self) -> Message:
stored_text = data_to_text(self.template, await self.retrieve_messages())
self.status = stored_text
# self.status = stored_text
return Message(text=stored_text)

async def retrieve_messages_dataframe(self) -> DataFrame:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def get_base_args(self):
"""
return {
"_user_id": self.user_id,
"_session_id": self.session_id,
"_session_id": self.graph.session_id,
"_tracing_service": self._tracing_service,
}

Expand Down

Large diffs are not rendered by default.

Loading
Loading