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
5 changes: 4 additions & 1 deletion python/semantic_kernel/memory/volatile_memory_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,16 @@ async def get_nearest_match(
Returns:
Tuple[MemoryRecord, float]: The record and the relevance score.
"""
return self.get_nearest_matches(
results = await self.get_nearest_matches(
collection_name=collection_name,
embedding=embedding,
limit=1,
min_relevance_score=min_relevance_score,
with_embeddings=with_embedding,
)
if len(results) == 0:
raise ServiceResourceNotFoundError("No match found")
return results[0]

async def get_nearest_matches(
self,
Expand Down
55 changes: 55 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,55 @@
# Copyright (c) Microsoft. All rights reserved.

import numpy as np
import pytest

from pytest import raises

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


@pytest.mark.asyncio
async def test_get_nearest_match_awaits_and_returns_tuple() -> None:
"""get_nearest_match must await get_nearest_matches (issue #14521)."""
store = VolatileMemoryStore()
await store.create_collection("c")
await store.upsert(
"c",
MemoryRecord.local_record(
"id1",
"text",
"desc",
None,
np.array([1.0, 0.0], dtype=float),
),
)
match = await store.get_nearest_match("c", np.array([1.0, 0.0], dtype=float))
assert isinstance(match, tuple)
assert len(match) == 2
record, score = match
assert record.id == "id1"
assert score >= 0.0


@pytest.mark.asyncio
async def test_get_nearest_match_raises_when_no_match() -> None:
store = VolatileMemoryStore()
await store.create_collection("c")
await store.upsert(
"c",
MemoryRecord.local_record(
"id1",
"text",
"desc",
None,
np.array([1.0, 0.0], dtype=float),
),
)
with raises(ServiceResourceNotFoundError, match="No match found"):
await store.get_nearest_match(
"c",
np.array([1.0, 0.0], dtype=float),
min_relevance_score=1.1,
)
Loading