Skip to content

Commit

Permalink
fix: Error handling in loading components/features in Agent Component (
Browse files Browse the repository at this point in the history
  • Loading branch information
edwinjosechittilappilly authored and anovazzi1 committed Dec 19, 2024
1 parent 138cd48 commit f3f16bc
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 34 deletions.
68 changes: 45 additions & 23 deletions src/backend/base/langflow/components/agents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
)
from langflow.custom.utils import update_component_build_config
from langflow.io import BoolInput, DropdownInput, MultilineInput, Output
from langflow.logging import logger
from langflow.schema.dotdict import dotdict
from langflow.schema.message import Message

Expand Down Expand Up @@ -62,35 +63,56 @@ class AgentComponent(ToolCallingAgentComponent):
outputs = [Output(name="response", display_name="Response", method="message_response")]

async def message_response(self) -> Message:
llm_model, display_name = self.get_llm()
self.model_name = get_model_name(llm_model, display_name=display_name)
if llm_model is None:
msg = "No language model selected"
raise ValueError(msg)
self.chat_history = await self.get_memory_data()
try:
llm_model, display_name = self.get_llm()
if llm_model is None:
msg = "No language model selected"
raise ValueError(msg)
self.model_name = get_model_name(llm_model, display_name=display_name)
except Exception as e:
# Log the error for debugging purposes
logger.error(f"Error retrieving language model: {e}")
raise

try:
self.chat_history = await self.get_memory_data()
except Exception as e:
logger.error(f"Error retrieving chat history: {e}")
raise

if self.add_current_date_tool:
if not isinstance(self.tools, list): # type: ignore[has-type]
self.tools = []
# Convert CurrentDateComponent to a StructuredTool
current_date_tool = CurrentDateComponent().to_toolkit()[0]
if isinstance(current_date_tool, StructuredTool):
self.tools.append(current_date_tool)
else:
msg = "CurrentDateComponent must be converted to a StructuredTool"
raise ValueError(msg)
try:
if not isinstance(self.tools, list): # type: ignore[has-type]
self.tools = []
# Convert CurrentDateComponent to a StructuredTool
current_date_tool = CurrentDateComponent().to_toolkit()[0]
if isinstance(current_date_tool, StructuredTool):
self.tools.append(current_date_tool)
else:
msg = "CurrentDateComponent must be converted to a StructuredTool"
raise TypeError(msg)
except Exception as e:
logger.error(f"Error adding current date tool: {e}")
raise

if not self.tools:
msg = "Tools are required to run the agent."
logger.error(msg)
raise ValueError(msg)
self.set(
llm=llm_model,
tools=self.tools,
chat_history=self.chat_history,
input_value=self.input_value,
system_prompt=self.system_prompt,
)
agent = self.create_agent_runnable()

try:
self.set(
llm=llm_model,
tools=self.tools,
chat_history=self.chat_history,
input_value=self.input_value,
system_prompt=self.system_prompt,
)
agent = self.create_agent_runnable()
except Exception as e:
logger.error(f"Error setting up the agent: {e}")
raise

return await self.run_agent(agent)

async def get_memory_data(self):
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 f3f16bc

Please sign in to comment.