2023-10-30 01:21:33 +00:00
|
|
|
"""Test GooseAI"""
|
|
|
|
|
|
|
|
import pytest
|
2023-11-20 21:09:30 +00:00
|
|
|
from langchain_core.pydantic_v1 import SecretStr
|
2023-10-30 01:21:33 +00:00
|
|
|
from pytest import MonkeyPatch
|
|
|
|
|
2023-12-11 21:53:30 +00:00
|
|
|
from langchain_community.llms.gooseai import GooseAI
|
|
|
|
from langchain_community.utils.openai import is_openai_v1
|
2023-11-15 00:50:23 +00:00
|
|
|
|
|
|
|
|
|
|
|
def _openai_v1_installed() -> bool:
|
|
|
|
try:
|
|
|
|
return is_openai_v1()
|
|
|
|
except Exception as _:
|
|
|
|
return False
|
2023-10-30 01:21:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.requires("openai")
|
|
|
|
def test_api_key_is_secret_string() -> None:
|
2024-05-13 18:55:07 +00:00
|
|
|
llm = GooseAI(gooseai_api_key="secret-api-key") # type: ignore[arg-type, call-arg]
|
2023-10-30 01:21:33 +00:00
|
|
|
assert isinstance(llm.gooseai_api_key, SecretStr)
|
|
|
|
assert llm.gooseai_api_key.get_secret_value() == "secret-api-key"
|
|
|
|
|
|
|
|
|
2023-11-15 00:50:23 +00:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
_openai_v1_installed(), reason="GooseAI currently only works with openai<1"
|
|
|
|
)
|
2023-10-30 01:21:33 +00:00
|
|
|
@pytest.mark.requires("openai")
|
|
|
|
def test_api_key_masked_when_passed_via_constructor() -> None:
|
2024-05-13 18:55:07 +00:00
|
|
|
llm = GooseAI(gooseai_api_key="secret-api-key") # type: ignore[arg-type, call-arg]
|
2023-10-30 01:21:33 +00:00
|
|
|
assert str(llm.gooseai_api_key) == "**********"
|
|
|
|
assert "secret-api-key" not in repr(llm.gooseai_api_key)
|
|
|
|
assert "secret-api-key" not in repr(llm)
|
|
|
|
|
|
|
|
|
2023-11-15 00:50:23 +00:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
_openai_v1_installed(), reason="GooseAI currently only works with openai<1"
|
|
|
|
)
|
2023-10-30 01:21:33 +00:00
|
|
|
@pytest.mark.requires("openai")
|
|
|
|
def test_api_key_masked_when_passed_from_env() -> None:
|
|
|
|
with MonkeyPatch.context() as mp:
|
|
|
|
mp.setenv("GOOSEAI_API_KEY", "secret-api-key")
|
2024-05-13 18:55:07 +00:00
|
|
|
llm = GooseAI() # type: ignore[call-arg]
|
2023-10-30 01:21:33 +00:00
|
|
|
assert str(llm.gooseai_api_key) == "**********"
|
|
|
|
assert "secret-api-key" not in repr(llm.gooseai_api_key)
|
|
|
|
assert "secret-api-key" not in repr(llm)
|