feat(processor): make Ollama model + request timeout configurable - #43
feat(processor): make Ollama model + request timeout configurable#43agu2347 wants to merge 1 commit into
Conversation
The Ollama model was hardcoded in run_ai_analysis() (_ollama_client.chat(model='llama3.2:1b', ...)) even though OLLAMA_HOST was already configurable, so swapping models required a code change. Add OLLAMA_MODEL (default llama3.2:1b, unchanged) and OLLAMA_REQUEST_TIMEOUT (default 30s) env vars, following the same os.getenv() pattern already used for OLLAMA_HOST. The timeout is wired into the ollama.Client constructor (forwarded to the underlying httpx client) so a hung Ollama instance can't stall anomaly processing indefinitely. Documented in .env.example, docker-compose.yml, and k8s/06-processing-service.yml. Added two tests: one asserting the chat() call uses OLLAMA_MODEL instead of a literal, and one proving a reconfigured OLLAMA_MODEL changes the model used with no code change (via monkeypatch.setattr on the already-imported module, since importlib.reload would re-execute the module's prometheus_client.Counter/Histogram registrations and raise a duplicate-timeseries error). Closes Uday9909#31
|
Hey @agu2347! 🌟 Welcome to Sentinel — and thank you so much for jumping in with Here's what happens next:
In the meantime, if you have questions about your PR or the project, just reply here. We're around! Thanks again for being part of this. 🙌 |
Uday9909
left a comment
There was a problem hiding this comment.
Yo this is a clean one. I verified the ollama.Client timeout kwarg actually exists in the pinned ollama 0.4.7, the env vars are documented in all three places (.env.example, docker-compose, k8s manifest), and CI is green. Fully closes #31.
One thing. test_ollama_model_env_var_overrides_default in test_processor.py does not actually test the env var. It monkeypatches processor.OLLAMA_MODEL directly, so the os.getenv line in processor.py is never exercised. If someone replaced os.getenv with the hardcoded literal, that test would still pass. Either rename it to something like test_run_ai_analysis_uses_module_ollama_model, or actually set the env var and reload the module. You already found reload blows up on the duplicate prometheus registrations, so renaming is the honest move unless you split the os.getenv read into a tiny helper worth testing.
Small nit: OLLAMA_REQUEST_TIMEOUT does float(os.getenv(...)) at import, so a non-numeric value in the env crashes the whole processor before it starts. Not blocking, but a try/except with a log would make that failure obvious instead of a cryptic traceback.
|
@agu2347 another thing before opening a pr on an issue , please first get it assigned to yourself! |
Problem
The Ollama model was hardcoded in
processing-service/processor.py:126:OLLAMA_HOSTwas already configurable via env var, but the model and any client options were not - changing the model required editing source.Fix
OLLAMA_MODEL(defaultllama3.2:1b, unchanged) andOLLAMA_REQUEST_TIMEOUT(default30seconds) env vars, following the exactos.getenv()pattern already used forOLLAMA_HOST._ollama_client.chat(...)now usesmodel=OLLAMA_MODELinstead of the literal.OLLAMA_REQUEST_TIMEOUTis passed toollama.Client(host=OLLAMA_HOST, timeout=OLLAMA_REQUEST_TIMEOUT). Theollamaclient (0.4.7, as pinned inrequirements.txt) forwards**kwargsstraight to its underlyinghttpx.Client, which does accepttimeout- verified directly against the installed package's constructor signature, not assumed..env.example,docker-compose.yml, andk8s/06-processing-service.yml, next to the existingOLLAMA_HOSTentries.Tests
Added two tests to
processing-service/test_processor.py:test_run_ai_analysis_uses_configured_model- asserts thechat()call receivesmodel=OLLAMA_MODEL, not a literal.test_ollama_model_env_var_overrides_default- asserts the default is unchanged (llama3.2:1b), then reconfigures the model and assertsrun_ai_analysispicks up the new value with no code change.Note on the second test: I initially wrote it with
importlib.reload(processor)to simulate a fresh process picking up a changedOLLAMA_MODELenv var, but that reruns every module-level statement, including theprometheus_client.Counter/Histogramregistrations - which raisesValueError: Duplicated timeseries in CollectorRegistryon the second import. Switched tomonkeypatch.setattr(processor, "OLLAMA_MODEL", ...)on the already-imported module instead, which exercises the same code path inrun_ai_analysis(a plain module-global lookup) without the reload side effect, and auto-reverts after the test.Verification
pytest processing-service/test_processor.py -v- 25 passed (23 pre-existing + 2 new), run against the pinned dependency versions fromrequirements.txt(kafka-python 2.0.6, elasticsearch 8.17.0, drain3 0.9.11, scikit-learn 1.6.1, prometheus-client 0.21.1, ollama 0.4.7, joblib 1.4.2, regex 2024.11.6).ruff checkagainst the project's actualpyproject.tomlconfig (select = ["E","W","F","I","N"], line-length 100) - clean on both changed files.docker-compose.ymlandk8s/06-processing-service.ymlparse as valid YAML (yaml.safe_load_all), and I checkedtests/integration/test_k8s_manifests.py- its structural checks (apiVersion/kind/metadata/resource requests) are unaffected by adding two moreenv:entries.Acceptance criteria (from the issue)
OLLAMA_MODELchanges the model used without code changesllama3.2:1b).env.example/ k8s / docker-compose updatedCloses #31