Files
DocsGPT/tests/vectorstore/test_model_registry.py
T
Alex 574f96341e refactor: rename the application package to docsgpt
The backend import package is now docsgpt, the name it will carry on PyPI;
application was far too generic to install into anyone's site-packages.
git mv plus a mechanical rewrite of every import, dotted string and path
reference: 734 Python files, the compose files, Dockerfile, workflows, docs,
setup scripts, devcontainer, k8s manifests, vscode config, pytest and coverage
config, .gitignore. Behaviour is unchanged.

Kept for one release:
- A top-level application package whose meta-path finder resolves
  application.x.y to the already-imported docsgpt.x.y object, so old imports
  and entry points (celery -A application.app.celery,
  uvicorn application.asgi:asgi_app) keep working with a FutureWarning.
- Celery registers every application.* task name as an alias of its
  docsgpt.* task on start-up, so messages queued by the previous release still
  run. The redbeat key prefix moves to redbeat:docsgpt:v2: so schedule entries
  the previous release wrote are left unread instead of firing twice.

The backend image builds from the repository root (docker build -f
docsgpt/Dockerfile .) so it can ship the alias package; a root .dockerignore
allow-lists docsgpt/ and application/ and keeps caches, local data, .env
files, the sample index files and the Dockerfile out. Compose and the image
workflows point at the new context.
2026-09-07 10:20:43 +01:00

118 lines
4.4 KiB
Python

"""The registry is the single source of truth for embedding-model facts."""
import os
import pytest
from docsgpt.vectorstore import model_registry as reg
class TestResolve:
def test_resolves_canonical_name(self):
assert reg.resolve(reg.MPNET.name) is reg.MPNET
@pytest.mark.parametrize(
"alias",
[
"huggingface_sentence-transformers-all-mpnet-base-v2",
"sentence-transformers/all-mpnet-base-v2",
"all-mpnet-base-v2",
],
)
def test_resolves_legacy_spellings_of_mpnet(self, alias):
"""Every spelling the old factory dict accepted must still work."""
assert reg.resolve(alias) is reg.MPNET
def test_resolution_is_case_insensitive_and_trims(self):
assert reg.resolve(" GRANITE-311M ") is reg.GRANITE_311M
def test_unknown_name_is_none_not_an_error(self):
"""Unknown names are a valid configuration: an arbitrary HF repo."""
assert reg.resolve("some-org/some-model") is None
def test_empty_and_none_resolve_to_none(self):
assert reg.resolve(None) is None
assert reg.resolve("") is None
class TestModelFacts:
def test_granite_311m_matches_the_existing_column_width(self):
"""768 is what makes granite a drop-in for an mpnet index."""
assert reg.GRANITE_311M.dimension == reg.MPNET.dimension == 768
def test_granite_97m_is_narrower(self):
assert reg.GRANITE_97M.dimension == 384
def test_pooling_and_normalisation_are_recorded(self):
assert reg.MPNET.pooling == "mean"
assert reg.GRANITE_311M.pooling == "cls"
assert all(m.normalize for m in reg.MODELS)
def test_granite_context_is_much_wider_than_mpnet(self):
assert reg.GRANITE_311M.max_input_tokens == 32768
assert reg.MPNET.max_input_tokens == 384
def test_local_runners_carry_a_repo_and_onnx_file(self):
for model in reg.MODELS:
if model.provider == "fastembed":
assert model.repo, f"{model.name} has no repo"
assert model.onnx_file, f"{model.name} has no onnx_file"
def test_openai_model_needs_no_local_artifacts(self):
assert reg.OPENAI_ADA_002.provider == "openai"
assert reg.OPENAI_ADA_002.repo is None
class TestHelpers:
def test_dimension_for_known_and_unknown(self):
assert reg.dimension_for(reg.GRANITE_97M.name) == 384
assert reg.dimension_for("nope/nope") is None
def test_max_input_tokens_for_known_and_unknown(self):
assert reg.max_input_tokens_for("granite-311m") == 32768
assert reg.max_input_tokens_for("nope/nope") is None
def test_known_names_lists_canonical_spellings(self):
names = reg.known_names()
assert reg.MPNET.name in names
assert reg.GRANITE_311M.name in names
def test_defaults_point_at_the_intended_models(self):
"""Existing installs stay on mpnet; new installs get granite."""
assert reg.DEFAULT_LEGACY == reg.MPNET.name
assert reg.DEFAULT_NEW_INSTALL == reg.GRANITE_311M.name
def test_no_alias_collisions_between_models(self):
seen = {}
for model in reg.MODELS:
for key in (model.name, *model.aliases):
assert key.lower() not in seen, f"{key} claimed twice"
seen[key.lower()] = model
class TestRegistryMatchesTheHub:
"""Every registry entry restates facts the model's repository already holds.
Restating them is what makes an offline install work, but a restatement can
drift from its source and nothing else would notice: wrong pooling is not a
crash, only worse retrieval. Opt in with ``DOCSGPT_HUB_TESTS=1``; needs
network.
"""
@pytest.mark.skipif(
os.environ.get("DOCSGPT_HUB_TESTS") != "1",
reason="set DOCSGPT_HUB_TESTS=1 to check the registry against the hub",
)
@pytest.mark.parametrize(
"model", [m for m in reg.MODELS if m.provider == "fastembed"]
)
def test_entry_matches_repository_metadata(self, model):
from docsgpt.vectorstore.embeddings_local import _describe_from_repo
described = _describe_from_repo(model.repo)
assert described is not None, f"{model.repo} declares no pooling metadata"
assert described.pooling == model.pooling
assert described.normalize == model.normalize
if described.dimension:
assert described.dimension == model.dimension