-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_local_llm.py
More file actions
276 lines (209 loc) · 11 KB
/
Copy pathtest_local_llm.py
File metadata and controls
276 lines (209 loc) · 11 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
import json
import threading
from http.server import BaseHTTPRequestHandler, HTTPServer
import pytest
from typer.testing import CliRunner
from buffdata.cli.main import app
from buffdata.engine.client import (
LLMProvider,
OpenAICompatibleClient,
ProviderError,
_is_local_network_host,
create_llm_client,
)
from buffdata.models.formats import write_dataset
from buffdata.models.schemas import DatasetItem, PipelineConfig
# --- _is_local_network_host: pure logic, no network involved ------------------------------
@pytest.mark.parametrize("host", [
"localhost",
"127.0.0.1",
"127.5.5.5",
"::1",
"10.0.0.1",
"10.255.255.255",
"172.16.0.1",
"172.31.255.255",
"192.168.1.1",
"192.168.0.50",
"169.254.1.1", # link-local
"myhost.local",
"MyHost.LOCAL", # case-insensitive
"LOCALHOST",
])
def test_is_local_network_host_accepts_loopback_and_private_addresses(host):
assert _is_local_network_host(host) is True
@pytest.mark.parametrize("host", [
"8.8.8.8",
"1.1.1.1",
"93.184.216.34",
"api.openai.com",
"generativelanguage.googleapis.com",
"myserver.example.com",
"172.32.0.1", # just outside the 172.16/12 private range
"172.15.255.255", # just below it
"",
])
def test_is_local_network_host_rejects_public_hosts(host):
assert _is_local_network_host(host) is False
# --- provider aliases resolve to OpenAICompatibleClient with the right preset -------------
@pytest.mark.parametrize(("provider", "expected_url"), [
("ollama", "http://localhost:11434/v1"),
("lmstudio", "http://localhost:1234/v1"),
("vllm", "http://localhost:8000/v1"),
("llamacpp", "http://localhost:8080/v1"),
])
def test_local_provider_aliases_resolve_to_their_preset_base_url(provider, expected_url):
client = create_llm_client(provider, model="llama3.1")
assert isinstance(client, OpenAICompatibleClient)
assert client.base_url == expected_url
assert client.provider == LLMProvider(provider) # reports its own name, not "openai_compatible"
def test_local_provider_explicit_base_url_overrides_the_preset():
client = create_llm_client("ollama", model="llama3.1", base_url="http://192.168.1.50:11434/v1")
assert client.base_url == "http://192.168.1.50:11434/v1"
def test_local_provider_env_var_overrides_the_preset_but_not_an_explicit_base_url(monkeypatch):
monkeypatch.setenv("OLLAMA_BASE_URL", "http://gpu-box.local:11434/v1")
client = create_llm_client("ollama", model="llama3.1")
assert client.base_url == "http://gpu-box.local:11434/v1"
client_explicit = create_llm_client("ollama", model="llama3.1", base_url="http://other:11434/v1")
assert client_explicit.base_url == "http://other:11434/v1"
def test_local_provider_without_a_model_raises_naming_the_specific_provider(monkeypatch):
monkeypatch.delenv("BUFFDATA_DEFAULT_MODEL", raising=False)
with pytest.raises(ProviderError, match="provider 'ollama' has no universal default model"):
create_llm_client("ollama")
def test_local_provider_api_key_defaults_to_not_required_when_unset(monkeypatch):
monkeypatch.delenv("OLLAMA_API_KEY", raising=False)
client = create_llm_client("ollama", model="llama3.1")
assert client.api_key == "not-required"
def test_local_provider_reads_its_own_api_key_env_var(monkeypatch):
monkeypatch.setenv("VLLM_API_KEY", "team-shared-token")
client = create_llm_client("vllm", model="mistral")
assert client.api_key == "team-shared-token"
# --- network_policy="local": structural guarantee ------------------------------------------
@pytest.mark.parametrize("provider", ["gemini", "openai", "anthropic", "azure_openai", "bedrock_anthropic"])
def test_network_policy_local_rejects_cloud_providers_outright(provider):
with pytest.raises(ProviderError, match="requires a provider whose endpoint you control"):
create_llm_client(provider, model="whatever", network_policy="local")
def test_network_policy_local_accepts_a_loopback_base_url():
client = create_llm_client("ollama", model="llama3.1", network_policy="local")
assert isinstance(client, OpenAICompatibleClient)
def test_network_policy_local_accepts_a_private_lan_base_url():
client = create_llm_client(
"openai_compatible", model="llama3.1",
base_url="http://192.168.1.50:8000/v1", network_policy="local",
)
assert client.base_url == "http://192.168.1.50:8000/v1"
def test_network_policy_local_blocks_a_public_base_url():
with pytest.raises(ProviderError, match="blocked base_url"):
create_llm_client(
"openai_compatible", model="llama3.1",
base_url="https://api.some-cloud-llm.com/v1", network_policy="local",
)
def test_network_policy_local_requires_a_resolvable_base_url(monkeypatch):
monkeypatch.delenv("OPENAI_COMPATIBLE_BASE_URL", raising=False)
with pytest.raises(ProviderError, match="requires a base URL to validate"):
create_llm_client("openai_compatible", model="llama3.1", network_policy="local")
def test_network_policy_accepts_only_the_three_known_values():
with pytest.raises(ProviderError, match="network_policy must be"):
create_llm_client("ollama", model="llama3.1", network_policy="paranoid")
# --- PipelineConfig: fast pre-flight for network_policy="local" ---------------------------
def test_pipeline_config_accepts_local_network_policy_with_an_eligible_provider():
config = PipelineConfig(provider="ollama", model="llama3.1", network_policy="local", quality_mode="off")
assert config.network_policy == "local"
def test_pipeline_config_rejects_local_network_policy_with_a_cloud_provider():
with pytest.raises(ValueError, match="requires a provider whose endpoint you control"):
PipelineConfig(provider="gemini", network_policy="local")
def test_pipeline_config_accepts_a_base_url_field():
config = PipelineConfig(provider="ollama", base_url="http://192.168.1.50:11434/v1")
assert config.base_url == "http://192.168.1.50:11434/v1"
# --- Real end-to-end round trip against a local OpenAI-compatible server ------------------
# A minimal HTTP server standing in for Ollama/LM Studio/vLLM's real /v1/chat/completions
# endpoint -- this proves the real `openai` SDK client actually sends a real HTTP request to
# 127.0.0.1 and parses a real HTTP response, not a mocked object.
class _FakeLocalLLMHandler(BaseHTTPRequestHandler):
received_requests: list = []
def do_POST(self):
length = int(self.headers.get("Content-Length", 0))
body = json.loads(self.rfile.read(length))
_FakeLocalLLMHandler.received_requests.append({"path": self.path, "body": body})
response = {
"id": "chatcmpl-fake-local-1",
"object": "chat.completion",
"created": 1700000000,
"model": body.get("model", "unknown"),
"choices": [{
"index": 0,
"message": {"role": "assistant", "content": "Hello from the fake local LLM server!"},
"finish_reason": "stop",
}],
"usage": {"prompt_tokens": 12, "completion_tokens": 8, "total_tokens": 20},
}
payload = json.dumps(response).encode("utf-8")
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.send_header("Content-Length", str(len(payload)))
self.end_headers()
self.wfile.write(payload)
def log_message(self, format, *args):
pass # silence default request logging
@pytest.fixture
def fake_local_llm_server():
_FakeLocalLLMHandler.received_requests = []
server = HTTPServer(("127.0.0.1", 0), _FakeLocalLLMHandler)
port = server.server_port
thread = threading.Thread(target=server.serve_forever, daemon=True)
thread.start()
try:
yield f"http://127.0.0.1:{port}/v1"
finally:
server.shutdown()
thread.join(timeout=5)
def test_ollama_provider_makes_a_real_http_round_trip_to_a_local_server(fake_local_llm_server):
client = create_llm_client("ollama", model="llama3.1", base_url=fake_local_llm_server)
result = client.generate_text("What model are you?")
assert result == "Hello from the fake local LLM server!"
assert len(_FakeLocalLLMHandler.received_requests) == 1
sent = _FakeLocalLLMHandler.received_requests[0]
assert sent["path"] == "/v1/chat/completions"
assert sent["body"]["model"] == "llama3.1"
assert sent["body"]["messages"][0]["content"] == "What model are you?"
assert client.usage["total_tokens"] == 20
@pytest.mark.asyncio
async def test_ollama_provider_makes_a_real_async_http_round_trip(fake_local_llm_server):
client = create_llm_client("ollama", model="llama3.1", base_url=fake_local_llm_server)
result = await client.generate_text_async("ping")
assert result == "Hello from the fake local LLM server!"
def test_network_policy_local_end_to_end_against_a_real_loopback_server(fake_local_llm_server):
# network_policy="local" doesn't just permit the client to be constructed -- confirm a
# real call through it still reaches the real (loopback) server successfully.
client = create_llm_client("ollama", model="llama3.1", base_url=fake_local_llm_server, network_policy="local")
result = client.generate_text("hi")
assert result == "Hello from the fake local LLM server!"
# --- CLI wiring -------------------------------------------------------------------------
def test_cli_score_reaches_a_real_local_server_via_base_url_flag(fake_local_llm_server, tmp_path):
# score_cmd uses generate_structured_async (chat.completions.parse), which the fake
# server's plain-text response can't satisfy -- so this deliberately fails downstream,
# the same way tests/test_network_policy.py's CLI tests let strict-mode runs fail for an
# unrelated, expected reason. What this proves is that --provider/--base-url actually
# reached a real client that made a real HTTP request to the local server (confirmed via
# the server's own request log), rather than being blocked or short-circuited earlier.
source = tmp_path / "train.jsonl"
output = tmp_path / "scored.jsonl"
write_dataset([DatasetItem.from_dict({"text": "a sample row"})], source)
CliRunner().invoke(app, [
"score", str(source), "-o", str(output),
"--provider", "ollama", "--base-url", fake_local_llm_server, "--model", "llama3.1",
])
assert len(_FakeLocalLLMHandler.received_requests) >= 1
assert _FakeLocalLLMHandler.received_requests[0]["body"]["model"] == "llama3.1"
def test_cli_score_with_network_policy_local_blocks_a_public_base_url(tmp_path):
source = tmp_path / "train.jsonl"
output = tmp_path / "scored.jsonl"
write_dataset([DatasetItem.from_dict({"text": "a sample row"})], source)
result = CliRunner().invoke(app, [
"score", str(source), "-o", str(output),
"--provider", "openai_compatible", "--base-url", "https://not-local.example.com/v1",
"--model", "llama3.1", "--network-policy", "local",
])
assert result.exit_code != 0
assert "blocked base_url" in str(result.output) + str(result.exception)
assert not output.exists()