| """Patch chromadb DefaultEmbeddingFunction to avoid downloading ONNX model.""" |
| import re, sys, pathlib |
|
|
| def patch_chromadb(): |
| try: |
| import chromadb.utils.embedding_functions as ef_mod |
| except ImportError: |
| print("[chromadb] not installed; skipping patch") |
| return |
| fp = pathlib.Path(ef_mod.__file__) |
| txt = fp.read_text() |
| if "# patched: default embedding disabled" in txt: |
| print("[chromadb] already patched") |
| return |
| new = re.sub( |
| r"def DefaultEmbeddingFunction\(\)[^:]*:.*?return ONNXMiniLM_L6_V2\(\)", |
| "def DefaultEmbeddingFunction():\n # patched: default embedding disabled\n return None", |
| txt, flags=re.DOTALL, count=1, |
| ) |
| if new != txt: |
| fp.write_text(new) |
| print(f"[chromadb] patched: {fp}") |
| else: |
| print("[chromadb] pattern not found, skipping") |
|
|
| def patch_posthog(): |
| try: |
| import posthog |
| except ImportError: |
| print("[posthog] not installed; skipping") |
| return |
| fp = pathlib.Path(posthog.__file__) |
| txt = fp.read_text() |
| if "# patched: telemetry no-op" in txt: |
| print("[posthog] already patched") |
| return |
| new = re.sub(r"def capture\(([^)]*)\):", r"def capture(\1):\n return None # patched: telemetry no-op", txt, count=1) |
| if new != txt: |
| fp.write_text(new) |
| print(f"[posthog] patched: {fp}") |
|
|
| patch_chromadb() |
| patch_posthog() |
|
|