mirror of
https://github.com/tiennm99/DocsGPT.git
synced 2026-10-14 16:16:48 +00:00
fix: chunk-budget blowups, FastEmbed built-ins, and re-embed gaps
Follow-up review pass over the embeddings branch. - Fold an oversized header back into the body, and drop header duplication when it would leave under a quarter of the chunk budget. A header at or over max_tokens collapsed the body budget to one token, so a document became one chunk per body token, each still over the cap: a 95 KB file produced 20k chunks of 2563 tokens against a 1250 cap. Also clamp max_tokens to at least 1, as the strategy chunkers already do. - Emit a header-only document as its own chunk. With no body piece to attach it to, splitting returned nothing and the document was dropped from the index with no error and no log line. - Skip add_custom_model for a repository FastEmbed already ships. It rejects a name it knows, so configuring any of its ~30 built-ins (MiniLM, bge, e5, gte, ...) failed every embed call and every query. - Decide "the user chose this model" by comparing against the field default rather than model_fields_set, which is true for anything read from .env. Every setup script has always written EMBEDDINGS_NAME, so an upgraded remote-embeddings install inherited mpnet's 384-token window and silently clipped ~80% off every chunk. - Cut tiktoken splits at character offsets instead of decoding each token window. A multi-byte character straddling a boundary decoded to U+FFFD on both sides, destroying one character at roughly one boundary in five on CJK text -- including at the default max_tokens of 2000. - Let the re-embed script open a FAISS index whose width does not match the configured model. That mismatch is the main reason to run it, and the error recommending the script was raised by the script itself, so the advice failed on every source. - Re-embed graph_nodes.name_embedding when GraphRAG is enabled. Those vectors seed every traversal and share the chunk vectors' width, so a same-width model swap left the graph retrieving from the old space with nothing to report it. - Prefetch the models before copying the application source, so editing any file no longer re-downloads ~780 MB of artifacts on every build. - Mirror the setup.sh embedding menu into setup.ps1: granite default, legacy mpnet as an explicit option, and both engine flows updated. Windows users were otherwise stranded on mpnet with no granite path. - Drop the unused EmbeddingsWrapper.tokenizer property.
This commit is contained in:
1 parent
25e07f5cee
commit
de22be5a21
16 files changed
+707
-54
No files matched your search
@@ -25,10 +25,57 @@ def fake_fastembed():
|
||||
instance = MagicMock()
|
||||
instance.embed.return_value = iter([np.array([0.1, 0.2, 0.3])])
|
||||
text_embedding.return_value = instance
|
||||
# Registration checks this before calling ``add_custom_model``; an empty
|
||||
# list means "no built-in collides", which is the case for every name in
|
||||
# our registry.
|
||||
text_embedding.list_supported_models.return_value = []
|
||||
with patch("fastembed.TextEmbedding", text_embedding):
|
||||
yield text_embedding, instance
|
||||
|
||||
|
||||
class TestBuiltinModelRegistration:
|
||||
"""FastEmbed ships ~30 models of its own and refuses to re-register any of
|
||||
them, so registering unconditionally broke every natively-supported name."""
|
||||
|
||||
def test_builtin_name_is_not_re_registered(self, fake_fastembed):
|
||||
text_embedding, _ = fake_fastembed
|
||||
text_embedding.list_supported_models.return_value = [
|
||||
{"model": "BAAI/bge-small-en-v1.5"}
|
||||
]
|
||||
EmbeddingsWrapper("BAAI/bge-small-en-v1.5")
|
||||
text_embedding.add_custom_model.assert_not_called()
|
||||
assert text_embedding.call_args.kwargs["model_name"] == "BAAI/bge-small-en-v1.5"
|
||||
|
||||
def test_builtin_match_ignores_case(self, fake_fastembed):
|
||||
text_embedding, _ = fake_fastembed
|
||||
text_embedding.list_supported_models.return_value = [
|
||||
{"model": "baai/BGE-Small-EN-v1.5"}
|
||||
]
|
||||
EmbeddingsWrapper("BAAI/bge-small-en-v1.5")
|
||||
text_embedding.add_custom_model.assert_not_called()
|
||||
|
||||
def test_unknown_name_is_still_registered(self, fake_fastembed):
|
||||
text_embedding, _ = fake_fastembed
|
||||
text_embedding.list_supported_models.return_value = [
|
||||
{"model": "BAAI/bge-small-en-v1.5"}
|
||||
]
|
||||
EmbeddingsWrapper("some-org/custom-embedder")
|
||||
text_embedding.add_custom_model.assert_called_once()
|
||||
|
||||
def test_real_fastembed_accepts_its_own_builtin(self):
|
||||
"""Runs against the installed FastEmbed, not the MagicMock.
|
||||
|
||||
The mocked tests above cannot catch this: the failure was
|
||||
``add_custom_model`` raising, and a MagicMock never raises.
|
||||
"""
|
||||
fastembed = pytest.importorskip("fastembed")
|
||||
builtins = [m["model"] for m in fastembed.TextEmbedding.list_supported_models()]
|
||||
assert builtins, "expected FastEmbed to ship built-in models"
|
||||
spec = embeddings_local._spec_for(builtins[0])
|
||||
# Must not raise ValueError("... is already registered ...").
|
||||
embeddings_local._register(spec)
|
||||
|
||||
|
||||
class TestRegistryDrivenLoading:
|
||||
def test_registered_model_loads_by_repo_not_by_configured_name(self, fake_fastembed):
|
||||
text_embedding, _ = fake_fastembed
|
||||
|
||||
@@ -150,3 +150,48 @@ class TestInputLimitResolution:
|
||||
def test_unknown_model_dimension_is_probed_not_assumed(self):
|
||||
"""The old hardcoded 768 made the probe below unreachable."""
|
||||
assert self._remote("some-org/mystery").dimension is None
|
||||
|
||||
|
||||
class TestEmbeddingsNameIsExplicit:
|
||||
"""Which names count as chosen decides whether a remote server inherits a
|
||||
context window it may not have.
|
||||
|
||||
The tests above monkeypatch the predicate, so they cannot see it being
|
||||
wrong. These drive the real one.
|
||||
"""
|
||||
|
||||
def _default(self):
|
||||
from application.core.settings import Settings
|
||||
|
||||
return Settings.model_fields["EMBEDDINGS_NAME"].default
|
||||
|
||||
def test_the_default_name_is_not_a_choice(self, monkeypatch):
|
||||
"""Every setup script has always written this value unconditionally.
|
||||
|
||||
Reading it as deliberate lends the server mpnet's 384-token window and
|
||||
clips ~80% off every chunk, silently, on upgrade. ``model_fields_set``
|
||||
could not tell the difference: pydantic marks a field set for anything
|
||||
that reached it, ``.env`` included.
|
||||
"""
|
||||
monkeypatch.setattr(base.settings, "EMBEDDINGS_NAME", self._default())
|
||||
assert base._embeddings_name_is_explicit() is False
|
||||
|
||||
def test_a_different_name_is_a_choice(self, monkeypatch):
|
||||
monkeypatch.setattr(
|
||||
base.settings,
|
||||
"EMBEDDINGS_NAME",
|
||||
"ibm-granite/granite-embedding-311m-multilingual-r2",
|
||||
)
|
||||
assert base._embeddings_name_is_explicit() is True
|
||||
|
||||
def test_dotenv_written_default_does_not_clip(self, monkeypatch):
|
||||
"""End to end: the common upgrade path must send the full chunk."""
|
||||
monkeypatch.setattr(base.settings, "EMBEDDINGS_MAX_INPUT_TOKENS", None)
|
||||
monkeypatch.setattr(base.settings, "EMBEDDINGS_NAME", self._default())
|
||||
captured = _capture_post(monkeypatch)
|
||||
|
||||
long_text = " ".join(["word"] * 1000)
|
||||
emb = RemoteEmbeddings(api_url="http://embeddings", model_name=self._default())
|
||||
emb.embed_documents([long_text])
|
||||
|
||||
assert captured["payload"]["input"][0] == long_text
|
||||
Reference in new issue
Block a user