forked from microsoft/semantic-kernel
-
Notifications
You must be signed in to change notification settings - Fork 3
Add semantic memory retrieval test #781
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Bryan-Roe
merged 3 commits into
main
from
3jhfjp-codex/validate-semantic-memory-retrieval-accuracy
Aug 3, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
01-core-implementations/python/tests/unit/memory/test_semantic_memory_retrieval.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| import asyncio | ||
| import numpy as np | ||
| import pytest | ||
|
|
||
| from semantic_kernel.memory.volatile_memory_store import VolatileMemoryStore | ||
| from semantic_kernel.memory.semantic_text_memory import SemanticTextMemory | ||
| from semantic_kernel.connectors.ai.embedding_generator_base import EmbeddingGeneratorBase | ||
|
|
||
| class FakeEmbeddingGenerator(EmbeddingGeneratorBase): | ||
| async def generate_embeddings(self, texts, settings=None, **kwargs) -> np.ndarray: | ||
Bryan-Roe marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| Generate embeddings for a list of texts. | ||
|
|
||
| Returns: | ||
| np.ndarray: A 2D array where each row is an embedding corresponding to a text. | ||
| """ | ||
| vectors = [] | ||
| for text in texts: | ||
| hash_digest = hashlib.sha256(text.encode('utf-8')).hexdigest() | ||
| vec = np.array([ | ||
| len(text), | ||
| int(hash_digest[:8], 16) % 10, | ||
| int(hash_digest[:16], 16) % 100, | ||
| ], dtype=float) | ||
| vectors.append(vec) | ||
| return np.vstack(vectors) | ||
|
|
||
| def create_memory() -> SemanticTextMemory: | ||
| storage = VolatileMemoryStore() | ||
| generator = FakeEmbeddingGenerator() | ||
| return SemanticTextMemory(storage=storage, embeddings_generator=generator) | ||
|
|
||
| @pytest.mark.asyncio | ||
| async def test_retrieval_accuracy(): | ||
| memory = create_memory() | ||
|
|
||
| await memory.save_information(collection="aboutMe", id="info1", text="I enjoy hiking") | ||
| await memory.save_information(collection="aboutMe", id="info2", text="I work as a tour guide") | ||
| await memory.save_information(collection="aboutMe", id="info3", text="I visited Iceland last year") | ||
|
|
||
| result = await memory.search(collection="aboutMe", query="hiking", limit=1) | ||
| assert result[0].text == "I enjoy hiking" | ||
|
|
||
| result = await memory.search(collection="aboutMe", query="tour", limit=1) | ||
| assert result[0].text == "I work as a tour guide" | ||
|
|
||
| result = await memory.search(collection="aboutMe", query="Iceland", limit=1) | ||
| assert result[0].text == "I visited Iceland last year" | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.