From 2b17003575a93cafae1756163762711fe7f95141 Mon Sep 17 00:00:00 2001 From: Xu Wu Date: Fri, 20 Dec 2024 13:57:53 +0800 Subject: [PATCH 1/3] add azure vector semantic search mode support for async queries --- .../querying/response_synthesizers/index.md | 2 +- .../vector_stores/azureaisearch/base.py | 65 +++++++++++++++++-- 2 files changed, 62 insertions(+), 5 deletions(-) diff --git a/docs/docs/module_guides/querying/response_synthesizers/index.md b/docs/docs/module_guides/querying/response_synthesizers/index.md index a3fff09d6bba5..d934de35cfd6d 100644 --- a/docs/docs/module_guides/querying/response_synthesizers/index.md +++ b/docs/docs/module_guides/querying/response_synthesizers/index.md @@ -125,7 +125,7 @@ Several response synthesizers are implemented already in LlamaIndex: ## Custom Response Synthesizers -Each response synthesizer inherits from `llama_index.response_synthesizers.base.BaseSynthesizer`. The base API is extremely simple, which makes it easy to create your own response synthesizer. +Each response synthesizer inherits from `llama_index.core.response_synthesizers.base.BaseSynthesizer`. The base API is extremely simple, which makes it easy to create your own response synthesizer. Maybe you want to customize which template is used at each step in `tree_summarize`, or maybe a new research paper came out detailing a new way to generate a response to a query, you can create your own response synthesizer and plug it into any query engine or use it on it's own. diff --git a/llama-index-integrations/vector_stores/llama-index-vector-stores-azureaisearch/llama_index/vector_stores/azureaisearch/base.py b/llama-index-integrations/vector_stores/llama-index-vector-stores-azureaisearch/llama_index/vector_stores/azureaisearch/base.py index 4bac8671043b1..d96d4f06d534c 100644 --- a/llama-index-integrations/vector_stores/llama-index-vector-stores-azureaisearch/llama_index/vector_stores/azureaisearch/base.py +++ b/llama-index-integrations/vector_stores/llama-index-vector-stores-azureaisearch/llama_index/vector_stores/azureaisearch/base.py @@ -12,6 +12,7 @@ from azure.search.documents.indexes.aio import ( SearchIndexClient as AsyncSearchIndexClient, ) + from llama_index.core.bridge.pydantic import PrivateAttr from llama_index.core.schema import BaseNode, MetadataMode, TextNode from llama_index.core.vector_stores.types import ( @@ -30,9 +31,9 @@ ) from llama_index.vector_stores.azureaisearch.azureaisearch_utils import ( create_node_from_result, - process_batch_results, create_search_request, handle_search_error, + process_batch_results, ) logger = logging.getLogger(__name__) @@ -398,10 +399,10 @@ async def _acreate_index(self, index_name: Optional[str]) -> None: ExhaustiveKnnParameters, HnswAlgorithmConfiguration, HnswParameters, + SearchableField, SearchField, SearchFieldDataType, SearchIndex, - SearchableField, SemanticConfiguration, SemanticField, SemanticPrioritizedFields, @@ -1576,11 +1577,11 @@ def _create_query_vector(self) -> Optional[List[Any]]: return vector_queries def _create_query_result( - self, search_query: str, vector_queries: Optional[List[Any]] + self, search_query: str, vectors: Optional[List[Any]] ) -> VectorStoreQueryResult: results = self._search_client.search( search_text=search_query, - vector_queries=vector_queries, + vector_queries=vectors, top=self._query.similarity_top_k, select=self._select_fields, filter=self._odata_filter, @@ -1631,5 +1632,61 @@ def _create_query_result( nodes=node_result, similarities=score_result, ids=id_result ) + async def _acreate_query_result( + self, search_query: str, vectors: Optional[List[Any]] + ) -> VectorStoreQueryResult: + results = await self._search_client.search( + search_text=search_query, + vector_queries=vectors, + top=self._query.similarity_top_k, + select=self._select_fields, + filter=self._odata_filter, + query_type="semantic", + semantic_configuration_name="mySemanticConfig", + ) + + id_result = [] + node_result = [] + score_result = [] + async for result in results: + node_id = result[self._field_mapping["id"]] + metadata_str = result[self._field_mapping["metadata"]] + metadata = json.loads(metadata_str) if metadata_str else {} + # use reranker_score instead of score + score = result["@search.reranker_score"] + chunk = result[self._field_mapping["chunk"]] + + try: + node = metadata_dict_to_node(metadata) + node.set_content(chunk) + except Exception: + # NOTE: deprecated legacy logic for backward compatibility + metadata, node_info, relationships = legacy_metadata_dict_to_node( + metadata + ) + + node = TextNode( + text=chunk, + id_=node_id, + metadata=metadata, + start_char_idx=node_info.get("start", None), + end_char_idx=node_info.get("end", None), + relationships=relationships, + ) + + logger.debug(f"Retrieved node id {node_id} with node data of {node}") + + id_result.append(node_id) + node_result.append(node) + score_result.append(score) + + logger.debug( + f"Search query '{search_query}' returned {len(id_result)} results." + ) + + return VectorStoreQueryResult( + nodes=node_result, similarities=score_result, ids=id_result + ) + CognitiveSearchVectorStore = AzureAISearchVectorStore From 6e0b4c8350bddb4b4cf15f882aebf364da4f9ccf Mon Sep 17 00:00:00 2001 From: Xu Wu Date: Fri, 20 Dec 2024 14:01:28 +0800 Subject: [PATCH 2/3] revert doc change --- docs/docs/module_guides/querying/response_synthesizers/index.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/module_guides/querying/response_synthesizers/index.md b/docs/docs/module_guides/querying/response_synthesizers/index.md index d934de35cfd6d..a3fff09d6bba5 100644 --- a/docs/docs/module_guides/querying/response_synthesizers/index.md +++ b/docs/docs/module_guides/querying/response_synthesizers/index.md @@ -125,7 +125,7 @@ Several response synthesizers are implemented already in LlamaIndex: ## Custom Response Synthesizers -Each response synthesizer inherits from `llama_index.core.response_synthesizers.base.BaseSynthesizer`. The base API is extremely simple, which makes it easy to create your own response synthesizer. +Each response synthesizer inherits from `llama_index.response_synthesizers.base.BaseSynthesizer`. The base API is extremely simple, which makes it easy to create your own response synthesizer. Maybe you want to customize which template is used at each step in `tree_summarize`, or maybe a new research paper came out detailing a new way to generate a response to a query, you can create your own response synthesizer and plug it into any query engine or use it on it's own. From 31262e7590cdccece189931755c316bc61d0de09 Mon Sep 17 00:00:00 2001 From: Xu Wu Date: Sat, 21 Dec 2024 09:02:08 +0800 Subject: [PATCH 3/3] use async client --- .../llama_index/vector_stores/azureaisearch/base.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/llama-index-integrations/vector_stores/llama-index-vector-stores-azureaisearch/llama_index/vector_stores/azureaisearch/base.py b/llama-index-integrations/vector_stores/llama-index-vector-stores-azureaisearch/llama_index/vector_stores/azureaisearch/base.py index d96d4f06d534c..a2e1dd406bc53 100644 --- a/llama-index-integrations/vector_stores/llama-index-vector-stores-azureaisearch/llama_index/vector_stores/azureaisearch/base.py +++ b/llama-index-integrations/vector_stores/llama-index-vector-stores-azureaisearch/llama_index/vector_stores/azureaisearch/base.py @@ -12,7 +12,6 @@ from azure.search.documents.indexes.aio import ( SearchIndexClient as AsyncSearchIndexClient, ) - from llama_index.core.bridge.pydantic import PrivateAttr from llama_index.core.schema import BaseNode, MetadataMode, TextNode from llama_index.core.vector_stores.types import ( @@ -1635,7 +1634,7 @@ def _create_query_result( async def _acreate_query_result( self, search_query: str, vectors: Optional[List[Any]] ) -> VectorStoreQueryResult: - results = await self._search_client.search( + results = await self._async_search_client.search( search_text=search_query, vector_queries=vectors, top=self._query.similarity_top_k,