mirror of https://github.com/hwchase17/langchain
- **Description:** `AzureMLChatOnlineEndpoint` object from langchain/chat_models/azureml_endpoint.py safe to print without having any secrets included in raw format in the string representation. - **Issue:** #12165, - **Tag maintainer:** @eyurtsev --------- Co-authored-by: Faysal Bougamale <faysal.bougamale@horiba.com> Co-authored-by: Bagatur <baskaryan@gmail.com>pull/13222/head
parent
52f34de9b7
commit
d266b3ea4a
@ -0,0 +1,65 @@
|
||||
"""Test AzureML chat endpoint."""
|
||||
|
||||
import os
|
||||
|
||||
import pytest
|
||||
from pytest import CaptureFixture, FixtureRequest
|
||||
|
||||
from langchain.chat_models.azureml_endpoint import AzureMLChatOnlineEndpoint
|
||||
from langchain.pydantic_v1 import SecretStr
|
||||
|
||||
|
||||
@pytest.fixture(scope="class")
|
||||
def api_passed_via_environment_fixture() -> AzureMLChatOnlineEndpoint:
|
||||
"""Fixture to create an AzureMLChatOnlineEndpoint instance
|
||||
with API key passed from environment"""
|
||||
os.environ["AZUREML_ENDPOINT_API_KEY"] = "my-api-key"
|
||||
azure_chat = AzureMLChatOnlineEndpoint(
|
||||
endpoint_url="https://<your-endpoint>.<your_region>.inference.ml.azure.com/score"
|
||||
)
|
||||
del os.environ["AZUREML_ENDPOINT_API_KEY"]
|
||||
return azure_chat
|
||||
|
||||
|
||||
@pytest.fixture(scope="class")
|
||||
def api_passed_via_constructor_fixture() -> AzureMLChatOnlineEndpoint:
|
||||
"""Fixture to create an AzureMLChatOnlineEndpoint instance
|
||||
with API key passed from constructor"""
|
||||
azure_chat = AzureMLChatOnlineEndpoint(
|
||||
endpoint_url="https://<your-endpoint>.<your_region>.inference.ml.azure.com/score",
|
||||
endpoint_api_key="my-api-key",
|
||||
)
|
||||
return azure_chat
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"fixture_name",
|
||||
["api_passed_via_constructor_fixture", "api_passed_via_environment_fixture"],
|
||||
)
|
||||
class TestAzureMLChatOnlineEndpoint:
|
||||
def test_api_key_is_secret_string(
|
||||
self, fixture_name: str, request: FixtureRequest
|
||||
) -> None:
|
||||
"""Test that the API key is a SecretStr instance"""
|
||||
azure_chat = request.getfixturevalue(fixture_name)
|
||||
assert isinstance(azure_chat.endpoint_api_key, SecretStr)
|
||||
|
||||
def test_api_key_masked(
|
||||
self, fixture_name: str, request: FixtureRequest, capsys: CaptureFixture
|
||||
) -> None:
|
||||
"""Test that the API key is masked"""
|
||||
azure_chat = request.getfixturevalue(fixture_name)
|
||||
print(azure_chat.endpoint_api_key, end="")
|
||||
captured = capsys.readouterr()
|
||||
assert (
|
||||
(str(azure_chat.endpoint_api_key) == "**********")
|
||||
and (repr(azure_chat.endpoint_api_key) == "SecretStr('**********')")
|
||||
and (captured.out == "**********")
|
||||
)
|
||||
|
||||
def test_api_key_is_readable(
|
||||
self, fixture_name: str, request: FixtureRequest
|
||||
) -> None:
|
||||
"""Test that the real secret value of the API key can be read"""
|
||||
azure_chat = request.getfixturevalue(fixture_name)
|
||||
assert azure_chat.endpoint_api_key.get_secret_value() == "my-api-key"
|
Loading…
Reference in New Issue