parse ollama /api/show parameters string
This commit is contained in:
1 parent
71abc0f762
commit
3661443184
2 files changed
+42
-1
No files matched your search
+14
-1
@@ -166,10 +166,23 @@ class CodegenClient:
|
|||||||
data = json.loads(response.read().decode("utf-8"))
|
data = json.loads(response.read().decode("utf-8"))
|
||||||
except (urllib.error.URLError, TimeoutError, ValueError, KeyError):
|
except (urllib.error.URLError, TimeoutError, ValueError, KeyError):
|
||||||
return None
|
return None
|
||||||
params = data.get("parameters") or {}
|
params = data.get("parameters")
|
||||||
|
if isinstance(params, dict):
|
||||||
num_ctx = params.get("num_ctx")
|
num_ctx = params.get("num_ctx")
|
||||||
if isinstance(num_ctx, int) and num_ctx > 0:
|
if isinstance(num_ctx, int) and num_ctx > 0:
|
||||||
return num_ctx
|
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 {}
|
model_info = data.get("model_info") or {}
|
||||||
for key, value in model_info.items():
|
for key, value in model_info.items():
|
||||||
if key.endswith(".context_length") and isinstance(value, int) and value > 0:
|
if key.endswith(".context_length") and isinstance(value, int) and value > 0:
|
||||||
|
|||||||
@@ -715,6 +715,34 @@ def test_context_window_from_num_ctx():
|
|||||||
httpd.server_close()
|
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():
|
def test_context_window_from_model_info():
|
||||||
httpd, base = _show_server({"model_info": {"qwen35.context_length": 262144}})
|
httpd, base = _show_server({"model_info": {"qwen35.context_length": 262144}})
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in new issue
Block a user