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

Mask API key for Aleph Alpha LLM #12377

Merged
merged 5 commits into from
Oct 27, 2023
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
17 changes: 12 additions & 5 deletions libs/langchain/langchain/llms/aleph_alpha.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
from typing import Any, Dict, List, Optional, Sequence
from typing import Any, Dict, List, Optional, Sequence, Union

from langchain.callbacks.manager import CallbackManagerForLLMRun
from langchain.llms.base import LLM
from langchain.llms.utils import enforce_stop_tokens
from langchain.pydantic_v1 import Extra, root_validator
from langchain.pydantic_v1 import Extra, SecretStr, root_validator
from langchain.utils import get_from_dict_or_env


def _to_secret(value: Union[SecretStr, str]) -> SecretStr:
"""Convert a string to a SecretStr if needed."""
if isinstance(value, SecretStr):
return value
return SecretStr(value)


class AlephAlpha(LLM):
"""Aleph Alpha large language models.

Expand Down Expand Up @@ -169,14 +176,14 @@ class Config:
@root_validator()
def validate_environment(cls, values: Dict) -> Dict:
"""Validate that api key and python package exists in environment."""
aleph_alpha_api_key = get_from_dict_or_env(
values, "aleph_alpha_api_key", "ALEPH_ALPHA_API_KEY"
values["aleph_alpha_api_key"] = _to_secret(
get_from_dict_or_env(values, "aleph_alpha_api_key", "ALEPH_ALPHA_API_KEY")
)
try:
from aleph_alpha_client import Client

values["client"] = Client(
token=aleph_alpha_api_key,
token=values["aleph_alpha_api_key"].get_secret_value(),
host=values["host"],
hosting=values["hosting"],
request_timeout_seconds=values["request_timeout_seconds"],
Expand Down
28 changes: 25 additions & 3 deletions libs/langchain/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions libs/langchain/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ cli = [
# Please use new-line on formatting to make it easier to add new packages without
# merge-conflicts
extended_testing = [
"aleph-alpha-client",
"amazon-textract-caller",
"aiosqlite",
"assemblyai",
Expand Down
36 changes: 36 additions & 0 deletions libs/langchain/tests/unit_tests/llms/test_aleph_alpha.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
"""Test Aleph Alpha specific stuff."""

import pytest
from pytest import CaptureFixture, MonkeyPatch
Copy link
Collaborator

Choose a reason for hiding this comment

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

neat! didn't know about CaptureFixture!


from langchain.llms.aleph_alpha import AlephAlpha
from langchain.pydantic_v1 import SecretStr


Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you skip these tests if aleph-alpha is not installed? Most langchain dependencies are optional.

Two options:

  1. Add the dependency to extended testing extra in pyproject.toml , and then use the langchain custom @pytest.mark.requires marker. This marker will skip it in regular unit tests, but run the tests on CI in extended unit tests where this dependency will be installed
  2. Use pytest skip to skip running the entire module if the alpeh alpha package is missing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. I'll implement the first option later today.

@pytest.mark.requires("aleph_alpha_client")
def test_api_key_is_secret_string() -> None:
llm = AlephAlpha(aleph_alpha_api_key="secret-api-key")
assert isinstance(llm.aleph_alpha_api_key, SecretStr)


@pytest.mark.requires("aleph_alpha_client")
def test_api_key_masked_when_passed_via_constructor(
capsys: CaptureFixture,
) -> None:
llm = AlephAlpha(aleph_alpha_api_key="secret-api-key")
print(llm.aleph_alpha_api_key, end="")
captured = capsys.readouterr()

assert captured.out == "**********"


@pytest.mark.requires("aleph_alpha_client")
def test_api_key_masked_when_passed_from_env(
monkeypatch: MonkeyPatch, capsys: CaptureFixture
) -> None:
monkeypatch.setenv("ALEPH_ALPHA_API_KEY", "secret-api-key")
llm = AlephAlpha()
print(llm.aleph_alpha_api_key, end="")
captured = capsys.readouterr()

assert captured.out == "**********"
Loading