From 93293a16eca715f954c4aceadffd6058b31c0006 Mon Sep 17 00:00:00 2001 From: eavanvalkenburg Date: Thu, 19 Jun 2025 20:00:42 +0200 Subject: [PATCH 1/3] int test fixes --- python/samples/concepts/memory/simple_memory.py | 2 +- .../samples/concepts/rag/rag_with_vector_collection.py | 1 + python/semantic_kernel/connectors/chroma.py | 10 +++++----- python/tests/samples/test_concepts.py | 4 ++-- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/python/samples/concepts/memory/simple_memory.py b/python/samples/concepts/memory/simple_memory.py index e7e1518a14c1..a2bd521e5606 100644 --- a/python/samples/concepts/memory/simple_memory.py +++ b/python/samples/concepts/memory/simple_memory.py @@ -79,7 +79,7 @@ async def main(): ) as record_collection: # Create the collection after wiping it print_with_color("Creating test collection!", Colors.CGREY) - await record_collection.delete_create_collection() + await record_collection.ensure_collection_exists() # First add vectors to the records print_with_color("Adding records!", Colors.CBLUE) diff --git a/python/samples/concepts/rag/rag_with_vector_collection.py b/python/samples/concepts/rag/rag_with_vector_collection.py index 5299c8186000..dce3a22dae2f 100644 --- a/python/samples/concepts/rag/rag_with_vector_collection.py +++ b/python/samples/concepts/rag/rag_with_vector_collection.py @@ -45,6 +45,7 @@ async def main(): kernel.add_service(OpenAIChatCompletion()) async with InMemoryCollection(record_type=BudgetItem) as collection: + await collection.ensure_collection_exists() # Add information to the collection await collection.upsert( [ diff --git a/python/semantic_kernel/connectors/chroma.py b/python/semantic_kernel/connectors/chroma.py index 1a39724f0eb9..70f2df4ce7be 100644 --- a/python/semantic_kernel/connectors/chroma.py +++ b/python/semantic_kernel/connectors/chroma.py @@ -299,15 +299,15 @@ def _unpack_results( for idx, id in enumerate(ids): record: dict[str, Any] = {"id": id} # Add vector field if present - if documents is not None and idx < len(documents[0]): + if documents is not None and documents[0] is not None and idx < len(documents[0]): record["document"] = documents[0][idx] - elif embeddings is not None and idx < len(embeddings[0]): + elif embeddings is not None and embeddings[0] is not None and idx < len(embeddings[0]): record["embedding"] = embeddings[0][idx] # Add distance if present - if distances is not None and isinstance(distances, list) and idx < len(distances[0]): + if distances is not None and distances[0] is not None and idx < len(distances[0]): record["distance"] = distances[0][idx] # Add metadata if present - if metadatas is not None and idx < len(metadatas[0]) and metadatas[0] is not None: + if metadatas is not None and metadatas[0] is not None and idx < len(metadatas[0]): metadata = metadatas[0] if isinstance(metadatas[0], dict) else metadatas[0][idx] # type: ignore if metadata and isinstance(metadata, dict): record.update(metadata) @@ -350,7 +350,7 @@ async def _inner_search( results = self._get_collection().query(**args) records = self._unpack_results(results, options.include_vectors, include_distances=True) return KernelSearchResults( - results=self._get_vector_search_results_from_results(records), total_count=len(records) + results=self._get_vector_search_results_from_results(records), total_count=len(records) if records else 0 ) @override diff --git a/python/tests/samples/test_concepts.py b/python/tests/samples/test_concepts.py index 0f73073be048..8b15f5023bb7 100644 --- a/python/tests/samples/test_concepts.py +++ b/python/tests/samples/test_concepts.py @@ -46,7 +46,7 @@ from samples.concepts.prompt_templates.configuring_prompts import main as configuring_prompts from samples.concepts.prompt_templates.load_yaml_prompt import main as load_yaml_prompt from samples.concepts.prompt_templates.template_language import main as template_language -from samples.concepts.rag.rag_with_vector_collection import main as rag_with_text_memory_plugin +from samples.concepts.rag.rag_with_vector_collection import main as rag_with_vector_collection from samples.concepts.service_selector.custom_service_selector import main as custom_service_selector from samples.concepts.text_completion.text_completion import main as text_completion from samples.getting_started_with_agents.chat_completion.step01_chat_completion_agent_simple import ( @@ -256,7 +256,7 @@ id="simple_memory", marks=pytest.mark.skipif(os.getenv(MEMORY_CONCEPT_SAMPLE, None) is None, reason="Not running memory samples."), ), - param(rag_with_text_memory_plugin, [], id="rag_with_text_memory_plugin"), + param(rag_with_vector_collection, [], id="rag_with_vector_collection"), param( custom_service_selector, [], From cb97d3e37327923c1b49ee7699c5c9f0c63b4c23 Mon Sep 17 00:00:00 2001 From: eavanvalkenburg Date: Thu, 19 Jun 2025 20:01:37 +0200 Subject: [PATCH 2/3] same fix for notebook --- .../samples/getting_started/05-memory-and-embeddings.ipynb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/python/samples/getting_started/05-memory-and-embeddings.ipynb b/python/samples/getting_started/05-memory-and-embeddings.ipynb index f93e66894870..aeea07868f66 100644 --- a/python/samples/getting_started/05-memory-and-embeddings.ipynb +++ b/python/samples/getting_started/05-memory-and-embeddings.ipynb @@ -681,7 +681,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "id": "77fdfa86", "metadata": {}, "outputs": [ @@ -702,7 +702,9 @@ "from semantic_kernel.connectors.azure_ai_search import AzureAISearchCollection\n", "\n", "azs_memory = AzureAISearchCollection(record_type=GitHubFile)\n", - "await azs_memory.delete_create_collection()\n", + "# We explicitly delete the collection if it exists to ensure a clean state\n", + "await azs_memory.ensure_collection_deleted()\n", + "await azs_memory.ensure_collection_exists()\n", "# Add records to the collection\n", "await azs_memory.upsert(github_files)" ] From f801a0f9e7cdad95a37ff5c08446493a45d37927 Mon Sep 17 00:00:00 2001 From: eavanvalkenburg Date: Thu, 19 Jun 2025 20:07:29 +0200 Subject: [PATCH 3/3] mypy fix --- python/semantic_kernel/connectors/chroma.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/python/semantic_kernel/connectors/chroma.py b/python/semantic_kernel/connectors/chroma.py index 70f2df4ce7be..cf5d28b20808 100644 --- a/python/semantic_kernel/connectors/chroma.py +++ b/python/semantic_kernel/connectors/chroma.py @@ -304,8 +304,8 @@ def _unpack_results( elif embeddings is not None and embeddings[0] is not None and idx < len(embeddings[0]): record["embedding"] = embeddings[0][idx] # Add distance if present - if distances is not None and distances[0] is not None and idx < len(distances[0]): - record["distance"] = distances[0][idx] + if distances is not None and distances[0] is not None and idx < len(distances[0]): # type: ignore + record["distance"] = distances[0][idx] # type: ignore # Add metadata if present if metadatas is not None and metadatas[0] is not None and idx < len(metadatas[0]): metadata = metadatas[0] if isinstance(metadatas[0], dict) else metadatas[0][idx] # type: ignore