-
Notifications
You must be signed in to change notification settings - Fork 51.9k
fix(model_metadata): respect context_length from custom_providers config with scoping #8005
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -939,6 +939,54 @@ def get_model_context_length( | |
| if config_context_length is not None and isinstance(config_context_length, int) and config_context_length > 0: | ||
| return config_context_length | ||
|
|
||
| # 0a. Config-driven overrides from custom_providers[].models[].context_length. | ||
| # When provider/base_url is known, scope the lookup to the matching custom | ||
| # provider entry to avoid collisions between identically-named models on | ||
| # different endpoints. When neither provider nor base_url is known and there | ||
| # is only one custom provider defined, allow matching that single entry. | ||
| try: | ||
| from hermes_cli.config import read_raw_config | ||
| from hermes_cli.runtime_provider import _normalize_custom_provider_name | ||
|
|
||
| _cfg = read_raw_config() | ||
| _custom_providers = _cfg.get("custom_providers", []) if isinstance(_cfg, dict) else [] | ||
| if isinstance(_custom_providers, list): | ||
| _normalized_base_url = ( | ||
| base_url.strip().rstrip("/") | ||
| if isinstance(base_url, str) and base_url.strip() | ||
| else None | ||
| ) | ||
| _active_provider = None | ||
| if isinstance(provider, str) and provider.strip(): | ||
| _ap = provider.strip() | ||
| if _ap.lower().startswith("custom:"): | ||
| _ap = _ap.split(":", 1)[1] | ||
| _active_provider = _normalize_custom_provider_name(_ap) | ||
| _valid_entries = [cp for cp in _custom_providers if isinstance(cp, dict)] | ||
| for cp in _valid_entries: | ||
| _cp_base = str(cp.get("base_url", "")).strip().rstrip("/") | ||
| _cp_name = _normalize_custom_provider_name(str(cp.get("name", ""))) | ||
|
|
||
| if _normalized_base_url is not None and _cp_base: | ||
| if _cp_base != _normalized_base_url: | ||
| continue | ||
| elif _active_provider is not None and _cp_name: | ||
| if _cp_name != _active_provider: | ||
| continue | ||
| elif len(_valid_entries) > 1: | ||
| # Ambiguous: skip unscoped entries when multiple providers exist | ||
| continue | ||
|
Comment on lines
+942
to
+978
|
||
|
|
||
| _models = cp.get("models", {}) | ||
| if isinstance(_models, dict): | ||
| _model_entry = _models.get(model, {}) | ||
| if isinstance(_model_entry, dict): | ||
| _ctx = _model_entry.get("context_length") | ||
| if isinstance(_ctx, int) and _ctx > 0: | ||
| return _ctx | ||
| except Exception: | ||
| pass | ||
|
|
||
| # Normalise provider-prefixed model names (e.g. "local:model-name" → | ||
| # "model-name") so cache lookups and server queries use the bare ID that | ||
| # local servers actually know about. Ollama "model:tag" colons are preserved. | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block calls
hermes_cli.config.load_config()on everyget_model_context_length()invocation, which does a deep-merge with defaults and reads from disk. Since this function can be called in hot paths (e.g., gateway request handling), consider using the lightweightread_raw_config()or caching the custom_providers lookup (e.g., via an internal module-level cache with a short TTL) to avoid repeated disk I/O and merges.