Spaces:
Build error

File size: 3,162 Bytes
3560dd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d25c80a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3560dd8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# -*- coding: utf-8 -*-
"""rag_documents.embedding_model must name the weights that made the vectors.

2026-08-06: after migrating the corpus to bge-m3, documents still carried
`embedding_model = nomic-embed-text`. Two separate causes:

  1. The migration re-embedded every vector but never updated the label.
  2. Ingestion recorded settings.OLLAMA_EMBED_MODEL — the *local* name — even
     when the vectors came from the remote provider. In cloud-only mode the
     local name ("bge-m3") and the remote one ("baai/bge-m3") differ, and
     during the cutover they were different models entirely.

A stale label is worse than an absent one: that column is exactly what you
consult to decide whether a document needs re-embedding, so a wrong value
makes a migrated corpus look unmigrated, or hides one that genuinely is stale.
"""

import inspect

from app.services.embed import EmbedResult, EmbedService


def test_remote_results_are_tagged_with_the_remote_model():
    """EmbedResult.model is the ground truth both call sites rely on."""
    src = inspect.getsource(EmbedService._embed_remote_batch)
    assert "model=self._remote_model" in src


def test_local_results_are_tagged_with_the_local_model():
    """Every local backend tags with the local label, not just Ollama.

    _embed_local became a dispatcher when TEI was added (2026-08-06), so
    asserting on it alone would pass while a backend underneath it tagged
    vectors with the wrong model.
    """
    for backend in (EmbedService._embed_local_ollama, EmbedService._embed_tei_batch):
        src = inspect.getsource(backend)
        assert "model=self._model" in src, f"{backend.__name__} loses the local label"


def test_tei_label_matches_the_remote_label():
    """TEI and OpenRouter serve the same bge-m3 weights (cosine 0.999999 on
    identical inputs, measured 2026-08-06). Labelling them differently would
    make identical vectors read as a model change and trigger a pointless
    re-embed of the whole corpus.
    """
    from app.config import Settings

    defaults = Settings.model_fields
    assert (
        defaults["TEI_EMBED_MODEL"].default
        == defaults["EMBED_REMOTE_MODEL"].default
    )


def test_ingest_labels_from_the_result_not_the_config():
    """Ingestion must not assume the local model produced the vectors."""
    from app.services import ingest

    src = inspect.getsource(ingest)
    assert "actual_embed_model = next(" in src
    assert '"model": actual_embed_model' in src
    assert '"model": self._embed_model' not in src, "config name still used as the label"


def test_migration_relabels_documents_it_re_embeds():
    from pathlib import Path

    root = Path(inspect.getsourcefile(EmbedService)).parents[2]
    src = (root / "app_tools" / "migrate_embedding_model.py").read_text(encoding="utf-8")

    assert "UPDATE rag_documents SET embedding_model" in src
    assert "model_name = res.model" in src, "label must come from the result"
    # Only documents that actually have vectors get relabelled — anything
    # stranded keeps its old label so it still reads as needing attention.
    assert "WHERE embedding IS NOT NULL" in src