Skip to content

feat(processor): make Ollama model + request timeout configurable - #43

Open
agu2347 wants to merge 1 commit into
Uday9909:mainfrom
agu2347:feat/configurable-ollama-model-31
Open

feat(processor): make Ollama model + request timeout configurable#43
agu2347 wants to merge 1 commit into
Uday9909:mainfrom
agu2347:feat/configurable-ollama-model-31

Conversation

@agu2347

@agu2347 agu2347 commented Aug 10, 2026

Copy link
Copy Markdown

Problem

The Ollama model was hardcoded in processing-service/processor.py:126:

response = _ollama_client.chat(model='llama3.2:1b', messages=[...])

OLLAMA_HOST was already configurable via env var, but the model and any client options were not - changing the model required editing source.

Fix

  • Added OLLAMA_MODEL (default llama3.2:1b, unchanged) and OLLAMA_REQUEST_TIMEOUT (default 30 seconds) env vars, following the exact os.getenv() pattern already used for OLLAMA_HOST.
  • _ollama_client.chat(...) now uses model=OLLAMA_MODEL instead of the literal.
  • OLLAMA_REQUEST_TIMEOUT is passed to ollama.Client(host=OLLAMA_HOST, timeout=OLLAMA_REQUEST_TIMEOUT). The ollama client (0.4.7, as pinned in requirements.txt) forwards **kwargs straight to its underlying httpx.Client, which does accept timeout - verified directly against the installed package's constructor signature, not assumed.
  • Documented both vars in .env.example, docker-compose.yml, and k8s/06-processing-service.yml, next to the existing OLLAMA_HOST entries.

Tests

Added two tests to processing-service/test_processor.py:

  • test_run_ai_analysis_uses_configured_model - asserts the chat() call receives model=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 asserts run_ai_analysis picks 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 changed OLLAMA_MODEL env var, but that reruns every module-level statement, including the prometheus_client.Counter/Histogram registrations - which raises ValueError: Duplicated timeseries in CollectorRegistry on the second import. Switched to monkeypatch.setattr(processor, "OLLAMA_MODEL", ...) on the already-imported module instead, which exercises the same code path in run_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 from requirements.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 check against the project's actual pyproject.toml config (select = ["E","W","F","I","N"], line-length 100) - clean on both changed files.
  • docker-compose.yml and k8s/06-processing-service.yml parse as valid YAML (yaml.safe_load_all), and I checked tests/integration/test_k8s_manifests.py - its structural checks (apiVersion/kind/metadata/resource requests) are unaffected by adding two more env: entries.

Acceptance criteria (from the issue)

  • Setting OLLAMA_MODEL changes the model used without code changes
  • Default unchanged (llama3.2:1b)
  • .env.example / k8s / docker-compose updated

Closes #31

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
@agu2347
agu2347 requested a review from Uday9909 as a code owner August 10, 2026 10:08
@github-actions

Copy link
Copy Markdown
Contributor

Hey @agu2347! 🌟

Welcome to Sentinel — and thank you so much for jumping in with feat(processor): make Ollama model + request timeout configurable! We genuinely appreciate you taking the time to contribute.

Here's what happens next:

  • Someone on the team will review your changes in the next day or two
  • If anything needs tweaking, we'll let you know — no pressure, we're happy to help
  • Once it looks good, we'll get it merged in

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 Uday9909 left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Uday9909

Copy link
Copy Markdown
Owner

@agu2347 another thing before opening a pr on an issue , please first get it assigned to yourself!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(processor): make Ollama model + client configurable

2 participants