Skip to content

Commit

Permalink
fix: Add get_base_args method and refactor component initialization i…
Browse files Browse the repository at this point in the history
…n Agent (#6026)

* feat: Add get_base_args method to Component class

Introduces a new method to retrieve base initialization arguments for components, including user ID, session ID, and tracing service. This method provides a convenient way to access essential context information during component initialization.

* refactor: Update AgentComponent to use get_base_args method

Modify AgentComponent to pass base initialization arguments when creating CurrentDateComponent and MemoryComponent, ensuring consistent context initialization across components.

* [autofix.ci] apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
ogabrielluiz and autofix-ci[bot] authored Jan 31, 2025
1 parent 2f9cd3e commit 5804336
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 14 deletions.
5 changes: 2 additions & 3 deletions src/backend/base/langflow/components/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ async def message_response(self) -> Message:
if not isinstance(self.tools, list): # type: ignore[has-type]
self.tools = []
# Convert CurrentDateComponent to a StructuredTool
current_date_tool = (await CurrentDateComponent().to_toolkit()).pop(0)
# current_date_tool = CurrentDateComponent().to_toolkit()[0]
current_date_tool = (await CurrentDateComponent(**self.get_base_args()).to_toolkit()).pop(0)
if isinstance(current_date_tool, StructuredTool):
self.tools.append(current_date_tool)
else:
Expand Down Expand Up @@ -122,7 +121,7 @@ async def get_memory_data(self):
# filter out empty values
memory_kwargs = {k: v for k, v in memory_kwargs.items() if v}

return await MemoryComponent().set(**memory_kwargs).retrieve_messages()
return await MemoryComponent(**self.get_base_args()).set(**memory_kwargs).retrieve_messages()

def get_llm(self):
if isinstance(self.agent_llm, str):
Expand Down
15 changes: 15 additions & 0 deletions src/backend/base/langflow/custom/custom_component/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,21 @@ def _there_is_overlap_in_inputs_and_outputs(self) -> set[str]:
# Return the intersection of the sets
return input_names & output_names

def get_base_args(self):
"""Get the base arguments required for component initialization.
Returns:
dict: A dictionary containing the base arguments:
- _user_id: The ID of the current user
- _session_id: The ID of the current session
- _tracing_service: The tracing service instance for logging/monitoring
"""
return {
"_user_id": self.user_id,
"_session_id": self.session_id,
"_tracing_service": self._tracing_service,
}

@property
def ctx(self):
if not hasattr(self, "graph") or self.graph is None:
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

0 comments on commit 5804336

Please sign in to comment.