Skip to content
Snippets Groups Projects
Unverified Commit f69f4d17 authored by San Nguyen's avatar San Nguyen Committed by GitHub
Browse files

fix: azure openai api key not picking up env var (#13267)

parent a6132313
Branches
Tags
No related merge requests found
......@@ -46,7 +46,7 @@ class AzureOpenAIEmbedding(OpenAIEmbedding):
# azure specific
azure_endpoint: Optional[str] = None,
azure_deployment: Optional[str] = None,
azure_ad_token_provider: AzureADTokenProvider = None,
azure_ad_token_provider: Optional[AzureADTokenProvider] = None,
deployment_name: Optional[str] = None,
max_retries: int = 10,
reuse_client: bool = True,
......@@ -60,6 +60,8 @@ class AzureOpenAIEmbedding(OpenAIEmbedding):
"azure_endpoint", azure_endpoint, "AZURE_OPENAI_ENDPOINT", ""
)
api_key = get_from_param_or_env("api_key", api_key, "AZURE_OPENAI_API_KEY")
azure_deployment = resolve_from_aliases(
azure_deployment,
deployment_name,
......
......@@ -27,7 +27,7 @@ exclude = ["**/BUILD"]
license = "MIT"
name = "llama-index-embeddings-azure-openai"
readme = "README.md"
version = "0.1.8"
version = "0.1.9"
[tool.poetry.dependencies]
python = ">=3.8.1,<4.0"
......
......@@ -11,7 +11,7 @@ def test_custom_http_client(azure_openai_mock: MagicMock) -> None:
Should get passed on to the implementation from OpenAI.
"""
custom_http_client = httpx.Client()
embedding = AzureOpenAIEmbedding(http_client=custom_http_client)
embedding = AzureOpenAIEmbedding(http_client=custom_http_client, api_key="mock")
embedding._get_client()
azure_openai_mock.assert_called()
kwargs = azure_openai_mock.call_args.kwargs
......
......@@ -93,7 +93,7 @@ class AzureOpenAI(OpenAI):
# azure specific
azure_endpoint: Optional[str] = None,
azure_deployment: Optional[str] = None,
azure_ad_token_provider: AzureADTokenProvider = None,
azure_ad_token_provider: Optional[AzureADTokenProvider] = None,
use_azure_ad: bool = False,
callback_manager: Optional[CallbackManager] = None,
# aliases for engine
......@@ -186,6 +186,16 @@ class AzureOpenAI(OpenAI):
if self.use_azure_ad:
self._azure_ad_token = refresh_openai_azuread_token(self._azure_ad_token)
self.api_key = self._azure_ad_token.token
else:
import os
self.api_key = self.api_key or os.getenv("AZURE_OPENAI_API_KEY")
if self.api_key is None:
raise ValueError(
"You must set an `api_key` parameter. "
"Alternatively, you can set the AZURE_OPENAI_API_KEY env var OR set `use_azure_ad=True`."
)
return {
"api_key": self.api_key,
......
......@@ -29,7 +29,7 @@ exclude = ["**/BUILD"]
license = "MIT"
name = "llama-index-llms-azure-openai"
readme = "README.md"
version = "0.1.6"
version = "0.1.7"
[tool.poetry.dependencies]
python = ">=3.8.1,<4.0"
......
......@@ -40,7 +40,9 @@ def test_custom_http_client(sync_azure_openai_mock: MagicMock) -> None:
mock_instance = sync_azure_openai_mock.return_value
# Valid mocked result required to not run into another error
mock_instance.chat.completions.create.return_value = mock_chat_completion_v1()
azure_openai = AzureOpenAI(engine="foo bar", http_client=custom_http_client)
azure_openai = AzureOpenAI(
engine="foo bar", http_client=custom_http_client, api_key="mock"
)
azure_openai.complete("test prompt")
sync_azure_openai_mock.assert_called()
kwargs = sync_azure_openai_mock.call_args.kwargs
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment