parse ollama /api/show parameters string

This commit is contained in:
Denton Social committed 2026-09-24 18:38:42 -05:00
1 parent 71abc0f762
commit 3661443184
2 files changed
+45 -4

No files matched your search

+17 -4
View File
@@ -166,10 +166,23 @@ class CodegenClient:
data = json.loads(response.read().decode("utf-8"))
except (urllib.error.URLError, TimeoutError, ValueError, KeyError):
return None
params = data.get("parameters") or {}
num_ctx = params.get("num_ctx")
if isinstance(num_ctx, int) and num_ctx > 0:
return num_ctx
params = data.get("parameters")
if isinstance(params, dict):
num_ctx = params.get("num_ctx")
if isinstance(num_ctx, int) and num_ctx > 0:
return num_ctx
elif isinstance(params, str):
# ollama serves parameters as modelfile text, not a dict:
# "num_ctx 100000\n..." — pull the num_ctx line if present.
for line in params.splitlines():
parts = line.split()
if len(parts) == 2 and parts[0] == "num_ctx":
try:
num_ctx = int(parts[1])
except ValueError:
break
if num_ctx > 0:
return num_ctx
model_info = data.get("model_info") or {}
for key, value in model_info.items():
if key.endswith(".context_length") and isinstance(value, int) and value > 0:
+28
View File
@@ -715,6 +715,34 @@ def test_context_window_from_num_ctx():
httpd.server_close()
def test_context_window_parses_parameters_string():
"""Real ollama serves `parameters` as modelfile text, not a dict."""
httpd, base = _show_server(
{"parameters": 'num_ctx 100000\nstop "<|end_of_text|>"\ntemperature 0.0'}
)
try:
client = CodegenClient(base_url=base, model="test")
assert client._context_window() == 100000
finally:
httpd.shutdown()
httpd.server_close()
def test_context_window_string_parameters_falls_back_to_model_info():
httpd, base = _show_server(
{
"parameters": 'stop "<|end_of_text|>"',
"model_info": {"qwen35.context_length": 262144},
}
)
try:
client = CodegenClient(base_url=base, model="test")
assert client._context_window() == 262144
finally:
httpd.shutdown()
httpd.server_close()
def test_context_window_from_model_info():
httpd, base = _show_server({"model_info": {"qwen35.context_length": 262144}})
try: