Skip to content

Commit 853ad24

Browse files
awharrison-28kinfeylemillermicrosoft
authored andcommitted
Python: ACS should use Hnsw for vector indexing (microsoft#2534)
### Motivation and Context This PR builds on microsoft#2435 - the previous PR updates azure-search to the latest package , where it is recommended to use HnswVectorSearchAlgorithmConfiguration. The previous implementation appears to be in a semi-deprecated state. This new flow is the publicly recommended use for ACS according to https://github.com/Azure/cognitive-search-vector-pr/tree/main/demo-python/code. ### Description - update `azure-search-documents` dependency from `11.4.0b6 -> 11.4.0b8` - index configuration changed from `VectorSearchAlgorithmConfiguration -> HnswVectorSearchAlgorithmConfiguration` - update `search_client.search()` call in `get_nearest_matches()` to be consistent with the new index api ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [ ] The code builds clean without any errors or warnings - [ ] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [ ] All unit tests pass, and I have added new tests where possible - [ ] I didn't break anyone 😄 --------- Co-authored-by: Kinfey <[email protected]> Co-authored-by: Lee Miller <[email protected]> Co-authored-by: Kinfey <[email protected]>
1 parent 6dfd6e1 commit 853ad24

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

python/poetry.lock

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

python/pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ psycopg = "^3.1.9"
5656
psycopg-binary = "^3.1.9"
5757

5858
[tool.poetry.group.azure_cognitive_search.dependencies]
59-
azure-search-documents = {version = "11.4.0b6", allow-prereleases = true}
59+
azure-search-documents = {version = "11.4.0b8", allow-prereleases = true}
6060
azure-core = "^1.28.0"
6161
azure-identity = "^1.13.0"
6262

python/semantic_kernel/connectors/memory/azure_cognitive_search/azure_cognitive_search_memory_store.py

+11-9
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88
from azure.core.exceptions import ResourceNotFoundError
99
from azure.search.documents.indexes.aio import SearchIndexClient
1010
from azure.search.documents.indexes.models import (
11+
HnswVectorSearchAlgorithmConfiguration,
1112
SearchIndex,
1213
VectorSearch,
13-
VectorSearchAlgorithmConfiguration,
1414
)
15+
from azure.search.documents.models import Vector
1516
from numpy import ndarray
1617

1718
from semantic_kernel.connectors.memory.azure_cognitive_search.utils import (
@@ -58,7 +59,6 @@ def __init__(
5859
Instantiate using Async Context Manager:
5960
async with AzureCognitiveSearchMemoryStore(<...>) as memory:
6061
await memory.<...>
61-
6262
"""
6363
try:
6464
pass
@@ -82,14 +82,14 @@ async def close_async(self):
8282
async def create_collection_async(
8383
self,
8484
collection_name: str,
85-
vector_config: Optional[VectorSearchAlgorithmConfiguration] = None,
85+
vector_config: Optional[HnswVectorSearchAlgorithmConfiguration] = None,
8686
) -> None:
8787
"""Creates a new collection if it does not exist.
8888
8989
Arguments:
9090
collection_name {str} -- The name of the collection to create.
91-
vector_config {VectorSearchAlgorithmConfiguration} -- Optional search algorithm configuration
92-
(default: {None}).
91+
vector_config {HnswVectorSearchAlgorithmConfiguration} -- Optional search algorithm configuration
92+
(default: {None}).
9393
semantic_config {SemanticConfiguration} -- Optional search index configuration (default: {None}).
9494
Returns:
9595
None
@@ -100,7 +100,7 @@ async def create_collection_async(
100100
else:
101101
vector_search = VectorSearch(
102102
algorithm_configurations=[
103-
VectorSearchAlgorithmConfiguration(
103+
HnswVectorSearchAlgorithmConfiguration(
104104
name="az-vector-config",
105105
kind="hnsw",
106106
hnsw_parameters={
@@ -403,12 +403,14 @@ async def get_nearest_matches_async(
403403
collection_name.lower()
404404
)
405405

406+
vector = Vector(
407+
value=embedding.flatten(), k=limit, fields=SEARCH_FIELD_EMBEDDING
408+
)
409+
406410
search_results = await search_client.search(
407411
search_text="*",
408-
vector_fields=SEARCH_FIELD_EMBEDDING,
409-
vector=embedding.tolist(),
412+
vectors=[vector],
410413
select=get_field_selection(with_embeddings),
411-
top_k=limit,
412414
)
413415

414416
if not search_results or search_results is None:

0 commit comments

Comments
 (0)