feat: add context_length to /v1/models response

- Add get_jean_context_lengths() to fetch n_ctx from Jean /v1/models
- Add preset_ctx_map with known CTX values for each preset
- Add context_length to Jean model metadata in /v1/models
- Add context_length to Ollama model metadata from details.context_length
- This allows OpenFox to know the max tokens each model can handle
This commit is contained in:
Folzo
2026-07-20 12:05:48 +02:00
parent e9cd1edbff
commit afd340d8a4
+40
View File
@@ -38,6 +38,7 @@ app = Flask(__name__)
presets_cache = {"data": [], "timestamp": 0} presets_cache = {"data": [], "timestamp": 0}
ollama_cache = {"data": [], "timestamp": 0} ollama_cache = {"data": [], "timestamp": 0}
jean_ctx_cache = {}
CACHE_TTL = 30 CACHE_TTL = 30
switch_lock = threading.Lock() switch_lock = threading.Lock()
vram_lock = threading.Lock() vram_lock = threading.Lock()
@@ -73,6 +74,38 @@ def get_ollama_models(force_refresh=False):
if not force_refresh and ollama_cache["data"] and (now - ollama_cache["timestamp"]) < CACHE_TTL: if not force_refresh and ollama_cache["data"] and (now - ollama_cache["timestamp"]) < CACHE_TTL:
return ollama_cache["data"] return ollama_cache["data"]
def get_jean_context_lengths():
"""Récupère les context_length depuis Jean /v1/models (avec cache)"""
preset_ctx_map = {
"ornith-35b-q6": 64000,
"qwen3.6-27b-mtp": 55000,
"qwen3.6-35b-moe-compact": 131072,
"qwen3.6-35b-moe-compact-reason": 131072,
"qwen3.6-35b-moe-nano": 100000,
}
try:
url = f"http://{JEAN_PROXY_HOST}:{JEAN_PROXY_PORT}/v1/models"
r = requests.get(url, timeout=2)
if r.ok:
models = r.json().get("data", [])
for m in models:
meta = m.get("meta", {})
n_ctx = meta.get("n_ctx")
if n_ctx:
alias = m.get("id", "")
jean_ctx_cache[alias] = n_ctx
log.debug(f"[Jean] Context length pour {alias}: {n_ctx}")
except Exception as e:
log.debug(f"[Jean] Impossible de récupérer context_length: {e}")
for preset_id, ctx in preset_ctx_map.items():
if preset_id not in jean_ctx_cache:
jean_ctx_cache[preset_id] = ctx
return jean_ctx_cache
try: try:
url = f"http://{OLLAMA_PROXY_HOST}:{OLLAMA_PROXY_PORT}/api/tags" url = f"http://{OLLAMA_PROXY_HOST}:{OLLAMA_PROXY_PORT}/api/tags"
r = requests.get(url, timeout=3) r = requests.get(url, timeout=3)
@@ -316,6 +349,7 @@ def ensure_vram_for_backend(target_backend: str) -> bool:
def list_models(): def list_models():
"""Expose tous les presets Jean + modèles Ollama comme modèles OpenAI""" """Expose tous les presets Jean + modèles Ollama comme modèles OpenAI"""
presets = get_presets() presets = get_presets()
jean_ctx = get_jean_context_lengths()
models = [] models = []
for p in presets: for p in presets:
@@ -342,6 +376,10 @@ def list_models():
if p.get("bench"): if p.get("bench"):
meta["bench"] = p["bench"] meta["bench"] = p["bench"]
ctx_len = jean_ctx.get(model_id)
if ctx_len:
meta["context_length"] = ctx_len
if meta: if meta:
model_info["meta"] = meta model_info["meta"] = meta
@@ -372,6 +410,8 @@ def list_models():
meta["quantization"] = details["quantization_level"] meta["quantization"] = details["quantization_level"]
if details.get("family"): if details.get("family"):
meta["family"] = details["family"] meta["family"] = details["family"]
if details.get("context_length"):
meta["context_length"] = details["context_length"]
if m.get("capabilities"): if m.get("capabilities"):
meta["capabilities"] = m["capabilities"] meta["capabilities"] = m["capabilities"]