feat(instrument): Port Ollama LLM provider adapter (M3)#106
Closed
mmercuri wants to merge 1 commit into
Closed
Conversation
Self-contained M3 fan-out PR that lands the LayerLens Ollama provider
adapter together with the minimum base infrastructure required to
host it. Sister provider PRs (OpenAI, Anthropic, Azure, Bedrock,
Vertex, LiteLLM, Cohere, Mistral) land in parallel.
Source (~259 LOC): ateam/stratix/sdk/python/adapters/llm_providers/
ollama_adapter.py.
Adapter
- src/layerlens/instrument/adapters/providers/ollama_adapter.py wraps
ollama.Client.{chat,generate,embeddings}, emits model.invoke +
cost.record events. api_cost_usd is always 0.0 (Ollama is local /
self-hosted); optional infra_cost_usd derived from
prompt_eval_duration + eval_duration when cost_per_second is set.
- providers/__init__.py exposes OllamaAdapter via lazy __getattr__ —
importing the package does NOT load the ollama SDK.
Pricing
- _base/pricing.py adds explicit zero-cost entries for the canonical
Ollama model tags (llama3.x, mistral, mixtral, phi3, qwen2.5,
gemma2, deepseek-r1, codellama, nomic-embed-text, mxbai-embed-large,
all-minilm). Comment documents that 0.0 is intentional (self-hosted)
and distinct from the hosted Bedrock/Together rates listed above.
Tests
- tests/instrument/adapters/providers/test_ollama_adapter.py — 13
respx-based HTTP-fixture tests covering the chat/generate/embeddings
paths, error path (HTTP 500 -> ResponseError + policy.violation),
cost_per_second infra-cost math, endpoint detection from OLLAMA_HOST,
lazy-import contract, and disconnect lifecycle.
- Lazy-import + default-install + resolved-dep-tree guards all green.
Sample
- samples/instrument/ollama/{__init__.py,main.py,README.md} — runnable
in mocked mode (default, respx-backed) or live mode
(LAYERLENS_OLLAMA_LIVE=1) against a real ollama serve daemon.
Sample pulls the model first, then runs a chat round-trip.
Doc
- docs/adapters/providers/ollama.md — install, quickstart, ollama
serve setup for macOS/Linux/Windows/Docker, GPU notes (CUDA / ROCm /
Metal / CPU), env-var reference.
pyproject
- providers-ollama = ["ollama>=0.2"] extra. httpx is already a core
dep so the extra surface is documented but minimal.
- Per-file ruff override for src/layerlens/instrument/adapters/
providers/**.py (ARG002 — wrapped SDK callbacks).
- pyright executionEnvironment relaxation for providers (matches the
existing cli relaxation).
Acceptance
- uv run pytest tests/instrument/adapters/providers/test_ollama_adapter.py: 13 passed
- uv run pytest tests/instrument/test_lazy_imports.py: 3 passed
- uv run pytest tests/instrument/test_default_install.py: 3 passed
- uv run mypy --strict src/layerlens/instrument/adapters/providers: clean
- uv run ruff check src/ tests/: clean
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
M3 LLM-provider fan-out — self-contained PR that lands the Ollama
adapter together with the minimum base infrastructure required to
host it.
ateam/stratix/sdk/python/adapters/llm_providers/ollama_adapter.py(~259 LOC)providers/openai_adapter.py+ flat_base/)http://localhost:11434(overridable viaOLLAMA_HOST)0.0USD/token in the canonical pricing manifest with an inline comment explaining why (self-hosted). Optionalinfra_cost_usdderived from compute duration whencost_per_secondis configured by the caller. Token counting is still required — the adapter recordsprompt_eval_count/eval_countfrom the daemon response on every call.What's in the box
src/layerlens/instrument/adapters/providers/ollama_adapter.pysrc/layerlens/instrument/adapters/providers/__init__.py(__getattr__)src/layerlens/instrument/adapters/providers/_base/pricing.pytests/instrument/adapters/providers/test_ollama_adapter.pysamples/instrument/ollama/{__init__.py,main.py,README.md}ollama servesetup + GPU notes)docs/adapters/providers/ollama.mdproviders-ollama = ["ollama>=0.2"]_compat/,instrument/_vendored/,instrument/adapters/_base/,instrument/adapters/providers/_base/Acceptance
uv run pytest tests/instrument/adapters/providers/test_ollama_adapter.py -x— 13 passeduv run pytest tests/instrument/test_lazy_imports.py— 3 passeduv run pytest tests/instrument/test_default_install.py— 3 passeduv run mypy --strict src/layerlens/instrument/adapters/providers/ollama— clean (6 source files)uv run ruff check src/ tests/— cleanTest plan
ollamais NOT loaded byimport layerlens(covered bytest_lazy_imports.py::test_layerlens_import_does_not_pull_frameworks)pip install layerlens(no extras) does not change the runtime dep set (test_default_install.py)LAYERLENS_OLLAMA_LIVE=1 python -m samples.instrument.ollama.mainagainst a localollama servedaemonapi_cost_usdis exactly0.0on every emittedcost.recordevent (self-hosted invariant)