-
Notifications
You must be signed in to change notification settings - Fork 276
Expand file tree
/
Copy pathllm_config.py
More file actions
353 lines (292 loc) · 12 KB
/
llm_config.py
File metadata and controls
353 lines (292 loc) · 12 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
import concurrent.futures
import os
import threading
import time
from collections.abc import MutableMapping as MutableMappingABC
from dataclasses import dataclass
from typing import Any, Callable, Dict, MutableMapping, Tuple
from urllib.parse import urlsplit
from openai import OpenAI
CACHE_KEY = "dbx_oauth"
VALIDATION_TTL_SECONDS = 300
class DatabricksLLMConfigError(RuntimeError):
"""Raised when Databricks LLM configuration is invalid."""
@dataclass(frozen=True)
class DatabricksLLMConfig:
serving_base_url: str
workspace_host: str
model: str
auth_mode: str
_token_lock = threading.Lock()
_token_cache: Dict[str, Any] = {}
_validation_cache: Dict[Tuple[str, str], int] = {}
def _requests_module():
import requests
return requests
def _normalize_host(raw_host: str) -> str:
host = (raw_host or "").strip().rstrip("/")
if not host:
raise DatabricksLLMConfigError("Databricks workspace host is empty.")
if not host.startswith(("http://", "https://")):
host = "https://" + host
parts = urlsplit(host)
if not parts.scheme or not parts.netloc:
raise DatabricksLLMConfigError(f"Invalid Databricks workspace host: {raw_host!r}")
return f"{parts.scheme}://{parts.netloc}"
def _normalize_serving_base_url(raw_url: str) -> str:
value = (raw_url or "").strip()
if not value:
raise DatabricksLLMConfigError(
"DATABRICKS_SERVING_BASE_URL must be set to https://<workspace-host>/serving-endpoints."
)
if not value.startswith(("http://", "https://")):
value = "https://" + value
parts = urlsplit(value)
if not parts.scheme or not parts.netloc:
raise DatabricksLLMConfigError(f"Invalid DATABRICKS_SERVING_BASE_URL: {raw_url!r}")
path = parts.path.rstrip("/")
if path != "/serving-endpoints":
raise DatabricksLLMConfigError(
"DATABRICKS_SERVING_BASE_URL must end with /serving-endpoints for the target workspace."
)
return f"{parts.scheme}://{parts.netloc}/serving-endpoints"
def get_databricks_llm_config() -> DatabricksLLMConfig:
serving_base_url = _normalize_serving_base_url(
os.environ.get("DATABRICKS_SERVING_BASE_URL", "")
)
workspace_host = serving_base_url[: -len("/serving-endpoints")]
configured_host = os.environ.get("DATABRICKS_HOST", "").strip()
if configured_host:
normalized_host = _normalize_host(configured_host)
if normalized_host != workspace_host:
raise DatabricksLLMConfigError(
"DATABRICKS_HOST must match the workspace host in DATABRICKS_SERVING_BASE_URL."
)
model = os.environ.get("DATABRICKS_MODEL", "").strip()
if not model:
raise DatabricksLLMConfigError(
"DATABRICKS_MODEL must be set to a serving endpoint available in the workspace."
)
client_id = os.environ.get("DATABRICKS_CLIENT_ID", "").strip()
client_secret = os.environ.get("DATABRICKS_CLIENT_SECRET", "").strip()
token = os.environ.get("DATABRICKS_TOKEN", "").strip()
if client_id and client_secret:
auth_mode = "oauth-m2m"
elif token:
auth_mode = "pat"
else:
raise DatabricksLLMConfigError(
"No Databricks auth configured. Set DATABRICKS_CLIENT_ID and "
"DATABRICKS_CLIENT_SECRET, or provide DATABRICKS_TOKEN."
)
return DatabricksLLMConfig(
serving_base_url=serving_base_url,
workspace_host=workspace_host,
model=model,
auth_mode=auth_mode,
)
def get_serving_base_url() -> str:
return get_databricks_llm_config().serving_base_url
def get_model_name() -> str:
return get_databricks_llm_config().model
def _is_token_fresh(cache: MutableMapping[str, Any] | Dict[str, Any]) -> bool:
return bool(
cache.get("access_token")
and int(cache.get("expires_at", 0)) > int(time.time()) + 30
)
def _write_token_cache(
access_token: str,
expires_at: int,
config: DatabricksLLMConfig,
cache: MutableMapping[str, Any] | None = None,
) -> None:
token_record = {
"access_token": access_token,
"expires_at": expires_at,
"workspace_host": config.workspace_host,
"auth_mode": config.auth_mode,
"client_id": os.environ.get("DATABRICKS_CLIENT_ID", "").strip(),
}
_token_cache.clear()
_token_cache.update(token_record)
if cache is not None:
cache[CACHE_KEY] = dict(token_record)
def _token_cache_matches(
cache: MutableMapping[str, Any] | Dict[str, Any],
config: DatabricksLLMConfig,
) -> bool:
return bool(
cache.get("workspace_host") == config.workspace_host
and cache.get("auth_mode") == config.auth_mode
and cache.get("client_id", "") == os.environ.get("DATABRICKS_CLIENT_ID", "").strip()
)
def get_databricks_bearer_token(
cache: MutableMapping[str, Any] | None = None,
) -> str:
config = get_databricks_llm_config()
if config.auth_mode == "pat":
return os.environ["DATABRICKS_TOKEN"].strip()
if cache:
cached = cache.get(CACHE_KEY, {})
if (
isinstance(cached, MutableMappingABC)
and _token_cache_matches(cached, config)
and _is_token_fresh(cached)
):
_write_token_cache(
str(cached["access_token"]),
int(cached["expires_at"]),
config,
cache=cache,
)
return str(cached["access_token"])
if _token_cache_matches(_token_cache, config) and _is_token_fresh(_token_cache):
access_token = str(_token_cache["access_token"])
expires_at = int(_token_cache["expires_at"])
_write_token_cache(access_token, expires_at, config, cache=cache)
return access_token
with _token_lock:
if _token_cache_matches(_token_cache, config) and _is_token_fresh(_token_cache):
access_token = str(_token_cache["access_token"])
expires_at = int(_token_cache["expires_at"])
_write_token_cache(access_token, expires_at, config, cache=cache)
return access_token
requests = _requests_module()
try:
response = requests.post(
f"{config.workspace_host}/oidc/v1/token",
headers={"Content-Type": "application/x-www-form-urlencoded"},
data={"grant_type": "client_credentials", "scope": "all-apis"},
auth=(
os.environ["DATABRICKS_CLIENT_ID"].strip(),
os.environ["DATABRICKS_CLIENT_SECRET"].strip(),
),
timeout=30,
)
except Exception as exc:
raise DatabricksLLMConfigError(
f"Could not reach Databricks OAuth token endpoint for "
f"{config.workspace_host}: {type(exc).__name__}: {str(exc)[:200]}"
) from exc
if response.status_code >= 400:
raise DatabricksLLMConfigError(
f"Failed Databricks OAuth authentication for {config.workspace_host} "
f"(HTTP {response.status_code}). Check the service principal credentials "
"for that workspace."
)
payload = response.json()
access_token = payload.get("access_token")
expires_in = int(payload.get("expires_in", 300))
if not access_token:
raise DatabricksLLMConfigError(
f"Token endpoint response is missing access_token: {payload}"
)
expires_at = int(time.time()) + expires_in
_write_token_cache(str(access_token), expires_at, config, cache=cache)
return str(access_token)
def validate_databricks_llm_config(
cache: MutableMapping[str, Any] | None = None,
) -> DatabricksLLMConfig:
config = get_databricks_llm_config()
cache_key = (config.serving_base_url, config.model)
cached_expiry = _validation_cache.get(cache_key, 0)
if cached_expiry > int(time.time()):
return config
requests = _requests_module()
token = get_databricks_bearer_token(cache=cache)
headers = {"Authorization": f"Bearer {token}"}
endpoint_url = f"{config.workspace_host}/api/2.0/serving-endpoints/{config.model}"
try:
response = requests.get(endpoint_url, headers=headers, timeout=30)
except Exception as exc:
raise DatabricksLLMConfigError(
f"Could not validate DATABRICKS_MODEL={config.model!r} in workspace "
f"{config.workspace_host}: {type(exc).__name__}: {str(exc)[:200]}"
) from exc
if response.status_code == 404:
try:
list_response = requests.get(
f"{config.workspace_host}/api/2.0/serving-endpoints",
headers=headers,
timeout=30,
)
except Exception:
list_response = None
available: list[str] = []
if list_response is not None and list_response.status_code < 400:
try:
payload = list_response.json()
available = sorted(
endpoint.get("name", "").strip()
for endpoint in payload.get("endpoints", [])
if endpoint.get("name", "").strip()
)
except Exception:
available = []
available_text = ", ".join(available[:10]) if available else "no endpoints were returned"
raise DatabricksLLMConfigError(
f"DATABRICKS_MODEL={config.model!r} was not found in workspace "
f"{config.workspace_host}. Available endpoints include: {available_text}."
)
if response.status_code >= 400:
raise DatabricksLLMConfigError(
f"Failed to validate DATABRICKS_MODEL={config.model!r} in workspace "
f"{config.workspace_host} (HTTP {response.status_code}). "
f"Response: {response.text[:300]}"
)
_validation_cache[cache_key] = int(time.time()) + VALIDATION_TTL_SECONDS
return config
def build_openai_client(
*,
validate: bool = True,
cache: MutableMapping[str, Any] | None = None,
) -> OpenAI:
config = (
validate_databricks_llm_config(cache=cache)
if validate
else get_databricks_llm_config()
)
token = get_databricks_bearer_token(cache=cache)
return OpenAI(api_key=token, base_url=config.serving_base_url)
def create_foundation_model_client(
cache: MutableMapping[str, Any] | None = None,
) -> OpenAI:
return build_openai_client(validate=True, cache=cache)
def resolve_bearer_token(cache: MutableMapping[str, Any] | None = None) -> str:
return get_databricks_bearer_token(cache=cache)
def run_jobs_parallel(
jobs: Dict[str, Tuple[Callable[..., Any], Tuple[Any, ...], Dict[str, Any]]],
max_workers: int | None = None,
) -> Tuple[Dict[str, Any], list[str]]:
"""Run independent jobs in parallel and collect per-job failures."""
if max_workers is None:
raw_worker_count = os.environ.get("LLM_MAX_CONCURRENCY", "5")
try:
worker_count = int(raw_worker_count)
except ValueError as exc:
raise DatabricksLLMConfigError(
"LLM_MAX_CONCURRENCY must be a positive integer."
) from exc
else:
worker_count = max_workers
if worker_count < 1:
raise DatabricksLLMConfigError(
"LLM_MAX_CONCURRENCY must be a positive integer."
)
results: Dict[str, Any] = {}
errors: list[str] = []
def _call(fn: Callable[..., Any], args: Tuple[Any, ...], kwargs: Dict[str, Any]) -> Any:
return fn(*args, **kwargs)
with concurrent.futures.ThreadPoolExecutor(max_workers=worker_count) as executor:
futures = {
executor.submit(_call, fn, args, kwargs): name
for name, (fn, args, kwargs) in jobs.items()
}
concurrent.futures.wait(list(futures.keys()))
for future, name in [(future, futures[future]) for future in futures]:
try:
results[name] = future.result()
except Exception as exc:
errors.append(f"{name}: {type(exc).__name__}: {str(exc)[:200]}")
results[name] = None
return results, errors