Skip to content

Building AI agents, atomically

chat memory test

KennyVaneetvelde0c6c200aaa69
diff --git a/atomic_agents/lib/components/chat_memory.py b/atomic_agents/lib/components/chat_memory.py
index e23f45d..d8bb47c 100644
--- a/atomic_agents/lib/components/chat_memory.py
+++ b/atomic_agents/lib/components/chat_memory.py
@@ -125,117 +125,4 @@ class ChatMemory:
Returns:
int: The number of messages.
"""
- return len(self.history)
-
-
-if __name__ == "__main__":
- from rich.console import Console
- from rich.panel import Panel
- from rich.text import Text
- from rich.table import Table
-
- console = Console()
-
- def print_panel(title, content):
- console.print(Panel(content, title=title, expand=False, border_style="cyan"))
-
- # Create a ChatMemory with a maximum of 5 messages
- memory = ChatMemory(max_messages=5)
-
- console.print("[bold magenta]ChatMemory Test Cases[/bold magenta]", justify="center")
- console.print()
-
- # Test Case 1
- content = Text()
- memory.add_message("user", "Hello, how are you?")
- memory.add_message("assistant", "I'm doing well, thank you for asking!")
- memory.add_message("user", "Can you help me with the weather?")
- content.append(f"Total messages: {memory.get_message_count()}\n\n")
- content.append("Current history:\n")
- for msg in memory.get_history():
- content.append(f"- {msg.role}: {msg.content}\n", style=f"{'green' if msg.role == 'user' else 'blue'}")
- print_panel("1. Adding regular messages", content)
-
- # Test Case 2
- content = Text()
- tool_message = {
- "id": "weather_1",
- "type": "function",
- "function": {"name": "get_weather", "arguments": '{"location": "New York"}'}
- }
- memory.add_message("assistant", "Certainly! I'll check the weather for you.", tool_message=tool_message)
- content.append(f"Total messages: {memory.get_message_count()}\n\n")
- content.append("Last message:\n")
- last_msg = memory.get_history()[-1]
- content.append(f"- {last_msg.role}: {last_msg.content}\n", style="blue")
- content.append(f" Tool call: {last_msg.tool_calls}\n", style="yellow")
- print_panel("2. Adding a message with a tool call", content)
-
- # Test Case 3
- content = Text()
- memory.add_message("tool", "The weather in New York is sunny with a high of 75°F.", tool_id="weather_1")
- content.append(f"Total messages: {memory.get_message_count()}\n\n")
- content.append("Last message:\n")
- last_msg = memory.get_history()[-1]
- content.append(f"- {last_msg.role}: {last_msg.content}\n", style="yellow")
- content.append(f" Tool call ID: {last_msg.tool_call_id}\n", style="yellow")
- print_panel("3. Adding a tool response", content)
-
- # Test Case 4
- content = Text()
- memory.add_message("user", "Thanks for the weather info!")
- content.append(f"Total messages: {memory.get_message_count()}\n\n")
- content.append("Current history (should only have 5 most recent messages):\n")
- for msg in memory.get_history():
- content.append(f"- {msg.role}: {msg.content}\n", style=f"{'green' if msg.role == 'user' else 'blue' if msg.role == 'assistant' else 'yellow'}")
- print_panel("4. Testing overflow management", content)
-
- # Test Case 5
- content = Text()
- dumped_data = memory.dump()
- content.append("Dumped data:\n")
- content.append(str(dumped_data), style="dim")
- content.append("\n\n")
- new_memory = ChatMemory(max_messages=5)
- new_memory.load(dumped_data)
- content.append("Loaded memory:\n")
- for msg in new_memory.get_history():
- content.append(f"- {msg.role}: {msg.content}\n", style=f"{'green' if msg.role == 'user' else 'blue' if msg.role == 'assistant' else 'yellow'}")
- content.append(f"\nTotal messages in loaded memory: {new_memory.get_message_count()}")
- print_panel("5. Testing dump and load", content)
-
- # Test Case 6
- content = Text()
- copied_memory = memory.copy()
- content.append(f"Original memory message count: {memory.get_message_count()}\n")
- content.append(f"Copied memory message count: {copied_memory.get_message_count()}\n\n")
- content.append("Adding a message to the copy:\n")
- copied_memory.add_message("user", "This is a new message in the copy.")
- content.append(f"Original memory message count: {memory.get_message_count()}\n")
- content.append(f"Copied memory message count: {copied_memory.get_message_count()}")
- print_panel("6. Testing copy", content)
-
- # Test Case 7
- content = Text()
- large_buffer = [
- {"role": "user", "content": f"Message {i}"} for i in range(10)
- ]
- large_memory = ChatMemory(max_messages=5)
- large_memory.load(large_buffer)
- content.append(f"Total messages after loading large buffer: {large_memory.get_message_count()}\n\n")
- content.append("Current history (should only have 5 most recent messages):\n")
- for msg in large_memory.get_history():
- content.append(f"- {msg.role}: {msg.content}\n", style="green")
- print_panel("7. Testing loading a larger buffer than max_messages", content)
-
- # Test Case 8
- content = Text()
- mixed_memory = ChatMemory(max_messages=10)
- mixed_memory.add_message("user", "Hello")
- mixed_memory.add_message("assistant", {"text": "Hi there!", "confidence": 0.95})
- mixed_memory.add_message("system", {"command": "reset_conversation"})
- content.append(f"Total messages in mixed memory: {mixed_memory.get_message_count()}\n\n")
- content.append("Mixed memory contents:\n")
- for msg in mixed_memory.get_history():
- content.append(f"- {msg.role}: {msg.content}\n", style=f"{'green' if msg.role == 'user' else 'blue' if msg.role == 'assistant' else 'red'}")
- print_panel("8. Testing adding messages with different content types", content)
\ No newline at end of file
+ return len(self.history)
\ No newline at end of file
diff --git a/examples/chat_memory.py b/examples/chat_memory.py
new file mode 100644
index 0000000..0f616e3
--- /dev/null
+++ b/examples/chat_memory.py
@@ -0,0 +1,112 @@
+from rich.console import Console
+from rich.panel import Panel
+from rich.text import Text
+from rich.table import Table
+
+from atomic_agents.lib.components.chat_memory import ChatMemory
+
+console = Console()
+
+def print_panel(title, content):
+ console.print(Panel(content, title=title, expand=False, border_style="cyan"))
+
+# Create a ChatMemory with a maximum of 5 messages
+memory = ChatMemory(max_messages=5)
+
+console.print("[bold magenta]ChatMemory Test Cases[/bold magenta]", justify="center")
+console.print()
+
+# Test Case 1
+content = Text()
+memory.add_message("user", "Hello, how are you?")
+memory.add_message("assistant", "I'm doing well, thank you for asking!")
+memory.add_message("user", "Can you help me with the weather?")
+content.append(f"Total messages: {memory.get_message_count()}\n\n")
+content.append("Current history:\n")
+for msg in memory.get_history():
+ content.append(f"- {msg.role}: {msg.content}\n", style=f"{'green' if msg.role == 'user' else 'blue'}")
+print_panel("1. Adding regular messages", content)
+
+# Test Case 2
+content = Text()
+tool_message = {
+ "id": "weather_1",
+ "type": "function",
+ "function": {"name": "get_weather", "arguments": '{"location": "New York"}'}
+}
+memory.add_message("assistant", "Certainly! I'll check the weather for you.", tool_message=tool_message)
+content.append(f"Total messages: {memory.get_message_count()}\n\n")
+content.append("Last message:\n")
+last_msg = memory.get_history()[-1]
+content.append(f"- {last_msg.role}: {last_msg.content}\n", style="blue")
+content.append(f" Tool call: {last_msg.tool_calls}\n", style="yellow")
+print_panel("2. Adding a message with a tool call", content)
+
+# Test Case 3
+content = Text()
+memory.add_message("tool", "The weather in New York is sunny with a high of 75°F.", tool_id="weather_1")
+content.append(f"Total messages: {memory.get_message_count()}\n\n")
+content.append("Last message:\n")
+last_msg = memory.get_history()[-1]
+content.append(f"- {last_msg.role}: {last_msg.content}\n", style="yellow")
+content.append(f" Tool call ID: {last_msg.tool_call_id}\n", style="yellow")
+print_panel("3. Adding a tool response", content)
+
+# Test Case 4
+content = Text()
+memory.add_message("user", "Thanks for the weather info!")
+content.append(f"Total messages: {memory.get_message_count()}\n\n")
+content.append("Current history (should only have 5 most recent messages):\n")
+for msg in memory.get_history():
+ content.append(f"- {msg.role}: {msg.content}\n", style=f"{'green' if msg.role == 'user' else 'blue' if msg.role == 'assistant' else 'yellow'}")
+print_panel("4. Testing overflow management", content)
+
+# Test Case 5
+content = Text()
+dumped_data = memory.dump()
+content.append("Dumped data:\n")
+content.append(str(dumped_data), style="dim")
+content.append("\n\n")
+new_memory = ChatMemory(max_messages=5)
+new_memory.load(dumped_data)
+content.append("Loaded memory:\n")
+for msg in new_memory.get_history():
+ content.append(f"- {msg.role}: {msg.content}\n", style=f"{'green' if msg.role == 'user' else 'blue' if msg.role == 'assistant' else 'yellow'}")
+content.append(f"\nTotal messages in loaded memory: {new_memory.get_message_count()}")
+print_panel("5. Testing dump and load", content)
+
+# Test Case 6
+content = Text()
+copied_memory = memory.copy()
+content.append(f"Original memory message count: {memory.get_message_count()}\n")
+content.append(f"Copied memory message count: {copied_memory.get_message_count()}\n\n")
+content.append("Adding a message to the copy:\n")
+copied_memory.add_message("user", "This is a new message in the copy.")
+content.append(f"Original memory message count: {memory.get_message_count()}\n")
+content.append(f"Copied memory message count: {copied_memory.get_message_count()}")
+print_panel("6. Testing copy", content)
+
+# Test Case 7
+content = Text()
+large_buffer = [
+ {"role": "user", "content": f"Message {i}"} for i in range(10)
+]
+large_memory = ChatMemory(max_messages=5)
+large_memory.load(large_buffer)
+content.append(f"Total messages after loading large buffer: {large_memory.get_message_count()}\n\n")
+content.append("Current history (should only have 5 most recent messages):\n")
+for msg in large_memory.get_history():
+ content.append(f"- {msg.role}: {msg.content}\n", style="green")
+print_panel("7. Testing loading a larger buffer than max_messages", content)
+
+# Test Case 8
+content = Text()
+mixed_memory = ChatMemory(max_messages=10)
+mixed_memory.add_message("user", "Hello")
+mixed_memory.add_message("assistant", {"text": "Hi there!", "confidence": 0.95})
+mixed_memory.add_message("system", {"command": "reset_conversation"})
+content.append(f"Total messages in mixed memory: {mixed_memory.get_message_count()}\n\n")
+content.append("Mixed memory contents:\n")
+for msg in mixed_memory.get_history():
+ content.append(f"- {msg.role}: {msg.content}\n", style=f"{'green' if msg.role == 'user' else 'blue' if msg.role == 'assistant' else 'red'}")
+print_panel("8. Testing adding messages with different content types", content)
\ No newline at end of file
diff --git a/tests/lib/components/test_chat_memory.py b/tests/lib/components/test_chat_memory.py
new file mode 100644
index 0000000..28490af
--- /dev/null
+++ b/tests/lib/components/test_chat_memory.py
@@ -0,0 +1,111 @@
+import pytest
+from atomic_agents.lib.components.chat_memory import ChatMemory, Message
+
+def chat_memory():
+ return ChatMemory(max_messages=5)
+
+def test_initialization(chat_memory):
+ assert chat_memory.history == []
+ assert chat_memory.max_messages == 5
+
+def test_add_message(chat_memory):
+ chat_memory.add_message("user", "Hello")
+ assert len(chat_memory.history) == 1
+ assert chat_memory.history[0].role == "user"
+ assert chat_memory.history[0].content == "Hello"
+
+def test_add_message_with_tool(chat_memory):
+ tool_message = {
+ "id": "weather_1",
+ "type": "function",
+ "function": {"name": "get_weather", "arguments": '{"location": "New York"}'}
+ }
+ chat_memory.add_message("assistant", "Checking weather", tool_message=tool_message)
+ assert len(chat_memory.history) == 1
+ assert chat_memory.history[0].role == "assistant"
+ assert chat_memory.history[0].content == "Checking weather"
+ assert chat_memory.history[0].tool_calls == [tool_message]
+ assert chat_memory.history[0].tool_call_id == "weather_1"
+
+def test_add_tool_response(chat_memory):
+ chat_memory.add_message("tool", "Weather is sunny", tool_id="weather_1")
+ assert len(chat_memory.history) == 1
+ assert chat_memory.history[0].role == "tool"
+ assert chat_memory.history[0].content == "Weather is sunny"
+ assert chat_memory.history[0].tool_call_id == "weather_1"
+
+def test_manage_overflow(chat_memory):
+ for i in range(7):
+ chat_memory.add_message("user", f"Message {i}")
+ assert len(chat_memory.history) == 5
+ assert chat_memory.history[0].content == "Message 2"
+ assert chat_memory.history[-1].content == "Message 6"
+
+def test_get_history(chat_memory):
+ chat_memory.add_message("user", "Hello")
+ chat_memory.add_message("assistant", "Hi there")
+ history = chat_memory.get_history()
+ assert len(history) == 2
+ assert isinstance(history[0], Message)
+ assert history[0].role == "user"
+ assert history[1].role == "assistant"
+
+def test_dump_and_load():
+ original_memory = ChatMemory(max_messages=3)
+ original_memory.add_message("user", "Hello")
+ original_memory.add_message("assistant", "Hi there")
+
+ dumped_data = original_memory.dump()
+
+ new_memory = ChatMemory(max_messages=3)
+ new_memory.load(dumped_data)
+
+ assert len(new_memory.history) == 2
+ assert new_memory.history[0].role == "user"
+ assert new_memory.history[0].content == "Hello"
+ assert new_memory.history[1].role == "assistant"
+ assert new_memory.history[1].content == "Hi there"
+
+def test_copy(chat_memory):
+ chat_memory.add_message("user", "Hello")
+ copied_memory = chat_memory.copy()
+
+ assert len(copied_memory.history) == 1
+ assert copied_memory.history[0].role == "user"
+ assert copied_memory.history[0].content == "Hello"
+ assert copied_memory.max_messages == chat_memory.max_messages
+
+ chat_memory.add_message("assistant", "Hi there")
+ assert len(chat_memory.history) == 2
+ assert len(copied_memory.history) == 1
+
+def test_get_message_count(chat_memory):
+ assert chat_memory.get_message_count() == 0
+ chat_memory.add_message("user", "Hello")
+ chat_memory.add_message("assistant", "Hi there")
+ assert chat_memory.get_message_count() == 2
+
+def test_load_with_invalid_data():
+ memory = ChatMemory()
+ invalid_data = [
+ {"role": "user", "content": "Valid message"},
+ {"role": "invalid_role", "content": "Invalid role"},
+ {"role": "user", "invalid_field": "Missing content"},
+ ]
+ memory.load(invalid_data)
+ assert len(memory.history) == 2
+ assert memory.history[0].role == "user"
+ assert memory.history[0].content == "Valid message"
+ assert memory.history[1].role == "invalid_role"
+ assert memory.history[1].content == "Invalid role"
+
+def test_add_message_with_dict_content(chat_memory):
+ dict_content = {"text": "Hello", "confidence": 0.95}
+ chat_memory.add_message("assistant", dict_content)
+ assert len(chat_memory.history) == 1
+ assert chat_memory.history[0].role == "assistant"
+ assert chat_memory.history[0].content == dict_content
+
+if __name__ == "__main__":
+ pytest.main()