Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python/semantic_kernel/memory/volatile_memory_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ async def get_nearest_matches(
# Get all the records in the collection
memory_records = list(self._store[collection_name].values())

if not memory_records:
return []

# Convert the collection of embeddings into a numpy array (stacked)
embeddings = array([x._embedding for x in memory_records], dtype=float)
embeddings = embeddings.reshape(embeddings.shape[0], -1)
Expand Down
39 changes: 39 additions & 0 deletions python/tests/unit/memory/test_volatile_memory_store.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Copyright (c) Microsoft. All rights reserved.

import numpy as np

from semantic_kernel.memory.memory_record import MemoryRecord
from semantic_kernel.memory.volatile_memory_store import VolatileMemoryStore


async def test_get_nearest_matches_empty_collection():
store = VolatileMemoryStore()
await store.create_collection("test")
result = await store.get_nearest_matches("test", np.array([1.0, 2.0]), limit=3)
assert result == []


async def test_get_nearest_matches_missing_collection():
store = VolatileMemoryStore()
result = await store.get_nearest_matches("missing", np.array([1.0, 2.0]), limit=3)
assert result == []


async def test_get_nearest_matches_with_records():
store = VolatileMemoryStore()
await store.create_collection("test")
await store.upsert(
"test",
MemoryRecord(
is_reference=False,
external_source_name=None,
id="first",
description="first record",
text="hello",
additional_metadata=None,
embedding=np.array([1.0, 0.0]),
),
)
result = await store.get_nearest_matches("test", np.array([1.0, 0.0]), limit=3)
assert len(result) == 1
assert result[0][0].id == "first"
Loading