@dbx-tools/dbx-model-catalog 0.1.4 → 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +62 -10
- package/dist/_generated/json-schema.d.ts +3830 -0
- package/dist/_generated/published.d.ts +7 -0
- package/dist/_generated/schemas.d.ts +27 -2
- package/dist/browser.d.ts +12 -3
- package/dist/browser.js +9487 -1141
- package/dist/browser.js.map +32 -9
- package/dist/catalog-index.d.ts +28 -0
- package/dist/catalog.json +1 -0
- package/dist/cli.js +8811 -49414
- package/dist/cli.js.map +16 -222
- package/dist/filter.d.ts +2 -0
- package/dist/identity.d.ts +15 -0
- package/dist/index.d.ts +8 -4
- package/dist/index.js +8843 -49435
- package/dist/index.js.map +15 -221
- package/dist/local-client.d.ts +4 -0
- package/dist/{client.d.ts → pynect.d.ts} +1 -1
- package/dist/pynect.js +553 -0
- package/dist/pynect.js.map +15 -0
- package/dist/search-index.json +1 -0
- package/dist/search.d.ts +10 -0
- package/dist/snapshot.d.ts +6 -0
- package/dist/snapshot.js +9494 -0
- package/dist/snapshot.js.map +36 -0
- package/dist/types.d.ts +5 -0
- package/dist/validation.d.ts +5 -0
- package/package.json +27 -2
- package/dist/python/pyproject.toml +0 -16
- package/dist/python/src/dbx_model_catalog/__init__.py +0 -79
- package/dist/python/src/dbx_model_catalog/client.py +0 -251
- package/dist/python/src/dbx_model_catalog/database.py +0 -296
- package/dist/python/src/dbx_model_catalog/filters.py +0 -41
- package/dist/python/src/dbx_model_catalog/models.py +0 -847
- package/dist/python/src/dbx_model_catalog/publishing.py +0 -27
- package/dist/python/src/dbx_model_catalog/py.typed +0 -0
- package/dist/python/src/dbx_model_catalog/query.py +0 -593
- package/dist/python/src/dbx_model_catalog/schema/__init__.py +0 -0
- package/dist/python/src/dbx_model_catalog/schema/model-catalog.schema.json +0 -1016
- package/dist/python/src/dbx_model_catalog/search.py +0 -912
- package/dist/python/src/dbx_model_catalog_server/__init__.py +0 -3
- package/dist/python/src/dbx_model_catalog_server/app.py +0 -142
|
@@ -1,912 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
import math
|
|
4
|
-
import re
|
|
5
|
-
import statistics
|
|
6
|
-
import unicodedata
|
|
7
|
-
from dataclasses import dataclass
|
|
8
|
-
from datetime import date, datetime
|
|
9
|
-
from typing import Literal
|
|
10
|
-
|
|
11
|
-
from pydantic import AwareDatetime, BaseModel, ConfigDict, Field
|
|
12
|
-
from rapidfuzz import fuzz
|
|
13
|
-
|
|
14
|
-
from .models import CatalogModelRecord, ModelCatalog
|
|
15
|
-
|
|
16
|
-
SearchMatchKind = Literal["exact", "token", "fuzzy"]
|
|
17
|
-
|
|
18
|
-
_TOKEN_PATTERN = re.compile(r"[a-z]+|\d+")
|
|
19
|
-
_VERSION_PATTERN = re.compile(
|
|
20
|
-
r"(?<![a-z0-9])(\d+)(?:[.\-_](\d+))?(?:[.\-_](\d+))?(?![a-z0-9])",
|
|
21
|
-
re.IGNORECASE,
|
|
22
|
-
)
|
|
23
|
-
_STATUS_SCORE = {
|
|
24
|
-
"active": 100.0,
|
|
25
|
-
"preview": 85.0,
|
|
26
|
-
"beta": 85.0,
|
|
27
|
-
"alpha": 70.0,
|
|
28
|
-
"legacy": 40.0,
|
|
29
|
-
"deprecated": 15.0,
|
|
30
|
-
"retired": 0.0,
|
|
31
|
-
}
|
|
32
|
-
_STATUS_PRIORITY = {
|
|
33
|
-
status: position for position, status in enumerate(_STATUS_SCORE)
|
|
34
|
-
}
|
|
35
|
-
_SEARCH_INDEX_SCHEMA_VERSION = 1
|
|
36
|
-
_NEUTRAL_SCORE = 50.0
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
class CatalogSearchWeights(BaseModel):
|
|
40
|
-
model_config = ConfigDict(extra="forbid", frozen=True)
|
|
41
|
-
|
|
42
|
-
relevance: float
|
|
43
|
-
leaderboard: float
|
|
44
|
-
benchmark: float
|
|
45
|
-
version: float
|
|
46
|
-
freshness: float
|
|
47
|
-
lifecycle: float
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
SEARCH_WEIGHTS = CatalogSearchWeights(
|
|
51
|
-
relevance=0.70,
|
|
52
|
-
leaderboard=0.04,
|
|
53
|
-
benchmark=0.08,
|
|
54
|
-
version=0.08,
|
|
55
|
-
freshness=0.07,
|
|
56
|
-
lifecycle=0.03,
|
|
57
|
-
)
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
class CatalogSearchScores(BaseModel):
|
|
61
|
-
model_config = ConfigDict(extra="forbid")
|
|
62
|
-
|
|
63
|
-
relevance: float = Field(ge=0, le=100)
|
|
64
|
-
leaderboard: float = Field(ge=0, le=100)
|
|
65
|
-
benchmark: float = Field(ge=0, le=100)
|
|
66
|
-
version: float = Field(ge=0, le=100)
|
|
67
|
-
freshness: float = Field(ge=0, le=100)
|
|
68
|
-
lifecycle: float = Field(ge=0, le=100)
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
class CatalogSearchQualityScores(BaseModel):
|
|
72
|
-
model_config = ConfigDict(extra="forbid")
|
|
73
|
-
|
|
74
|
-
leaderboard: float = Field(ge=0, le=100)
|
|
75
|
-
benchmark: float = Field(ge=0, le=100)
|
|
76
|
-
version: float = Field(ge=0, le=100)
|
|
77
|
-
freshness: float = Field(ge=0, le=100)
|
|
78
|
-
lifecycle: float = Field(ge=0, le=100)
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
class CatalogSearchResult(BaseModel):
|
|
82
|
-
model_config = ConfigDict(extra="forbid")
|
|
83
|
-
|
|
84
|
-
record: CatalogModelRecord
|
|
85
|
-
score: float = Field(ge=0, le=100)
|
|
86
|
-
match: SearchMatchKind
|
|
87
|
-
matched_fields: list[str]
|
|
88
|
-
scores: CatalogSearchScores
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
class CatalogSearchIndexDocument(BaseModel):
|
|
92
|
-
model_config = ConfigDict(extra="forbid")
|
|
93
|
-
|
|
94
|
-
id: str
|
|
95
|
-
exact_identities: list[str]
|
|
96
|
-
identities: list[str]
|
|
97
|
-
keywords: str
|
|
98
|
-
description: str
|
|
99
|
-
tokens: list[str]
|
|
100
|
-
family: str | None = None
|
|
101
|
-
version: list[int]
|
|
102
|
-
quality_score: float = Field(ge=0, le=100)
|
|
103
|
-
scores: CatalogSearchQualityScores
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
class CatalogSearchIndexPayload(BaseModel):
|
|
107
|
-
model_config = ConfigDict(extra="forbid")
|
|
108
|
-
|
|
109
|
-
schema_version: int
|
|
110
|
-
catalog_schema_version: int
|
|
111
|
-
updated_at: AwareDatetime
|
|
112
|
-
weights: CatalogSearchWeights
|
|
113
|
-
documents: list[CatalogSearchIndexDocument]
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
@dataclass(frozen=True)
|
|
117
|
-
class _SearchField:
|
|
118
|
-
name: str
|
|
119
|
-
value: str
|
|
120
|
-
weight: float
|
|
121
|
-
identity: bool = False
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
@dataclass(frozen=True)
|
|
125
|
-
class _IndexedField:
|
|
126
|
-
name: str
|
|
127
|
-
value: str
|
|
128
|
-
tokens: frozenset[str]
|
|
129
|
-
weight: float
|
|
130
|
-
identity: bool
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
@dataclass(frozen=True)
|
|
134
|
-
class _IndexedRecord:
|
|
135
|
-
record: CatalogModelRecord
|
|
136
|
-
fields: tuple[_IndexedField, ...]
|
|
137
|
-
identity_tokens: frozenset[str]
|
|
138
|
-
all_tokens: frozenset[str]
|
|
139
|
-
version: tuple[int, ...]
|
|
140
|
-
freshness_date: date | None
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
@dataclass(frozen=True)
|
|
144
|
-
class _ScoredRecord:
|
|
145
|
-
result: CatalogSearchResult
|
|
146
|
-
identity_exact: bool
|
|
147
|
-
version: tuple[int, ...]
|
|
148
|
-
release_date: date | None
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
class CatalogSearchIndex:
|
|
152
|
-
"""Precomputed searchable fields and quality signals for one catalog snapshot."""
|
|
153
|
-
|
|
154
|
-
def __init__(
|
|
155
|
-
self,
|
|
156
|
-
records: list[CatalogModelRecord],
|
|
157
|
-
*,
|
|
158
|
-
reference_date: date | None = None,
|
|
159
|
-
) -> None:
|
|
160
|
-
self._records = tuple(_index_record(record) for record in records)
|
|
161
|
-
self._reference_date = reference_date or _reference_date(self._records)
|
|
162
|
-
self._family_versions = _family_versions(self._records)
|
|
163
|
-
self._freshness_range = _freshness_range(self._records)
|
|
164
|
-
self._quality_scores = {
|
|
165
|
-
indexed.record.id: self._quality_for(indexed) for indexed in self._records
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
@classmethod
|
|
169
|
-
def from_catalog(cls, catalog: ModelCatalog) -> CatalogSearchIndex:
|
|
170
|
-
records = [
|
|
171
|
-
CatalogModelRecord(id=model_id, **model.model_dump())
|
|
172
|
-
for model_id, model in catalog.models.items()
|
|
173
|
-
]
|
|
174
|
-
return cls(records, reference_date=catalog.updated_at.date())
|
|
175
|
-
|
|
176
|
-
def search(
|
|
177
|
-
self,
|
|
178
|
-
query: str,
|
|
179
|
-
*,
|
|
180
|
-
limit: int | None = 50,
|
|
181
|
-
score_cutoff: float = 58,
|
|
182
|
-
record_ids: set[str] | None = None,
|
|
183
|
-
) -> list[CatalogSearchResult]:
|
|
184
|
-
normalized_query = _normalize(query)
|
|
185
|
-
if not normalized_query or limit == 0:
|
|
186
|
-
return []
|
|
187
|
-
|
|
188
|
-
query_tokens = _tokens(normalized_query)
|
|
189
|
-
query_text_tokens = [token for token in query_tokens if not token.isdigit()]
|
|
190
|
-
requested_version = _version(query)
|
|
191
|
-
scored = [
|
|
192
|
-
candidate
|
|
193
|
-
for indexed in self._records
|
|
194
|
-
if record_ids is None or indexed.record.id in record_ids
|
|
195
|
-
if (
|
|
196
|
-
candidate := self._score_record(
|
|
197
|
-
indexed,
|
|
198
|
-
normalized_query,
|
|
199
|
-
query_text_tokens,
|
|
200
|
-
requested_version,
|
|
201
|
-
score_cutoff,
|
|
202
|
-
)
|
|
203
|
-
)
|
|
204
|
-
]
|
|
205
|
-
scored.sort(key=lambda candidate: _sort_key(candidate, requested_version))
|
|
206
|
-
results = [candidate.result for candidate in scored]
|
|
207
|
-
return results if limit is None else results[:limit]
|
|
208
|
-
|
|
209
|
-
def payload(
|
|
210
|
-
self,
|
|
211
|
-
*,
|
|
212
|
-
catalog_schema_version: int,
|
|
213
|
-
updated_at: datetime,
|
|
214
|
-
) -> CatalogSearchIndexPayload:
|
|
215
|
-
documents = []
|
|
216
|
-
for indexed in self._records:
|
|
217
|
-
quality = self._quality_scores[indexed.record.id]
|
|
218
|
-
identity_values = list(
|
|
219
|
-
dict.fromkeys(field.value for field in indexed.fields if field.identity)
|
|
220
|
-
)
|
|
221
|
-
exact_identity_values = list(
|
|
222
|
-
dict.fromkeys(
|
|
223
|
-
field.value
|
|
224
|
-
for field in indexed.fields
|
|
225
|
-
if field.name in {"id", "name", "alias"}
|
|
226
|
-
)
|
|
227
|
-
)
|
|
228
|
-
keyword_values = [
|
|
229
|
-
field.value
|
|
230
|
-
for field in indexed.fields
|
|
231
|
-
if field.name not in {"description", "notice"}
|
|
232
|
-
]
|
|
233
|
-
description_values = [
|
|
234
|
-
field.value
|
|
235
|
-
for field in indexed.fields
|
|
236
|
-
if field.name in {"description", "notice"}
|
|
237
|
-
]
|
|
238
|
-
documents.append(
|
|
239
|
-
CatalogSearchIndexDocument(
|
|
240
|
-
id=indexed.record.id,
|
|
241
|
-
exact_identities=exact_identity_values,
|
|
242
|
-
identities=identity_values,
|
|
243
|
-
keywords=" ".join(dict.fromkeys(keyword_values)),
|
|
244
|
-
description=" ".join(dict.fromkeys(description_values)),
|
|
245
|
-
tokens=sorted(indexed.all_tokens),
|
|
246
|
-
family=indexed.record.family,
|
|
247
|
-
version=list(indexed.version),
|
|
248
|
-
quality_score=round(_quality_score(quality), 3),
|
|
249
|
-
scores=CatalogSearchQualityScores(**_rounded_values(quality)),
|
|
250
|
-
)
|
|
251
|
-
)
|
|
252
|
-
return CatalogSearchIndexPayload(
|
|
253
|
-
schema_version=_SEARCH_INDEX_SCHEMA_VERSION,
|
|
254
|
-
catalog_schema_version=catalog_schema_version,
|
|
255
|
-
updated_at=updated_at,
|
|
256
|
-
weights=SEARCH_WEIGHTS,
|
|
257
|
-
documents=documents,
|
|
258
|
-
)
|
|
259
|
-
|
|
260
|
-
def _score_record(
|
|
261
|
-
self,
|
|
262
|
-
indexed: _IndexedRecord,
|
|
263
|
-
query: str,
|
|
264
|
-
query_text_tokens: list[str],
|
|
265
|
-
requested_version: tuple[int, ...],
|
|
266
|
-
score_cutoff: float,
|
|
267
|
-
) -> _ScoredRecord | None:
|
|
268
|
-
field_scores = _field_scores(indexed, query, query_text_tokens)
|
|
269
|
-
if not field_scores:
|
|
270
|
-
return None
|
|
271
|
-
token_scores = _query_token_scores(query_text_tokens, indexed.all_tokens)
|
|
272
|
-
if not _passes_token_gate(
|
|
273
|
-
indexed,
|
|
274
|
-
query_text_tokens,
|
|
275
|
-
token_scores,
|
|
276
|
-
require_identity=bool(requested_version),
|
|
277
|
-
):
|
|
278
|
-
return None
|
|
279
|
-
relevance = _relevance_score(field_scores, token_scores)
|
|
280
|
-
if relevance < score_cutoff:
|
|
281
|
-
return None
|
|
282
|
-
match, matched_fields, identity_match, identity_exact = _match_summary(
|
|
283
|
-
field_scores,
|
|
284
|
-
relevance,
|
|
285
|
-
query_text_tokens,
|
|
286
|
-
indexed.identity_tokens,
|
|
287
|
-
)
|
|
288
|
-
quality = self._query_quality(
|
|
289
|
-
indexed,
|
|
290
|
-
query_text_tokens,
|
|
291
|
-
requested_version,
|
|
292
|
-
identity_match,
|
|
293
|
-
)
|
|
294
|
-
scores = CatalogSearchScores(
|
|
295
|
-
relevance=relevance,
|
|
296
|
-
**quality.model_dump(),
|
|
297
|
-
)
|
|
298
|
-
return _scored_record(
|
|
299
|
-
indexed,
|
|
300
|
-
scores,
|
|
301
|
-
match,
|
|
302
|
-
matched_fields,
|
|
303
|
-
identity_exact,
|
|
304
|
-
)
|
|
305
|
-
|
|
306
|
-
def _quality_for(self, indexed: _IndexedRecord) -> CatalogSearchQualityScores:
|
|
307
|
-
return CatalogSearchQualityScores(
|
|
308
|
-
leaderboard=_leaderboard_score(
|
|
309
|
-
indexed.record,
|
|
310
|
-
[],
|
|
311
|
-
self._reference_date,
|
|
312
|
-
),
|
|
313
|
-
benchmark=_benchmark_score(
|
|
314
|
-
indexed.record,
|
|
315
|
-
[],
|
|
316
|
-
self._reference_date,
|
|
317
|
-
),
|
|
318
|
-
version=_family_version_score(
|
|
319
|
-
indexed,
|
|
320
|
-
self._family_versions,
|
|
321
|
-
),
|
|
322
|
-
freshness=_freshness_score(
|
|
323
|
-
indexed.freshness_date,
|
|
324
|
-
self._freshness_range,
|
|
325
|
-
),
|
|
326
|
-
lifecycle=_STATUS_SCORE.get(indexed.record.status.value, _NEUTRAL_SCORE),
|
|
327
|
-
)
|
|
328
|
-
|
|
329
|
-
def _query_quality(
|
|
330
|
-
self,
|
|
331
|
-
indexed: _IndexedRecord,
|
|
332
|
-
query_text_tokens: list[str],
|
|
333
|
-
requested_version: tuple[int, ...],
|
|
334
|
-
identity_match: bool,
|
|
335
|
-
) -> CatalogSearchQualityScores:
|
|
336
|
-
base_quality = self._quality_scores[indexed.record.id]
|
|
337
|
-
if requested_version:
|
|
338
|
-
version = _requested_version_score(indexed.version, requested_version)
|
|
339
|
-
elif identity_match:
|
|
340
|
-
version = base_quality.version
|
|
341
|
-
else:
|
|
342
|
-
version = _NEUTRAL_SCORE
|
|
343
|
-
return CatalogSearchQualityScores(
|
|
344
|
-
leaderboard=_leaderboard_score(
|
|
345
|
-
indexed.record,
|
|
346
|
-
query_text_tokens,
|
|
347
|
-
self._reference_date,
|
|
348
|
-
),
|
|
349
|
-
benchmark=_benchmark_score(
|
|
350
|
-
indexed.record,
|
|
351
|
-
query_text_tokens,
|
|
352
|
-
self._reference_date,
|
|
353
|
-
),
|
|
354
|
-
version=version,
|
|
355
|
-
freshness=base_quality.freshness,
|
|
356
|
-
lifecycle=base_quality.lifecycle,
|
|
357
|
-
)
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
def search_catalog_records(
|
|
361
|
-
records: list[CatalogModelRecord],
|
|
362
|
-
query: str,
|
|
363
|
-
*,
|
|
364
|
-
limit: int | None = 50,
|
|
365
|
-
score_cutoff: float = 58,
|
|
366
|
-
) -> list[CatalogSearchResult]:
|
|
367
|
-
return CatalogSearchIndex(records).search(
|
|
368
|
-
query,
|
|
369
|
-
limit=limit,
|
|
370
|
-
score_cutoff=score_cutoff,
|
|
371
|
-
)
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
def build_catalog_search_index(catalog: ModelCatalog) -> CatalogSearchIndexPayload:
|
|
375
|
-
return CatalogSearchIndex.from_catalog(catalog).payload(
|
|
376
|
-
catalog_schema_version=catalog.schema_version,
|
|
377
|
-
updated_at=catalog.updated_at,
|
|
378
|
-
)
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
def _index_record(record: CatalogModelRecord) -> _IndexedRecord:
|
|
382
|
-
fields = tuple(
|
|
383
|
-
_IndexedField(
|
|
384
|
-
name=field.name,
|
|
385
|
-
value=normalized,
|
|
386
|
-
tokens=frozenset(_tokens(normalized)),
|
|
387
|
-
weight=field.weight,
|
|
388
|
-
identity=field.identity,
|
|
389
|
-
)
|
|
390
|
-
for field in _search_fields(record)
|
|
391
|
-
if (normalized := _normalize(field.value))
|
|
392
|
-
)
|
|
393
|
-
identity_tokens = frozenset(
|
|
394
|
-
token
|
|
395
|
-
for field in fields
|
|
396
|
-
if field.identity
|
|
397
|
-
for token in field.tokens
|
|
398
|
-
)
|
|
399
|
-
all_tokens = frozenset(token for field in fields for token in field.tokens)
|
|
400
|
-
return _IndexedRecord(
|
|
401
|
-
record=record,
|
|
402
|
-
fields=fields,
|
|
403
|
-
identity_tokens=identity_tokens,
|
|
404
|
-
all_tokens=all_tokens,
|
|
405
|
-
version=_version(record.name) or _version(record.id),
|
|
406
|
-
freshness_date=max(
|
|
407
|
-
(value for value in (record.last_updated, record.release_date) if value),
|
|
408
|
-
default=None,
|
|
409
|
-
),
|
|
410
|
-
)
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
def _field_scores(
|
|
414
|
-
indexed: _IndexedRecord,
|
|
415
|
-
query: str,
|
|
416
|
-
query_text_tokens: list[str],
|
|
417
|
-
) -> list[tuple[float, _IndexedField, SearchMatchKind]]:
|
|
418
|
-
requested_tokens = set(query_text_tokens)
|
|
419
|
-
return [
|
|
420
|
-
_field_score(field, query, requested_tokens)
|
|
421
|
-
for field in indexed.fields
|
|
422
|
-
]
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
def _field_score(
|
|
426
|
-
field: _IndexedField,
|
|
427
|
-
query: str,
|
|
428
|
-
requested_tokens: set[str],
|
|
429
|
-
) -> tuple[float, _IndexedField, SearchMatchKind]:
|
|
430
|
-
weighted = max(
|
|
431
|
-
fuzz.WRatio(query, field.value),
|
|
432
|
-
fuzz.token_set_ratio(query, field.value),
|
|
433
|
-
) * field.weight
|
|
434
|
-
if query == field.value:
|
|
435
|
-
return 100, field, "exact"
|
|
436
|
-
if query in field.value:
|
|
437
|
-
return max(weighted, 96 * field.weight), field, "token"
|
|
438
|
-
if requested_tokens and requested_tokens.issubset(field.tokens):
|
|
439
|
-
return max(weighted, 94 * field.weight), field, "token"
|
|
440
|
-
return weighted, field, "fuzzy"
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
def _query_token_scores(
|
|
444
|
-
query_tokens: list[str],
|
|
445
|
-
candidate_tokens: frozenset[str],
|
|
446
|
-
) -> list[float]:
|
|
447
|
-
return [
|
|
448
|
-
max(
|
|
449
|
-
(fuzz.ratio(token, candidate) for candidate in candidate_tokens),
|
|
450
|
-
default=0,
|
|
451
|
-
)
|
|
452
|
-
for token in query_tokens
|
|
453
|
-
]
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
def _passes_token_gate(
|
|
457
|
-
indexed: _IndexedRecord,
|
|
458
|
-
query_tokens: list[str],
|
|
459
|
-
token_scores: list[float],
|
|
460
|
-
*,
|
|
461
|
-
require_identity: bool,
|
|
462
|
-
) -> bool:
|
|
463
|
-
if any(
|
|
464
|
-
score < (80 if len(token) <= 3 else 65)
|
|
465
|
-
for token, score in zip(query_tokens, token_scores, strict=True)
|
|
466
|
-
):
|
|
467
|
-
return False
|
|
468
|
-
if not require_identity or not query_tokens:
|
|
469
|
-
return True
|
|
470
|
-
identity_scores = _query_token_scores(query_tokens, indexed.identity_tokens)
|
|
471
|
-
return min(identity_scores) >= 75
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
def _relevance_score(
|
|
475
|
-
field_scores: list[tuple[float, _IndexedField, SearchMatchKind]],
|
|
476
|
-
token_scores: list[float],
|
|
477
|
-
) -> float:
|
|
478
|
-
relevance = max(score for score, _, _ in field_scores)
|
|
479
|
-
if not token_scores:
|
|
480
|
-
return relevance
|
|
481
|
-
return max(relevance, statistics.fmean(token_scores) * 0.94)
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
def _match_summary(
|
|
485
|
-
field_scores: list[tuple[float, _IndexedField, SearchMatchKind]],
|
|
486
|
-
relevance: float,
|
|
487
|
-
query_tokens: list[str],
|
|
488
|
-
identity_tokens: frozenset[str],
|
|
489
|
-
) -> tuple[SearchMatchKind, list[str], bool, bool]:
|
|
490
|
-
leading = [item for item in field_scores if item[0] >= relevance - 8]
|
|
491
|
-
matched_fields = list(
|
|
492
|
-
dict.fromkeys(field.name for _, field, _ in leading)
|
|
493
|
-
)[:5]
|
|
494
|
-
kinds = {kind for _, _, kind in leading}
|
|
495
|
-
match: SearchMatchKind = (
|
|
496
|
-
"exact"
|
|
497
|
-
if "exact" in kinds
|
|
498
|
-
else "token"
|
|
499
|
-
if "token" in kinds
|
|
500
|
-
else "fuzzy"
|
|
501
|
-
)
|
|
502
|
-
identity_match = (
|
|
503
|
-
bool(query_tokens) and set(query_tokens).issubset(identity_tokens)
|
|
504
|
-
) or any(field.identity for _, field, _ in leading)
|
|
505
|
-
identity_exact = any(
|
|
506
|
-
kind == "exact" and field.name in {"id", "name", "alias"}
|
|
507
|
-
for _, field, kind in leading
|
|
508
|
-
)
|
|
509
|
-
return match, matched_fields, identity_match, identity_exact
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
def _scored_record(
|
|
513
|
-
indexed: _IndexedRecord,
|
|
514
|
-
scores: CatalogSearchScores,
|
|
515
|
-
match: SearchMatchKind,
|
|
516
|
-
matched_fields: list[str],
|
|
517
|
-
identity_exact: bool,
|
|
518
|
-
) -> _ScoredRecord:
|
|
519
|
-
result = CatalogSearchResult(
|
|
520
|
-
record=indexed.record,
|
|
521
|
-
score=round(_combined_score(scores), 3),
|
|
522
|
-
match=match,
|
|
523
|
-
matched_fields=matched_fields,
|
|
524
|
-
scores=CatalogSearchScores(**_rounded_values(scores)),
|
|
525
|
-
)
|
|
526
|
-
return _ScoredRecord(
|
|
527
|
-
result=result,
|
|
528
|
-
identity_exact=identity_exact,
|
|
529
|
-
version=indexed.version,
|
|
530
|
-
release_date=indexed.record.release_date,
|
|
531
|
-
)
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
def _sort_key(
|
|
535
|
-
candidate: _ScoredRecord,
|
|
536
|
-
requested_version: tuple[int, ...],
|
|
537
|
-
) -> tuple[object, ...]:
|
|
538
|
-
record = candidate.result.record
|
|
539
|
-
requested_key = (
|
|
540
|
-
_version_distance(candidate.version, requested_version)
|
|
541
|
-
if requested_version
|
|
542
|
-
else ()
|
|
543
|
-
)
|
|
544
|
-
release_ordinal = candidate.release_date.toordinal() if candidate.release_date else 0
|
|
545
|
-
return (
|
|
546
|
-
0 if candidate.identity_exact else 1,
|
|
547
|
-
*requested_key,
|
|
548
|
-
-candidate.result.score,
|
|
549
|
-
-candidate.result.scores.relevance,
|
|
550
|
-
_STATUS_PRIORITY.get(record.status.value, len(_STATUS_PRIORITY)),
|
|
551
|
-
-release_ordinal,
|
|
552
|
-
record.name.casefold(),
|
|
553
|
-
record.id.casefold(),
|
|
554
|
-
)
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
def _combined_score(scores: CatalogSearchScores) -> float:
|
|
558
|
-
return sum(
|
|
559
|
-
getattr(scores, name) * weight
|
|
560
|
-
for name, weight in SEARCH_WEIGHTS.model_dump().items()
|
|
561
|
-
)
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
def _quality_score(scores: CatalogSearchQualityScores) -> float:
|
|
565
|
-
quality_weights = SEARCH_WEIGHTS.model_dump(exclude={"relevance"})
|
|
566
|
-
total = sum(quality_weights.values())
|
|
567
|
-
return sum(
|
|
568
|
-
getattr(scores, name) * weight
|
|
569
|
-
for name, weight in quality_weights.items()
|
|
570
|
-
) / total
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
def _family_versions(
|
|
574
|
-
records: tuple[_IndexedRecord, ...],
|
|
575
|
-
) -> dict[str, tuple[tuple[int, ...], ...]]:
|
|
576
|
-
grouped: dict[str, set[tuple[int, ...]]] = {}
|
|
577
|
-
for indexed in records:
|
|
578
|
-
family = _normalize(indexed.record.family or "")
|
|
579
|
-
if family and indexed.version:
|
|
580
|
-
grouped.setdefault(family, set()).add(indexed.version)
|
|
581
|
-
return {
|
|
582
|
-
family: tuple(sorted(versions))
|
|
583
|
-
for family, versions in grouped.items()
|
|
584
|
-
}
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
def _family_version_score(
|
|
588
|
-
indexed: _IndexedRecord,
|
|
589
|
-
family_versions: dict[str, tuple[tuple[int, ...], ...]],
|
|
590
|
-
) -> float:
|
|
591
|
-
family = _normalize(indexed.record.family or "")
|
|
592
|
-
versions = family_versions.get(family, ())
|
|
593
|
-
if not indexed.version or len(versions) < 2:
|
|
594
|
-
return _NEUTRAL_SCORE
|
|
595
|
-
position = versions.index(indexed.version)
|
|
596
|
-
return 25 + 75 * position / (len(versions) - 1)
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
def _requested_version_score(
|
|
600
|
-
candidate: tuple[int, ...],
|
|
601
|
-
requested: tuple[int, ...],
|
|
602
|
-
) -> float:
|
|
603
|
-
if not candidate:
|
|
604
|
-
return 0.0
|
|
605
|
-
left = (*candidate, 0, 0, 0)[:3]
|
|
606
|
-
right = (*requested, 0, 0, 0)[:3]
|
|
607
|
-
if left[0] != right[0]:
|
|
608
|
-
return max(0.0, 40.0 - abs(left[0] - right[0]) * 15.0)
|
|
609
|
-
return max(
|
|
610
|
-
0.0,
|
|
611
|
-
100.0
|
|
612
|
-
- abs(left[1] - right[1]) * 15.0
|
|
613
|
-
- abs(left[2] - right[2]) * 5.0,
|
|
614
|
-
)
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
def _version_distance(
|
|
618
|
-
candidate: tuple[int, ...],
|
|
619
|
-
requested: tuple[int, ...],
|
|
620
|
-
) -> tuple[int, ...]:
|
|
621
|
-
if not candidate:
|
|
622
|
-
return (1, 1, 1_000, 1_000, 1_000)
|
|
623
|
-
padded_candidate = (*candidate, 0, 0, 0)[:3]
|
|
624
|
-
padded_requested = (*requested, 0, 0, 0)[:3]
|
|
625
|
-
return (
|
|
626
|
-
0,
|
|
627
|
-
0 if padded_candidate[0] == padded_requested[0] else 1,
|
|
628
|
-
abs(padded_candidate[0] - padded_requested[0]),
|
|
629
|
-
abs(padded_candidate[1] - padded_requested[1]),
|
|
630
|
-
abs(padded_candidate[2] - padded_requested[2]),
|
|
631
|
-
)
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
def _freshness_range(
|
|
635
|
-
records: tuple[_IndexedRecord, ...],
|
|
636
|
-
) -> tuple[int, int] | None:
|
|
637
|
-
ordinals = [
|
|
638
|
-
indexed.freshness_date.toordinal()
|
|
639
|
-
for indexed in records
|
|
640
|
-
if indexed.freshness_date
|
|
641
|
-
]
|
|
642
|
-
return (min(ordinals), max(ordinals)) if ordinals else None
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
def _freshness_score(
|
|
646
|
-
value: date | None,
|
|
647
|
-
observed_range: tuple[int, int] | None,
|
|
648
|
-
) -> float:
|
|
649
|
-
if value is None or observed_range is None:
|
|
650
|
-
return _NEUTRAL_SCORE
|
|
651
|
-
minimum, maximum = observed_range
|
|
652
|
-
if minimum == maximum:
|
|
653
|
-
return _NEUTRAL_SCORE
|
|
654
|
-
return 25 + 75 * (value.toordinal() - minimum) / (maximum - minimum)
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
def _reference_date(records: tuple[_IndexedRecord, ...]) -> date:
|
|
658
|
-
dates = [
|
|
659
|
-
value
|
|
660
|
-
for indexed in records
|
|
661
|
-
for value in (
|
|
662
|
-
indexed.freshness_date,
|
|
663
|
-
*(ranking.published_at for ranking in indexed.record.rankings),
|
|
664
|
-
*(benchmark.published_at for benchmark in indexed.record.benchmarks),
|
|
665
|
-
)
|
|
666
|
-
if value
|
|
667
|
-
]
|
|
668
|
-
return max(dates, default=date.today())
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
def _leaderboard_score(
|
|
672
|
-
record: CatalogModelRecord,
|
|
673
|
-
query_tokens: list[str],
|
|
674
|
-
reference_date: date,
|
|
675
|
-
) -> float:
|
|
676
|
-
rankings = list(record.rankings)
|
|
677
|
-
matched = [
|
|
678
|
-
ranking
|
|
679
|
-
for ranking in rankings
|
|
680
|
-
if _evidence_matches(query_tokens, ranking.domain, ranking.category)
|
|
681
|
-
]
|
|
682
|
-
selected = matched or [
|
|
683
|
-
ranking
|
|
684
|
-
for ranking in rankings
|
|
685
|
-
if ranking.category.casefold() == "overall"
|
|
686
|
-
]
|
|
687
|
-
if not selected:
|
|
688
|
-
return _NEUTRAL_SCORE
|
|
689
|
-
scores = [
|
|
690
|
-
_age_adjusted_score(
|
|
691
|
-
_rank_score(ranking.rank, offset=20),
|
|
692
|
-
ranking.published_at,
|
|
693
|
-
reference_date,
|
|
694
|
-
half_life_days=180,
|
|
695
|
-
confidence_count=(
|
|
696
|
-
getattr(ranking, "votes", None)
|
|
697
|
-
or getattr(ranking, "observations", None)
|
|
698
|
-
),
|
|
699
|
-
confidence_target=5_000,
|
|
700
|
-
)
|
|
701
|
-
for ranking in selected
|
|
702
|
-
]
|
|
703
|
-
return _aggregate_evidence(scores)
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
def _benchmark_score(
|
|
707
|
-
record: CatalogModelRecord,
|
|
708
|
-
query_tokens: list[str],
|
|
709
|
-
reference_date: date,
|
|
710
|
-
) -> float:
|
|
711
|
-
benchmarks = list(record.benchmarks)
|
|
712
|
-
matched = [
|
|
713
|
-
benchmark
|
|
714
|
-
for benchmark in benchmarks
|
|
715
|
-
if _evidence_matches(query_tokens, benchmark.name)
|
|
716
|
-
]
|
|
717
|
-
selected = matched or benchmarks
|
|
718
|
-
if not selected:
|
|
719
|
-
return _NEUTRAL_SCORE
|
|
720
|
-
scores = [
|
|
721
|
-
_age_adjusted_score(
|
|
722
|
-
_rank_score(benchmark.rank, offset=10),
|
|
723
|
-
benchmark.published_at,
|
|
724
|
-
reference_date,
|
|
725
|
-
half_life_days=365,
|
|
726
|
-
confidence_count=benchmark.task_count,
|
|
727
|
-
confidence_target=20,
|
|
728
|
-
)
|
|
729
|
-
for benchmark in selected
|
|
730
|
-
]
|
|
731
|
-
return _aggregate_evidence(scores)
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
def _rank_score(rank: int, *, offset: int) -> float:
|
|
735
|
-
return (offset + 1) / (offset + rank) * 100
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
def _age_adjusted_score(
|
|
739
|
-
score: float,
|
|
740
|
-
published_at: date | None,
|
|
741
|
-
reference_date: date,
|
|
742
|
-
*,
|
|
743
|
-
half_life_days: int,
|
|
744
|
-
confidence_count: int | None,
|
|
745
|
-
confidence_target: int,
|
|
746
|
-
) -> float:
|
|
747
|
-
age_days = (
|
|
748
|
-
max(0, (reference_date - published_at).days)
|
|
749
|
-
if published_at
|
|
750
|
-
else half_life_days
|
|
751
|
-
)
|
|
752
|
-
age_factor = 0.5 ** (age_days / half_life_days)
|
|
753
|
-
confidence = (
|
|
754
|
-
min(1.0, math.log1p(confidence_count) / math.log1p(confidence_target))
|
|
755
|
-
if confidence_count is not None
|
|
756
|
-
else 0.0
|
|
757
|
-
)
|
|
758
|
-
evidence_factor = age_factor * (0.75 + confidence * 0.25)
|
|
759
|
-
return _NEUTRAL_SCORE + (score - _NEUTRAL_SCORE) * evidence_factor
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
def _aggregate_evidence(scores: list[float]) -> float:
|
|
763
|
-
if not scores:
|
|
764
|
-
return _NEUTRAL_SCORE
|
|
765
|
-
aggregate = statistics.median(scores) * 0.6 + statistics.fmean(scores) * 0.4
|
|
766
|
-
coverage_factor = 0.85 + min(len(scores), 5) / 5 * 0.15
|
|
767
|
-
return _NEUTRAL_SCORE + (aggregate - _NEUTRAL_SCORE) * coverage_factor
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
def _evidence_matches(query_tokens: list[str], *values: str) -> bool:
|
|
771
|
-
if not query_tokens:
|
|
772
|
-
return False
|
|
773
|
-
evidence_tokens = set(_tokens(_normalize(" ".join(values))))
|
|
774
|
-
return bool(evidence_tokens.intersection(query_tokens))
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
def _search_fields(record: CatalogModelRecord) -> list[_SearchField]:
|
|
778
|
-
return [
|
|
779
|
-
*_base_search_fields(record),
|
|
780
|
-
*_capability_search_fields(record),
|
|
781
|
-
*_availability_search_fields(record),
|
|
782
|
-
*_notice_search_fields(record),
|
|
783
|
-
]
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
def _base_search_fields(record: CatalogModelRecord) -> list[_SearchField]:
|
|
787
|
-
return [
|
|
788
|
-
_SearchField("id", record.id, 1, True),
|
|
789
|
-
_SearchField("name", record.name, 1, True),
|
|
790
|
-
*(_SearchField("alias", alias, 1, True) for alias in record.aliases),
|
|
791
|
-
_SearchField("publisher", record.publisher or "", 0.98, True),
|
|
792
|
-
_SearchField("family", record.family or "", 0.98, True),
|
|
793
|
-
_SearchField("type", record.type.value, 0.9),
|
|
794
|
-
_SearchField("status", record.status.value, 0.86),
|
|
795
|
-
_SearchField("license", record.license or "", 0.84),
|
|
796
|
-
*(_SearchField("task", value, 0.94) for value in record.tasks),
|
|
797
|
-
*(_SearchField("use case", value, 0.94) for value in record.use_cases),
|
|
798
|
-
*(
|
|
799
|
-
_SearchField("input modality", str(value), 0.9)
|
|
800
|
-
for value in record.modalities.input
|
|
801
|
-
),
|
|
802
|
-
*(
|
|
803
|
-
_SearchField("output modality", str(value), 0.9)
|
|
804
|
-
for value in record.modalities.output
|
|
805
|
-
),
|
|
806
|
-
*(
|
|
807
|
-
_SearchField("leaderboard domain", ranking.domain, 0.92)
|
|
808
|
-
for ranking in record.rankings
|
|
809
|
-
),
|
|
810
|
-
*(
|
|
811
|
-
_SearchField("leaderboard category", ranking.category, 0.88)
|
|
812
|
-
for ranking in record.rankings
|
|
813
|
-
),
|
|
814
|
-
*(
|
|
815
|
-
_SearchField("benchmark", benchmark.name, 0.92)
|
|
816
|
-
for benchmark in record.benchmarks
|
|
817
|
-
),
|
|
818
|
-
_SearchField("description", record.description, 0.76),
|
|
819
|
-
]
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
def _capability_search_fields(record: CatalogModelRecord) -> list[_SearchField]:
|
|
823
|
-
fields = []
|
|
824
|
-
for capability, value in record.capabilities.model_dump(mode="python").items():
|
|
825
|
-
supported = value is True or (
|
|
826
|
-
isinstance(value, dict) and value.get("supported") is True
|
|
827
|
-
)
|
|
828
|
-
if supported:
|
|
829
|
-
fields.append(
|
|
830
|
-
_SearchField("capability", capability.replace("_", " "), 0.94)
|
|
831
|
-
)
|
|
832
|
-
if capability == "reasoning" and isinstance(value, dict):
|
|
833
|
-
fields.extend(
|
|
834
|
-
_SearchField("reasoning effort", f"{effort} reasoning", 0.94)
|
|
835
|
-
for effort in value.get("effort") or []
|
|
836
|
-
)
|
|
837
|
-
if value.get("toggle") is True:
|
|
838
|
-
fields.append(_SearchField("reasoning control", "reasoning toggle", 0.92))
|
|
839
|
-
if value.get("budget"):
|
|
840
|
-
fields.append(_SearchField("reasoning control", "reasoning token budget", 0.92))
|
|
841
|
-
if value.get("description"):
|
|
842
|
-
fields.append(
|
|
843
|
-
_SearchField(
|
|
844
|
-
"reasoning description", str(value["description"]), 0.8
|
|
845
|
-
)
|
|
846
|
-
)
|
|
847
|
-
return fields
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
def _availability_search_fields(record: CatalogModelRecord) -> list[_SearchField]:
|
|
851
|
-
fields = []
|
|
852
|
-
for cloud, availability in record.availability.items():
|
|
853
|
-
fields.append(_SearchField("cloud", cloud, 0.9))
|
|
854
|
-
fields.append(_SearchField("availability", str(availability.status), 0.82))
|
|
855
|
-
fields.extend(
|
|
856
|
-
_SearchField("serving mode", str(mode), 0.9)
|
|
857
|
-
for mode in availability.serving_modes
|
|
858
|
-
)
|
|
859
|
-
if availability.details:
|
|
860
|
-
fields.append(
|
|
861
|
-
_SearchField("availability details", availability.details, 0.72)
|
|
862
|
-
)
|
|
863
|
-
return fields
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
def _notice_search_fields(record: CatalogModelRecord) -> list[_SearchField]:
|
|
867
|
-
fields = []
|
|
868
|
-
for notice in record.notices:
|
|
869
|
-
fields.append(_SearchField("notice", str(notice.kind), 0.74))
|
|
870
|
-
fields.append(_SearchField("notice", notice.title or "", 0.74))
|
|
871
|
-
fields.append(_SearchField("notice", notice.text, 0.7))
|
|
872
|
-
return fields
|
|
873
|
-
|
|
874
|
-
|
|
875
|
-
def _rounded_values(
|
|
876
|
-
scores: CatalogSearchScores | CatalogSearchQualityScores,
|
|
877
|
-
) -> dict[str, float]:
|
|
878
|
-
return {
|
|
879
|
-
name: round(value, 3)
|
|
880
|
-
for name, value in scores.model_dump().items()
|
|
881
|
-
}
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
def _normalize(value: str) -> str:
|
|
885
|
-
normalized = unicodedata.normalize("NFKC", value).casefold()
|
|
886
|
-
return " ".join(_TOKEN_PATTERN.findall(normalized))
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
def _tokens(value: str) -> list[str]:
|
|
890
|
-
return _TOKEN_PATTERN.findall(value)
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
def _version(value: str) -> tuple[int, ...]:
|
|
894
|
-
match = _VERSION_PATTERN.search(value.casefold())
|
|
895
|
-
if match is None:
|
|
896
|
-
return ()
|
|
897
|
-
return tuple(int(part) for part in match.groups() if part is not None)
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
__all__ = [
|
|
901
|
-
"CatalogSearchIndex",
|
|
902
|
-
"CatalogSearchIndexDocument",
|
|
903
|
-
"CatalogSearchIndexPayload",
|
|
904
|
-
"CatalogSearchQualityScores",
|
|
905
|
-
"CatalogSearchResult",
|
|
906
|
-
"CatalogSearchScores",
|
|
907
|
-
"CatalogSearchWeights",
|
|
908
|
-
"SEARCH_WEIGHTS",
|
|
909
|
-
"SearchMatchKind",
|
|
910
|
-
"build_catalog_search_index",
|
|
911
|
-
"search_catalog_records",
|
|
912
|
-
]
|