From af44829f8a30c59758afd8d27619791b1bba9c25 Mon Sep 17 00:00:00 2001 From: Claire Date: Mon, 30 Sep 2024 15:56:35 +0200 Subject: [PATCH 1/3] add a bearer token class to have a constructor --- .../vectorstores/azuresearch.py | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/libs/community/langchain_community/vectorstores/azuresearch.py b/libs/community/langchain_community/vectorstores/azuresearch.py index 3e022d31654cb..be833ed72380f 100644 --- a/libs/community/langchain_community/vectorstores/azuresearch.py +++ b/libs/community/langchain_community/vectorstores/azuresearch.py @@ -119,6 +119,21 @@ def _get_search_client( VectorSearchProfile, ) + class AzureBearerTokenCredential(TokenCredential): + def __init__(self, token: str): + # set the expiry to an hour from now. + self._token = AccessToken(token, int(time.time()) + 3600) + + def get_token( + self, + *scopes: str, + claims: str | None = None, + tenant_id: str | None = None, + enable_cae: bool = False, + **kwargs: Any, + ) -> AccessToken: + return self._token + additional_search_client_options = additional_search_client_options or {} default_fields = default_fields or [] credential: Union[AzureKeyCredential, TokenCredential, InteractiveBrowserCredential] @@ -131,11 +146,7 @@ def _get_search_client( else: credential = AzureKeyCredential(key) elif azure_ad_access_token is not None: - credential = TokenCredential( - lambda *scopes, **kwargs: AccessToken( - azure_ad_access_token, int(time.time()) + 3600 - ) - ) + credential = AzureBearerTokenCredential(azure_ad_access_token) else: credential = DefaultAzureCredential() index_client: SearchIndexClient = SearchIndexClient( From 1b262a0b5e2a55e7f2346ae752a5791f2262b7b9 Mon Sep 17 00:00:00 2001 From: Claire Date: Tue, 1 Oct 2024 12:00:26 +0200 Subject: [PATCH 2/3] fix typing to support python<3.10 --- .../langchain_community/vectorstores/azuresearch.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/libs/community/langchain_community/vectorstores/azuresearch.py b/libs/community/langchain_community/vectorstores/azuresearch.py index be833ed72380f..1af8a81c59dd8 100644 --- a/libs/community/langchain_community/vectorstores/azuresearch.py +++ b/libs/community/langchain_community/vectorstores/azuresearch.py @@ -127,8 +127,8 @@ def __init__(self, token: str): def get_token( self, *scopes: str, - claims: str | None = None, - tenant_id: str | None = None, + claims: Optional[str] = None, + tenant_id: Optional[str] = None, enable_cae: bool = False, **kwargs: Any, ) -> AccessToken: @@ -1831,3 +1831,5 @@ def _peek(iterable: Iterable, default: Optional[Any] = None) -> Tuple[Iterable, return iterable, value except StopIteration: return iterable, default + + From 97274726638f6e9feddeea4da3994659874c0215 Mon Sep 17 00:00:00 2001 From: Erick Friis Date: Sun, 15 Dec 2024 21:54:01 -0800 Subject: [PATCH 3/3] x --- libs/community/langchain_community/vectorstores/azuresearch.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/libs/community/langchain_community/vectorstores/azuresearch.py b/libs/community/langchain_community/vectorstores/azuresearch.py index 8e6140d7fa4e5..6748f81dc2529 100644 --- a/libs/community/langchain_community/vectorstores/azuresearch.py +++ b/libs/community/langchain_community/vectorstores/azuresearch.py @@ -1837,5 +1837,3 @@ def _peek(iterable: Iterable, default: Optional[Any] = None) -> Tuple[Iterable, return iterable, value except StopIteration: return iterable, default - -