placeborag 0.0.1__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,221 @@
1
+ # Byte-compiled / optimized / DLL files
2
+ __pycache__/
3
+ *.py[codz]
4
+ *$py.class
5
+
6
+ # C extensions
7
+ *.so
8
+
9
+ # Distribution / packaging
10
+ .Python
11
+ build/
12
+ develop-eggs/
13
+ dist/
14
+ downloads/
15
+ eggs/
16
+ .eggs/
17
+ lib/
18
+ lib64/
19
+ parts/
20
+ sdist/
21
+ var/
22
+ wheels/
23
+ share/python-wheels/
24
+ *.egg-info/
25
+ .installed.cfg
26
+ *.egg
27
+ MANIFEST
28
+
29
+ # PyInstaller
30
+ # Usually these files are written by a python script from a template
31
+ # before PyInstaller builds the exe, so as to inject date/other infos into it.
32
+ *.manifest
33
+ *.spec
34
+
35
+ # Installer logs
36
+ pip-log.txt
37
+ pip-delete-this-directory.txt
38
+
39
+ # Unit test / coverage reports
40
+ htmlcov/
41
+ .tox/
42
+ .nox/
43
+ .coverage
44
+ .coverage.*
45
+ .cache
46
+ nosetests.xml
47
+ coverage.xml
48
+ *.cover
49
+ *.py.cover
50
+ .hypothesis/
51
+ .pytest_cache/
52
+ cover/
53
+
54
+ # Translations
55
+ *.mo
56
+ *.pot
57
+
58
+ # Django stuff:
59
+ *.log
60
+ local_settings.py
61
+ db.sqlite3
62
+ db.sqlite3-journal
63
+
64
+ # Flask stuff:
65
+ instance/
66
+ .webassets-cache
67
+
68
+ # Scrapy stuff:
69
+ .scrapy
70
+
71
+ # Sphinx documentation
72
+ docs/_build/
73
+
74
+ # PyBuilder
75
+ .pybuilder/
76
+ target/
77
+
78
+ # Jupyter Notebook
79
+ .ipynb_checkpoints
80
+
81
+ # IPython
82
+ profile_default/
83
+ ipython_config.py
84
+
85
+ # pyenv
86
+ # For a library or package, you might want to ignore these files since the code is
87
+ # intended to run in multiple environments; otherwise, check them in:
88
+ # .python-version
89
+
90
+ # pipenv
91
+ # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
92
+ # However, in case of collaboration, if having platform-specific dependencies or dependencies
93
+ # having no cross-platform support, pipenv may install dependencies that don't work, or not
94
+ # install all needed dependencies.
95
+ # Pipfile.lock
96
+
97
+ # UV
98
+ # Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
99
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
100
+ # commonly ignored for libraries.
101
+ # uv.lock
102
+
103
+ # poetry
104
+ # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
105
+ # This is especially recommended for binary packages to ensure reproducibility, and is more
106
+ # commonly ignored for libraries.
107
+ # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
108
+ # poetry.lock
109
+ # poetry.toml
110
+
111
+ # pdm
112
+ # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
113
+ # pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
114
+ # https://pdm-project.org/en/latest/usage/project/#working-with-version-control
115
+ # pdm.lock
116
+ # pdm.toml
117
+ .pdm-python
118
+ .pdm-build/
119
+
120
+ # pixi
121
+ # Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
122
+ # pixi.lock
123
+ # Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
124
+ # in the .venv directory. It is recommended not to include this directory in version control.
125
+ .pixi
126
+
127
+ # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
128
+ __pypackages__/
129
+
130
+ # Celery stuff
131
+ celerybeat-schedule
132
+ celerybeat.pid
133
+
134
+ # Redis
135
+ *.rdb
136
+ *.aof
137
+ *.pid
138
+
139
+ # RabbitMQ
140
+ mnesia/
141
+ rabbitmq/
142
+ rabbitmq-data/
143
+
144
+ # ActiveMQ
145
+ activemq-data/
146
+
147
+ # SageMath parsed files
148
+ *.sage.py
149
+
150
+ # Environments
151
+ .env
152
+ .envrc
153
+ .venv
154
+ env/
155
+ venv/
156
+ ENV/
157
+ env.bak/
158
+ venv.bak/
159
+
160
+ # Spyder project settings
161
+ .spyderproject
162
+ .spyproject
163
+
164
+ # Rope project settings
165
+ .ropeproject
166
+
167
+ # mkdocs documentation
168
+ /site
169
+
170
+ # mypy
171
+ .mypy_cache/
172
+ .dmypy.json
173
+ dmypy.json
174
+
175
+ # Pyre type checker
176
+ .pyre/
177
+
178
+ # pytype static type analyzer
179
+ .pytype/
180
+
181
+ # Cython debug symbols
182
+ cython_debug/
183
+
184
+ # PyCharm
185
+ # JetBrains specific template is maintained in a separate JetBrains.gitignore that can
186
+ # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
187
+ # and can be added to the global gitignore or merged into this file. For a more nuclear
188
+ # option (not recommended) you can uncomment the following to ignore the entire idea folder.
189
+ # .idea/
190
+
191
+ # Abstra
192
+ # Abstra is an AI-powered process automation framework.
193
+ # Ignore directories containing user credentials, local state, and settings.
194
+ # Learn more at https://abstra.io/docs
195
+ .abstra/
196
+
197
+ # Visual Studio Code
198
+ # Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
199
+ # that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
200
+ # and can be added to the global gitignore or merged into this file. However, if you prefer,
201
+ # you could uncomment the following to ignore the entire vscode folder
202
+ # .vscode/
203
+ # Temporary file for partial code execution
204
+ tempCodeRunnerFile.py
205
+
206
+ # Ruff stuff:
207
+ .ruff_cache/
208
+
209
+ # PyPI configuration file
210
+ .pypirc
211
+
212
+ # Marimo
213
+ marimo/_static/
214
+ marimo/_lsp/
215
+ __marimo__/
216
+
217
+ # Streamlit
218
+ .streamlit/secrets.toml
219
+
220
+ # Local planning notes
221
+ PLAN.md
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 elaz48
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,93 @@
1
+ Metadata-Version: 2.4
2
+ Name: placeborag
3
+ Version: 0.0.1
4
+ Summary: Deterministic test doubles for the retrieval half of a RAG pipeline
5
+ Project-URL: Homepage, https://github.com/elaz48/placeborag
6
+ Project-URL: Issues, https://github.com/elaz48/placeborag/issues
7
+ Author: elaz48
8
+ License-Expression: MIT
9
+ License-File: LICENSE
10
+ Keywords: embeddings,mock,pytest,rag,retrieval,testing,vector-store
11
+ Classifier: Development Status :: 3 - Alpha
12
+ Classifier: Intended Audience :: Developers
13
+ Classifier: Programming Language :: Python :: 3
14
+ Classifier: Programming Language :: Python :: 3.10
15
+ Classifier: Programming Language :: Python :: 3.11
16
+ Classifier: Programming Language :: Python :: 3.12
17
+ Classifier: Programming Language :: Python :: 3.13
18
+ Classifier: Topic :: Software Development :: Testing
19
+ Classifier: Topic :: Software Development :: Testing :: Mocking
20
+ Classifier: Typing :: Typed
21
+ Requires-Python: >=3.10
22
+ Provides-Extra: dev
23
+ Requires-Dist: pytest>=7; extra == 'dev'
24
+ Description-Content-Type: text/markdown
25
+
26
+ # placeborag
27
+
28
+ Deterministic test doubles for the **retrieval** half of a RAG pipeline.
29
+
30
+ Existing mock LLM tooling stubs the chat completion endpoint and hands back pseudo-random embedding vectors of the correct shape. Correct shape, no semantic structure. Any document can come back at any rank, so every retrieval assertion in your test suite is decorative: you can assert that the pipeline ran, not that it retrieved the right thing.
31
+
32
+ placeborag gives you an embedder that is a pure function whose output geometry you can reason about — offline, in microseconds, with no model and no network.
33
+
34
+ ## Install
35
+
36
+ ```bash
37
+ pip install placeborag
38
+ ```
39
+
40
+ ## Use
41
+
42
+ ```python
43
+ from placeborag import FakeEmbedder, cosine_similarity
44
+
45
+ embedder = FakeEmbedder()
46
+
47
+ query = embedder.embed("refund policy")
48
+ related = embedder.embed("what is your refund policy for orders")
49
+ unrelated = embedder.embed("delivery times to remote islands")
50
+
51
+ assert cosine_similarity(query, related) > cosine_similarity(query, unrelated)
52
+ ```
53
+
54
+ The same text always gives the same vector, so a top-k assertion is stable across runs and across machines:
55
+
56
+ ```python
57
+ assert embedder.embed("refund policy") == embedder.embed("refund policy")
58
+ ```
59
+
60
+ Changing `model_name` changes the embedding space, which is what makes the "we swapped the embedding model and have to reindex" code path testable:
61
+
62
+ ```python
63
+ a = FakeEmbedder(model_name="text-embedding-3-small")
64
+ b = FakeEmbedder(model_name="text-embedding-3-large")
65
+
66
+ assert a.embed("refund policy") != b.embed("refund policy")
67
+ ```
68
+
69
+ Every vector is L2-normalized, including for the empty string and for non-ASCII input.
70
+
71
+ ## How it works
72
+
73
+ Character n-grams and word tokens are hashed into `dimensions` buckets with a signed hashing trick, summed, and normalized. Texts sharing tokens land near each other. There is no training data and no model file — the seed is derived from `(text, model_name, dimensions)`.
74
+
75
+ That makes the ranking explainable to whoever reads the failing test, which is the part random vectors can never give you.
76
+
77
+ ## Status
78
+
79
+ 0.0.1 is deliberately small: the hashing layer of `FakeEmbedder` and nothing else. It exists to prove the release pipeline.
80
+
81
+ Next up:
82
+
83
+ - declared clusters, so you can steer which texts are near which
84
+ - a fake vector store reproducing real backend score conventions (distance-lower-is-better vs score-higher-is-better) and pre-filter vs post-filter semantics
85
+ - pytest fixtures as a thin layer over the library
86
+
87
+ ## What this is not
88
+
89
+ Not an eval framework, not a benchmark, not a production vector store, and not another OpenAI-compatible mock server.
90
+
91
+ ## License
92
+
93
+ MIT
@@ -0,0 +1,68 @@
1
+ # placeborag
2
+
3
+ Deterministic test doubles for the **retrieval** half of a RAG pipeline.
4
+
5
+ Existing mock LLM tooling stubs the chat completion endpoint and hands back pseudo-random embedding vectors of the correct shape. Correct shape, no semantic structure. Any document can come back at any rank, so every retrieval assertion in your test suite is decorative: you can assert that the pipeline ran, not that it retrieved the right thing.
6
+
7
+ placeborag gives you an embedder that is a pure function whose output geometry you can reason about — offline, in microseconds, with no model and no network.
8
+
9
+ ## Install
10
+
11
+ ```bash
12
+ pip install placeborag
13
+ ```
14
+
15
+ ## Use
16
+
17
+ ```python
18
+ from placeborag import FakeEmbedder, cosine_similarity
19
+
20
+ embedder = FakeEmbedder()
21
+
22
+ query = embedder.embed("refund policy")
23
+ related = embedder.embed("what is your refund policy for orders")
24
+ unrelated = embedder.embed("delivery times to remote islands")
25
+
26
+ assert cosine_similarity(query, related) > cosine_similarity(query, unrelated)
27
+ ```
28
+
29
+ The same text always gives the same vector, so a top-k assertion is stable across runs and across machines:
30
+
31
+ ```python
32
+ assert embedder.embed("refund policy") == embedder.embed("refund policy")
33
+ ```
34
+
35
+ Changing `model_name` changes the embedding space, which is what makes the "we swapped the embedding model and have to reindex" code path testable:
36
+
37
+ ```python
38
+ a = FakeEmbedder(model_name="text-embedding-3-small")
39
+ b = FakeEmbedder(model_name="text-embedding-3-large")
40
+
41
+ assert a.embed("refund policy") != b.embed("refund policy")
42
+ ```
43
+
44
+ Every vector is L2-normalized, including for the empty string and for non-ASCII input.
45
+
46
+ ## How it works
47
+
48
+ Character n-grams and word tokens are hashed into `dimensions` buckets with a signed hashing trick, summed, and normalized. Texts sharing tokens land near each other. There is no training data and no model file — the seed is derived from `(text, model_name, dimensions)`.
49
+
50
+ That makes the ranking explainable to whoever reads the failing test, which is the part random vectors can never give you.
51
+
52
+ ## Status
53
+
54
+ 0.0.1 is deliberately small: the hashing layer of `FakeEmbedder` and nothing else. It exists to prove the release pipeline.
55
+
56
+ Next up:
57
+
58
+ - declared clusters, so you can steer which texts are near which
59
+ - a fake vector store reproducing real backend score conventions (distance-lower-is-better vs score-higher-is-better) and pre-filter vs post-filter semantics
60
+ - pytest fixtures as a thin layer over the library
61
+
62
+ ## What this is not
63
+
64
+ Not an eval framework, not a benchmark, not a production vector store, and not another OpenAI-compatible mock server.
65
+
66
+ ## License
67
+
68
+ MIT
@@ -0,0 +1,43 @@
1
+ [build-system]
2
+ requires = ["hatchling"]
3
+ build-backend = "hatchling.build"
4
+
5
+ [project]
6
+ name = "placeborag"
7
+ version = "0.0.1"
8
+ description = "Deterministic test doubles for the retrieval half of a RAG pipeline"
9
+ readme = "README.md"
10
+ requires-python = ">=3.10"
11
+ license = "MIT"
12
+ license-files = ["LICENSE"]
13
+ authors = [{ name = "elaz48" }]
14
+ keywords = ["rag", "retrieval", "testing", "embeddings", "vector-store", "mock", "pytest"]
15
+ classifiers = [
16
+ "Development Status :: 3 - Alpha",
17
+ "Intended Audience :: Developers",
18
+ "Programming Language :: Python :: 3",
19
+ "Programming Language :: Python :: 3.10",
20
+ "Programming Language :: Python :: 3.11",
21
+ "Programming Language :: Python :: 3.12",
22
+ "Programming Language :: Python :: 3.13",
23
+ "Topic :: Software Development :: Testing",
24
+ "Topic :: Software Development :: Testing :: Mocking",
25
+ "Typing :: Typed",
26
+ ]
27
+ dependencies = []
28
+
29
+ [project.urls]
30
+ Homepage = "https://github.com/elaz48/placeborag"
31
+ Issues = "https://github.com/elaz48/placeborag/issues"
32
+
33
+ [project.optional-dependencies]
34
+ dev = ["pytest>=7"]
35
+
36
+ [tool.hatch.build.targets.wheel]
37
+ packages = ["src/placeborag"]
38
+
39
+ [tool.hatch.build.targets.sdist]
40
+ include = ["src/placeborag", "tests", "README.md", "LICENSE"]
41
+
42
+ [tool.pytest.ini_options]
43
+ testpaths = ["tests"]
@@ -0,0 +1,17 @@
1
+ """Deterministic test doubles for the retrieval half of a RAG pipeline."""
2
+
3
+ from placeborag.embedder import (
4
+ DEFAULT_DIMENSIONS,
5
+ DEFAULT_MODEL_NAME,
6
+ FakeEmbedder,
7
+ cosine_similarity,
8
+ )
9
+
10
+ __all__ = [
11
+ "DEFAULT_DIMENSIONS",
12
+ "DEFAULT_MODEL_NAME",
13
+ "FakeEmbedder",
14
+ "cosine_similarity",
15
+ ]
16
+
17
+ __version__ = "0.0.1"
@@ -0,0 +1,135 @@
1
+ """A deterministic embedder built on feature hashing.
2
+
3
+ No model, no data file, no network. `embed` is a pure function of
4
+ `(text, model_name, dimensions)`, so the same text always produces the same
5
+ vector, and two texts that share tokens land near each other. That is what
6
+ makes a top-k assertion in a test mean something.
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import hashlib
12
+ import math
13
+ import re
14
+ from collections.abc import Iterable, Sequence
15
+
16
+ DEFAULT_MODEL_NAME = "placebo-hash-001"
17
+ DEFAULT_DIMENSIONS = 256
18
+
19
+ _MIN_DIMENSIONS = 2
20
+ _CHAR_NGRAM_SIZES = (3, 4)
21
+ _WORD_PATTERN = re.compile(r"\w+", re.UNICODE)
22
+ _KEY_SIZE = 32
23
+ _DIGEST_SIZE = 8
24
+ _SIGN_BIT = 1 << (_DIGEST_SIZE * 8 - 1)
25
+
26
+
27
+ class FakeEmbedder:
28
+ """Embeds text into a deterministic unit vector.
29
+
30
+ Args:
31
+ model_name: Salts the hash. A different name yields a different but
32
+ equally deterministic embedding space, which is what makes the
33
+ "we swapped the embedding model and have to reindex" code path
34
+ testable.
35
+ dimensions: Length of the returned vector.
36
+ """
37
+
38
+ def __init__(
39
+ self,
40
+ model_name: str = DEFAULT_MODEL_NAME,
41
+ dimensions: int = DEFAULT_DIMENSIONS,
42
+ ) -> None:
43
+ if not isinstance(model_name, str) or not model_name:
44
+ raise ValueError("model_name must be a non-empty string")
45
+ if not isinstance(dimensions, int) or isinstance(dimensions, bool):
46
+ raise TypeError("dimensions must be an int")
47
+ if dimensions < _MIN_DIMENSIONS:
48
+ raise ValueError(f"dimensions must be >= {_MIN_DIMENSIONS}, got {dimensions}")
49
+
50
+ self._model_name = model_name
51
+ self._dimensions = dimensions
52
+ self._key = hashlib.blake2b(
53
+ f"{model_name}:{dimensions}".encode(), digest_size=_KEY_SIZE
54
+ ).digest()
55
+
56
+ @property
57
+ def model_name(self) -> str:
58
+ return self._model_name
59
+
60
+ @property
61
+ def dimensions(self) -> int:
62
+ return self._dimensions
63
+
64
+ def embed(self, text: str) -> list[float]:
65
+ """Returns the L2-normalized vector for `text`."""
66
+ if not isinstance(text, str):
67
+ raise TypeError(f"text must be a str, got {type(text).__name__}")
68
+
69
+ features = _features(text) or [_sentinel(text)]
70
+ vector = self._accumulate(features)
71
+ norm = _norm(vector)
72
+ if norm == 0.0:
73
+ # Signs cancelled out exactly. Vanishingly unlikely, but a zero
74
+ # vector cannot be normalized, so fall back to the sentinel.
75
+ vector = self._accumulate([_sentinel(text)])
76
+ norm = _norm(vector)
77
+ return [value / norm for value in vector]
78
+
79
+ def embed_batch(self, texts: Iterable[str]) -> list[list[float]]:
80
+ """Returns one vector per input text, in order."""
81
+ return [self.embed(text) for text in texts]
82
+
83
+ def _accumulate(self, features: Sequence[str]) -> list[float]:
84
+ vector = [0.0] * self._dimensions
85
+ for feature in features:
86
+ index, sign = self._bucket(feature)
87
+ vector[index] += sign
88
+ return vector
89
+
90
+ def _bucket(self, feature: str) -> tuple[int, float]:
91
+ digest = hashlib.blake2b(
92
+ feature.encode("utf-8"), key=self._key, digest_size=_DIGEST_SIZE
93
+ ).digest()
94
+ value = int.from_bytes(digest, "big")
95
+ index = value % self._dimensions
96
+ sign = -1.0 if value & _SIGN_BIT else 1.0
97
+ return index, sign
98
+
99
+ def __repr__(self) -> str:
100
+ return (
101
+ f"{type(self).__name__}(model_name={self._model_name!r}, "
102
+ f"dimensions={self._dimensions})"
103
+ )
104
+
105
+
106
+ def cosine_similarity(left: Sequence[float], right: Sequence[float]) -> float:
107
+ """Cosine similarity of two vectors of equal length."""
108
+ if len(left) != len(right):
109
+ raise ValueError(f"dimension mismatch: {len(left)} != {len(right)}")
110
+ left_norm = _norm(left)
111
+ right_norm = _norm(right)
112
+ if left_norm == 0.0 or right_norm == 0.0:
113
+ raise ValueError("cosine similarity is undefined for a zero vector")
114
+ dot = sum(a * b for a, b in zip(left, right))
115
+ return dot / (left_norm * right_norm)
116
+
117
+
118
+ def _features(text: str) -> list[str]:
119
+ lowered = text.lower()
120
+ features = [f"w:{token}" for token in _WORD_PATTERN.findall(lowered)]
121
+ for size in _CHAR_NGRAM_SIZES:
122
+ features.extend(
123
+ f"c{size}:{lowered[start : start + size]}"
124
+ for start in range(len(lowered) - size + 1)
125
+ )
126
+ return features
127
+
128
+
129
+ def _sentinel(text: str) -> str:
130
+ """Feature used for inputs that produce no n-grams or word tokens."""
131
+ return f"\x00empty\x00{text}"
132
+
133
+
134
+ def _norm(vector: Sequence[float]) -> float:
135
+ return math.sqrt(sum(value * value for value in vector))
@@ -0,0 +1,104 @@
1
+ import math
2
+
3
+ import pytest
4
+
5
+ from placeborag import DEFAULT_DIMENSIONS, FakeEmbedder, cosine_similarity
6
+
7
+
8
+ def test_returns_the_same_vector_for_the_same_text():
9
+ embedder = FakeEmbedder()
10
+
11
+ first = embedder.embed("refund policy")
12
+ second = embedder.embed("refund policy")
13
+
14
+ assert first == second
15
+
16
+
17
+ def test_two_embedders_with_the_same_config_agree():
18
+ left = FakeEmbedder(model_name="shared", dimensions=64)
19
+ right = FakeEmbedder(model_name="shared", dimensions=64)
20
+
21
+ assert left.embed("delivery times") == right.embed("delivery times")
22
+
23
+
24
+ def test_changing_model_name_changes_the_embedding_space():
25
+ text = "refund policy"
26
+
27
+ small = FakeEmbedder(model_name="model-a").embed(text)
28
+ other = FakeEmbedder(model_name="model-b").embed(text)
29
+
30
+ assert small != other
31
+
32
+
33
+ def test_vector_length_follows_the_dimensions_argument():
34
+ assert len(FakeEmbedder().embed("hello")) == DEFAULT_DIMENSIONS
35
+ assert len(FakeEmbedder(dimensions=16).embed("hello")) == 16
36
+
37
+
38
+ @pytest.mark.parametrize(
39
+ "text",
40
+ ["", " ", "!!!", "a", "refund policy", "hogyan kérek vissza pénzt", "日本語のテキスト", "🙂🙂"],
41
+ )
42
+ def test_every_vector_is_normalized(text):
43
+ vector = FakeEmbedder(dimensions=32).embed(text)
44
+
45
+ assert math.isclose(math.sqrt(sum(v * v for v in vector)), 1.0, rel_tol=1e-9)
46
+
47
+
48
+ def test_related_text_scores_higher_than_unrelated_text():
49
+ embedder = FakeEmbedder()
50
+ query = embedder.embed("refund policy")
51
+ related = embedder.embed("what is your refund policy for orders")
52
+ unrelated = embedder.embed("delivery times to remote islands")
53
+
54
+ assert cosine_similarity(query, related) > cosine_similarity(query, unrelated)
55
+
56
+
57
+ def test_embed_batch_matches_embed():
58
+ embedder = FakeEmbedder(dimensions=32)
59
+ texts = ["refund policy", "delivery times", ""]
60
+
61
+ assert embedder.embed_batch(texts) == [embedder.embed(text) for text in texts]
62
+
63
+
64
+ def test_cosine_similarity_of_a_vector_with_itself_is_one():
65
+ vector = FakeEmbedder(dimensions=32).embed("refund policy")
66
+
67
+ assert math.isclose(cosine_similarity(vector, vector), 1.0, rel_tol=1e-9)
68
+
69
+
70
+ def test_cosine_similarity_rejects_mismatched_dimensions():
71
+ with pytest.raises(ValueError, match="dimension mismatch"):
72
+ cosine_similarity([1.0, 0.0], [1.0, 0.0, 0.0])
73
+
74
+
75
+ def test_cosine_similarity_rejects_a_zero_vector():
76
+ with pytest.raises(ValueError, match="zero vector"):
77
+ cosine_similarity([0.0, 0.0], [1.0, 0.0])
78
+
79
+
80
+ @pytest.mark.parametrize("dimensions", [0, 1, -8])
81
+ def test_rejects_dimensions_below_the_minimum(dimensions):
82
+ with pytest.raises(ValueError, match="dimensions must be"):
83
+ FakeEmbedder(dimensions=dimensions)
84
+
85
+
86
+ def test_rejects_a_non_int_dimensions():
87
+ with pytest.raises(TypeError, match="dimensions must be an int"):
88
+ FakeEmbedder(dimensions=32.0)
89
+
90
+
91
+ def test_rejects_an_empty_model_name():
92
+ with pytest.raises(ValueError, match="model_name must be"):
93
+ FakeEmbedder(model_name="")
94
+
95
+
96
+ def test_rejects_non_string_text():
97
+ with pytest.raises(TypeError, match="text must be a str"):
98
+ FakeEmbedder().embed(None)
99
+
100
+
101
+ def test_repr_shows_the_configuration():
102
+ assert repr(FakeEmbedder(model_name="m", dimensions=8)) == (
103
+ "FakeEmbedder(model_name='m', dimensions=8)"
104
+ )