2023-12-20 02:55:19 +00:00
|
|
|
"""Test chat model integration."""
|
|
|
|
|
2024-02-26 05:57:26 +00:00
|
|
|
import os
|
2023-12-20 02:55:19 +00:00
|
|
|
|
2024-02-26 05:57:26 +00:00
|
|
|
import pytest
|
|
|
|
|
|
|
|
from langchain_anthropic import ChatAnthropic, ChatAnthropicMessages
|
|
|
|
|
|
|
|
os.environ["ANTHROPIC_API_KEY"] = "foo"
|
2023-12-20 02:55:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_initialization() -> None:
|
|
|
|
"""Test chat model initialization."""
|
|
|
|
ChatAnthropicMessages(model_name="claude-instant-1.2", anthropic_api_key="xyz")
|
2024-01-24 22:48:31 +00:00
|
|
|
ChatAnthropicMessages(model="claude-instant-1.2", anthropic_api_key="xyz")
|
2024-02-26 05:57:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.requires("anthropic")
|
|
|
|
def test_anthropic_model_name_param() -> None:
|
|
|
|
llm = ChatAnthropic(model_name="foo")
|
|
|
|
assert llm.model == "foo"
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.requires("anthropic")
|
|
|
|
def test_anthropic_model_param() -> None:
|
|
|
|
llm = ChatAnthropic(model="foo")
|
|
|
|
assert llm.model == "foo"
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.requires("anthropic")
|
|
|
|
def test_anthropic_model_kwargs() -> None:
|
|
|
|
llm = ChatAnthropic(model_name="foo", model_kwargs={"foo": "bar"})
|
|
|
|
assert llm.model_kwargs == {"foo": "bar"}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.requires("anthropic")
|
|
|
|
def test_anthropic_invalid_model_kwargs() -> None:
|
|
|
|
with pytest.raises(ValueError):
|
|
|
|
ChatAnthropic(model="foo", model_kwargs={"max_tokens_to_sample": 5})
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.requires("anthropic")
|
|
|
|
def test_anthropic_incorrect_field() -> None:
|
|
|
|
with pytest.warns(match="not default parameter"):
|
|
|
|
llm = ChatAnthropic(model="foo", foo="bar")
|
|
|
|
assert llm.model_kwargs == {"foo": "bar"}
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.requires("anthropic")
|
|
|
|
def test_anthropic_initialization() -> None:
|
|
|
|
"""Test anthropic initialization."""
|
|
|
|
# Verify that chat anthropic can be initialized using a secret key provided
|
|
|
|
# as a parameter rather than an environment variable.
|
|
|
|
ChatAnthropic(model="test", anthropic_api_key="test")
|