DSPy: The framework for programming—not prompting—language models
Python
feat(dspy): add support for vector, hybrid and fulltext search in azure ai search
HARISHKUMAR11120012818fce29e4c
diff --git a/dspy/retrieve/azureaisearch_rm.py b/dspy/retrieve/azureaisearch_rm.pyindex 6847ada5e..90e00624c 100644--- a/dspy/retrieve/azureaisearch_rm.py+++ b/dspy/retrieve/azureaisearch_rm.py@@ -1,4 +1,5 @@-from typing import List, Optional, Union+import warnings+from typing import Any, Callable, List, Optional, Unionimport dspyfrom dsp.utils.utils import dotdict@@ -7,13 +8,22 @@ try:from azure.core.credentials import AzureKeyCredentialfrom azure.search.documents import SearchClientfrom azure.search.documents._paging import SearchItemPaged- from azure.search.documents.models import QueryType+ from azure.search.documents.models import QueryType, VectorFilterMode, VectorizedQueryexcept ImportError:raise ImportError("You need to install azure-search-documents library"- "Please use the command: pip install azure-search-documents",+ "Please use the command: pip install azure-search-documents==11.6.0b1",)+try:+ import openai+except ImportError:+ warnings.warn(+ "`openai` is not installed. Install it with `pip install openai` to use AzureOpenAI embedding models.",+ category=ImportWarning,+ )++class AzureAISearchRM(dspy.Retrieve):"""@@ -24,7 +34,11 @@ class AzureAISearchRM(dspy.Retrieve):search_api_key (str): The API key for accessing the Azure AI Search service.search_index_name (str): The name of the search index in the Azure AI Search service.field_text (str): The name of the field containing text content in the search index. This field will be mapped to the "content" field in the dsp framework.+ field_vector (Optional[str]): The name of the field containing vector content in the search index. Defaults to None.k (int, optional): The default number of top passages to retrieve. Defaults to 3.+ azure_openai_client (Optional[openai.AzureOpenAI]): An instance of the AzureOpenAI client. Defaults to None.+ openai_embed_model (Optional[str]): The name of the OpenAI embedding model. Defaults to "text-embedding-ada-002".+ embedding_func (Optional[Callable]): A function for generating embeddings. Defaults to None.semantic_ranker (bool, optional): Whether to use semantic ranking. Defaults to False.filter (str, optional): Additional filter query. Defaults to None.query_language (str, optional): The language of the query. Defaults to "en-Us".@@ -32,6 +46,10 @@ class AzureAISearchRM(dspy.Retrieve):use_semantic_captions (bool, optional): Whether to use semantic captions. Defaults to False.query_type (Optional[QueryType], optional): The type of query. Defaults to QueryType.FULL.semantic_configuration_name (str, optional): The name of the semantic configuration. Defaults to None.+ is_vector_search (Optional[bool]): Whether to enable vector search. Defaults to False.+ is_hybrid_search (Optional[bool]): Whether to enable hybrid search. Defaults to False.+ is_fulltext_search (Optional[bool]): Whether to enable fulltext search. Defaults to True.+ vector_filter_mode (Optional[VectorFilterMode]): The vector filter mode. Defaults to None.Examples:Below is a code snippet that demonstrates how to instantiate and use the AzureAISearchRM class:@@ -48,16 +66,32 @@ class AzureAISearchRM(dspy.Retrieve):search_service_name (str): The name of the Azure AI Search service.search_api_key (str): The API key for accessing the Azure AI Search service.search_index_name (str): The name of the search index in the Azure AI Search service.- field_text (str): The name of the field containing text content in the search index.endpoint (str): The endpoint URL for the Azure AI Search service.+ field_text (str): The name of the field containing text content in the search index.+ field_vector (Optional[str]): The name of the field containing vector content in the search index.+ azure_openai_client (Optional[openai.AzureOpenAI]): An instance of the AzureOpenAI client.+ openai_embed_model (Optional[str]): The name of the OpenAI embedding model.+ embedding_func (Optional[Callable]): A function for generating embeddings.credential (AzureKeyCredential): The Azure key credential for accessing the service.client (SearchClient): The Azure AI Search client instance.+ semantic_ranker (bool): Whether to use semantic ranking.+ filter (str): Additional filter query.+ query_language (str): The language of the query.+ query_speller (str): The speller mode.+ use_semantic_captions (bool): Whether to use semantic captions.+ query_type (Optional[QueryType]): The type of query.+ semantic_configuration_name (str): The name of the semantic configuration.+ is_vector_search (Optional[bool]): Whether to enable vector search.+ is_hybrid_search (Optional[bool]): Whether to enable hybrid search.+ is_fulltext_search (Optional[bool]): Whether to enable fulltext search.+ vector_filter_mode (Optional[VectorFilterMode]): The vector filter mode.Methods:forward(query_or_queries: Union[str, List[str]], k: Optional[int]) -> dspy.Prediction:Search for the top passages corresponding to the given query or queries.azure_search_request(+ self,key_content: str,client: SearchClient,query: str,@@ -68,13 +102,38 @@ class AzureAISearchRM(dspy.Retrieve):query_speller: str,use_semantic_captions: bool,query_type: QueryType,- semantic_configuration_name: str+ semantic_configuration_name: str,+ is_vector_search: bool,+ is_hybrid_search: bool,+ is_fulltext_search: bool,+ field_vector: str,+ vector_filter_mode: VectorFilterMode) -> List[dict]:Perform a search request to the Azure AI Search service.- process_azure_result(results: SearchItemPaged, content_key: str, content_score: str) -> List[dict]:+ process_azure_result(+ self,+ results:SearchItemPaged,+ content_key:str,+ content_score: str+ ) -> List[dict]:Process the results received from the Azure AI Search service and map them to the correct format.+ get_embeddings(+ self,+ query: str,+ k_nearest_neighbors: int,+ field_vector: str+ ) -> List | Any:+ Returns embeddings for the given query.++ check_sementic_configuration(+ self,+ semantic_configuration_name,+ query_type+ ):+ Checks semantic configuration.+Raises:ImportError: If the required Azure AI Search libraries are not installed.@@ -89,7 +148,11 @@ class AzureAISearchRM(dspy.Retrieve):search_api_key: str,search_index_name: str,field_text: str,+ field_vector: Optional[str] = None,k: int = 3,+ azure_openai_client: Optional[openai.AzureOpenAI] = None,+ openai_embed_model: Optional[str] = "text-embedding-ada-002",+ embedding_func: Optional[Callable] = None,semantic_ranker: bool = False,filter: str = None,query_language: str = "en-Us",@@ -97,18 +160,25 @@ class AzureAISearchRM(dspy.Retrieve):use_semantic_captions: bool = False,query_type: Optional[QueryType] = QueryType.FULL,semantic_configuration_name: str = None,-+ is_vector_search: Optional[bool] = False,+ is_hybride_search: Optional[bool] = False,+ is_fulltext_search: Optional[bool] = True,+ vector_filter_mode: Optional[VectorFilterMode.PRE_FILTER] = None,):self.search_service_name = search_service_nameself.search_api_key = search_api_keyself.search_index_name = search_index_name- self.endpoint=f"https://{self.search_service_name}.search.windows.net"- self.field_text = field_text # field name of the text content+ self.endpoint = f"https://{self.search_service_name}.search.windows.net"+ self.field_text = field_text # field name of the text content+ self.field_vector = field_vector # field name of the vector content+ self.azure_openai_client = azure_openai_client+ self.openai_embed_model = openai_embed_model+ self.embedding_func = embedding_func# Create a clientself.credential = AzureKeyCredential(self.search_api_key)- self.client = SearchClient(endpoint=self.endpoint,- index_name=self.search_index_name,- credential=self.credential)+ self.client = SearchClient(+ endpoint=self.endpoint, index_name=self.search_index_name, credential=self.credential,+ )self.semantic_ranker = semantic_rankerself.filter = filterself.query_language = query_language@@ -116,39 +186,106 @@ class AzureAISearchRM(dspy.Retrieve):self.use_semantic_captions = use_semantic_captionsself.query_type = query_typeself.semantic_configuration_name = semantic_configuration_name+ self.is_vector_search = is_vector_search+ self.is_hybride_search = is_hybride_search+ self.is_fulltext_search = is_fulltext_search+ self.vector_filter_mode = vector_filter_modesuper().__init__(k=k)- def azure_search_request(self,key_content: str, client: SearchClient, query: str, top: int, semantic_ranker: bool, filter: str, query_language: str, query_speller: str, use_semantic_captions: bool, query_type: QueryType, semantic_configuration_name: str):+ def azure_search_request(+ self,+ key_content: str,+ client: SearchClient,+ query: str,+ top: int,+ semantic_ranker: bool,+ filter: str,+ query_language: str,+ query_speller: str,+ use_semantic_captions: bool,+ query_type: QueryType,+ semantic_configuration_name: str,+ is_vector_search: bool,+ is_hybrid_search: bool,+ is_fulltext_search: bool,+ field_vector: str,+ vector_filter_mode: VectorFilterMode,+ ):"""Search in Azure AI Search Index"""- # TODO: Add Support for Vector Search And Hybride Search- if semantic_ranker:- results = client.search(search_text=query,- filter=filter,- query_type=query_type,- query_language = query_language,- query_speller=query_speller,- semantic_configuration_name=semantic_configuration_name,- top=top,- query_caption = (- 'extractive|highlight-false'- if use_semantic_captions- else None- ),- )- else:- results = client.search(search_text=query,top=top,filter=filter)+ if is_vector_search:+ vector_query = self.get_embeddings(query, top, field_vector)+ if semantic_ranker:+ self.check_sementic_configuration(semantic_configuration_name, query_type)+ results = client.search(+ search_text=None,+ filter=filter,+ query_type=query_type,+ vector_queries=[vector_query],+ vector_filter_mode=vector_filter_mode,+ semantic_configuration_name=semantic_configuration_name,+ top=top,+ )+ else:+ results = client.search(+ search_text=None,+ filter=filter,+ vector_queries=[vector_query],+ vector_filter_mode=vector_filter_mode,+ top=top,+ )+ if is_hybrid_search:+ vector_query = self.get_embeddings(query, top, field_vector)+ if semantic_ranker:+ self.check_sementic_configuration(semantic_configuration_name, query_type)+ results = client.search(+ search_text=query,+ filter=filter,+ query_type=query_type,+ query_language=query_language,+ query_speller=query_speller,+ semantic_configuration_name=semantic_configuration_name,+ top=top,+ vector_queries=[vector_query],+ vector_filter_mode=vector_filter_mode,+ query_caption=("extractive|highlight-false" if use_semantic_captions else None),+ )+ else:+ results = client.search(+ search_text=query,+ filter=filter,+ query_language=query_language,+ query_speller=query_speller,+ top=top,+ vector_queries=[vector_query],+ vector_filter_mode=vector_filter_mode,+ )+ if is_fulltext_search:+ if semantic_ranker:+ self.check_sementic_configuration(semantic_configuration_name, query_type)+ results = client.search(+ search_text=query,+ filter=filter,+ query_type=query_type,+ query_language=query_language,+ query_speller=query_speller,+ semantic_configuration_name=semantic_configuration_name,+ top=top,+ query_caption=("extractive|highlight-false" if use_semantic_captions else None),+ )+ else:+ results = client.search(search_text=query, top=top, filter=filter)- sorted_results = sorted(results, key=lambda x: x['@search.score'], reverse=True)+ sorted_results = sorted(results, key=lambda x: x["@search.score"], reverse=True)sorted_results = self.process_azure_result(sorted_results, key_content, key_content)return sorted_results- def process_azure_result(self,results:SearchItemPaged, content_key:str, content_score: str):+ def process_azure_result(self, results: SearchItemPaged, content_key: str, content_score: str):"""process received result from Azure AI Search as dictionary array and map content and score to correct format"""@@ -156,16 +293,16 @@ class AzureAISearchRM(dspy.Retrieve):for result in results:tmp = {}for key, value in result.items():- if(key == content_key):- tmp["text"] = value # assign content- elif(key == content_score):+ if key == content_key:+ tmp["text"] = value # assign content+ elif key == content_score:tmp["score"] = valueelse:tmp[key] = valueres.append(tmp)return res- def forward(self, query_or_queries: Union[str, List[str]], k: Optional[int]) -> dspy.Prediction:+ def forward(self, query_or_queries: Union[str, List[str]], k: Optional[int]) -> dspy.Prediction:"""Search with pinecone for self.k top passages for query@@ -177,25 +314,77 @@ class AzureAISearchRM(dspy.Retrieve):"""k = k if k is not None else self.k- queries = (- [query_or_queries]- if isinstance(query_or_queries, str)- else query_or_queries- )+ queries = [query_or_queries] if isinstance(query_or_queries, str) else query_or_queriesqueries = [q for q in queries if q] # Filter empty queriespassages = []for query in queries:- results = self.azure_search_request(self.field_text,- self.client, query,- k,- self.semantic_ranker,- self.filter,- self.query_language,- self.query_speller,- self.use_semantic_captions,- self.query_type,- self.semantic_configuration_name)- passages.extend(dotdict({"long_text": d['text']}) for d in results)+ results = self.azure_search_request(+ self.field_text,+ self.client,+ query,+ k,+ self.semantic_ranker,+ self.filter,+ self.query_language,+ self.query_speller,+ self.use_semantic_captions,+ self.query_type,+ self.semantic_configuration_name,+ self.is_vector_search,+ self.is_hybride_search,+ self.is_fulltext_search,+ self.field_vector,+ self.vector_filter_mode,+ )+ passages.extend(dotdict({"long_text": d["text"]}) for d in results)return passages++ def get_embeddings(self, query: str, k_nearest_neighbors: int, field_vector: str) -> List | Any:+ """+ Returns embeddings for the given query.++ Args:+ query (str): The query for which embeddings are to be retrieved.+ k_nearest_neighbors (int): The number of nearest neighbors to consider.+ field_vector (str): The field vector to use for embeddings.++ Returns:+ list: A list containing the vectorized query.+ Any: The result of embedding_func if azure_openai_client is not provided.++ Raises:+ AssertionError: If neither azure_openai_client nor embedding_func is provided,+ or if field_vector is not provided.+ """+ assert (+ self.azure_openai_client or self.embedding_func+ ), "Either azure_openai_client or embedding_func must be provided."+ assert field_vector, "field_vector must be provided."++ if self.azure_openai_client is not None:+ embedding = (+ self.azure_openai_client.embeddings.create(input=query, model=self.openai_embed_model).data[0].embedding+ )+ vector_query = VectorizedQuery(+ vector=embedding, k_nearest_neighbors=k_nearest_neighbors, fields=field_vector,+ )+ return [vector_query]+ else:+ return self.embedding_func(query)++ def check_sementic_configuration(self, semantic_configuration_name, query_type):+ """+ Checks semantic configuration.++ Args:+ semantic_configuration_name: The name of the semantic configuration.+ query_type: The type of the query.++ Raises:+ AssertionError: If semantic_configuration_name is not provided+ or if query_type is not QueryType.SEMANTIC.+ """+ assert semantic_configuration_name, "Semantic configuration name must be provided."+ assert query_type == QueryType.SEMANTIC, "Query type must be QueryType.SEMANTIC."