Ollama: Five Python Scripts to Automate Local Tasks

Five Python scripts automate tasks around Ollama, from document assembly to model testing. Each script generates an inspectable file to track inputs, responses, or failures. However, a server address on the machine alone does not guarantee local computation.
A Log for Resuming Interrupted Batches
For interrupted processing, the chosen mechanism is a JSONL log: each call must be followed by a JSON record on a separate line. Upon restart, entries marked "ok" should allow for the recognition of tasks that have already succeeded. A line that cannot be decoded, particularly if a write was interrupted, must be counted rather than masked.
The identifier of a task combines the prompt, its version, the model's fingerprint, options, and the schema value "none." It corresponds to the first 16 characters of a SHA-256 fingerprint; the initial version of the prompt is "v1." The batch settings are temperature set to 0 and num_predict set to 200.
The log opens in append mode, with UTF-8 encoding. The processing includes separate counters for ignored items, successes, errors, and unreadable lines. An optional limit should also cap the number of calls.
PDFs Require Separate Preparation
Document collection remains limited to plain text: PDFs are not opened. When a document contains only images, its text must first be extracted, checked, and then exported in UTF-8. Even a PDF with searchable text requires this explicit export.
The collector retains only .txt and .md extensions, in a sorted and reproducible order. It excludes hidden files, symbolic links, and paths resolved outside the root directory. The pathlib library handles most path operations.
The default ceilings are 20,000 bytes for each file and 60,000 bytes for the entire collection. An overflow or a different encoding than UTF-8 triggers an error, without silently reducing the content. These thresholds aim for readable text for inspection, not a token measurement: even 60 KB can exceed a model's context capacity.
Fingerprints for Documents and Models
The file pack.txt gathers texts in blocks accompanied by their source path and a SHA-256 fingerprint. A separate manifest, manifest.json, retains paths, sizes, full fingerprints, and the total volume.
The AI brief the pros read every evening
The 7 best AI stories, the essentials in 5 min. Free.
Included when you join: our pick of the best AI guides & comparisons.
Choose your rhythm
Free · No spam · Unsubscribe in 1 click
The collection demonstration displays 4 files totaling 552 bytes. It details ideas.md, with 155 bytes and the short fingerprint e2d8ca426543, followed by reminder.txt, with 90 bytes and 7efa15e039a7. The other entries are retro-october.txt, with 178 bytes and 78c27d8e792b, and standup-2026-11-03.md, with 129 bytes and 7b41af11368b. The hidden file .hidden-note.md is not included in this demonstration.
Model tracking also relies on a fingerprint: model_digest records the identity of its content. A name like qwen3.5:4b can indeed refer to different weights after a new download.
Disabling the Cloud and Configuring the Local Server
Ollama can route remotely hosted models through its local server: thus, a localhost address does not guarantee on-site computation. The procedure indicated by Ollama involves setting OLLAMA_NO_CLOUD=1 in the daemon's environment and then restarting it. A variable defined only in the Python process does not change the already active server.
The scripts share a utility and the server present on the machine, without additional orchestration; their command interfaces use argparse. The default address is http://127.0.0.1:11434, which can be replaced by LOCAL_LLM_URL. The urllib configuration excludes passing through a proxy for the local loop.
With stream=False, each request waits for a complete JSON object, rather than a succession of fragments. LOCAL_LLM_TIMEOUT sets the maximum wait time, defaulting to 120 seconds, for a blocked server to result in a failed request. The LocalLLMError exception is intended to group HTTP, network, system, timeout, or JSON decoding errors, as well as those reported by the server.
The "off" value of LOCAL_LLM_THINK is intended to disable reasoning. The parameter think: false prevents the affected models from consuming the short response budget reserved for testing the fifth script.
Validation, Search, and Model Testing
Beyond document grouping and batch processing, the entire setup includes the validation of extracted data, note searching, and evaluating a change in model or prompt. For extractions, the announced deliverables are a CSV of accepted data and a JSONL of rejections. The search should produce a local index of vector representations of the same identity, while the tests should leave a CSV associating results and durations.
The configuration given for demonstration outputs associates qwen3.5:4b and nomic-embed-text, with Ollama 0.40.13 and Python 3.13. These executions are presented as having been performed without resorting to a hosted service.
Brief AI — AI news in French
The essentials of artificial intelligence news, decoded and explained every day.