text_generation_serializer
Serialize text generation model data to upstream-compatible CSV and db.json.
Replicates the exact conversion logic from the upstream repository's convert.py, ensuring that synced files are byte-compatible with what convert.py would produce from the same CSV input.
The pipeline is
PRIMARY API records → reverse-convert to CSV rows → forward-convert to db.json
The forward conversion is a faithful reproduction of convert.py's algorithm, guaranteeing field ordering, default merging, and value transformations match.
TEXT_CSV_FIELDNAMES
module-attribute
TEXT_CSV_FIELDNAMES: list[str] = [
"name",
"parameters_bn",
"display_name",
"url",
"baseline",
"description",
"style",
"tags",
"instruct_format",
"settings",
]
_PRIMARY_AUTHORITATIVE_FIELDS
module-attribute
_PRIMARY_AUTHORITATIVE_FIELDS: frozenset[str] = frozenset(
{
"name",
"parameters_bn",
"display_name",
"url",
"baseline",
"description",
"style",
"tags",
"settings",
}
)
TextGenerationSyncArtifacts
dataclass
Output artifacts from text generation serialization.
Attributes:
-
csv_content(str) –The models.csv file content as a string.
-
json_content(str) –The db.json file content as a string.
Source code in src/horde_model_reference/sync/text_generation_serializer.py
TextGenerationSerializer
Serialize text generation records to upstream-compatible CSV and db.json.
Produces two coordinated outputs: a models.csv and a db.json that are byte-compatible with the upstream repository's convert.py output.
The serialization pipeline: 1. Strip backend-prefixed entries to get base records 2. Reverse-convert base records to CSV row format 3. Merge with existing CSV (preserving row order for unchanged models) 4. Forward-convert merged CSV rows to db.json (replicating convert.py)
Source code in src/horde_model_reference/sync/text_generation_serializer.py
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 | |
__init__
Initialize with bundled defaults and generation params.
serialize
serialize(
*,
primary_base_records: dict[str, LegacyRecordDict],
existing_csv_path: Path | None = None,
) -> TextGenerationSyncArtifacts
Produce models.csv and db.json from PRIMARY base records.
Parameters:
-
primary_base_records(dict[str, LegacyRecordDict]) –Model records keyed by base name (no backend prefixes).
-
existing_csv_path(Path | None, default:None) –Path to existing models.csv in the cloned repo. If provided and exists, row order of unchanged models is preserved.
Returns:
-
TextGenerationSyncArtifacts–Artifacts containing CSV and JSON file contents.
Source code in src/horde_model_reference/sync/text_generation_serializer.py
_read_existing_csv
Parse an existing models.csv file preserving row order.
Parameters:
-
csv_path(Path) –Path to the CSV file.
Returns:
Source code in src/horde_model_reference/sync/text_generation_serializer.py
_record_to_csv_row
Reverse-convert a PRIMARY API record to a CSV row dict.
Extracts only fields that belong in the CSV schema and converts types appropriately (int→str, list→comma-separated, dict→JSON).
Auto-generated values (size tag, style tag, auto display_name) are stripped so the forward conversion can regenerate them identically to convert.py.
Parameters:
-
name(str) –The base model name (dict key).
-
record(LegacyRecordDict) –The PRIMARY API record dict.
Returns:
Source code in src/horde_model_reference/sync/text_generation_serializer.py
_strip_auto_tags
Remove auto-generated tags (style + size bucket) and return CSV string.
convert.py adds the style and a size tag (e.g., "3B") automatically. To avoid duplication on round-trip, strip them before writing CSV.
Parameters:
-
tags(list[str]) –The full tag list from the record.
-
style(str | None) –The style value (added as tag by convert.py).
-
parameters(int | None) –The parameter count (used to derive size tag).
Returns:
-
str–Comma-separated string of non-auto-generated tags.
Source code in src/horde_model_reference/sync/text_generation_serializer.py
_apply_changes
_apply_changes(
*,
existing_rows: list[dict[str, str]],
primary_csv_rows: dict[str, dict[str, str]],
) -> list[dict[str, str]]
Merge PRIMARY data into existing CSV rows, preserving order and CSV-only fields.
Merge semantics: - Existing models present in PRIMARY: update field-by-field. PRIMARY non-empty values win; empty PRIMARY values fall back to existing CSV. - Existing models absent from PRIMARY: preserved (transition-period safety net). - New models in PRIMARY not in existing: appended at end.
Parameters:
-
existing_rows(list[dict[str, str]]) –Ordered list of CSV row dicts from the existing file.
-
primary_csv_rows(dict[str, dict[str, str]]) –PRIMARY records converted to CSV rows, keyed by name.
Returns:
Source code in src/horde_model_reference/sync/text_generation_serializer.py
_merge_row_fields
staticmethod
Merge a PRIMARY CSV row with an existing CSV row, field by field.
PRIMARY non-empty values overwrite existing values. Empty/missing PRIMARY values fall back to the existing CSV value, preserving CSV-only fields like instruct_format.
Parameters:
-
existing_row(dict[str, str]) –The row from the existing GitHub CSV.
-
primary_row(dict[str, str]) –The row derived from PRIMARY API data.
Returns:
Source code in src/horde_model_reference/sync/text_generation_serializer.py
_forward_convert
Convert CSV rows to db.json dict, replicating convert.py exactly.
This is a faithful reproduction of the upstream convert.py algorithm. Field ordering, default merging, empty-value stripping, and backend prefix generation all match convert.py's behavior.
Parameters:
Returns:
-
dict[str, LegacyRecordDict]–The complete db.json dict with all entries (base + backend prefixes).
Source code in src/horde_model_reference/sync/text_generation_serializer.py
_render_csv
Render CSV rows to a string matching upstream models.csv format.
Parameters:
Returns:
-
str–CSV file content as a string.
Source code in src/horde_model_reference/sync/text_generation_serializer.py
_render_json
Render db.json dict to a string matching convert.py's output format.
Uses 4-space indentation and a trailing newline, exactly as convert.py does.
Parameters:
-
db_dict(dict[str, LegacyRecordDict]) –The complete db.json dict.
Returns:
-
str–JSON file content as a string.
Source code in src/horde_model_reference/sync/text_generation_serializer.py
_format_parameters_bn
Format a parameters-in-billions value as a minimal string.
Produces the simplest representation: no trailing zeros, no unnecessary decimal point. Matches how values appear in the upstream models.csv.
Parameters:
-
value(float) –Parameter count in billions (e.g., 3.0, 0.56, 123.0).
Returns:
-
str–Minimal string representation (e.g., "3", "0.56", "123").
Examples:
>>> _format_parameters_bn(3.0)
'3'
>>> _format_parameters_bn(0.56)
'0.56'
>>> _format_parameters_bn(123.0)
'123'