2.8 KiB
2.8 KiB
grok2api Model Patching Pattern
When adding a new Grok model to the grok2api codebase, 6 files across 3 layers must be updated.
Files to Patch
Layer 1: Control Plane — Model Registry
app/control/model/enums.py
- Add new
ModeIdenum value (e.g.,GROK_4_6 = 5) - Add mapping in
to_api_str()method - Add to
ALL_MODES_FULLtuple
app/control/model/registry.py
- Add
ModelSpecentries for the new model and its variants - Example pattern:
ModelSpec("grok-4.6", ModeId.GROK_4_6, Tier.SUPER, Capability.CHAT, True, "Grok 4.6"),
ModelSpec("grok-4.6-super", ModeId.GROK_4_6, Tier.SUPER, Capability.CHAT, True, "Grok 4.6 Super"),
ModelSpec("grok-4.6-heavy", ModeId.GROK_4_6, Tier.HEAVY, Capability.CHAT, True, "Grok 4.6 Heavy"),
ModelSpec("grok-4.6-best", ModeId.GROK_4_6, Tier.SUPER, Capability.CHAT, True, "Grok 4.6 Best", prefer_best=True),
Layer 2: Control Plane — Account Models
app/control/account/models.py
- Add
grok_4_6: QuotaWindow | None = NonetoAccountQuotaSet - Add
mode_id == 5case inget()method - Add
mode_id == 5case inset()method - Add
grok_4_6toto_dict()andfrom_dict()
app/control/account/commands.py
- Add
quota_grok_4_6: dict[str, Any] | None = NonetoAccountPatch
Layer 3: Data Plane — Runtime Tables
app/dataplane/shared/enums.py
- Add
GROK_4_6 = 5toModeId - Add to
ALL_MODE_IDStuple
app/dataplane/account/table.py
- Add
quota_grok_4_6_by_idx,total_grok_4_6_by_idx,window_grok_4_6_by_idx,reset_grok_4_6_at_by_idxcolumn arrays - Update
_quota_col(),_reset_col(),_total_col(),_window_col()to handlemode_id == 5 - Update
_append_slot()signature and body withgrok_4_6parameters - Update
_update_slot()signature and body withgrok_4_6parameters
Verification
After patching, run syntax check:
cd C:\Users\User\grok2api\grok2api
python -c "
import ast, sys
for f in ['app/control/model/enums.py','app/control/model/registry.py',
'app/control/account/models.py','app/control/account/commands.py',
'app/dataplane/shared/enums.py','app/dataplane/account/table.py']:
ast.parse(open(f).read()); print(f'OK: {f}')
"
Pitfalls
- Columnar arrays: the table.py uses typed
array.arraycolumns. Each new mode needs 4 new columns (quota, total, window, reset). The_append_slotand_update_slotmethods have long parameter lists that must match exactly. - Mode ID consistency: the control-plane
ModeIdand data-planeModeIdmust have the same integer values. The data-plane enum is a separate copy indataplane/shared/enums.py. - The protocol layer (
xai_chat.py) usesmode_id.to_api_str()generically — no changes needed there if the mode_id mapping is correct.