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

partners[openai]: add tests for secret_str for keys #20982

Merged
merged 3 commits into from May 1, 2024

Conversation

sepiatone
Copy link
Contributor

@sepiatone sepiatone commented Apr 28, 2024

Description: Add tests to check API keys and Active Directory tokens are masked
Issue: Resolves #12165 for OpenAI and Azure OpenAI models
Dependencies: None

Also resolves #12473 which may be closed.

Additional contributors @alex4321 (#12473) and @onesolpark (#12542)

@efriis efriis added the partner label Apr 28, 2024
Copy link

vercel bot commented Apr 28, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
langchain ⬜️ Ignored (Inspect) Visit Preview Apr 30, 2024 8:35pm

@efriis efriis self-assigned this Apr 28, 2024
@sepiatone sepiatone marked this pull request as ready for review April 28, 2024 09:06
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. 🔌: openai Primarily related to OpenAI integrations 🤖:improvement Medium size change to existing code to handle new use-cases labels Apr 28, 2024
Copy link
Collaborator

@ccurme ccurme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

monkeypatch.setenv("AZURE_OPENAI_API_KEY", "secret-api-key")
monkeypatch.setenv("AZURE_OPENAI_AD_TOKEN", "secret-ad-token")
chat_model = AzureChatOpenAI(
openai_api_key="secret-api-key",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we remove the secrets from the constructor in this case, to enforce that they're read from the env vars?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I was a tad too aggressive with the copy-paste.

# azure openai
def test_azure_openai_api_key_is_secret_string() -> None:
"""Test that the API key is stored as a SecretStr."""
llm = AzureOpenAI(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(nit) Are we able to parametrize these at all? e.g.,

from typing import Type

import pytest


@pytest.mark.parametrize(
    "model_class", [AzureChatOpenAI, AzureOpenAI, AzureOpenAIEmbeddings]
)
def test_api_key_is_secret_string(model_class: Type) -> None:
    model_instance = model_class(
        openai_api_key="secret-api-key",
        azure_endpoint="endpoint",
        azure_ad_token="secret-ad-token",
        api_version="version",
    )
    assert isinstance(model_instance.openai_api_key, SecretStr)
    ...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Thank you - I'd overlooked this simplification.

@dosubot dosubot bot added the lgtm PR looks good. Use to confirm that a PR is ready for merging. label May 1, 2024
@baskaryan baskaryan merged commit 12b1caf into langchain-ai:master May 1, 2024
21 checks passed
@sepiatone sepiatone deleted the openai_ss branch May 1, 2024 11:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:improvement Medium size change to existing code to handle new use-cases lgtm PR looks good. Use to confirm that a PR is ready for merging. 🔌: openai Primarily related to OpenAI integrations partner size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

For New Contributors: Use SecretStr for api_keys
4 participants