Skip to content

Building AI agents, atomically

test: add tests for audio data and fix edge case

Renaudc0953fe6b937
diff --git a/atomic-agents/atomic_agents/lib/components/agent_memory.py b/atomic-agents/atomic_agents/lib/components/agent_memory.py
index ad56a89..614ec18 100644
--- a/atomic-agents/atomic_agents/lib/components/agent_memory.py
+++ b/atomic-agents/atomic_agents/lib/components/agent_memory.py
@@ -261,11 +261,14 @@ class AgentMemory:
"""
Process multimodal objects to convert string paths to Path objects.
+ Note: this is necessary only for PDF and Image instructor types. The from_path
+ behavior is slightly different for Audio as it keeps the source as a string.
+
Args:
obj: The object to process.
"""
- if isinstance(obj, INSTRUCTOR_MULTIMODAL_TYPES) and isinstance(obj.source, str):
+ if isinstance(obj, (Image, PDF)) and isinstance(obj.source, str):
# Check if the string looks like a file path (not a URL or base64 data)
if not obj.source.startswith(("http://", "https://", "data:")):
obj.source = Path(obj.source)
diff --git a/atomic-agents/tests/files/audio_sample.mp3 b/atomic-agents/tests/files/audio_sample.mp3
new file mode 100644
index 0000000..941e42b
Binary files /dev/null and b/atomic-agents/tests/files/audio_sample.mp3 differ
diff --git a/atomic-agents/tests/lib/components/test_agent_memory.py b/atomic-agents/tests/lib/components/test_agent_memory.py
index 2dd4008..20b8339 100644
--- a/atomic-agents/tests/lib/components/test_agent_memory.py
+++ b/atomic-agents/tests/lib/components/test_agent_memory.py
@@ -49,6 +49,7 @@ class TestMultimodalSchema(BaseIOSchema):
instruction_text: str = Field(..., description="The instruction text")
images: List[instructor.Image] = Field(..., description="The images to analyze")
pdfs: List[instructor.multimodal.PDF] = Field(..., description="The PDFs to analyze")
+ audio: instructor.multimodal.Audio = Field(..., description="The Audio to analyze")
@pytest.fixture
@@ -149,10 +150,14 @@ def test_dump_and_load(memory):
def test_dump_and_load_multimodal_data(memory):
test_image = instructor.Image.from_path(path="atomic-agents/tests/files/image_sample.jpg")
test_pdf = instructor.multimodal.PDF.from_path(path="atomic-agents/tests/files/pdf_sample.pdf")
+ test_audio = instructor.multimodal.Audio.from_path(path="atomic-agents/tests/files/audio_sample.mp3")
# multimodal message
memory.add_message(
- role="user", content=TestMultimodalSchema(instruction_text="Analyze this image", images=[test_image], pdfs=[test_pdf])
+ role="user",
+ content=TestMultimodalSchema(
+ instruction_text="Analyze this image", images=[test_image], pdfs=[test_pdf], audio=test_audio
+ ),
)
dumped_data = memory.dump()
@@ -166,6 +171,7 @@ def test_dump_and_load_multimodal_data(memory):
assert new_memory.history[0].content.instruction_text == memory.history[0].content.instruction_text
assert new_memory.history[0].content.images == memory.history[0].content.images
assert new_memory.history[0].content.pdfs == memory.history[0].content.pdfs
+ assert new_memory.history[0].content.audio == memory.history[0].content.audio
def test_load_invalid_data(memory):
@@ -313,10 +319,12 @@ def test_get_history_with_multimodal_content(memory):
# Create a mock image
mock_image = instructor.Image(source="test_url", media_type="image/jpeg", detail="low")
mock_pdf = instructor.multimodal.PDF(source="test_pdf_url")
+ mock_audio = instructor.multimodal.Audio(source="test_audio_url", media_type="audio/mpeg")
# Add a multimodal message
memory.add_message(
- "user", TestMultimodalSchema(instruction_text="Analyze this image", images=[mock_image], pdfs=[mock_pdf])
+ "user",
+ TestMultimodalSchema(instruction_text="Analyze this image", images=[mock_image], pdfs=[mock_pdf], audio=mock_audio),
)
# Get history and verify format
@@ -327,6 +335,7 @@ def test_get_history_with_multimodal_content(memory):
assert history[0]["content"][0] == '{"instruction_text": "Analyze this image"}'
assert history[0]["content"][1] == mock_image
assert history[0]["content"][2] == mock_pdf
+ assert history[0]["content"][3] == mock_audio
def test_get_history_with_multiple_images_multimodal_content(memory):