askme-public/tests/test_backends.py
Lukasz Boryn 0e0bae4b9b feat: initial public release of askme
* local question answering over analysis notebooks with LightRAG, Neo4j, Qdrant and Ollama
* RAGAS evaluation with a generator x judge comparison matrix, fully local
* docs in README and OKF
2026-11-03 17:50:52 +01:00

96 lines
3.1 KiB
Python

"""Tests for src/askme/backends.py."""
from __future__ import annotations
import asyncio
from unittest.mock import AsyncMock, patch
import pytest
from askme.backends import (
ModelSpec,
lightrag_model_func,
openai_client,
parse_spec,
release,
)
@pytest.mark.parametrize(
"spec,backend,model",
[
("qwen2.5:7b", "ollama", "qwen2.5:7b"),
("ollama:gemma3:4b", "ollama", "gemma3:4b"),
("llamacpp:gemma-4-12b-qat", "llamacpp", "gemma-4-12b-qat"),
],
)
def test_parse_spec(spec, backend, model):
"""
Scenario: one model list mixes Ollama tags and router ids
Given a spec string (bare tag, ollama: or llamacpp: prefix)
When parse_spec runs
Then backend and model are split, and the label keeps the original spec
"""
parsed = parse_spec(spec)
assert (parsed.backend, parsed.model, parsed.label) == (backend, model, spec)
def test_parse_spec_bare_tag_with_colon_stays_ollama():
"""
Scenario: Ollama tags contain colons themselves
Given "gemma2:9b" (no backend prefix)
When parse_spec runs
Then it is an Ollama model named "gemma2:9b", not backend "gemma2"
"""
assert parse_spec("gemma2:9b") == ModelSpec("ollama", "gemma2:9b", "gemma2:9b")
@pytest.mark.parametrize(
"spec,url",
[
("qwen2.5:7b", "http://localhost:11434/v1"),
("llamacpp:gemma-4-12b-qat", "http://localhost:8080/v1"),
],
)
def test_openai_client_targets_backend(spec, url):
"""
Scenario: both backends speak the OpenAI API
Given a model spec
When openai_client builds a client
Then its base_url is that backend's /v1 endpoint
"""
assert str(openai_client(parse_spec(spec)).base_url).rstrip("/") == url
def test_lightrag_model_func_binds_model_and_backend():
"""
Scenario: a generator overrides LightRAG's LLM for one query
Given llamacpp:gemma-4-12b-qat
When the returned model_func is called like LightRAG calls it
Then openai_complete_if_cache gets that model, the router base_url and the call args
"""
with patch("askme.backends.openai_complete_if_cache", new=AsyncMock(return_value="a")) as c:
func = lightrag_model_func(parse_spec("llamacpp:gemma-4-12b-qat"))
out = asyncio.run(func("prompt", keyword_extraction=True))
assert out == "a"
args, kwargs = c.call_args
assert args == ("gemma-4-12b-qat", "prompt")
assert kwargs["base_url"] == "http://localhost:8080/v1"
assert kwargs["keyword_extraction"] is True
@pytest.mark.parametrize(
"spec,target",
[("qwen2.5:7b", "unload_ollama_model"), ("llamacpp:gemma-4-12b-qat", "unload_llamacpp_model")],
)
def test_release_unloads_from_its_backend(spec, target):
"""
Scenario: free the GPU before the next model loads
Given a model spec
When release() runs
Then the matching backend's unload is awaited with the bare model name
"""
with patch(f"askme.backends.{target}", new=AsyncMock()) as unload:
asyncio.run(release(parse_spec(spec)))
unload.assert_awaited_once_with(parse_spec(spec).model)