placeborag 0.0.1__py3-none-any.whl

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.
placeborag/__init__.py ADDED
@@ -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"
placeborag/embedder.py ADDED
@@ -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,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,6 @@
1
+ placeborag/__init__.py,sha256=yhWcPC1U6-9xjrT9cVCI-bTx5RIKrb0_r24aefTia2M,336
2
+ placeborag/embedder.py,sha256=m6gDhXxRdBqu-JEbrqPdp6CLVL6wBAlbtf_oIDdNImw,4717
3
+ placeborag-0.0.1.dist-info/METADATA,sha256=eYPRErRqPMG1FnftTeikpV8ZvVbymZn3pTyF6PM8kgQ,3609
4
+ placeborag-0.0.1.dist-info/WHEEL,sha256=lCkmxWfQsSc9CfIClYeavTdQeEX2toPqufh9gI35EQA,87
5
+ placeborag-0.0.1.dist-info/licenses/LICENSE,sha256=3ssjZ-LOyHMTRRux78rkd-T98lJAyd4RAiX8q9UHJR0,1063
6
+ placeborag-0.0.1.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.31.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -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.