-
Notifications
You must be signed in to change notification settings - Fork 8.3k
refactor(agent): standardize memory handling and update chat history logic #8715
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
Changes from 6 commits
7bd0d03
3a6c8d0
65a5773
cda1e7b
77dc9fd
0734aca
a1f3ad6
5b7cfd4
ff1ad07
5903f86
6b211d5
2fd80cb
3282f27
adcf878
9bb547c
1b81a87
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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." | ||
|
|
@@ -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( | ||
|
|
@@ -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
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are these needed?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
|
|
@@ -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: | ||
|
|
@@ -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): | ||
|
|
||
Large diffs are not rendered by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cleanup logs