Skip to content
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

Feat/fix Azure AI Search Hybrid Semantic Search Unusability due to hardcoded parameter #17683

Merged
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class AzureAISearchVectorStore(BasePydanticVectorStore):
doc_id_field_key="doc_id",
language_analyzer="en.lucene",
vector_algorithm_type="exhaustiveKnn",
semantic_configuration_name="mySemanticConfig",
)
```
"""
Expand All @@ -141,6 +142,7 @@ class AzureAISearchVectorStore(BasePydanticVectorStore):
_vector_profile_name: str = PrivateAttr()
_compression_type: str = PrivateAttr()
_user_agent: str = PrivateAttr()
_semantic_configuration_name: str = PrivateAttr()

def _normalise_metadata_to_index_fields(
self,
Expand Down Expand Up @@ -369,7 +371,7 @@ def _create_index(self, index_name: Optional[str]) -> None:
)
logger.info(f"Configuring {index_name} semantic search")
semantic_config = SemanticConfiguration(
name="mySemanticConfig",
name=self._semantic_configuration_name,
prioritized_fields=SemanticPrioritizedFields(
content_fields=[SemanticField(field_name=self._field_mapping["chunk"])],
),
Expand Down Expand Up @@ -491,7 +493,7 @@ async def _acreate_index(self, index_name: Optional[str]) -> None:
)
logger.info(f"Configuring {index_name} semantic search")
semantic_config = SemanticConfiguration(
name="mySemanticConfig",
name=self._semantic_configuration_name,
prioritized_fields=SemanticPrioritizedFields(
content_fields=[SemanticField(field_name=self._field_mapping["chunk"])],
),
Expand Down Expand Up @@ -553,6 +555,7 @@ def __init__(
# https://learn.microsoft.com/en-us/azure/search/index-add-language-analyzers
language_analyzer: str = "en.lucene",
compression_type: str = "none",
semantic_configuration_name: Optional[str] = None,
user_agent: Optional[str] = None,
**kwargs: Any,
) -> None:
Expand Down Expand Up @@ -633,7 +636,7 @@ def __init__(
raise ValueError(
"Only 'exhaustiveKnn' and 'hnsw' are supported for vector_algorithm_type"
)

self._semantic_configuration_name = semantic_configuration_name
self._language_analyzer = language_analyzer
self._compression_type = compression_type.lower()

Expand Down Expand Up @@ -1162,24 +1165,30 @@ def _create_odata_filter(self, metadata_filters: MetadataFilters) -> str:

def query(self, query: VectorStoreQuery, **kwargs: Any) -> VectorStoreQueryResult:
odata_filter = None
semantic_configuration_name = None
if query.filters is not None:
odata_filter = self._create_odata_filter(query.filters)
azure_query_result_search: AzureQueryResultSearchBase = (
AzureQueryResultSearchDefault(
query,
self._field_mapping,
odata_filter,
self._search_client,
self._async_search_client,
if self._semantic_configuration_name is not None:
semantic_configuration_name = self._semantic_configuration_name
if query.mode == VectorStoreQueryMode.DEFAULT:
azure_query_result_search: AzureQueryResultSearchBase = (
AzureQueryResultSearchDefault(
query,
self._field_mapping,
odata_filter,
self._search_client,
self._async_search_client,
semantic_configuration_name,
)
)
)
if query.mode == VectorStoreQueryMode.SPARSE:
azure_query_result_search = AzureQueryResultSearchSparse(
query,
self._field_mapping,
odata_filter,
self._search_client,
self._async_search_client,
semantic_configuration_name,
)
elif query.mode == VectorStoreQueryMode.HYBRID:
azure_query_result_search = AzureQueryResultSearchHybrid(
Expand All @@ -1188,6 +1197,7 @@ def query(self, query: VectorStoreQuery, **kwargs: Any) -> VectorStoreQueryResul
odata_filter,
self._search_client,
self._async_search_client,
semantic_configuration_name,
)
elif query.mode == VectorStoreQueryMode.SEMANTIC_HYBRID:
azure_query_result_search = AzureQueryResultSearchSemanticHybrid(
Expand All @@ -1196,6 +1206,7 @@ def query(self, query: VectorStoreQuery, **kwargs: Any) -> VectorStoreQueryResul
odata_filter,
self._search_client,
self._async_search_client,
self._semantic_configuration_name,
)
return azure_query_result_search.search()

Expand Down Expand Up @@ -1244,6 +1255,7 @@ async def aquery(
odata_filter,
self._search_client,
self._async_search_client,
self._semantic_configuration_name,
)
return await azure_query_result_search.asearch()

Expand Down Expand Up @@ -1378,12 +1390,14 @@ def __init__(
odata_filter: Optional[str],
search_client: SearchClient,
async_search_client: AsyncSearchClient,
semantic_configuration_name: Optional[str],
) -> None:
self._query = query
self._field_mapping = field_mapping
self._odata_filter = odata_filter
self._search_client = search_client
self._async_search_client = async_search_client
self._semantic_configuration_name = semantic_configuration_name

@property
def _select_fields(self) -> List[str]:
Expand All @@ -1409,6 +1423,7 @@ def _create_query_result(
top=self._query.similarity_top_k,
select=self._select_fields,
filter=self._odata_filter,
semantic_configuration_name=self._semantic_configuration_name,
)

id_result = []
Expand Down Expand Up @@ -1462,6 +1477,7 @@ async def _acreate_query_result(
top=self._query.similarity_top_k,
select=self._select_fields,
filter=self._odata_filter,
semantic_configuration_name=self._semantic_configuration_name,
)

id_result = []
Expand Down Expand Up @@ -1585,7 +1601,7 @@ def _create_query_result(
select=self._select_fields,
filter=self._odata_filter,
query_type="semantic",
semantic_configuration_name="mySemanticConfig",
semantic_configuration_name=self._semantic_configuration_name,
)

id_result = []
Expand Down Expand Up @@ -1641,7 +1657,7 @@ async def _acreate_query_result(
select=self._select_fields,
filter=self._odata_filter,
query_type="semantic",
semantic_configuration_name="mySemanticConfig",
semantic_configuration_name=self._semantic_configuration_name,
)

id_result = []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ exclude = ["**/BUILD"]
license = "MIT"
name = "llama-index-vector-stores-azureaisearch"
readme = "README.md"
version = "0.3.2"
version = "0.3.3"

[tool.poetry.dependencies]
python = ">=3.9,<4.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def create_mock_vector_store(
index_name=index_name,
index_management=index_management,
embedding_dimensionality=2, # Assuming a dimensionality of 2 for simplicity
semantic_configuration_name="default",
)


Expand Down Expand Up @@ -243,6 +244,7 @@ def test_azureaisearch_query() -> None:
top=2,
select=["id", "content", "metadata", "doc_id"],
filter=None,
semantic_configuration_name="default",
)

# Assert the result structure
Expand Down
Loading