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

Fix Azure OpenAI authentication from UI #794

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ class AzureChatOpenAIProvider(BaseProvider, AzureChatOpenAI):
pypi_package_deps = ["langchain_openai"]
# Confusingly, langchain uses both OPENAI_API_KEY and AZURE_OPENAI_API_KEY for azure
# https://github.com/langchain-ai/langchain/blob/f2579096993ae460516a0aae1d3e09f3eb5c1772/libs/partners/openai/langchain_openai/llms/azure.py#L85
auth_strategy = EnvAuthStrategy(name="AZURE_OPENAI_API_KEY")
auth_strategy = EnvAuthStrategy(
name="AZURE_OPENAI_API_KEY", keyword_param="openai_api_key"
)
registry = True

fields = [
Expand Down
23 changes: 15 additions & 8 deletions packages/jupyter-ai-magics/jupyter_ai_magics/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,13 @@
from langchain.schema.output_parser import StrOutputParser
from langchain.schema.runnable import Runnable
from langchain.utils import get_from_dict_or_env
from langchain_community.chat_models import (
BedrockChat,
ChatAnthropic,
QianfanChatEndpoint,
)
from langchain_community.chat_models import BedrockChat, QianfanChatEndpoint
from langchain_community.llms import (
AI21,
Anthropic,
Bedrock,
Cohere,
GPT4All,
HuggingFaceEndpoint,
OpenAI,
SagemakerEndpoint,
Together,
)
Expand Down Expand Up @@ -111,10 +105,23 @@


class EnvAuthStrategy(BaseModel):
"""Require one auth token via an environment variable."""
"""
Describes a provider that uses a single authentication token, which is
passed either as an environment variable or as a keyword argument.
"""

type: Literal["env"] = "env"

name: str
"""The name of the environment variable, e.g. `'ANTHROPIC_API_KEY'`."""

keyword_param: Optional[str]
"""
If unset (default), the authentication token is provided as a keyword
argument with the parameter equal to the environment variable name in
lowercase. If set to some string `k`, the authentication token will be
passed using the keyword parameter `k`.
"""


class MultiEnvAuthStrategy(BaseModel):
Expand Down
6 changes: 5 additions & 1 deletion packages/jupyter-ai/jupyter_ai/config_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,8 +448,12 @@ def _provider_params(self, key, listing):
_, Provider = get_em_provider(gid, listing)
authn_fields = {}
if Provider.auth_strategy and Provider.auth_strategy.type == "env":
keyword_param = (
Provider.auth_strategy.keyword_param
or Provider.auth_strategy.name.lower()
)
key_name = Provider.auth_strategy.name
authn_fields[key_name.lower()] = config.api_keys[key_name]
authn_fields[keyword_param] = config.api_keys[key_name]

return {
"model_id": lid,
Expand Down
Loading