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

community: Azure Search Vector Store is missing Access Token Authentication #24330

Merged
merged 46 commits into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
c5c912c
Azure Search Access Token authentication
levalencia Jul 17, 2024
572719f
make format
levalencia Jul 17, 2024
8b22589
added azure.identity
levalencia Jul 17, 2024
4f447ad
type annotations
levalencia Jul 17, 2024
0c8b4c4
missing parameter
levalencia Jul 17, 2024
60f0271
reformat
levalencia Jul 17, 2024
0cecf7c
fix in from_texts and afrom_texts
levalencia Jul 17, 2024
83fa182
Incompatible types in assignment
levalencia Jul 17, 2024
9b16a5f
Missing positional argument "azure_ad_access_token" in call to "Azur…
levalencia Jul 17, 2024
074ed26
DEFAULT_ACCESS_TOKEN typo
levalencia Jul 17, 2024
60775a1
removed dependency
levalencia Jul 17, 2024
6f4db95
poetry lock consistency
levalencia Jul 17, 2024
2eb4cd3
azure core
levalencia Jul 17, 2024
e29820e
azure-identity>=1.15.0,<2
levalencia Jul 17, 2024
2c4ceef
test imports
levalencia Jul 18, 2024
1313733
Merge branch 'master' into AzureSearchVectorDatabase
levalencia Jul 18, 2024
f23b76e
removed import, is in the txt file
levalencia Jul 18, 2024
3beef0a
Merge branch 'AzureSearchVectorDatabase' of https://github.com/levale…
levalencia Jul 18, 2024
0277c6c
refactored bearer token with access token
levalencia Jul 18, 2024
ea60b36
removed azure core
levalencia Jul 18, 2024
05dacac
added azure-core
levalencia Jul 18, 2024
8d7db29
unit test fix
levalencia Jul 18, 2024
61831b3
Merge branch 'master' into AzureSearchVectorDatabase
levalencia Jul 24, 2024
5e61883
poetry lock
levalencia Jul 24, 2024
cd37ca9
Merge branch 'AzureSearchVectorDatabase' of https://github.com/levale…
levalencia Jul 24, 2024
dfdc945
poetry lock
levalencia Jul 24, 2024
c593cda
reordered arguments in order to enable Key to have a default value of…
levalencia Jul 24, 2024
7de2060
resolve conflic
levalencia Jul 24, 2024
8a842cb
make format
levalencia Jul 24, 2024
2dbfb39
Merge branch 'master' into AzureSearchVectorDatabase
levalencia Jul 24, 2024
f9f992c
Merge branch 'master' into AzureSearchVectorDatabase
levalencia Jul 25, 2024
73ea8b8
Merge branch 'master' into AzureSearchVectorDatabase
levalencia Aug 1, 2024
9948b96
Merge branch 'master' into AzureSearchVectorDatabase
levalencia Aug 7, 2024
c030c51
parameters errors
levalencia Aug 7, 2024
bd7cfdf
format
levalencia Aug 7, 2024
f3ff9c2
change
levalencia Aug 7, 2024
817c37c
Merge branch 'master' into AzureSearchVectorDatabase
levalencia Aug 14, 2024
63a3fec
Merge branch 'master' into AzureSearchVectorDatabase
levalencia Aug 16, 2024
0ed1ec2
Merge branch 'master' into AzureSearchVectorDatabase
levalencia Aug 19, 2024
4611e6c
test
levalencia Aug 19, 2024
19e5312
Merge branch 'AzureSearchVectorDatabase' of https://github.com/levale…
levalencia Aug 19, 2024
453ace9
Merge branch 'master' into AzureSearchVectorDatabase
levalencia Aug 19, 2024
5506a7c
Merge branch 'master' into AzureSearchVectorDatabase
levalencia Aug 26, 2024
7d8057a
Merge branch 'master' into AzureSearchVectorDatabase
efriis Aug 26, 2024
907a629
x
efriis Aug 26, 2024
bef2959
x
efriis Aug 26, 2024
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
1 change: 0 additions & 1 deletion libs/community/extended_testing_deps.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ cloudpickle>=2.0.0
cohere>=4,<6
databricks-vectorsearch>=0.21,<0.22
datasets>=2.15.0,<3
dedoc>=2.2.6,<3
dgml-utils>=0.3.0,<0.4
elasticsearch>=8.12.0,<9
esprima>=4.0.1,<5
Expand Down
39 changes: 29 additions & 10 deletions libs/community/langchain_community/vectorstores/azuresearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import itertools
import json
import logging
import time
import uuid
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -79,8 +80,9 @@

def _get_search_client(
endpoint: str,
key: str,
index_name: str,
key: Optional[str] = None,
azure_ad_access_token: Optional[str] = None,
semantic_configuration_name: Optional[str] = None,
fields: Optional[List[SearchField]] = None,
vector_search: Optional[VectorSearch] = None,
Expand All @@ -95,7 +97,7 @@ def _get_search_client(
async_: bool = False,
additional_search_client_options: Optional[Dict[str, Any]] = None,
) -> Union[SearchClient, AsyncSearchClient]:
from azure.core.credentials import AzureKeyCredential
from azure.core.credentials import AccessToken, AzureKeyCredential, TokenCredential
from azure.core.exceptions import ResourceNotFoundError
from azure.identity import DefaultAzureCredential, InteractiveBrowserCredential
from azure.search.documents import SearchClient
Expand All @@ -119,13 +121,23 @@ def _get_search_client(

additional_search_client_options = additional_search_client_options or {}
default_fields = default_fields or []
if key is None:
credential = DefaultAzureCredential()
elif key.upper() == "INTERACTIVE":
credential = InteractiveBrowserCredential()
credential.get_token("https://search.azure.com/.default")
credential: Union[AzureKeyCredential, TokenCredential, InteractiveBrowserCredential]

# Determine the appropriate credential to use
if key is not None:
if key.upper() == "INTERACTIVE":
credential = InteractiveBrowserCredential()
credential.get_token("https://search.azure.com/.default")
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
)
)
else:
credential = AzureKeyCredential(key)
credential = DefaultAzureCredential()
index_client: SearchIndexClient = SearchIndexClient(
endpoint=endpoint, credential=credential, user_agent=user_agent
)
Expand Down Expand Up @@ -253,6 +265,7 @@ def __init__(
self,
azure_search_endpoint: str,
azure_search_key: str,
azure_ad_access_token: Optional[str],
index_name: str,
embedding_function: Union[Callable, Embeddings],
search_type: str = "hybrid",
Expand Down Expand Up @@ -321,8 +334,9 @@ def __init__(
user_agent += " " + kwargs["user_agent"]
self.client = _get_search_client(
azure_search_endpoint,
azure_search_key,
index_name,
azure_search_key,
azure_ad_access_token,
semantic_configuration_name=semantic_configuration_name,
fields=fields,
vector_search=vector_search,
Expand All @@ -336,8 +350,9 @@ def __init__(
)
self.async_client = _get_search_client(
azure_search_endpoint,
azure_search_key,
index_name,
azure_search_key,
azure_ad_access_token,
semantic_configuration_name=semantic_configuration_name,
fields=fields,
vector_search=vector_search,
Expand Down Expand Up @@ -1387,6 +1402,7 @@ def from_texts(
metadatas: Optional[List[dict]] = None,
azure_search_endpoint: str = "",
azure_search_key: str = "",
azure_ad_access_token: Optional[str] = None,
index_name: str = "langchain-index",
fields: Optional[List[SearchField]] = None,
**kwargs: Any,
Expand All @@ -1395,6 +1411,7 @@ def from_texts(
azure_search = cls(
azure_search_endpoint,
azure_search_key,
azure_ad_access_token,
index_name,
embedding,
fields=fields,
Expand All @@ -1411,6 +1428,7 @@ async def afrom_texts(
metadatas: Optional[List[dict]] = None,
azure_search_endpoint: str = "",
azure_search_key: str = "",
azure_ad_access_token: Optional[str] = None,
index_name: str = "langchain-index",
fields: Optional[List[SearchField]] = None,
**kwargs: Any,
Expand All @@ -1419,6 +1437,7 @@ async def afrom_texts(
azure_search = cls(
azure_search_endpoint,
azure_search_key,
azure_ad_access_token,
index_name,
embedding,
fields=fields,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# Vector store settings
vector_store_address: str = os.getenv("AZURE_SEARCH_ENDPOINT", "")
vector_store_password: str = os.getenv("AZURE_SEARCH_ADMIN_KEY", "")
access_token: str = os.getenv("AZURE_SEARCH_ACCESS_TOKEN", "")
index_name: str = "embeddings-vector-store-test"


Expand All @@ -25,6 +26,7 @@ def similarity_search_test() -> None:
vector_store: AzureSearch = AzureSearch(
azure_search_endpoint=vector_store_address,
azure_search_key=vector_store_password,
azure_ad_access_token=access_token,
index_name=index_name,
embedding_function=embeddings.embed_query,
)
Expand Down Expand Up @@ -68,6 +70,7 @@ def test_semantic_hybrid_search() -> None:
vector_store: AzureSearch = AzureSearch(
azure_search_endpoint=vector_store_address,
azure_search_key=vector_store_password,
azure_ad_access_token=access_token,
index_name=index_name,
embedding_function=embeddings.embed_query,
semantic_configuration_name="default",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def embed_query(self, text: str) -> List[float]:
DEFAULT_INDEX_NAME = "langchain-index"
DEFAULT_ENDPOINT = "https://my-search-service.search.windows.net"
DEFAULT_KEY = "mykey"
DEFAULT_ACCESS_TOKEN = "myaccesstoken1"
DEFAULT_EMBEDDING_MODEL = FakeEmbeddingsWithDimension()


Expand Down Expand Up @@ -127,6 +128,7 @@ def create_vector_store(
return AzureSearch(
azure_search_endpoint=DEFAULT_ENDPOINT,
azure_search_key=DEFAULT_KEY,
azure_ad_access_token=DEFAULT_ACCESS_TOKEN,
index_name=DEFAULT_INDEX_NAME,
embedding_function=DEFAULT_EMBEDDING_MODEL,
additional_search_client_options=additional_search_client_options,
Expand Down
Loading