Make Ollama context size and threads configurable
build / build (push) Successful in 3m23s

This commit is contained in:
ml
2026-10-04 12:45:54 +02:00
parent 7a91a37049
commit 8980f9599b
3 changed files with 12 additions and 3 deletions
+2 -1
View File
@@ -60,7 +60,8 @@ implausibly - is `12_adaptive_preprocess_ocr.sh` below.
steps are short-lived and stateless (one `docker run` per receipt). steps are short-lived and stateless (one `docker run` per receipt).
`3_extract_data.sh` reaches it via `--network host` at `3_extract_data.sh` reaches it via `--network host` at
`http://127.0.0.1:11434` by default (override with `OLLAMA_URL`/ `http://127.0.0.1:11434` by default (override with `OLLAMA_URL`/
`OLLAMA_MODEL`). `OLLAMA_MODEL`; `OLLAMA_NUM_CTX` (default 3072) and `OLLAMA_NUM_THREAD`
(default: Ollama's) set the request options).
- `build-image.sh` / `upload-image.sh` - build/push the - `build-image.sh` / `upload-image.sh` - build/push the
`tooling-container/` image, tagged from `tooling-container/VERSION`. `tooling-container/` image, tagged from `tooling-container/VERSION`.
Image name defaults to `fuelish/tools`, overridable via Image name defaults to `fuelish/tools`, overridable via
+1 -1
View File
@@ -1 +1 @@
0.1.0 0.1.1
+9 -1
View File
@@ -10,6 +10,11 @@ import requests
OLLAMA_URL = os.environ.get("OLLAMA_URL", "http://127.0.0.1:11434/api/chat") OLLAMA_URL = os.environ.get("OLLAMA_URL", "http://127.0.0.1:11434/api/chat")
MODEL = os.environ.get("OLLAMA_MODEL", "qwen2.5:3b") MODEL = os.environ.get("OLLAMA_MODEL", "qwen2.5:3b")
# Ollama restarts its runner when a request's context size or thread count
# differs from the loaded one; set these to match other clients of a shared
# Ollama. OLLAMA_NUM_THREAD unset = Ollama's default.
NUM_CTX = int(os.environ.get("OLLAMA_NUM_CTX", "3072"))
NUM_THREAD = os.environ.get("OLLAMA_NUM_THREAD")
SCHEMA = { SCHEMA = {
"type": "object", "type": "object",
@@ -57,6 +62,9 @@ Respond with ONLY the JSON object, no other text.
def structure(ocr_text: str) -> dict: def structure(ocr_text: str) -> dict:
options = {"temperature": 0, "num_ctx": NUM_CTX}
if NUM_THREAD:
options["num_thread"] = int(NUM_THREAD)
payload = { payload = {
"model": MODEL, "model": MODEL,
"messages": [ "messages": [
@@ -64,7 +72,7 @@ def structure(ocr_text: str) -> dict:
], ],
"format": SCHEMA, "format": SCHEMA,
"stream": False, "stream": False,
"options": {"temperature": 0, "num_ctx": 3072}, "options": options,
} }
start = time.time() start = time.time()