Skip to content

Commit e41cebf

Browse files
authored
Fix Azure OpenAI authentication from UI (#794)
* fix Azure OpenAI authentication from UI * pre-commit
1 parent 21814b2 commit e41cebf

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

packages/jupyter-ai-magics/jupyter_ai_magics/partner_providers/openai.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ class AzureChatOpenAIProvider(BaseProvider, AzureChatOpenAI):
8383
pypi_package_deps = ["langchain_openai"]
8484
# Confusingly, langchain uses both OPENAI_API_KEY and AZURE_OPENAI_API_KEY for azure
8585
# https://github.com/langchain-ai/langchain/blob/f2579096993ae460516a0aae1d3e09f3eb5c1772/libs/partners/openai/langchain_openai/llms/azure.py#L85
86-
auth_strategy = EnvAuthStrategy(name="AZURE_OPENAI_API_KEY")
86+
auth_strategy = EnvAuthStrategy(
87+
name="AZURE_OPENAI_API_KEY", keyword_param="openai_api_key"
88+
)
8789
registry = True
8890

8991
fields = [

packages/jupyter-ai-magics/jupyter_ai_magics/providers.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,13 @@
3333
from langchain.schema.output_parser import StrOutputParser
3434
from langchain.schema.runnable import Runnable
3535
from langchain.utils import get_from_dict_or_env
36-
from langchain_community.chat_models import (
37-
BedrockChat,
38-
ChatAnthropic,
39-
QianfanChatEndpoint,
40-
)
36+
from langchain_community.chat_models import BedrockChat, QianfanChatEndpoint
4137
from langchain_community.llms import (
4238
AI21,
43-
Anthropic,
4439
Bedrock,
4540
Cohere,
4641
GPT4All,
4742
HuggingFaceEndpoint,
48-
OpenAI,
4943
SagemakerEndpoint,
5044
Together,
5145
)
@@ -111,10 +105,23 @@
111105

112106

113107
class EnvAuthStrategy(BaseModel):
114-
"""Require one auth token via an environment variable."""
108+
"""
109+
Describes a provider that uses a single authentication token, which is
110+
passed either as an environment variable or as a keyword argument.
111+
"""
115112

116113
type: Literal["env"] = "env"
114+
117115
name: str
116+
"""The name of the environment variable, e.g. `'ANTHROPIC_API_KEY'`."""
117+
118+
keyword_param: Optional[str]
119+
"""
120+
If unset (default), the authentication token is provided as a keyword
121+
argument with the parameter equal to the environment variable name in
122+
lowercase. If set to some string `k`, the authentication token will be
123+
passed using the keyword parameter `k`.
124+
"""
118125

119126

120127
class MultiEnvAuthStrategy(BaseModel):

packages/jupyter-ai/jupyter_ai/config_manager.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,8 +448,12 @@ def _provider_params(self, key, listing):
448448
_, Provider = get_em_provider(gid, listing)
449449
authn_fields = {}
450450
if Provider.auth_strategy and Provider.auth_strategy.type == "env":
451+
keyword_param = (
452+
Provider.auth_strategy.keyword_param
453+
or Provider.auth_strategy.name.lower()
454+
)
451455
key_name = Provider.auth_strategy.name
452-
authn_fields[key_name.lower()] = config.api_keys[key_name]
456+
authn_fields[keyword_param] = config.api_keys[key_name]
453457

454458
return {
455459
"model_id": lid,

0 commit comments

Comments
 (0)