-
Notifications
You must be signed in to change notification settings - Fork 16.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Mask API Key for OpenAI based ChatModels (OpenAI,AzureOpenAi,Konko)
- Loading branch information
1 parent
b213850
commit 57d1e94
Showing
6 changed files
with
168 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import pytest | ||
from pytest import MonkeyPatch | ||
|
||
from langchain.chat_models.konko import ChatKonko | ||
from langchain.pydantic_v1 import SecretStr | ||
|
||
|
||
@pytest.mark.requires("konko") | ||
def test_api_key_is_secret_string_and_matches_input() -> None: | ||
llm = ChatKonko( | ||
openai_api_key="secret-openai-api-key", konko_api_key="secret-konko-api-key" | ||
) | ||
assert isinstance(llm.openai_api_key, SecretStr) | ||
assert isinstance(llm.konko_api_key, SecretStr) | ||
assert llm.openai_api_key.get_secret_value() == "secret-openai-api-key" | ||
assert llm.konko_api_key.get_secret_value() == "secret-konko-api-key" | ||
|
||
|
||
@pytest.mark.requires("konko") | ||
def test_api_key_masked_when_passed_via_constructor() -> None: | ||
llm = ChatKonko( | ||
openai_api_key="secret-openai-api-key", konko_api_key="secret-konko-api-key" | ||
) | ||
assert str(llm.openai_api_key) == "**********" | ||
assert str(llm.konko_api_key) == "**********" | ||
assert "secret-openai-api-key" not in repr(llm.openai_api_key) | ||
assert "secret-konko-api-key" not in repr(llm.konko_api_key) | ||
assert "secret-openai-api-key" not in repr(llm) | ||
assert "secret-konko-api-key" not in repr(llm) | ||
|
||
|
||
@pytest.mark.requires("konko") | ||
def test_api_key_masked_when_passed_via_env() -> None: | ||
with MonkeyPatch.context() as mp: | ||
mp.setenv("KONKO_API_KEY", "secret-konko-api-key") | ||
llm = ChatKonko() | ||
assert str(llm.konko_api_key) == "**********" | ||
assert "secret-konko-api-key" not in repr(llm.konko_api_key) | ||
assert "secret-konko-api-key" not in repr(llm) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters