Skip to content

Python: ACS should use Hnsw for vector indexing #2534

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
merged 27 commits into from
Aug 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
6 changes: 3 additions & 3 deletions python/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion python/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ psycopg = "^3.1.9"
psycopg-binary = "^3.1.9"

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
from azure.core.exceptions import ResourceNotFoundError
from azure.search.documents.indexes.aio import SearchIndexClient
from azure.search.documents.indexes.models import (
HnswVectorSearchAlgorithmConfiguration,
SearchIndex,
VectorSearch,
VectorSearchAlgorithmConfiguration,
)
from azure.search.documents.models import Vector
from numpy import ndarray

from semantic_kernel.connectors.memory.azure_cognitive_search.utils import (
Expand Down Expand Up @@ -58,7 +59,6 @@ def __init__(
Instantiate using Async Context Manager:
async with AzureCognitiveSearchMemoryStore(<...>) as memory:
await memory.<...>

"""
try:
pass
Expand All @@ -82,14 +82,14 @@ async def close_async(self):
async def create_collection_async(
self,
collection_name: str,
vector_config: Optional[VectorSearchAlgorithmConfiguration] = None,
vector_config: Optional[HnswVectorSearchAlgorithmConfiguration] = None,
) -> None:
"""Creates a new collection if it does not exist.

Arguments:
collection_name {str} -- The name of the collection to create.
vector_config {VectorSearchAlgorithmConfiguration} -- Optional search algorithm configuration
(default: {None}).
vector_config {HnswVectorSearchAlgorithmConfiguration} -- Optional search algorithm configuration
(default: {None}).
semantic_config {SemanticConfiguration} -- Optional search index configuration (default: {None}).
Returns:
None
Expand All @@ -100,7 +100,7 @@ async def create_collection_async(
else:
vector_search = VectorSearch(
algorithm_configurations=[
VectorSearchAlgorithmConfiguration(
HnswVectorSearchAlgorithmConfiguration(
name="az-vector-config",
kind="hnsw",
hnsw_parameters={
Expand Down Expand Up @@ -403,12 +403,14 @@ async def get_nearest_matches_async(
collection_name.lower()
)

vector = Vector(
value=embedding.flatten(), k=limit, fields=SEARCH_FIELD_EMBEDDING
)

search_results = await search_client.search(
search_text="*",
vector_fields=SEARCH_FIELD_EMBEDDING,
vector=embedding.tolist(),
vectors=[vector],
select=get_field_selection(with_embeddings),
top_k=limit,
)

if not search_results or search_results is None:
Expand Down