Building AI agents, atomically
modified run_async() to incorporate system_role and added a test
Koichi KAWAMURA302cf5c5da4a
diff --git a/atomic-agents/atomic_agents/agents/base_agent.py b/atomic-agents/atomic_agents/agents/base_agent.pyindex ad397a7..9bc4059 100644--- a/atomic-agents/atomic_agents/agents/base_agent.py+++ b/atomic-agents/atomic_agents/agents/base_agent.py@@ -215,16 +215,21 @@ class BaseAgent:self.current_user_input = user_inputself.memory.add_message("user", user_input)- messages = [- {- "role": "system",- "content": self.system_prompt_generator.generate_prompt(),- }- ] + self.memory.get_history()+ if self.system_role is None:+ self.messages = []+ else:+ self.messages = [+ {+ "role": self.system_role,+ "content": self.system_prompt_generator.generate_prompt(),+ }+ ]++ self.messages += self.memory.get_history()response_stream = self.client.chat.completions.create_partial(model=self.model,- messages=messages,+ messages=self.messages,response_model=self.output_schema,**self.model_api_parameters,stream=True,diff --git a/atomic-agents/tests/agents/test_base_agent.py b/atomic-agents/tests/agents/test_base_agent.pyindex fb4dc80..038dee7 100644--- a/atomic-agents/tests/agents/test_base_agent.py+++ b/atomic-agents/tests/agents/test_base_agent.py@@ -22,6 +22,17 @@ def mock_instructor():mock.chat.completions.create_partial = Mock()return mock+def mock_instructor_async():+ mock = Mock(spec=instructor.Instructor)+ mock.chat.completions.create = Mock()++ # Mock the create_partial method to return an async generator+ async def mock_create_partial(*args, **kwargs):+ yield BaseAgentOutputSchema(chat_message="Mocked response")++ mock.chat.completions.create_partial = mock_create_partial+ return mock@pytest.fixturedef mock_memory():@@ -282,6 +293,28 @@ async def test_run_async(agent, mock_memory):assert agent.current_user_input == mock_inputmock_memory.add_message.assert_has_calls([call("user", mock_input), call("assistant", mock_output)])+async def test_run_async_with_no_system_role(mock_instructor_async, mock_memory):+ # Create a BaseAgentConfig with system_role set to None+ config = BaseAgentConfig(+ client=mock_instructor_async,+ model="gpt-4o-mini",+ memory=mock_memory,+ system_prompt_generator=None, # No system prompt generator+ system_role=None, # Ensure system_role is None+ )+ agent = BaseAgent(config)++ # Create a mock input+ mock_input = BaseAgentInputSchema(chat_message="Test input")++ # Collect all responses from the actual run_async method+ responses = []+ async for response in agent.run_async(mock_input):+ responses.append(response)++ # Assertions+ assert agent.messages == [] # Ensure self.messages was set to an empty list@pytest.mark.asyncioasync def test_stream_response_async(agent, mock_memory, mock_instructor, mock_system_prompt_generator):