ModelEngine-Group / ModelEngine-Group/nexent
Improvement: `sort_models_by_id` only sorts by the **first** character of the id
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 5.9k
- Forks
- 731
- Avg merge
- 19h 34m
- Merged PRs (30d)
- 172
Description
backend/utils/model_name_utils.py:50-65:
def sort_models_by_id(model_list: List[dict]) -> List[dict]:
if isinstance(model_list, list):
model_list.sort(
key=lambda m: str((m.get("id") if isinstance(m, dict) else m) or "")[:1].lower(),
reverse=False
)
return model_list
[:1] keeps only the leading character. Anything else is sorted in an effectively arbitrary order (Python's sort is stable, so the order falls back to insertion order). For a list like:
[{"id": "qwen-7b"}, {"id": "qwen-72b"}, {"id": "qwen-1.5"}, {"id": "deepseek-r1"}]
…you get deepseek-r1, qwen-7b, qwen-72b, qwen-1.5 — every q* model collapses to a single equivalence class and their relative order is whatever the caller passed in.
Per the docstring ("Sort model list by the first letter of id"), the current behaviour technically matches the spec, but the spec itself is what the user/UX really wants challenged: in the model-selection UI in the frontend, this means models with similar prefixes are randomly shuffled.
Suggested improvement
Sort by the entire id with a natural sort to keep numeric suffixes in human order:
import re
def _natural_key(s: str):
return [int(t) if t.isdigit() else t for t in re.split(r'(\d+)', s.lower())]
def sort_models_by_id(model_list):
if isinstance(model_list, list):
model_list.sort(key=lambda m: _natural_key(
str((m.get("id") if isinstance(m, dict) else m) or "")
))
return model_list
This gives deepseek-r1, qwen-1.5, qwen-7b, qwen-72b — predictable for users.
Category: H (improvement). Severity: Low.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in backend/utils/model_name_utils.py:50-65 and review the sort_models_by_id docstring and current key behavior. Update the ordering so complete ids use predictable natural ordering, then verify the documented deepseek and qwen example produces the stated order and preserves handling of dictionaries, strings, empty values, and non-list input.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 84/100