Skip to content

Commit 8d8b5c4

Browse files
committed
run make fix
1 parent 9e5e5dd commit 8d8b5c4

File tree

2 files changed

+24
-26
lines changed

2 files changed

+24
-26
lines changed

template_langgraph/llms/azure_openais.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from functools import lru_cache
21
import threading
3-
from typing import Optional
2+
from functools import lru_cache
43

54
from azure.identity import DefaultAzureCredential
65
from langchain_openai import AzureChatOpenAI, AzureOpenAIEmbeddings
@@ -37,38 +36,40 @@ class AzureOpenAiWrapper:
3736
_credentials: dict = {}
3837
_tokens: dict = {}
3938
_token_lock = threading.Lock()
40-
39+
4140
def __init__(self, settings: Settings = None):
4241
if settings is None:
4342
settings = get_azure_openai_settings()
44-
43+
4544
self.settings = settings
46-
self._chat_model: Optional[AzureChatOpenAI] = None
47-
self._reasoning_model: Optional[AzureChatOpenAI] = None
48-
self._embedding_model: Optional[AzureOpenAIEmbeddings] = None
45+
self._chat_model: AzureChatOpenAI | None = None
46+
self._reasoning_model: AzureChatOpenAI | None = None
47+
self._embedding_model: AzureOpenAIEmbeddings | None = None
4948

5049
def _get_auth_key(self) -> str:
5150
"""Generate a key for authentication caching based on settings."""
5251
return f"{self.settings.azure_openai_endpoint}_{self.settings.azure_openai_use_microsoft_entra_id}"
5352

54-
def _get_auth_token(self) -> Optional[str]:
53+
def _get_auth_token(self) -> str | None:
5554
"""Get authentication token with lazy initialization and caching."""
5655
if self.settings.azure_openai_use_microsoft_entra_id.lower() != "true":
5756
return None
58-
57+
5958
auth_key = self._get_auth_key()
60-
59+
6160
with self._token_lock:
6261
if auth_key not in self._credentials:
6362
logger.info("Initializing Microsoft Entra ID authentication")
6463
self._credentials[auth_key] = DefaultAzureCredential()
65-
64+
6665
if auth_key not in self._tokens:
6766
logger.info("Getting authentication token")
68-
self._tokens[auth_key] = self._credentials[auth_key].get_token("https://cognitiveservices.azure.com/.default").token
69-
67+
self._tokens[auth_key] = (
68+
self._credentials[auth_key].get_token("https://cognitiveservices.azure.com/.default").token
69+
)
70+
7071
return self._tokens[auth_key]
71-
72+
7273
@property
7374
def chat_model(self) -> AzureChatOpenAI:
7475
"""Lazily initialize and return chat model."""
@@ -92,7 +93,7 @@ def chat_model(self) -> AzureChatOpenAI:
9293
streaming=True,
9394
)
9495
return self._chat_model
95-
96+
9697
@property
9798
def reasoning_model(self) -> AzureChatOpenAI:
9899
"""Lazily initialize and return reasoning model."""
@@ -115,7 +116,7 @@ def reasoning_model(self) -> AzureChatOpenAI:
115116
streaming=True,
116117
)
117118
return self._reasoning_model
118-
119+
119120
@property
120121
def embedding_model(self) -> AzureOpenAIEmbeddings:
121122
"""Lazily initialize and return embedding model."""

tests/test_azure_openais.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import logging
22
import threading
3-
import time
43
from unittest.mock import Mock, patch
5-
import pytest
64

75
from template_langgraph.llms.azure_openais import AzureOpenAiWrapper, Settings
86

@@ -26,8 +24,7 @@ def test_lazy_initialization_api_key(self, caplog):
2624
with caplog.at_level(logging.INFO):
2725
# Creating instances should not trigger authentication
2826
wrapper1 = AzureOpenAiWrapper(settings)
29-
wrapper2 = AzureOpenAiWrapper(settings)
30-
27+
3128
# No authentication logs yet
3229
assert "Using API key for authentication" not in caplog.text
3330

@@ -49,7 +46,7 @@ def test_lazy_initialization_api_key(self, caplog):
4946
# Should still be only one authentication log per model type
5047
assert caplog.text.count("Using API key for authentication") >= 1
5148

52-
@patch('template_langgraph.llms.azure_openais.DefaultAzureCredential')
49+
@patch("template_langgraph.llms.azure_openais.DefaultAzureCredential")
5350
def test_singleton_credential_entra_id(self, mock_credential_class, caplog):
5451
"""Test that Microsoft Entra ID credentials are reused across instances."""
5552
# Mock the credential and token
@@ -85,7 +82,7 @@ def test_singleton_credential_entra_id(self, mock_credential_class, caplog):
8582
assert caplog.text.count("Initializing Microsoft Entra ID authentication") == 1
8683
assert caplog.text.count("Getting authentication token") == 1
8784

88-
@patch('template_langgraph.llms.azure_openais.DefaultAzureCredential')
85+
@patch("template_langgraph.llms.azure_openais.DefaultAzureCredential")
8986
def test_thread_safety(self, mock_credential_class):
9087
"""Test that authentication is thread-safe."""
9188
mock_credential = Mock()
@@ -174,10 +171,10 @@ def test_create_embedding_method_compatibility(self):
174171
pass # Expected due to dummy credentials
175172

176173
# Verify the method exists and is callable
177-
assert hasattr(wrapper, 'create_embedding')
178-
assert callable(getattr(wrapper, 'create_embedding'))
174+
assert hasattr(wrapper, "create_embedding")
175+
assert callable(getattr(wrapper, "create_embedding"))
179176

180-
@patch('template_langgraph.llms.azure_openais.DefaultAzureCredential')
177+
@patch("template_langgraph.llms.azure_openais.DefaultAzureCredential")
181178
def test_mixed_authentication_methods(self, mock_credential_class, caplog):
182179
"""Test using both authentication methods in different instances."""
183180
mock_credential = Mock()
@@ -212,4 +209,4 @@ def test_mixed_authentication_methods(self, mock_credential_class, caplog):
212209

213210
# Should see both authentication methods being used
214211
assert "Using API key for authentication" in caplog.text
215-
assert "Initializing Microsoft Entra ID authentication" in caplog.text
212+
assert "Initializing Microsoft Entra ID authentication" in caplog.text

0 commit comments

Comments
 (0)