-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_llm.py
More file actions
488 lines (420 loc) · 18.7 KB
/
Copy pathtest_llm.py
File metadata and controls
488 lines (420 loc) · 18.7 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
"""tests/test_llm.py — Unit tests for the LLM factory (core/llm.py)."""
from __future__ import annotations
import builtins
import sys
from unittest.mock import MagicMock, patch
import pytest
from core.llm import LLMConfig, get_llm
class TestGetLlmAnthropic:
def test_returns_chat_anthropic(self):
mock_model = MagicMock()
with patch.dict(
"sys.modules",
{
"langchain_anthropic": MagicMock(
ChatAnthropic=MagicMock(return_value=mock_model)
)
},
):
config = LLMConfig(provider="anthropic", anthropic_api_key="sk-ant-test123")
result = get_llm(config)
assert result is mock_model
def test_raises_without_api_key(self):
with patch.dict(
"sys.modules",
{"langchain_anthropic": MagicMock(ChatAnthropic=MagicMock())},
):
config = LLMConfig(provider="anthropic", anthropic_api_key=None)
with pytest.raises(ValueError, match="anthropic_api_key"):
get_llm(config)
def test_raises_on_import_error(self, monkeypatch: pytest.MonkeyPatch) -> None:
"""When ``langchain_anthropic`` cannot be loaded, get_llm must raise ImportError."""
real_import = builtins.__import__
def _guard_import(
name: str,
globals_arg: dict[str, object] | None = None,
locals_arg: dict[str, object] | None = None,
fromlist: tuple[str, ...] = (),
level: int = 0,
):
if level == 0 and name == "langchain_anthropic":
raise ImportError("simulated missing langchain_anthropic")
return real_import(name, globals_arg, locals_arg, fromlist, level)
monkeypatch.setattr(builtins, "__import__", _guard_import)
monkeypatch.delitem(sys.modules, "langchain_anthropic", raising=False)
config = LLMConfig(provider="anthropic", anthropic_api_key="sk-ant-test123")
with pytest.raises(ImportError):
get_llm(config)
class TestGetLlmOpenAI:
def test_returns_chat_openai(self):
mock_model = MagicMock()
with patch.dict(
"sys.modules",
{
"langchain_openai": MagicMock(
ChatOpenAI=MagicMock(return_value=mock_model)
)
},
):
config = LLMConfig(provider="openai", openai_api_key="sk-openai-test")
result = get_llm(config)
assert result is mock_model
def test_raises_without_api_key(self):
with patch.dict(
"sys.modules",
{"langchain_openai": MagicMock(ChatOpenAI=MagicMock())},
):
config = LLMConfig(provider="openai", openai_api_key=None)
with pytest.raises(ValueError, match="openai_api_key"):
get_llm(config)
class TestGetLlmOllama:
def test_returns_chat_ollama(self):
mock_model = MagicMock()
with patch.dict(
"sys.modules",
{
"langchain_ollama": MagicMock(
ChatOllama=MagicMock(return_value=mock_model)
)
},
):
config = LLMConfig(provider="ollama")
result = get_llm(config)
assert result is mock_model
class TestGetLlmUnknown:
def test_raises_on_unknown_provider(self):
# Force an unknown provider via direct config construction
config = LLMConfig.__new__(LLMConfig)
object.__setattr__(config, "provider", "unknown_provider")
with pytest.raises(ValueError, match="Unknown LLM provider"):
get_llm(config)
class TestGetLlmGoogle:
def test_returns_chat_google(self) -> None:
mock_model = MagicMock()
mock_module = MagicMock()
mock_module.ChatGoogleGenerativeAI.return_value = mock_model
with patch.dict("sys.modules", {"langchain_google_genai": mock_module}):
config = LLMConfig(provider="google", google_api_key="gai-test-key")
result = get_llm(config)
assert result is mock_model
def test_raises_without_api_key(self) -> None:
mock_module = MagicMock()
with patch.dict("sys.modules", {"langchain_google_genai": mock_module}):
config = LLMConfig(provider="google", google_api_key=None)
with pytest.raises(ValueError, match="google_api_key"):
get_llm(config)
class TestGetLlmBedrock:
pytest.importorskip("botocore")
def test_returns_chat_bedrock(self) -> None:
mock_model = MagicMock()
mock_module = MagicMock()
mock_module.ChatBedrock.return_value = mock_model
with patch.dict("sys.modules", {"langchain_aws": mock_module}):
config = LLMConfig(
provider="bedrock",
aws_access_key_id="AKIATEST",
aws_secret_access_key="secret",
)
result = get_llm(config)
assert result is mock_model
def test_works_without_static_credentials_for_irsa(self) -> None:
"""Bedrock must use boto3 default chain when static keys are omitted."""
mock_module = MagicMock()
mock_module.ChatBedrock.return_value = MagicMock()
with patch.dict("sys.modules", {"langchain_aws": mock_module}):
get_llm(
LLMConfig(
provider="bedrock",
aws_region="eu-west-1",
bedrock_model="anthropic.claude-3-5-sonnet-20241022-v2:0",
)
)
kwargs = mock_module.ChatBedrock.call_args.kwargs
assert "aws_access_key_id" not in kwargs
assert "aws_secret_access_key" not in kwargs
assert kwargs["region_name"] == "eu-west-1"
def test_passes_static_credentials_when_both_set(self) -> None:
mock_module = MagicMock()
mock_module.ChatBedrock.return_value = MagicMock()
with patch.dict("sys.modules", {"langchain_aws": mock_module}):
get_llm(
LLMConfig(
provider="bedrock",
aws_access_key_id="AKIATEST",
aws_secret_access_key="secret",
)
)
kwargs = mock_module.ChatBedrock.call_args.kwargs
assert kwargs["aws_access_key_id"] == "AKIATEST"
assert kwargs["aws_secret_access_key"] == "secret"
def test_raises_when_only_access_key_set(self) -> None:
mock_module = MagicMock()
with patch.dict("sys.modules", {"langchain_aws": mock_module}):
with pytest.raises(ValueError, match="AWS_SECRET_ACCESS_KEY"):
get_llm(
LLMConfig(
provider="bedrock",
aws_access_key_id="AKIATEST",
)
)
def test_raises_when_only_secret_key_set(self) -> None:
mock_module = MagicMock()
with patch.dict("sys.modules", {"langchain_aws": mock_module}):
with pytest.raises(ValueError, match="AWS_ACCESS_KEY_ID"):
get_llm(
LLMConfig(
provider="bedrock",
aws_secret_access_key="secret",
)
)
class TestGetLlmAzure:
def test_returns_azure_chat_openai(self) -> None:
mock_model = MagicMock()
mock_module = MagicMock()
mock_module.AzureChatOpenAI.return_value = mock_model
with patch.dict("sys.modules", {"langchain_openai": mock_module}):
config = LLMConfig(
provider="azure",
azure_openai_api_key="azure-key",
azure_openai_endpoint="https://my-resource.openai.azure.com/",
)
result = get_llm(config)
assert result is mock_model
def test_raises_without_api_key(self) -> None:
mock_module = MagicMock()
with patch.dict("sys.modules", {"langchain_openai": mock_module}):
config = LLMConfig(provider="azure", azure_openai_api_key=None)
with pytest.raises(ValueError, match="azure_openai_api_key"):
get_llm(config)
def test_raises_without_endpoint(self) -> None:
mock_module = MagicMock()
with patch.dict("sys.modules", {"langchain_openai": mock_module}):
config = LLMConfig(
provider="azure",
azure_openai_api_key="azure-key",
azure_openai_endpoint=None,
)
with pytest.raises(ValueError, match="azure_openai_endpoint"):
get_llm(config)
# ---------------------------------------------------------------------------
# ImportError handling for each provider
# ---------------------------------------------------------------------------
class TestGetLlmImportErrors:
"""get_llm should raise ImportError when the provider package is missing."""
def test_openai_import_error(self) -> None:
"""get_llm should raise ImportError when langchain_openai is missing."""
config = LLMConfig(provider="openai", openai_api_key="sk-test1234567890")
with patch.dict("sys.modules", {"langchain_openai": None}):
with pytest.raises(ImportError):
get_llm(config)
def test_google_import_error(self) -> None:
"""get_llm should raise ImportError when langchain_google_genai is missing."""
config = LLMConfig(provider="google", google_api_key="test-key-abcdef")
with patch.dict("sys.modules", {"langchain_google_genai": None}):
with pytest.raises(ImportError):
get_llm(config)
def test_azure_import_error(self) -> None:
"""get_llm should raise ImportError when langchain_openai is missing (Azure)."""
config = LLMConfig(
provider="azure",
azure_openai_api_key="azure-key",
azure_openai_endpoint="https://my-resource.openai.azure.com/",
)
with patch.dict("sys.modules", {"langchain_openai": None}):
with pytest.raises(ImportError):
get_llm(config)
def test_ollama_import_error(self) -> None:
"""get_llm should raise ImportError when langchain_ollama is missing."""
config = LLMConfig(provider="ollama")
with patch.dict("sys.modules", {"langchain_ollama": None}):
with pytest.raises(ImportError):
get_llm(config)
# ---------------------------------------------------------------------------
# LLMConfig defaults
# ---------------------------------------------------------------------------
class TestGetLlmMock:
"""Mock provider should work without any API key."""
def test_returns_fake_model(self):
config = LLMConfig(provider="mock")
llm = get_llm(config)
assert llm is not None
def test_mock_returns_string_response(self):
config = LLMConfig(provider="mock")
llm = get_llm(config)
from langchain_core.messages import HumanMessage
result = llm.invoke([HumanMessage(content="test")])
assert isinstance(result.content, str)
assert len(result.content) > 0
class TestLLMConfigDefaults:
"""Verify LLMConfig provides sensible defaults."""
def test_default_anthropic_model(self) -> None:
config = LLMConfig(provider="anthropic", anthropic_api_key="sk-ant-test123")
assert "claude" in config.anthropic_model.lower()
def test_default_max_tokens(self) -> None:
config = LLMConfig(provider="anthropic", anthropic_api_key="sk-ant-test123")
assert config.max_tokens > 0
def test_default_request_timeout(self) -> None:
config = LLMConfig(provider="anthropic", anthropic_api_key="sk-ant-test123")
assert config.request_timeout_seconds == 120.0
class TestGetLlmTimeoutPropagation:
def test_anthropic_receives_default_request_timeout(self) -> None:
mock_ctor = MagicMock(return_value=MagicMock())
with patch.dict(
"sys.modules",
{"langchain_anthropic": MagicMock(ChatAnthropic=mock_ctor)},
):
config = LLMConfig(
provider="anthropic",
anthropic_api_key="sk-ant-test123",
request_timeout_seconds=90.0,
)
get_llm(config)
mock_ctor.assert_called_once()
assert mock_ctor.call_args.kwargs["default_request_timeout"] == 90.0
def test_openai_receives_request_timeout(self) -> None:
mock_ctor = MagicMock(return_value=MagicMock())
with patch.dict(
"sys.modules",
{"langchain_openai": MagicMock(ChatOpenAI=mock_ctor)},
):
config = LLMConfig(
provider="openai",
openai_api_key="sk-openai-test",
request_timeout_seconds=75.0,
)
get_llm(config)
assert mock_ctor.call_args.kwargs["request_timeout"] == 75.0
class TestGetLlmSdkRetriesDisabled:
"""Vendor SDK retries must be off — BaseAgent owns transient retry logic."""
def test_anthropic_max_retries_zero(self) -> None:
mock_ctor = MagicMock(return_value=MagicMock())
with patch.dict(
"sys.modules",
{"langchain_anthropic": MagicMock(ChatAnthropic=mock_ctor)},
):
get_llm(LLMConfig(provider="anthropic", anthropic_api_key="sk-ant-test123"))
assert mock_ctor.call_args.kwargs["max_retries"] == 0
def test_openai_max_retries_zero(self) -> None:
mock_ctor = MagicMock(return_value=MagicMock())
with patch.dict(
"sys.modules",
{"langchain_openai": MagicMock(ChatOpenAI=mock_ctor)},
):
get_llm(LLMConfig(provider="openai", openai_api_key="sk-openai-test"))
assert mock_ctor.call_args.kwargs["max_retries"] == 0
def test_azure_max_retries_zero(self) -> None:
mock_module = MagicMock()
mock_module.AzureChatOpenAI.return_value = MagicMock()
with patch.dict("sys.modules", {"langchain_openai": mock_module}):
get_llm(
LLMConfig(
provider="azure",
azure_openai_api_key="azure-key",
azure_openai_endpoint="https://example.openai.azure.com/",
)
)
assert mock_module.AzureChatOpenAI.call_args.kwargs["max_retries"] == 0
def test_google_max_retries_zero(self) -> None:
mock_module = MagicMock()
mock_module.ChatGoogleGenerativeAI.return_value = MagicMock()
with patch.dict("sys.modules", {"langchain_google_genai": mock_module}):
get_llm(LLMConfig(provider="google", google_api_key="gai-test-key"))
assert mock_module.ChatGoogleGenerativeAI.call_args.kwargs["max_retries"] == 0
def test_bedrock_boto_retries_single_attempt(self) -> None:
pytest.importorskip("botocore")
mock_module = MagicMock()
mock_module.ChatBedrock.return_value = MagicMock()
with patch.dict("sys.modules", {"langchain_aws": mock_module}):
get_llm(
LLMConfig(
provider="bedrock",
aws_access_key_id="AKIATEST",
aws_secret_access_key="secret",
)
)
config = mock_module.ChatBedrock.call_args.kwargs["config"]
assert config.retries["max_attempts"] == 1
class TestGetLlmBaseUrlPropagation:
def test_anthropic_base_url_forwarded(self) -> None:
mock_ctor = MagicMock(return_value=MagicMock())
with patch.dict(
"sys.modules",
{"langchain_anthropic": MagicMock(ChatAnthropic=mock_ctor)},
):
get_llm(
LLMConfig(
provider="anthropic",
anthropic_api_key="sk-ant-test123",
anthropic_base_url="https://litellm.internal/anthropic",
)
)
assert mock_ctor.call_args.kwargs["base_url"] == (
"https://litellm.internal/anthropic"
)
def test_openai_base_url_forwarded(self) -> None:
mock_ctor = MagicMock(return_value=MagicMock())
with patch.dict(
"sys.modules",
{"langchain_openai": MagicMock(ChatOpenAI=mock_ctor)},
):
get_llm(
LLMConfig(
provider="openai",
openai_api_key="sk-openai-test",
openai_base_url="https://openrouter.ai/api/v1",
)
)
assert mock_ctor.call_args.kwargs["base_url"] == "https://openrouter.ai/api/v1"
def test_google_base_url_forwarded(self) -> None:
mock_module = MagicMock()
mock_module.ChatGoogleGenerativeAI.return_value = MagicMock()
with patch.dict("sys.modules", {"langchain_google_genai": mock_module}):
get_llm(
LLMConfig(
provider="google",
google_api_key="gai-test-key",
google_base_url="https://gateway.internal/google",
)
)
assert mock_module.ChatGoogleGenerativeAI.call_args.kwargs["base_url"] == (
"https://gateway.internal/google"
)
def test_bedrock_endpoint_url_forwarded(self) -> None:
pytest.importorskip("botocore")
mock_module = MagicMock()
mock_module.ChatBedrock.return_value = MagicMock()
with patch.dict("sys.modules", {"langchain_aws": mock_module}):
get_llm(
LLMConfig(
provider="bedrock",
aws_access_key_id="AKIATEST",
aws_secret_access_key="secret",
bedrock_endpoint_url="https://bedrock-runtime.vpce.example",
)
)
assert mock_module.ChatBedrock.call_args.kwargs["endpoint_url"] == (
"https://bedrock-runtime.vpce.example"
)
def test_azure_base_url_forwarded(self) -> None:
mock_module = MagicMock()
mock_module.AzureChatOpenAI.return_value = MagicMock()
with patch.dict("sys.modules", {"langchain_openai": mock_module}):
get_llm(
LLMConfig(
provider="azure",
azure_openai_api_key="azure-key",
azure_openai_endpoint="https://example.openai.azure.com/",
azure_openai_base_url="https://gateway.internal/azure",
)
)
assert mock_module.AzureChatOpenAI.call_args.kwargs["base_url"] == (
"https://gateway.internal/azure"
)
def test_base_url_omitted_when_unset(self) -> None:
mock_ctor = MagicMock(return_value=MagicMock())
with patch.dict(
"sys.modules",
{"langchain_openai": MagicMock(ChatOpenAI=mock_ctor)},
):
get_llm(LLMConfig(provider="openai", openai_api_key="sk-openai-test"))
assert "base_url" not in mock_ctor.call_args.kwargs