community[patch]:Support filter for AzureAISearchRetriever (#22303)

**Description**: 
The AzureAISearchRetriever does not support the "$filter" argument
offered in the AISearch API:
https://learn.microsoft.com/en-us/rest/api/searchservice/documents/search-get?view=rest-searchservice-2023-11-01&tabs=HTTP
The $filter allows filtering of indexes based on values in metadata.

**Issue**: 
https://github.com/langchain-ai/langchain/issues/19885

**Dependencies**: 
No

**Twitter handle**: 
@Jeffreym9M
 

- [ ] **Add tests and docs**: Not relevant


- [x] **Lint and test**: Run `make format`, `make lint` and `make test`
from the root of the package(s) you've modified. See contribution
guidelines for more: https://python.langchain.com/docs/contributing/
This commit is contained in:
Jeffrey Mak 2024-06-05 19:53:19 -04:00 committed by GitHub
parent 148088a588
commit 5fc5ed463c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -36,6 +36,8 @@ class AzureAISearchRetriever(BaseRetriever):
"""Key in a retrieved result to set as the Document page_content.""" """Key in a retrieved result to set as the Document page_content."""
top_k: Optional[int] = None top_k: Optional[int] = None
"""Number of results to retrieve. Set to None to retrieve all results.""" """Number of results to retrieve. Set to None to retrieve all results."""
filter: Optional[str] = None
"""OData $filter expression to apply to the search query."""
class Config: class Config:
extra = Extra.forbid extra = Extra.forbid
@ -72,7 +74,8 @@ class AzureAISearchRetriever(BaseRetriever):
base_url = self.service_name base_url = self.service_name
endpoint_path = f"indexes/{self.index_name}/docs?api-version={self.api_version}" endpoint_path = f"indexes/{self.index_name}/docs?api-version={self.api_version}"
top_param = f"&$top={self.top_k}" if self.top_k else "" top_param = f"&$top={self.top_k}" if self.top_k else ""
return base_url + endpoint_path + f"&search={query}" + top_param filter_param = f"&$filter={self.filter}" if self.filter else ""
return base_url + endpoint_path + f"&search={query}" + top_param + filter_param
@property @property
def _headers(self) -> Dict[str, str]: def _headers(self) -> Dict[str, str]: