feature: add Azure Identity for Azure OpenAI and Azure AI Search authentication (#3262)

This commit is contained in:
David A. Torres
2025-08-21 02:00:27 +05:30
committed by GitHub
parent e4c5582808
commit 4487785cec
12 changed files with 686 additions and 15 deletions
@@ -50,3 +50,117 @@ def test_embed_text_with_default_headers(default_headers, expected_header):
assert embedder.client.api_key == "test"
assert embedder.client._api_version == "test_version"
assert embedder.client.default_headers.get("Test") == expected_header
@pytest.fixture
def base_embedder_config():
class DummyAzureKwargs:
api_key = None
azure_deployment = None
azure_endpoint = None
api_version = None
default_headers = None
class DummyConfig(BaseEmbedderConfig):
azure_kwargs = DummyAzureKwargs()
http_client = None
model = "test-model"
return DummyConfig()
def test_init_with_api_key(monkeypatch, base_embedder_config):
base_embedder_config.azure_kwargs.api_key = "test-key"
base_embedder_config.azure_kwargs.azure_deployment = "test-deployment"
base_embedder_config.azure_kwargs.azure_endpoint = "https://test.endpoint"
base_embedder_config.azure_kwargs.api_version = "2024-01-01"
base_embedder_config.azure_kwargs.default_headers = {"X-Test": "Header"}
with (
patch("mem0.embeddings.azure_openai.AzureOpenAI") as mock_azure_openai,
patch("mem0.embeddings.azure_openai.DefaultAzureCredential") as mock_cred,
patch("mem0.embeddings.azure_openai.get_bearer_token_provider") as mock_token_provider,
):
AzureOpenAIEmbedding(base_embedder_config)
mock_azure_openai.assert_called_once_with(
azure_deployment="test-deployment",
azure_endpoint="https://test.endpoint",
azure_ad_token_provider=None,
api_version="2024-01-01",
api_key="test-key",
http_client=None,
default_headers={"X-Test": "Header"},
)
mock_cred.assert_not_called()
mock_token_provider.assert_not_called()
def test_init_with_env_vars(monkeypatch, base_embedder_config):
monkeypatch.setenv("EMBEDDING_AZURE_OPENAI_API_KEY", "env-key")
monkeypatch.setenv("EMBEDDING_AZURE_DEPLOYMENT", "env-deployment")
monkeypatch.setenv("EMBEDDING_AZURE_ENDPOINT", "https://env.endpoint")
monkeypatch.setenv("EMBEDDING_AZURE_API_VERSION", "2024-02-02")
with patch("mem0.embeddings.azure_openai.AzureOpenAI") as mock_azure_openai:
AzureOpenAIEmbedding(base_embedder_config)
mock_azure_openai.assert_called_once_with(
azure_deployment="env-deployment",
azure_endpoint="https://env.endpoint",
azure_ad_token_provider=None,
api_version="2024-02-02",
api_key="env-key",
http_client=None,
default_headers=None,
)
def test_init_with_default_azure_credential(monkeypatch, base_embedder_config):
base_embedder_config.azure_kwargs.api_key = ""
with (
patch("mem0.embeddings.azure_openai.DefaultAzureCredential") as mock_cred,
patch("mem0.embeddings.azure_openai.get_bearer_token_provider") as mock_token_provider,
patch("mem0.embeddings.azure_openai.AzureOpenAI") as mock_azure_openai,
):
mock_cred_instance = Mock()
mock_cred.return_value = mock_cred_instance
mock_token_provider_instance = Mock()
mock_token_provider.return_value = mock_token_provider_instance
AzureOpenAIEmbedding(base_embedder_config)
mock_cred.assert_called_once()
mock_token_provider.assert_called_once_with(mock_cred_instance, "https://cognitiveservices.azure.com/.default")
mock_azure_openai.assert_called_once_with(
azure_deployment=None,
azure_endpoint=None,
azure_ad_token_provider=mock_token_provider_instance,
api_version=None,
api_key=None,
http_client=None,
default_headers=None,
)
def test_init_with_placeholder_api_key(monkeypatch, base_embedder_config):
base_embedder_config.azure_kwargs.api_key = "your-api-key"
with (
patch("mem0.embeddings.azure_openai.DefaultAzureCredential") as mock_cred,
patch("mem0.embeddings.azure_openai.get_bearer_token_provider") as mock_token_provider,
patch("mem0.embeddings.azure_openai.AzureOpenAI") as mock_azure_openai,
):
mock_cred_instance = Mock()
mock_cred.return_value = mock_cred_instance
mock_token_provider_instance = Mock()
mock_token_provider.return_value = mock_token_provider_instance
AzureOpenAIEmbedding(base_embedder_config)
mock_cred.assert_called_once()
mock_token_provider.assert_called_once_with(mock_cred_instance, "https://cognitiveservices.azure.com/.default")
mock_azure_openai.assert_called_once_with(
azure_deployment=None,
azure_endpoint=None,
azure_ad_token_provider=mock_token_provider_instance,
api_version=None,
api_key=None,
http_client=None,
default_headers=None,
)
+128
View File
@@ -124,7 +124,135 @@ def test_generate_with_http_proxies(default_headers):
http_client=mock_http_client_instance,
azure_deployment=None,
azure_endpoint=None,
azure_ad_token_provider=None,
api_version=None,
default_headers=default_headers,
)
mock_http_client.assert_called_once_with(proxies="http://testproxy.mem0.net:8000")
def test_init_with_api_key(monkeypatch):
# Patch environment variables to None to force config usage
monkeypatch.delenv("LLM_AZURE_OPENAI_API_KEY", raising=False)
monkeypatch.delenv("LLM_AZURE_DEPLOYMENT", raising=False)
monkeypatch.delenv("LLM_AZURE_ENDPOINT", raising=False)
monkeypatch.delenv("LLM_AZURE_API_VERSION", raising=False)
config = AzureOpenAIConfig(
model=MODEL,
temperature=TEMPERATURE,
max_tokens=MAX_TOKENS,
top_p=TOP_P,
)
# Set Azure kwargs directly
config.azure_kwargs.api_key = "test-key"
config.azure_kwargs.azure_deployment = "test-deployment"
config.azure_kwargs.azure_endpoint = "https://test-endpoint"
config.azure_kwargs.api_version = "2024-01-01"
config.azure_kwargs.default_headers = {"x-test": "header"}
config.http_client = None
with patch("mem0.llms.azure_openai.AzureOpenAI") as mock_azure_openai:
llm = AzureOpenAILLM(config)
mock_azure_openai.assert_called_once_with(
azure_deployment="test-deployment",
azure_endpoint="https://test-endpoint",
azure_ad_token_provider=None,
api_version="2024-01-01",
api_key="test-key",
http_client=None,
default_headers={"x-test": "header"},
)
assert llm.config.model == MODEL
def test_init_with_env_vars(monkeypatch):
monkeypatch.setenv("LLM_AZURE_OPENAI_API_KEY", "env-key")
monkeypatch.setenv("LLM_AZURE_DEPLOYMENT", "env-deployment")
monkeypatch.setenv("LLM_AZURE_ENDPOINT", "https://env-endpoint")
monkeypatch.setenv("LLM_AZURE_API_VERSION", "2024-02-02")
config = AzureOpenAIConfig(model=None)
config.azure_kwargs.api_key = None
config.azure_kwargs.azure_deployment = None
config.azure_kwargs.azure_endpoint = None
config.azure_kwargs.api_version = None
config.azure_kwargs.default_headers = None
config.http_client = None
with patch("mem0.llms.azure_openai.AzureOpenAI") as mock_azure_openai:
llm = AzureOpenAILLM(config)
mock_azure_openai.assert_called_once_with(
azure_deployment="env-deployment",
azure_endpoint="https://env-endpoint",
azure_ad_token_provider=None,
api_version="2024-02-02",
api_key="env-key",
http_client=None,
default_headers=None,
)
# Should default to "gpt-4o" if model is None
assert llm.config.model == "gpt-4o"
def test_init_with_default_azure_credential(monkeypatch):
# No API key in config or env, triggers DefaultAzureCredential
monkeypatch.delenv("LLM_AZURE_OPENAI_API_KEY", raising=False)
config = AzureOpenAIConfig(model=MODEL)
config.azure_kwargs.api_key = None
config.azure_kwargs.azure_deployment = "dep"
config.azure_kwargs.azure_endpoint = "https://endpoint"
config.azure_kwargs.api_version = "2024-03-03"
config.azure_kwargs.default_headers = None
config.http_client = None
with (
patch("mem0.llms.azure_openai.DefaultAzureCredential") as mock_cred,
patch("mem0.llms.azure_openai.get_bearer_token_provider") as mock_token_provider,
patch("mem0.llms.azure_openai.AzureOpenAI") as mock_azure_openai,
):
mock_cred_instance = mock_cred.return_value
mock_token_provider.return_value = "token-provider"
AzureOpenAILLM(config)
mock_cred.assert_called_once()
mock_token_provider.assert_called_once_with(mock_cred_instance, "https://cognitiveservices.azure.com/.default")
mock_azure_openai.assert_called_once_with(
azure_deployment="dep",
azure_endpoint="https://endpoint",
azure_ad_token_provider="token-provider",
api_version="2024-03-03",
api_key=None,
http_client=None,
default_headers=None,
)
def test_init_with_placeholder_api_key(monkeypatch):
# Placeholder API key should trigger DefaultAzureCredential
config = AzureOpenAIConfig(model=MODEL)
config.azure_kwargs.api_key = "your-api-key"
config.azure_kwargs.azure_deployment = "dep"
config.azure_kwargs.azure_endpoint = "https://endpoint"
config.azure_kwargs.api_version = "2024-04-04"
config.azure_kwargs.default_headers = None
config.http_client = None
with (
patch("mem0.llms.azure_openai.DefaultAzureCredential") as mock_cred,
patch("mem0.llms.azure_openai.get_bearer_token_provider") as mock_token_provider,
patch("mem0.llms.azure_openai.AzureOpenAI") as mock_azure_openai,
):
mock_cred_instance = mock_cred.return_value
mock_token_provider.return_value = "token-provider"
AzureOpenAILLM(config)
mock_cred.assert_called_once()
mock_token_provider.assert_called_once_with(mock_cred_instance, "https://cognitiveservices.azure.com/.default")
mock_azure_openai.assert_called_once_with(
azure_deployment="dep",
azure_endpoint="https://endpoint",
azure_ad_token_provider="token-provider",
api_version="2024-04-04",
api_key=None,
http_client=None,
default_headers=None,
)
+100
View File
@@ -0,0 +1,100 @@
from unittest import mock
from mem0.llms.azure_openai_structured import SCOPE, AzureOpenAIStructuredLLM
class DummyAzureKwargs:
def __init__(
self,
api_key=None,
azure_deployment="test-deployment",
azure_endpoint="https://test-endpoint.openai.azure.com",
api_version="2024-06-01-preview",
default_headers=None,
):
self.api_key = api_key
self.azure_deployment = azure_deployment
self.azure_endpoint = azure_endpoint
self.api_version = api_version
self.default_headers = default_headers
class DummyConfig:
def __init__(
self,
model=None,
azure_kwargs=None,
temperature=0.7,
max_tokens=256,
top_p=1.0,
http_client=None,
):
self.model = model
self.azure_kwargs = azure_kwargs or DummyAzureKwargs()
self.temperature = temperature
self.max_tokens = max_tokens
self.top_p = top_p
self.http_client = http_client
@mock.patch("mem0.llms.azure_openai_structured.AzureOpenAI")
def test_init_with_api_key(mock_azure_openai):
config = DummyConfig(model="test-model", azure_kwargs=DummyAzureKwargs(api_key="real-key"))
llm = AzureOpenAIStructuredLLM(config)
assert llm.config.model == "test-model"
mock_azure_openai.assert_called_once()
args, kwargs = mock_azure_openai.call_args
assert kwargs["api_key"] == "real-key"
assert kwargs["azure_ad_token_provider"] is None
@mock.patch("mem0.llms.azure_openai_structured.AzureOpenAI")
@mock.patch("mem0.llms.azure_openai_structured.get_bearer_token_provider")
@mock.patch("mem0.llms.azure_openai_structured.DefaultAzureCredential")
def test_init_with_default_credential(mock_credential, mock_token_provider, mock_azure_openai):
config = DummyConfig(model=None, azure_kwargs=DummyAzureKwargs(api_key=None))
mock_token_provider.return_value = "token-provider"
llm = AzureOpenAIStructuredLLM(config)
# Should set default model if not provided
assert llm.config.model == "gpt-4o-2024-08-06"
mock_credential.assert_called_once()
mock_token_provider.assert_called_once_with(mock_credential.return_value, SCOPE)
mock_azure_openai.assert_called_once()
args, kwargs = mock_azure_openai.call_args
assert kwargs["api_key"] is None
assert kwargs["azure_ad_token_provider"] == "token-provider"
def test_init_with_env_vars(monkeypatch, mocker):
mock_azure_openai = mocker.patch("mem0.llms.azure_openai_structured.AzureOpenAI")
monkeypatch.setenv("LLM_AZURE_DEPLOYMENT", "test-deployment")
monkeypatch.setenv("LLM_AZURE_ENDPOINT", "https://test-endpoint.openai.azure.com")
monkeypatch.setenv("LLM_AZURE_API_VERSION", "2024-06-01-preview")
config = DummyConfig(model="test-model", azure_kwargs=DummyAzureKwargs(api_key=None))
AzureOpenAIStructuredLLM(config)
mock_azure_openai.assert_called_once()
args, kwargs = mock_azure_openai.call_args
assert kwargs["api_key"] is None
assert kwargs["azure_deployment"] == "test-deployment"
assert kwargs["azure_endpoint"] == "https://test-endpoint.openai.azure.com"
assert kwargs["api_version"] == "2024-06-01-preview"
@mock.patch("mem0.llms.azure_openai_structured.AzureOpenAI")
def test_init_with_placeholder_api_key_uses_default_credential(
mock_azure_openai,
):
with (
mock.patch("mem0.llms.azure_openai_structured.DefaultAzureCredential") as mock_credential,
mock.patch("mem0.llms.azure_openai_structured.get_bearer_token_provider") as mock_token_provider,
):
config = DummyConfig(model=None, azure_kwargs=DummyAzureKwargs(api_key="your-api-key"))
mock_token_provider.return_value = "token-provider"
llm = AzureOpenAIStructuredLLM(config)
assert llm.config.model == "gpt-4o-2024-08-06"
mock_credential.assert_called_once()
mock_token_provider.assert_called_once_with(mock_credential.return_value, SCOPE)
mock_azure_openai.assert_called_once()
args, kwargs = mock_azure_openai.call_args
assert kwargs["api_key"] is None
assert kwargs["azure_ad_token_provider"] == "token-provider"
+111
View File
@@ -553,3 +553,114 @@ def test_search_basic(azure_ai_search_instance):
assert results[0].id == "doc1"
assert results[0].score == 0.95
assert results[0].payload == {"content": "Test content"}
def test_init_with_valid_api_key(mock_clients):
"""Test __init__ with a valid API key and all required parameters."""
mock_search_client, mock_index_client, mock_azure_key_credential = mock_clients
instance = AzureAISearch(
service_name="test-service",
collection_name="test-index",
api_key="test-api-key",
embedding_model_dims=128,
compression_type="scalar",
use_float16=True,
hybrid_search=True,
vector_filter_mode="preFilter",
)
# Check attributes
assert instance.service_name == "test-service"
assert instance.api_key == "test-api-key"
assert instance.index_name == "test-index"
assert instance.collection_name == "test-index"
assert instance.embedding_model_dims == 128
assert instance.compression_type == "scalar"
assert instance.use_float16 is True
assert instance.hybrid_search is True
assert instance.vector_filter_mode == "preFilter"
# Check that AzureKeyCredential was used
mock_azure_key_credential.assert_called_with("test-api-key")
# Check that user agent was set
mock_search_client._client._config.user_agent_policy.add_user_agent.assert_called_with("mem0")
mock_index_client._client._config.user_agent_policy.add_user_agent.assert_called_with("mem0")
# Check that create_col was called if collection does not exist
mock_index_client.create_or_update_index.assert_called_once()
def test_init_with_default_api_key_triggers_default_credential(monkeypatch, mock_clients):
"""Test __init__ uses DefaultAzureCredential if api_key is None or placeholder."""
mock_search_client, mock_index_client, mock_azure_key_credential = mock_clients
# Patch DefaultAzureCredential to a mock so we can check if it's called
with patch("mem0.vector_stores.azure_ai_search.DefaultAzureCredential") as mock_default_cred:
# Test with api_key=None
AzureAISearch(
service_name="test-service",
collection_name="test-index",
api_key=None,
embedding_model_dims=64,
)
mock_default_cred.assert_called_once()
# Test with api_key=""
AzureAISearch(
service_name="test-service",
collection_name="test-index",
api_key="",
embedding_model_dims=64,
)
assert mock_default_cred.call_count == 2
# Test with api_key="your-api-key"
AzureAISearch(
service_name="test-service",
collection_name="test-index",
api_key="your-api-key",
embedding_model_dims=64,
)
assert mock_default_cred.call_count == 3
def test_init_sets_compression_type_to_none_if_unspecified(mock_clients):
"""Test __init__ sets compression_type to 'none' if not specified."""
mock_search_client, mock_index_client, _ = mock_clients
instance = AzureAISearch(
service_name="test-service",
collection_name="test-index",
api_key="test-api-key",
embedding_model_dims=32,
)
assert instance.compression_type == "none"
def test_init_does_not_create_col_if_collection_exists(mock_clients):
"""Test __init__ does not call create_col if collection already exists."""
mock_search_client, mock_index_client, _ = mock_clients
# Simulate collection already exists
mock_index_client.list_index_names.return_value = ["test-index"]
AzureAISearch(
service_name="test-service",
collection_name="test-index",
api_key="test-api-key",
embedding_model_dims=16,
)
# create_or_update_index should not be called since collection exists
mock_index_client.create_or_update_index.assert_not_called()
def test_init_calls_create_col_if_collection_missing(mock_clients):
"""Test __init__ calls create_col if collection does not exist."""
mock_search_client, mock_index_client, _ = mock_clients
# Simulate collection does not exist
mock_index_client.list_index_names.return_value = []
AzureAISearch(
service_name="test-service",
collection_name="missing-index",
api_key="test-api-key",
embedding_model_dims=16,
)
mock_index_client.create_or_update_index.assert_called_once()