|
|
|
@ -9,6 +9,7 @@ from langchain_core.messages import AIMessage, HumanMessage, SystemMessage, Tool
|
|
|
|
|
from langchain_core.outputs import ChatGeneration, ChatResult
|
|
|
|
|
from langchain_core.pydantic_v1 import BaseModel, Field, SecretStr
|
|
|
|
|
from langchain_core.tools import BaseTool
|
|
|
|
|
from pytest import CaptureFixture, MonkeyPatch
|
|
|
|
|
|
|
|
|
|
from langchain_anthropic import ChatAnthropic
|
|
|
|
|
from langchain_anthropic.chat_models import (
|
|
|
|
@ -419,3 +420,53 @@ def test__format_messages_with_list_content_and_tool_calls() -> None:
|
|
|
|
|
)
|
|
|
|
|
actual = _format_messages(messages)
|
|
|
|
|
assert expected == actual
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_anthropic_api_key_is_secret_string() -> None:
|
|
|
|
|
"""Test that the API key is stored as a SecretStr."""
|
|
|
|
|
chat_model = ChatAnthropic(
|
|
|
|
|
model="claude-3-opus-20240229",
|
|
|
|
|
anthropic_api_key="secret-api-key",
|
|
|
|
|
)
|
|
|
|
|
assert isinstance(chat_model.anthropic_api_key, SecretStr)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_anthropic_api_key_masked_when_passed_from_env(
|
|
|
|
|
monkeypatch: MonkeyPatch, capsys: CaptureFixture
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Test that the API key is masked when passed from an environment variable."""
|
|
|
|
|
monkeypatch.setenv("ANTHROPIC_API_KEY ", "secret-api-key")
|
|
|
|
|
chat_model = ChatAnthropic(
|
|
|
|
|
model="claude-3-opus-20240229",
|
|
|
|
|
anthropic_api_key="secret-api-key",
|
|
|
|
|
)
|
|
|
|
|
print(chat_model.anthropic_api_key, end="") # noqa: T201
|
|
|
|
|
captured = capsys.readouterr()
|
|
|
|
|
|
|
|
|
|
assert captured.out == "**********"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_anthropic_api_key_masked_when_passed_via_constructor(
|
|
|
|
|
capsys: CaptureFixture,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""Test that the API key is masked when passed via the constructor."""
|
|
|
|
|
chat_model = ChatAnthropic(
|
|
|
|
|
model="claude-3-opus-20240229",
|
|
|
|
|
anthropic_api_key="secret-api-key",
|
|
|
|
|
)
|
|
|
|
|
print(chat_model.anthropic_api_key, end="") # noqa: T201
|
|
|
|
|
captured = capsys.readouterr()
|
|
|
|
|
|
|
|
|
|
assert captured.out == "**********"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def test_anthropic_uses_actual_secret_value_from_secretstr() -> None:
|
|
|
|
|
"""Test that the actual secret value is correctly retrieved."""
|
|
|
|
|
chat_model = ChatAnthropic(
|
|
|
|
|
model="claude-3-opus-20240229",
|
|
|
|
|
anthropic_api_key="secret-api-key",
|
|
|
|
|
)
|
|
|
|
|
assert (
|
|
|
|
|
cast(SecretStr, chat_model.anthropic_api_key).get_secret_value()
|
|
|
|
|
== "secret-api-key"
|
|
|
|
|
)
|
|
|
|
|