Building AI agents, atomically
Remove lifecycle events for now as they made little sense and complicated things
KennyVaneetvelde041a367657c0
diff --git a/atomic_agents/agents/base_agent.py b/atomic_agents/agents/base_agent.pyindex 20bb4db..cf0f0af 100644--- a/atomic_agents/agents/base_agent.py+++ b/atomic_agents/agents/base_agent.py@@ -1,4 +1,4 @@-from typing import Optional, Type+from typing import Callable, Optional, Typeimport instructorfrom pydantic import BaseModel, Field@@ -63,7 +63,7 @@ class BaseAgentConfig(BaseModel):)input_schema: Optional[Type[BaseModel]] = Field(None, description="The schema for the input data.")output_schema: Optional[Type[BaseModel]] = Field(None, description="The schema for the output data.")-+class Config:arbitrary_types_allowed = True@@ -138,51 +138,19 @@ class BaseAgent:Runs the chat agent with the given user input.Args:- user_input (Optional[str]): The input text from the user. If not provided, skips the initialization step.+ user_input (Optional[Type[BaseAgentIO]]): The input from the user. If not provided, skips adding to memory.Returns:- str: The response from the chat agent.+ Type[BaseAgentIO]: The response from the chat agent."""if user_input:- self._init_run(user_input)- self._pre_run()- response = self._get_and_handle_response()- self._post_run(response)- return response-- def _get_and_handle_response(self):- """- Handles obtaining and processing the response.+ self.current_user_input = user_input+ self.memory.add_message("user", str(user_input))- Returns:- Type[BaseModel]: The processed response.- """- return self.get_response(response_model=self.output_schema)-- def _init_run(self, user_input: Type[BaseAgentIO]):- """- Initializes the run with the given user input.-- Args:- user_input (str): The input text from the user.- """- self.current_user_input = user_input- self.memory.add_message("user", str(user_input))-- def _pre_run(self):- """- Prepares for the run. This method can be overridden by subclasses to add custom pre-run logic.- """- pass-- def _post_run(self, response):- """- Finalizes the run with the given response.-- Args:- response (Type[BaseModel]): The response from the chat agent.- """+ response = self.get_response(response_model=self.output_schema)self.memory.add_message("assistant", str(response))++ return responsedef get_context_provider(self, provider_name: str) -> Type[SystemPromptContextProviderBase]:"""@@ -221,4 +189,4 @@ class BaseAgent:if provider_name in self.system_prompt_generator.system_prompt_info.context_providers:del self.system_prompt_generator.system_prompt_info.context_providers[provider_name]else:- raise KeyError(f"Context provider '{provider_name}' not found.")+ raise KeyError(f"Context provider '{provider_name}' not found.")\ No newline at end of filediff --git a/tests/agents/test_base_agent.py b/tests/agents/test_base_agent.pyindex ec11e88..acbce59 100644--- a/tests/agents/test_base_agent.py+++ b/tests/agents/test_base_agent.py@@ -166,21 +166,6 @@ def test_base_agent_input_output_schema_config():assert BaseAgentOutputSchema.Config.title == "BaseAgentOutputSchema"assert "description" in BaseAgentOutputSchema.Config.json_schema_extra-def test_init_run(agent, mock_memory):- input_schema = BaseAgentInputSchema(chat_message="Test message")- agent._init_run(input_schema)- assert agent.current_user_input == input_schema- mock_memory.add_message.assert_called_once_with("user", str(input_schema))--def test_pre_run(agent):- # This test just ensures that _pre_run can be called without errors- agent._pre_run()--def test_post_run(agent, mock_memory):- output_schema = BaseAgentOutputSchema(chat_message="Test response")- agent._post_run(output_schema)- mock_memory.add_message.assert_called_once_with("assistant", str(output_schema))-# Update the existing test_run function to use the actual methods instead of mocksdef test_run(agent, mock_memory):mock_input = BaseAgentInputSchema(chat_message="Test input")@@ -192,6 +177,7 @@ def test_run(agent, mock_memory):assert result == mock_outputassert agent.current_user_input == mock_input+mock_memory.add_message.assert_has_calls([call("user", str(mock_input)),call("assistant", str(mock_output))diff --git a/tests/agents/test_tool_interface_agent.py b/tests/agents/test_tool_interface_agent.pyindex 6372024..0a7794d 100644--- a/tests/agents/test_tool_interface_agent.py+++ b/tests/agents/test_tool_interface_agent.py@@ -2,7 +2,7 @@ import pytestfrom unittest.mock import Mock, patchfrom pydantic import Fieldfrom atomic_agents.agents.tool_interface_agent import ToolInterfaceAgent, ToolInterfaceAgentConfig, ToolInputModel-from atomic_agents.agents.base_agent import BaseAgentIO+from atomic_agents.agents.base_agent import BaseAgentIO, BaseAgentOutputSchemafrom atomic_agents.lib.tools.base import BaseToolfrom atomic_agents.lib.components.system_prompt_generator import SystemPromptGenerator, SystemPromptInfofrom atomic_agents.lib.components.agent_memory import AgentMemory@@ -33,7 +33,9 @@ def mock_instructor():@pytest.fixturedef mock_memory():- return Mock(spec=AgentMemory)+ mock = Mock(spec=AgentMemory)+ mock.get_history.return_value = [] # Return an empty list instead of a Mock object+ return mock@pytest.fixturedef mock_tool():@@ -112,7 +114,10 @@ def test_get_and_handle_response_processed_output(mock_get_response, tool_interffrom pydantic import Field-def test_tool_interface_agent_run(tool_interface_agent):+from unittest.mock import patch++@patch('atomic_agents.agents.tool_interface_agent.ToolInterfaceAgent.get_response')+def test_tool_interface_agent_run(mock_get_response, tool_interface_agent):# Inspect the input schemaprint(f"Input schema fields: {tool_interface_agent.input_schema.model_fields}")@@ -136,20 +141,17 @@ def test_tool_interface_agent_run(tool_interface_agent):print(f"Error creating mock input: {e}")raise- mock_output = BaseAgentIO()-- tool_interface_agent._init_run = Mock()- tool_interface_agent._pre_run = Mock()- tool_interface_agent._get_and_handle_response = Mock(return_value=mock_output)- tool_interface_agent._post_run = Mock()+ # Mock the get_response method to return a BaseAgentOutputSchema+ mock_output = BaseAgentOutputSchema(chat_message="Mocked response")+ mock_get_response.return_value = mock_outputresult = tool_interface_agent.run(mock_input)- assert result == mock_output- tool_interface_agent._init_run.assert_called_once_with(mock_input)- tool_interface_agent._pre_run.assert_called_once()- tool_interface_agent._get_and_handle_response.assert_called_once()- tool_interface_agent._post_run.assert_called_once_with(mock_output)+ assert isinstance(result, BaseAgentOutputSchema)+ assert result.chat_message == "Mocked response"++ # Verify that get_response was called+ mock_get_response.assert_called_once()if __name__ == '__main__':pytest.main()\ No newline at end of file