Building AI agents, atomically
Chore: Get back to 100% test coverage
KennyVaneetvelde7b1f31596657
diff --git a/atomic_agents/lib/utils/format_tool_message.py b/atomic_agents/lib/utils/format_tool_message.pyindex d78196a..8e7e5fa 100644--- a/atomic_agents/lib/utils/format_tool_message.py+++ b/atomic_agents/lib/utils/format_tool_message.py@@ -1,4 +1,5 @@import uuid+import jsonfrom typing import Dict, Optional, Typefrom pydantic import BaseModel@@ -9,8 +10,7 @@ def format_tool_message(tool_call: Type[BaseModel], tool_id: Optional[str] = NonFormats a message for a tool call.Args:- tool_name (str): The type of the tool.- tool_calls (Dict): A dictionary containing the tool calls.+ tool_call (Type[BaseModel]): The Pydantic model instance representing the tool call.tool_id (str, optional): The unique identifier for the tool call. If not provided, a random UUID will be generated.Returns:@@ -24,6 +24,6 @@ def format_tool_message(tool_call: Type[BaseModel], tool_id: Optional[str] = Non"type": "function","function": {"name": tool_call.model_config["title"],- "arguments": tool_call.model_dump_json(),+ "arguments": json.dumps(tool_call.model_dump(), separators=(', ', ': '))},- }+ }\ No newline at end of filediff --git a/tests/agents/test_tool_interface_agent.py b/tests/agents/test_tool_interface_agent.pyindex 1080a1a..3edbc87 100644--- a/tests/agents/test_tool_interface_agent.py+++ b/tests/agents/test_tool_interface_agent.py@@ -1,29 +1,33 @@import pytestfrom unittest.mock import Mock, patchfrom pydantic import Field+import instructor+from atomic_agents.agents.tool_interface_agent import ToolInterfaceAgent, ToolInterfaceAgentConfig, ToolInputModel-from atomic_agents.agents.base_agent import BaseAgentIO, BaseAgentOutputSchemafrom atomic_agents.lib.tools.base import BaseTool-from atomic_agents.lib.components.system_prompt_generator import SystemPromptGenerator, SystemPromptInfo-from atomic_agents.lib.components.agent_memory import AgentMemory-import instructor+from atomic_agents.agents.base_agent import BaseAgentIO, BaseAgentOutputSchema+from atomic_agents.lib.components.agent_memory import Message-class MockToolInputSchema(BaseAgentIO):- query: str = Field(..., description="Test query")+class MockTool(BaseTool):+ class InputSchema(BaseAgentIO):+ query: str = Field(..., description="The query to process")- class Config:- title = "Mock Tool Input"- description = "Mock tool input description"+ class Config:+ title = "MockTool"+ description = "A mock tool for testing"-class MockToolOutputSchema(BaseAgentIO):- result: str+ class OutputSchema(BaseAgentIO):+ result: str = Field(..., description="The result of the tool operation")-class MockTool(BaseTool):- input_schema = MockToolInputSchema- output_schema = MockToolOutputSchema+ input_schema = InputSchema+ output_schema = OutputSchema++ def run(self, params: InputSchema) -> OutputSchema:+ return self.OutputSchema(result=f"Processed: {params.query}")- def run(self, params: MockToolInputSchema) -> MockToolOutputSchema:- return MockToolOutputSchema(result=f"Processed: {params.query}")+def mock_tool():+ return MockTool()@pytest.fixturedef mock_instructor():@@ -32,126 +36,97 @@ def mock_instructor():return mock@pytest.fixture-def mock_memory():- mock = Mock(spec=AgentMemory)- mock.get_history.return_value = [] # Return an empty list instead of a Mock object- return mock--def mock_tool():- return MockTool()--def tool_interface_agent_config(mock_instructor, mock_memory, mock_tool):- return ToolInterfaceAgentConfig(+def tool_interface_agent(mock_tool, mock_instructor):+ config = ToolInterfaceAgentConfig(client=mock_instructor,model="gpt-3.5-turbo",- memory=mock_memory,tool_instance=mock_tool,return_raw_output=False)--def tool_interface_agent(tool_interface_agent_config):- return ToolInterfaceAgent(tool_interface_agent_config)+ return ToolInterfaceAgent(config)def test_tool_interface_agent_initialization(tool_interface_agent, mock_tool):- assert isinstance(tool_interface_agent.tool_instance, BaseTool)- assert tool_interface_agent.return_raw_output is Falseassert tool_interface_agent.tool_instance == mock_tool--def test_tool_interface_agent_input_schema(tool_interface_agent):+ assert tool_interface_agent.return_raw_output == False+ assert tool_interface_agent.input_schema.__name__ == "MockTool"assert issubclass(tool_interface_agent.input_schema, ToolInputModel)- assert "tool_input" in tool_interface_agent.input_schema.model_fields--def test_tool_interface_agent_output_schema(tool_interface_agent):- assert tool_interface_agent.output_schema is not None--def test_tool_interface_agent_system_prompt_generator(tool_interface_agent):- assert isinstance(tool_interface_agent.system_prompt_generator, SystemPromptGenerator)- assert isinstance(tool_interface_agent.system_prompt_generator.system_prompt_info, SystemPromptInfo)+ assert tool_interface_agent.output_schema == BaseAgentOutputSchema-def test_tool_interface_agent_with_raw_output(mock_instructor, mock_memory, mock_tool):+def test_tool_interface_agent_input_schema(tool_interface_agent):+ assert 'tool_input' in tool_interface_agent.input_schema.model_fields+ assert tool_interface_agent.input_schema.model_fields['tool_input'].alias == 'tool_input_MockTool'+ assert tool_interface_agent.input_schema.model_config['title'] == "MockTool"+ assert "A mock tool for testing" in tool_interface_agent.input_schema.model_config['description']++@patch('atomic_agents.agents.base_agent.BaseAgent.get_response')+@patch('atomic_agents.lib.utils.format_tool_message.format_tool_message')+def test_tool_interface_agent_get_response_without_raw_output(mock_format_tool_message, mock_get_response, tool_interface_agent):+ mock_tool_input = tool_interface_agent.input_schema(tool_input_MockTool="Test query")+ mock_tool_output = MockTool.OutputSchema(result="Mocked result")+ mock_final_response = Mock()++ mock_get_response.side_effect = [mock_tool_input, mock_final_response]+ tool_interface_agent.tool_instance.run = Mock(return_value=mock_tool_output)+ mock_format_tool_message.return_value = {"id": "test_id", "type": "function", "function": {"name": "MockTool", "arguments": "{}"}}++ response = tool_interface_agent.get_response()++ tool_interface_agent.tool_instance.run.assert_called_once_with(mock_tool_input)+ assert mock_get_response.call_count == 2+ assert response == mock_final_response++@patch('atomic_agents.agents.base_agent.BaseAgent.get_response')+@patch('atomic_agents.lib.utils.format_tool_message.format_tool_message')+def test_tool_interface_agent_get_response_with_raw_output(mock_format_tool_message, mock_get_response, mock_tool, mock_instructor):config = ToolInterfaceAgentConfig(client=mock_instructor,model="gpt-3.5-turbo",- memory=mock_memory,tool_instance=mock_tool,return_raw_output=True)agent = ToolInterfaceAgent(config)- assert agent.return_raw_output is True- assert agent.output_schema == mock_tool.output_schema--# @patch('atomic_agents.agents.tool_interface_agent.ToolInterfaceAgent.get_response')-# def test_get_and_handle_response_raw_output(mock_get_response, tool_interface_agent):-# tool_interface_agent.return_raw_output = True-# mock_tool_input = MockToolInputSchema(query="test query")-# mock_tool_output = MockToolOutputSchema(result="Processed: test query")--# mock_get_response.return_value = mock_tool_input-# tool_interface_agent.tool_instance.run = Mock(return_value=mock_tool_output)--# result = tool_interface_agent._get_and_handle_response()--# assert result == mock_tool_output-# tool_interface_agent.tool_instance.run.assert_called_once_with(mock_tool_input)--# @patch('atomic_agents.agents.tool_interface_agent.ToolInterfaceAgent.get_response')-# def test_get_and_handle_response_processed_output(mock_get_response, tool_interface_agent):-# mock_tool_input = MockToolInputSchema(query="test query")-# mock_tool_output = MockToolOutputSchema(result="Processed: test query")-# mock_processed_response = BaseAgentIO()--# mock_get_response.side_effect = [mock_tool_input, mock_processed_response]-# tool_interface_agent.tool_instance.run = Mock(return_value=mock_tool_output)--# result = tool_interface_agent._get_and_handle_response()--# assert result == mock_processed_response-# tool_interface_agent.tool_instance.run.assert_called_once_with(mock_tool_input)-# assert mock_get_response.call_count == 2--from pydantic import Field--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 schema- print(f"Input schema fields: {tool_interface_agent.input_schema.model_fields}")+ mock_tool_input = agent.input_schema(tool_input_MockTool="Test query")+ mock_tool_output = MockTool.OutputSchema(result="Mocked result")- # Create a mock input that matches the schema structure- mock_input_data = {}- for field_name, field in tool_interface_agent.input_schema.model_fields.items():- if field.alias:- field_name = field.alias- if isinstance(field.annotation, type) and issubclass(field.annotation, str):- mock_input_data[field_name] = "Test input"- elif isinstance(field.annotation, type) and issubclass(field.annotation, dict):- mock_input_data[field_name] = {"key": "value"}- else:- mock_input_data[field_name] = None+ mock_get_response.return_value = mock_tool_input+ agent.tool_instance.run = Mock(return_value=mock_tool_output)+ mock_format_tool_message.return_value = {"id": "test_id", "type": "function", "function": {"name": "MockTool", "arguments": "{}"}}- print(f"Mock input data: {mock_input_data}")+ response = agent.get_response()- try:- mock_input = tool_interface_agent.input_schema(**mock_input_data)- except Exception as e:- print(f"Error creating mock input: {e}")- raise+ agent.tool_instance.run.assert_called_once_with(mock_tool_input)+ assert mock_get_response.call_count == 1+ assert response == mock_tool_output- # Mock the get_response method to return a BaseAgentOutputSchema- mock_output = BaseAgentOutputSchema(chat_message="Mocked response")- mock_get_response.return_value = mock_output+@patch('atomic_agents.agents.base_agent.BaseAgent.get_response')+@patch('atomic_agents.lib.utils.format_tool_message.format_tool_message')+def test_tool_interface_agent_memory_updates(mock_format_tool_message, mock_get_response, tool_interface_agent):+ mock_input = tool_interface_agent.input_schema(tool_input_MockTool="Test query")+ mock_tool_output = MockTool.OutputSchema(result="Mocked result")+ mock_final_response = BaseAgentOutputSchema(chat_message="Test response")- result = tool_interface_agent.run(mock_input)+ mock_get_response.side_effect = [mock_input, mock_final_response]+ tool_interface_agent.tool_instance.run = Mock(return_value=mock_tool_output)+ mock_format_tool_message.return_value = {"id": "test_id", "type": "function", "function": {"name": "MockTool", "arguments": "{}"}}- assert isinstance(result, BaseAgentOutputSchema)- assert result.chat_message == "Mocked response"+ tool_interface_agent.run(mock_input)- # Verify that get_response was called- mock_get_response.assert_called_once()+ history = tool_interface_agent.memory.get_history()++ # Check for TOOL CALL message+ assert any("TOOL CALL:" in msg.content for msg in history if isinstance(msg.content, str))++ # Check for TOOL RESPONSE message+ assert any("TOOL RESPONSE:" in msg.content for msg in history if isinstance(msg.content, str))++ # Check if the mocked result is in the memory+ assert any("Mocked result" in msg.content for msg in history if isinstance(msg.content, str))++ # Print the memory contents for debugging+ print("Memory contents:")+ for msg in history:+ print(f"Role: {msg.role}, Content: {msg.content}")-if __name__ == '__main__':- pytest.main()\ No newline at end of file+ # Assert that at least 3 messages were added (TOOL CALL, TOOL RESPONSE, and possibly the final response)+ assert len(history) >= 3\ No newline at end of filediff --git a/tests/lib/utils/test_format_tool_message.py b/tests/lib/utils/test_format_tool_message.pynew file mode 100644index 0000000..58ce9e4--- /dev/null+++ b/tests/lib/utils/test_format_tool_message.py@@ -0,0 +1,90 @@+import uuid+from pydantic import BaseModel+from atomic_agents.lib.utils.format_tool_message import format_tool_message++# Mock classes for testing+class MockToolCall(BaseModel):+ param1: str+ param2: int++ class Config:+ title = "MockTool"++def test_format_tool_message_with_provided_tool_id():+ tool_call = MockToolCall(param1="test", param2=42)+ tool_id = "test-tool-id"++ result = format_tool_message(tool_call, tool_id)++ assert result == {+ "id": "test-tool-id",+ "type": "function",+ "function": {+ "name": "MockTool",+ "arguments": '{"param1": "test", "param2": 42}'+ }+ }++def test_format_tool_message_without_tool_id():+ tool_call = MockToolCall(param1="test", param2=42)++ result = format_tool_message(tool_call)++ assert isinstance(result["id"], str)+ assert len(result["id"]) == 36 # UUID length+ assert result["type"] == "function"+ assert result["function"]["name"] == "MockTool"+ assert result["function"]["arguments"] == '{"param1": "test", "param2": 42}'++def test_format_tool_message_with_different_tool():+ class AnotherToolCall(BaseModel):+ field1: bool+ field2: float++ class Config:+ title = "AnotherTool"++ tool_call = AnotherToolCall(field1=True, field2=3.14)++ result = format_tool_message(tool_call)++ assert result["type"] == "function"+ assert result["function"]["name"] == "AnotherTool"+ assert result["function"]["arguments"] == '{"field1": true, "field2": 3.14}'++def test_format_tool_message_id_is_valid_uuid():+ tool_call = MockToolCall(param1="test", param2=42)++ result = format_tool_message(tool_call)++ try:+ uuid.UUID(result["id"])+ except ValueError:+ pytest.fail("The generated tool_id is not a valid UUID")++def test_format_tool_message_consistent_output():+ tool_call = MockToolCall(param1="test", param2=42)+ tool_id = "fixed-id"++ result1 = format_tool_message(tool_call, tool_id)+ result2 = format_tool_message(tool_call, tool_id)++ assert result1 == result2++def test_format_tool_message_with_complex_model():+ class ComplexToolCall(BaseModel):+ nested: dict+ list_field: list++ class Config:+ title = "ComplexTool"++ tool_call = ComplexToolCall(+ nested={"key": "value"},+ list_field=[1, 2, 3]+ )++ result = format_tool_message(tool_call)++ assert result["function"]["name"] == "ComplexTool"+ assert result["function"]["arguments"] == '{"nested": {"key": "value"}, "list_field": [1, 2, 3]}'\ No newline at end of file