build-small-hackathon/hackathon-advisor
16
1from pathlib import Path2import sys3from types import ModuleType4 5from hackathon_advisor.data import DEFAULT_EMBEDDING_MODEL_FILE, DEFAULT_EMBEDDING_MODEL_REPO6from hackathon_advisor.llama_embedding import (7 DEFAULT_N_CTX,8 LlamaCppEmbedder,9 SubprocessLlamaCppEmbedder,10 create_llama_cpp_embedder,11)12 13 14def test_llama_embedder_uses_q8_defaults_and_configured_context(15 monkeypatch,16 tmp_path: Path,17) -> None:18 model_path = tmp_path / "embedding.gguf"19 model_path.write_bytes(b"gguf")20 captured: dict = {}21 22 hub = ModuleType("huggingface_hub")23 24 def fake_hf_hub_download(repo_id: str, filename: str, repo_type: str) -> str:25 captured["download"] = {26 "repo_id": repo_id,27 "filename": filename,28 "repo_type": repo_type,29 }30 return str(model_path)31 32 hub.hf_hub_download = fake_hf_hub_download33 llama_cpp = ModuleType("llama_cpp")34 llama_cpp.LLAMA_POOLING_TYPE_MEAN = 135 36 class FakeLlama:37 def __init__(self, **kwargs) -> None:38 captured["llama_kwargs"] = kwargs39 40 def embed(self, text: str, normalize: bool) -> list[float]:41 captured["embed"] = {"text": text, "normalize": normalize}42 return [1.0, 0.0]43 44 llama_cpp.Llama = FakeLlama45 monkeypatch.setitem(sys.modules, "huggingface_hub", hub)46 monkeypatch.setitem(sys.modules, "llama_cpp", llama_cpp)47 48 vector = LlamaCppEmbedder().embed("private archive")49 50 assert vector == [1.0, 0.0]51 assert captured["download"] == {52 "repo_id": DEFAULT_EMBEDDING_MODEL_REPO,53 "filename": DEFAULT_EMBEDDING_MODEL_FILE,54 "repo_type": "model",55 }56 assert captured["llama_kwargs"]["n_ctx"] == DEFAULT_N_CTX57 assert captured["llama_kwargs"]["n_batch"] == DEFAULT_N_CTX58 assert captured["llama_kwargs"]["n_ubatch"] == DEFAULT_N_CTX59 assert captured["embed"] == {"text": "private archive", "normalize": True}60 61 62def test_create_llama_embedder_accepts_explicit_batch(monkeypatch) -> None:63 monkeypatch.setenv("ADVISOR_EMBEDDING_BATCH", "256")64 65 embedder = create_llama_cpp_embedder({"dimensions": 768})66 67 assert embedder.n_batch == 25668 69 70def test_create_llama_embedder_can_isolate_native_runtime(monkeypatch) -> None:71 monkeypatch.setenv("ADVISOR_EMBEDDING_SUBPROCESS", "1")72 73 embedder = create_llama_cpp_embedder({"dimensions": 768})74 75 assert isinstance(embedder, SubprocessLlamaCppEmbedder)76 embedder.close()77 78 79def test_create_llama_embedder_isolates_macos_minicpm_runtime(monkeypatch) -> None:80 monkeypatch.delenv("ADVISOR_EMBEDDING_SUBPROCESS", raising=False)81 monkeypatch.setenv("ADVISOR_MODEL_BACKEND", "minicpm-transformers")82 monkeypatch.setattr("hackathon_advisor.llama_embedding.platform.system", lambda: "Darwin")83 84 embedder = create_llama_cpp_embedder({"dimensions": 768})85 86 assert isinstance(embedder, SubprocessLlamaCppEmbedder)87 embedder.close()88 89 90def test_create_llama_embedder_keeps_in_process_when_isolation_disabled(monkeypatch) -> None:91 monkeypatch.setenv("ADVISOR_EMBEDDING_SUBPROCESS", "0")92 monkeypatch.setenv("ADVISOR_MODEL_BACKEND", "minicpm-transformers")93 monkeypatch.setattr("hackathon_advisor.llama_embedding.platform.system", lambda: "Darwin")94 95 embedder = create_llama_cpp_embedder({"dimensions": 768})96 97 assert isinstance(embedder, LlamaCppEmbedder)98 