-
Notifications
You must be signed in to change notification settings - Fork 579
Expand file tree
/
Copy pathtest_anthropic.py
More file actions
82 lines (68 loc) · 4.23 KB
/
test_anthropic.py
File metadata and controls
82 lines (68 loc) · 4.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""Tests for KAgentAnthropicLlm."""
from unittest import mock
from anthropic import AsyncAnthropic
from kagent.adk.models._anthropic import KAgentAnthropicLlm
class TestKAgentAnthropicLlm:
def test_default_construction(self):
llm = KAgentAnthropicLlm(model="claude-3-sonnet-20240229")
assert llm.model == "claude-3-sonnet-20240229"
assert llm.base_url is None
assert llm.extra_headers is None
assert llm.api_key_passthrough is None
def test_set_passthrough_key(self):
llm = KAgentAnthropicLlm(model="claude-3-sonnet-20240229", api_key_passthrough=True)
llm.set_passthrough_key("sk-bearer-token")
assert llm._api_key == "sk-bearer-token"
def test_set_passthrough_key_invalidates_cached_client(self):
llm = KAgentAnthropicLlm(model="claude-3-sonnet-20240229")
with mock.patch("anthropic.AsyncAnthropic"):
_ = llm._anthropic_client
assert "_anthropic_client" in llm.__dict__
llm.set_passthrough_key("new-token")
assert "_anthropic_client" not in llm.__dict__
def test_client_uses_base_url(self):
llm = KAgentAnthropicLlm(model="claude-3-sonnet-20240229", base_url="https://proxy.internal/anthropic")
with mock.patch("kagent.adk.models._anthropic.AsyncAnthropic") as mock_anthropic:
mock_anthropic.return_value = mock.MagicMock(spec=AsyncAnthropic)
_ = llm._anthropic_client
assert mock_anthropic.call_args.kwargs["base_url"] == "https://proxy.internal/anthropic"
def test_client_falls_back_to_env_base_url(self):
llm = KAgentAnthropicLlm(model="claude-3-sonnet-20240229")
with mock.patch.dict("os.environ", {"ANTHROPIC_BASE_URL": "https://env-proxy.internal/anthropic"}):
with mock.patch("kagent.adk.models._anthropic.AsyncAnthropic") as mock_anthropic:
mock_anthropic.return_value = mock.MagicMock(spec=AsyncAnthropic)
_ = llm._anthropic_client
assert mock_anthropic.call_args.kwargs["base_url"] == "https://env-proxy.internal/anthropic"
def test_explicit_base_url_takes_precedence_over_env(self):
llm = KAgentAnthropicLlm(model="claude-3-sonnet-20240229", base_url="https://explicit.internal/anthropic")
with mock.patch.dict("os.environ", {"ANTHROPIC_BASE_URL": "https://env.internal/anthropic"}):
with mock.patch("kagent.adk.models._anthropic.AsyncAnthropic") as mock_anthropic:
mock_anthropic.return_value = mock.MagicMock(spec=AsyncAnthropic)
_ = llm._anthropic_client
# Explicit base_url wins over env
assert mock_anthropic.call_args.kwargs["base_url"] == "https://explicit.internal/anthropic"
def test_client_uses_extra_headers(self):
llm = KAgentAnthropicLlm(model="claude-3-sonnet-20240229", extra_headers={"X-Org": "test-org"})
with mock.patch("kagent.adk.models._anthropic.AsyncAnthropic") as mock_anthropic:
mock_anthropic.return_value = mock.MagicMock(spec=AsyncAnthropic)
_ = llm._anthropic_client
assert mock_anthropic.call_args.kwargs["default_headers"] == {"X-Org": "test-org"}
def test_client_uses_passthrough_key(self):
llm = KAgentAnthropicLlm(model="claude-3-sonnet-20240229", api_key_passthrough=True)
llm.set_passthrough_key("sk-test-key")
with mock.patch("kagent.adk.models._anthropic.AsyncAnthropic") as mock_anthropic:
mock_anthropic.return_value = mock.MagicMock(spec=AsyncAnthropic)
_ = llm._anthropic_client
assert mock_anthropic.call_args.kwargs["api_key"] == "sk-test-key"
def test_create_llm_from_anthropic_model_config(self):
"""Integration: _create_llm_from_model_config returns KAgentAnthropicLlm for anthropic type."""
from kagent.adk.types import Anthropic, _create_llm_from_model_config
config = Anthropic(
type="anthropic",
model="claude-3-sonnet-20240229",
base_url="https://api.anthropic.com",
)
result = _create_llm_from_model_config(config)
assert isinstance(result, KAgentAnthropicLlm)
assert result.model == "claude-3-sonnet-20240229"
assert result.base_url == "https://api.anthropic.com"