llm-sketchkit 0.1.0a4__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.
- llm_sketchkit-0.1.0a4/LICENSE +159 -0
- llm_sketchkit-0.1.0a4/PKG-INFO +280 -0
- llm_sketchkit-0.1.0a4/README.md +247 -0
- llm_sketchkit-0.1.0a4/pyproject.toml +73 -0
- llm_sketchkit-0.1.0a4/python/llm_sketchkit/__init__.py +51 -0
- llm_sketchkit-0.1.0a4/python/llm_sketchkit/_proto.py +81 -0
- llm_sketchkit-0.1.0a4/python/llm_sketchkit/bloom.py +262 -0
- llm_sketchkit-0.1.0a4/python/llm_sketchkit/canon.py +57 -0
- llm_sketchkit-0.1.0a4/python/llm_sketchkit/frequentitems.py +370 -0
- llm_sketchkit-0.1.0a4/python/llm_sketchkit/hash.py +125 -0
- llm_sketchkit-0.1.0a4/python/llm_sketchkit/hashfamily.py +37 -0
- llm_sketchkit-0.1.0a4/python/llm_sketchkit/hllpp.py +421 -0
- llm_sketchkit-0.1.0a4/python/llm_sketchkit/hllpp_bias.py +1232 -0
- llm_sketchkit-0.1.0a4/python/llm_sketchkit/minhash.py +220 -0
- llm_sketchkit-0.1.0a4/python/llm_sketchkit/profiles.py +71 -0
- llm_sketchkit-0.1.0a4/python/llm_sketchkit/py.typed +1 -0
- llm_sketchkit-0.1.0a4/python/llm_sketchkit/sketches_pb2.py +59 -0
- llm_sketchkit-0.1.0a4/python/llm_sketchkit.egg-info/PKG-INFO +280 -0
- llm_sketchkit-0.1.0a4/python/llm_sketchkit.egg-info/SOURCES.txt +22 -0
- llm_sketchkit-0.1.0a4/python/llm_sketchkit.egg-info/dependency_links.txt +1 -0
- llm_sketchkit-0.1.0a4/python/llm_sketchkit.egg-info/requires.txt +13 -0
- llm_sketchkit-0.1.0a4/python/llm_sketchkit.egg-info/top_level.txt +1 -0
- llm_sketchkit-0.1.0a4/setup.cfg +4 -0
- llm_sketchkit-0.1.0a4/tests/test_package.py +563 -0
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction, and
|
|
10
|
+
distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by the copyright
|
|
13
|
+
owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all other entities
|
|
16
|
+
that control, are controlled by, or are under common control with that entity.
|
|
17
|
+
For the purposes of this definition, "control" means (i) the power, direct or
|
|
18
|
+
indirect, to cause the direction or management of such entity, whether by
|
|
19
|
+
contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
20
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
21
|
+
|
|
22
|
+
"You" (or "Your") shall mean an individual or Legal Entity exercising
|
|
23
|
+
permissions granted by this License.
|
|
24
|
+
|
|
25
|
+
"Source" form shall mean the preferred form for making modifications, including
|
|
26
|
+
but not limited to software source code, documentation source, and configuration
|
|
27
|
+
files.
|
|
28
|
+
|
|
29
|
+
"Object" form shall mean any form resulting from mechanical transformation or
|
|
30
|
+
translation of a Source form, including but not limited to compiled object code,
|
|
31
|
+
generated documentation, and conversions to other media types.
|
|
32
|
+
|
|
33
|
+
"Work" shall mean the work of authorship, whether in Source or Object form,
|
|
34
|
+
made available under the License, as indicated by a copyright notice that is
|
|
35
|
+
included in or attached to the work.
|
|
36
|
+
|
|
37
|
+
"Derivative Works" shall mean any work, whether in Source or Object form, that
|
|
38
|
+
is based on (or derived from) the Work and for which the editorial revisions,
|
|
39
|
+
annotations, elaborations, or other modifications represent, as a whole, an
|
|
40
|
+
original work of authorship. For the purposes of this License, Derivative Works
|
|
41
|
+
shall not include works that remain separable from, or merely link (or bind by
|
|
42
|
+
name) to the interfaces of, the Work and Derivative Works thereof.
|
|
43
|
+
|
|
44
|
+
"Contribution" shall mean any work of authorship, including the original version
|
|
45
|
+
of the Work and any modifications or additions to that Work or Derivative Works
|
|
46
|
+
thereof, that is intentionally submitted to Licensor for inclusion in the Work
|
|
47
|
+
by the copyright owner or by an individual or Legal Entity authorized to submit
|
|
48
|
+
on behalf of the copyright owner. For the purposes of this definition,
|
|
49
|
+
"submitted" means any form of electronic, verbal, or written communication sent
|
|
50
|
+
to the Licensor or its representatives, including but not limited to
|
|
51
|
+
communication on electronic mailing lists, source code control systems, and
|
|
52
|
+
issue tracking systems that are managed by, or on behalf of, the Licensor for
|
|
53
|
+
the purpose of discussing and improving the Work, but excluding communication
|
|
54
|
+
that is conspicuously marked or otherwise designated in writing by the copyright
|
|
55
|
+
owner as "Not a Contribution."
|
|
56
|
+
|
|
57
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf
|
|
58
|
+
of whom a Contribution has been received by Licensor and subsequently
|
|
59
|
+
incorporated within the Work.
|
|
60
|
+
|
|
61
|
+
2. Grant of Copyright License. Subject to the terms and conditions of this
|
|
62
|
+
License, each Contributor hereby grants to You a perpetual, worldwide,
|
|
63
|
+
non-exclusive, no-charge, royalty-free, irrevocable copyright license to
|
|
64
|
+
reproduce, prepare Derivative Works of, publicly display, publicly perform,
|
|
65
|
+
sublicense, and distribute the Work and such Derivative Works in Source or
|
|
66
|
+
Object form.
|
|
67
|
+
|
|
68
|
+
3. Grant of Patent License. Subject to the terms and conditions of this License,
|
|
69
|
+
each Contributor hereby grants to You a perpetual, worldwide, non-exclusive,
|
|
70
|
+
no-charge, royalty-free, irrevocable (except as stated in this section) patent
|
|
71
|
+
license to make, have made, use, offer to sell, sell, import, and otherwise
|
|
72
|
+
transfer the Work, where such license applies only to those patent claims
|
|
73
|
+
licensable by such Contributor that are necessarily infringed by their
|
|
74
|
+
Contribution(s) alone or by combination of their Contribution(s) with the Work
|
|
75
|
+
to which such Contribution(s) was submitted. If You institute patent litigation
|
|
76
|
+
against any entity (including a cross-claim or counterclaim in a lawsuit)
|
|
77
|
+
alleging that the Work or a Contribution incorporated within the Work
|
|
78
|
+
constitutes direct or contributory patent infringement, then any patent licenses
|
|
79
|
+
granted to You under this License for that Work shall terminate as of the date
|
|
80
|
+
such litigation is filed.
|
|
81
|
+
|
|
82
|
+
4. Redistribution. You may reproduce and distribute copies of the Work or
|
|
83
|
+
Derivative Works thereof in any medium, with or without modifications, and in
|
|
84
|
+
Source or Object form, provided that You meet the following conditions:
|
|
85
|
+
|
|
86
|
+
(a) You must give any other recipients of the Work or Derivative Works a copy of
|
|
87
|
+
this License; and
|
|
88
|
+
|
|
89
|
+
(b) You must cause any modified files to carry prominent notices stating that
|
|
90
|
+
You changed the files; and
|
|
91
|
+
|
|
92
|
+
(c) You must retain, in the Source form of any Derivative Works that You
|
|
93
|
+
distribute, all copyright, patent, trademark, and attribution notices from the
|
|
94
|
+
Source form of the Work, excluding those notices that do not pertain to any part
|
|
95
|
+
of the Derivative Works; and
|
|
96
|
+
|
|
97
|
+
(d) If the Work includes a "NOTICE" text file as part of its distribution, then
|
|
98
|
+
any Derivative Works that You distribute must include a readable copy of the
|
|
99
|
+
attribution notices contained within such NOTICE file, excluding those notices
|
|
100
|
+
that do not pertain to any part of the Derivative Works, in at least one of the
|
|
101
|
+
following places: within a NOTICE text file distributed as part of the
|
|
102
|
+
Derivative Works; within the Source form or documentation, if provided along
|
|
103
|
+
with the Derivative Works; or, within a display generated by the Derivative
|
|
104
|
+
Works, if and wherever such third-party notices normally appear. The contents of
|
|
105
|
+
the NOTICE file are for informational purposes only and do not modify the
|
|
106
|
+
License. You may add Your own attribution notices within Derivative Works that
|
|
107
|
+
You distribute, alongside or as an addendum to the NOTICE text from the Work,
|
|
108
|
+
provided that such additional attribution notices cannot be construed as
|
|
109
|
+
modifying the License.
|
|
110
|
+
|
|
111
|
+
You may add Your own copyright statement to Your modifications and may provide
|
|
112
|
+
additional or different license terms and conditions for use, reproduction, or
|
|
113
|
+
distribution of Your modifications, or for any such Derivative Works as a whole,
|
|
114
|
+
provided Your use, reproduction, and distribution of the Work otherwise complies
|
|
115
|
+
with the conditions stated in this License.
|
|
116
|
+
|
|
117
|
+
5. Submission of Contributions. Unless You explicitly state otherwise, any
|
|
118
|
+
Contribution intentionally submitted for inclusion in the Work by You to the
|
|
119
|
+
Licensor shall be under the terms and conditions of this License, without any
|
|
120
|
+
additional terms or conditions. Notwithstanding the above, nothing herein shall
|
|
121
|
+
supersede or modify the terms of any separate license agreement you may have
|
|
122
|
+
executed with Licensor regarding such Contributions.
|
|
123
|
+
|
|
124
|
+
6. Trademarks. This License does not grant permission to use the trade names,
|
|
125
|
+
trademarks, service marks, or product names of the Licensor, except as required
|
|
126
|
+
for reasonable and customary use in describing the origin of the Work and
|
|
127
|
+
reproducing the content of the NOTICE file.
|
|
128
|
+
|
|
129
|
+
7. Disclaimer of Warranty. Unless required by applicable law or agreed to in
|
|
130
|
+
writing, Licensor provides the Work (and each Contributor provides its
|
|
131
|
+
Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
132
|
+
KIND, either express or implied, including, without limitation, any warranties or
|
|
133
|
+
conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
134
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
135
|
+
appropriateness of using or redistributing the Work and assume any risks
|
|
136
|
+
associated with Your exercise of permissions under this License.
|
|
137
|
+
|
|
138
|
+
8. Limitation of Liability. In no event and under no legal theory, whether in
|
|
139
|
+
tort (including negligence), contract, or otherwise, unless required by
|
|
140
|
+
applicable law (such as deliberate and grossly negligent acts) or agreed to in
|
|
141
|
+
writing, shall any Contributor be liable to You for damages, including any
|
|
142
|
+
direct, indirect, special, incidental, or consequential damages of any character
|
|
143
|
+
arising as a result of this License or out of the use or inability to use the
|
|
144
|
+
Work (including but not limited to damages for loss of goodwill, work stoppage,
|
|
145
|
+
computer failure or malfunction, or any and all other commercial damages or
|
|
146
|
+
losses), even if such Contributor has been advised of the possibility of such
|
|
147
|
+
damages.
|
|
148
|
+
|
|
149
|
+
9. Accepting Warranty or Additional Liability. While redistributing the Work or
|
|
150
|
+
Derivative Works thereof, You may choose to offer, and charge a fee for,
|
|
151
|
+
acceptance of support, warranty, indemnity, or other liability obligations
|
|
152
|
+
and/or rights consistent with this License. However, in accepting such
|
|
153
|
+
obligations, You may act only on Your own behalf and on Your sole
|
|
154
|
+
responsibility, not on behalf of any other Contributor, and only if You agree to
|
|
155
|
+
indemnify, defend, and hold each Contributor harmless for any liability incurred
|
|
156
|
+
by, or claims asserted against, such Contributor by reason of your accepting any
|
|
157
|
+
such warranty or additional liability.
|
|
158
|
+
|
|
159
|
+
END OF TERMS AND CONDITIONS
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: llm-sketchkit
|
|
3
|
+
Version: 0.1.0a4
|
|
4
|
+
Summary: Deterministic Go and Python sketches for high-cardinality LLM data.
|
|
5
|
+
Author: Vijay Erramilli, Codex
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Documentation, https://github.com/llm-measurement/llm-sketchkit#readme
|
|
8
|
+
Project-URL: Issues, https://github.com/llm-measurement/llm-sketchkit/issues
|
|
9
|
+
Project-URL: Source, https://github.com/llm-measurement/llm-sketchkit
|
|
10
|
+
Keywords: sketches,hyperloglog,minhash,bloom-filter,streaming
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Typing :: Typed
|
|
19
|
+
Requires-Python: >=3.11
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: protobuf<7,>=6.33.5
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: mypy>=1.10; extra == "dev"
|
|
25
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
26
|
+
Requires-Dist: ruff>=0.5; extra == "dev"
|
|
27
|
+
Provides-Extra: oracle
|
|
28
|
+
Requires-Dist: datasketches==5.2.0; extra == "oracle"
|
|
29
|
+
Provides-Extra: release
|
|
30
|
+
Requires-Dist: build==1.5.0; extra == "release"
|
|
31
|
+
Requires-Dist: twine==6.2.0; extra == "release"
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
|
|
34
|
+
# llm-sketchkit
|
|
35
|
+
|
|
36
|
+
[](https://github.com/llm-measurement/llm-sketchkit/actions/workflows/ci.yml)
|
|
37
|
+
|
|
38
|
+
`llm-sketchkit` is a small Go and Python library for deterministic, mergeable
|
|
39
|
+
summaries of high-cardinality LLM data. It provides matching semantics for text
|
|
40
|
+
canonicalization, privacy-preserving keyed hashes, bounded sketches, and a
|
|
41
|
+
deterministic protobuf wire format.
|
|
42
|
+
|
|
43
|
+
Raw prompts and identifiers do not need to enter sketch state. Producers can
|
|
44
|
+
summarize locally and merge compatible sketches across processes or languages.
|
|
45
|
+
|
|
46
|
+
## When This Fits
|
|
47
|
+
|
|
48
|
+
Use `llm-sketchkit` inside telemetry producers and processing components when
|
|
49
|
+
exact per-key state would grow with cardinality or retain values that should not
|
|
50
|
+
enter aggregate state.
|
|
51
|
+
|
|
52
|
+
Typical uses include:
|
|
53
|
+
|
|
54
|
+
- estimating distinct prompts, users, sessions, tools, or documents without
|
|
55
|
+
keeping one counter per value;
|
|
56
|
+
- identifying token-heavy or request-heavy keys with deterministic lower and
|
|
57
|
+
upper bounds;
|
|
58
|
+
- testing approximate set membership for bounded deduplication;
|
|
59
|
+
- comparing large sets using fixed-size similarity signatures; and
|
|
60
|
+
- producing summaries in Go that can be read and merged in Python with the
|
|
61
|
+
same profiles and wire semantics.
|
|
62
|
+
|
|
63
|
+
Inputs can be canonicalized and keyed before entering sketch state. This keeps
|
|
64
|
+
raw values out of the sketch, but the resulting hashes remain pseudonymous and
|
|
65
|
+
linkable while the same secret is in use.
|
|
66
|
+
|
|
67
|
+
This is a sketch library, not a trace collector, sampling processor, storage
|
|
68
|
+
backend, dashboard, or differential-privacy system. The [FAQ](https://github.com/llm-measurement/llm-sketchkit/blob/main/docs/FAQ.md)
|
|
69
|
+
answers common questions about fit, memory, accuracy, and interoperability.
|
|
70
|
+
|
|
71
|
+
## Included Sketches
|
|
72
|
+
|
|
73
|
+
| Component | Use it for | Important property |
|
|
74
|
+
|---|---|---|
|
|
75
|
+
| HLL++ | Approximate distinct counts | Fixed memory and mergeable state |
|
|
76
|
+
| Weighted frequent-items | Heavy hitters and top items | Deterministic lower and upper bounds |
|
|
77
|
+
| Bloom filter | Set membership | No false negatives; configurable false-positive rate |
|
|
78
|
+
| MinHash | Approximate Jaccard similarity | Fixed-size mergeable signatures |
|
|
79
|
+
|
|
80
|
+
The Go and Python implementations share the same profiles, hash domains, test
|
|
81
|
+
vectors, and serialized representation.
|
|
82
|
+
|
|
83
|
+
## Evidence At A Glance
|
|
84
|
+
|
|
85
|
+
Measurements use deterministic workloads and report the least favorable of five
|
|
86
|
+
Linux benchmark runs where applicable.
|
|
87
|
+
|
|
88
|
+
- HMAC-SHA256-64 sustained at least **1.65 million 64-byte inputs/s/core** and
|
|
89
|
+
**850,340 1 KiB inputs/s/core** on an Intel Xeon Platinum 8573C with Go 1.26.5.
|
|
90
|
+
- HLL++ and weighted frequent-items updates took at most **10.58 ns/op** and
|
|
91
|
+
**145.6 ns/op**, respectively, with **0 allocations/op** in the measured paths.
|
|
92
|
+
- The HLL++ `small` profile's maximum observed relative error was **2.3301%**
|
|
93
|
+
across the characterization grid, within its **2.4375%** enforced bound.
|
|
94
|
+
- Bloom profile false-positive rates were at or below their configured targets
|
|
95
|
+
in the measured trials, with zero false negatives among inserted hashes.
|
|
96
|
+
- MinHash mean absolute error fell from **0.02845** at `k=128` to **0.02009** at
|
|
97
|
+
`k=256`, closely following the expected inverse-square-root relationship.
|
|
98
|
+
- Both weighted frequent-items oracle workloads retained **100% true top-20
|
|
99
|
+
recall** and valid no-false-positive query results in both implementations.
|
|
100
|
+
|
|
101
|
+
See the [visual scorecard](https://github.com/llm-measurement/llm-sketchkit/blob/main/reports/scorecard.md),
|
|
102
|
+
[raw measurement records](https://github.com/llm-measurement/llm-sketchkit/blob/main/reports/README.md),
|
|
103
|
+
and [DataSketches implementation rationale](https://github.com/llm-measurement/llm-sketchkit/blob/main/docs/DATASKETCHES.md)
|
|
104
|
+
for methods, limitations, and reproduction commands.
|
|
105
|
+
|
|
106
|
+
## Requirements
|
|
107
|
+
|
|
108
|
+
- Go 1.25 or newer, with the latest security patch for that release line
|
|
109
|
+
- Python 3.11 or newer
|
|
110
|
+
|
|
111
|
+
## Install
|
|
112
|
+
|
|
113
|
+
Go:
|
|
114
|
+
|
|
115
|
+
```sh
|
|
116
|
+
go get github.com/llm-measurement/llm-sketchkit@latest
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Python from a checkout:
|
|
120
|
+
|
|
121
|
+
```sh
|
|
122
|
+
git clone https://github.com/llm-measurement/llm-sketchkit.git
|
|
123
|
+
cd llm-sketchkit
|
|
124
|
+
python -m venv .venv
|
|
125
|
+
source .venv/bin/activate
|
|
126
|
+
python -m pip install --upgrade pip==26.2 setuptools==83.0.0
|
|
127
|
+
python -m pip install .
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
For development checks, install the optional tools:
|
|
131
|
+
|
|
132
|
+
```sh
|
|
133
|
+
python -m pip install -e '.[dev]'
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Quick Start
|
|
137
|
+
|
|
138
|
+
Generate a deployment secret and expose it to the process:
|
|
139
|
+
|
|
140
|
+
```sh
|
|
141
|
+
export LLM_SKETCHKIT_SECRET="$(python -c 'import secrets; print(secrets.token_hex(32))')"
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
Go HLL++ example:
|
|
145
|
+
|
|
146
|
+
```go
|
|
147
|
+
package main
|
|
148
|
+
|
|
149
|
+
import (
|
|
150
|
+
"fmt"
|
|
151
|
+
"log"
|
|
152
|
+
|
|
153
|
+
"github.com/llm-measurement/llm-sketchkit/go/sketchkit/canon"
|
|
154
|
+
sketchhash "github.com/llm-measurement/llm-sketchkit/go/sketchkit/hash"
|
|
155
|
+
"github.com/llm-measurement/llm-sketchkit/go/sketchkit/hllpp"
|
|
156
|
+
)
|
|
157
|
+
|
|
158
|
+
func main() {
|
|
159
|
+
secret, err := sketchhash.SecretFromEnv("LLM_SKETCHKIT_SECRET")
|
|
160
|
+
if err != nil {
|
|
161
|
+
log.Fatal(err)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
sketch, err := hllpp.New(
|
|
165
|
+
hllpp.ProfileSmall,
|
|
166
|
+
sketchhash.PromptV1,
|
|
167
|
+
sketchhash.HMACSHA25664,
|
|
168
|
+
)
|
|
169
|
+
if err != nil {
|
|
170
|
+
log.Fatal(err)
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
canonical, err := canon.CanonicalizeString(canon.TextV1, " cafe\u0301\r\n")
|
|
174
|
+
if err != nil {
|
|
175
|
+
log.Fatal(err)
|
|
176
|
+
}
|
|
177
|
+
digest, err := sketchhash.Hash64(secret, sketchhash.PromptV1, canonical)
|
|
178
|
+
if err != nil {
|
|
179
|
+
log.Fatal(err)
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
sketch.AddHash(digest)
|
|
183
|
+
fmt.Printf("estimated distinct prompts: %.0f\n", sketch.Estimate())
|
|
184
|
+
}
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Python HLL++ example:
|
|
188
|
+
|
|
189
|
+
```python
|
|
190
|
+
from llm_sketchkit import PROMPT_V1, canonicalize_text_v1, hash64, hllpp
|
|
191
|
+
from llm_sketchkit import secret_from_env
|
|
192
|
+
|
|
193
|
+
secret = secret_from_env("LLM_SKETCHKIT_SECRET")
|
|
194
|
+
sketch = hllpp.new("small", PROMPT_V1)
|
|
195
|
+
|
|
196
|
+
canonical = canonicalize_text_v1(" cafe\u0301\r\n")
|
|
197
|
+
sketch.add_hash(hash64(secret, PROMPT_V1, canonical))
|
|
198
|
+
|
|
199
|
+
print(f"estimated distinct prompts: {sketch.estimate():.0f}")
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
## Merge Sketches
|
|
203
|
+
|
|
204
|
+
Sketches merge only when their kind, profile, hash domain, hash algorithm, and
|
|
205
|
+
shape metadata match. A mismatch is an error rather than an implicit conversion.
|
|
206
|
+
|
|
207
|
+
```python
|
|
208
|
+
left = hllpp.new("small", PROMPT_V1)
|
|
209
|
+
right = hllpp.new("small", PROMPT_V1)
|
|
210
|
+
|
|
211
|
+
left.add_hash(hash64(secret, PROMPT_V1, canonicalize_text_v1("alpha")))
|
|
212
|
+
right.add_hash(hash64(secret, PROMPT_V1, canonicalize_text_v1("beta")))
|
|
213
|
+
|
|
214
|
+
left.merge(right)
|
|
215
|
+
print(f"merged distinct prompts: {left.estimate():.0f}")
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
## Security And Privacy
|
|
219
|
+
|
|
220
|
+
- Hash inputs with a registered domain and a high-entropy secret before adding
|
|
221
|
+
them to a sketch. The built-in secret loaders require at least 16 bytes and
|
|
222
|
+
reject known placeholder values.
|
|
223
|
+
- Keyed hashes are pseudonymous, not anonymous. Anyone with the secret can test
|
|
224
|
+
candidate values, and repeated hashes remain linkable while the same secret
|
|
225
|
+
and domain are in use.
|
|
226
|
+
- Never log, serialize, or commit the hash secret. Rotate it when the trust
|
|
227
|
+
boundary changes; rotation intentionally breaks comparison with older state.
|
|
228
|
+
- Bound raw input size before canonicalization. Canonicalization operates on
|
|
229
|
+
in-memory text and intentionally leaves application-specific limits to callers.
|
|
230
|
+
- Sketches reveal bounded aggregate information and may reveal membership or
|
|
231
|
+
recurrence. They do not provide differential privacy.
|
|
232
|
+
- Treat serialized sketches as untrusted input at process boundaries. The parse
|
|
233
|
+
APIs cap input size and reject invalid profiles, domains, shapes, counters, and
|
|
234
|
+
register values.
|
|
235
|
+
|
|
236
|
+
See [SECURITY.md](https://github.com/llm-measurement/llm-sketchkit/blob/main/SECURITY.md)
|
|
237
|
+
for private vulnerability reporting.
|
|
238
|
+
|
|
239
|
+
## Wire Compatibility
|
|
240
|
+
|
|
241
|
+
Deterministic protobuf encoding is part of the compatibility surface. Go and
|
|
242
|
+
Python are checked against the same canonicalization, hashing, sketch, and
|
|
243
|
+
cross-language merge fixtures in
|
|
244
|
+
[`vectors/`](https://github.com/llm-measurement/llm-sketchkit/tree/main/vectors/).
|
|
245
|
+
|
|
246
|
+
Run all local checks:
|
|
247
|
+
|
|
248
|
+
```sh
|
|
249
|
+
go test ./... -race
|
|
250
|
+
python -m pytest -q
|
|
251
|
+
ruff check .
|
|
252
|
+
mypy --strict
|
|
253
|
+
```
|
|
254
|
+
|
|
255
|
+
The optional Apache DataSketches comparison checks weighted frequent-items
|
|
256
|
+
query behavior against an independent implementation:
|
|
257
|
+
|
|
258
|
+
```sh
|
|
259
|
+
python -m pip install -e '.[oracle]'
|
|
260
|
+
python scripts/datasketches_oracle.py --check
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
## Reference
|
|
264
|
+
|
|
265
|
+
- [`spec/`](https://github.com/llm-measurement/llm-sketchkit/tree/main/spec/) defines canonicalization, hashing, profiles, and wire encoding.
|
|
266
|
+
- [`vectors/`](https://github.com/llm-measurement/llm-sketchkit/tree/main/vectors/) contains executable conformance fixtures.
|
|
267
|
+
- [`reports/`](https://github.com/llm-measurement/llm-sketchkit/tree/main/reports/) contains benchmark, accuracy, and oracle results.
|
|
268
|
+
- [`bench/`](https://github.com/llm-measurement/llm-sketchkit/tree/main/bench/) contains the Go benchmark harnesses.
|
|
269
|
+
- [`docs/FAQ.md`](https://github.com/llm-measurement/llm-sketchkit/blob/main/docs/FAQ.md) answers common adoption questions.
|
|
270
|
+
- [`CHANGELOG.md`](https://github.com/llm-measurement/llm-sketchkit/blob/main/CHANGELOG.md) records release-level changes.
|
|
271
|
+
|
|
272
|
+
## Status
|
|
273
|
+
|
|
274
|
+
`llm-sketchkit` is an alpha library. The compatibility surface consists of the
|
|
275
|
+
specifications, the Go and Python APIs exercised by the vectors, and the checked-in
|
|
276
|
+
conformance fixtures.
|
|
277
|
+
|
|
278
|
+
## License
|
|
279
|
+
|
|
280
|
+
Apache-2.0.
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
# llm-sketchkit
|
|
2
|
+
|
|
3
|
+
[](https://github.com/llm-measurement/llm-sketchkit/actions/workflows/ci.yml)
|
|
4
|
+
|
|
5
|
+
`llm-sketchkit` is a small Go and Python library for deterministic, mergeable
|
|
6
|
+
summaries of high-cardinality LLM data. It provides matching semantics for text
|
|
7
|
+
canonicalization, privacy-preserving keyed hashes, bounded sketches, and a
|
|
8
|
+
deterministic protobuf wire format.
|
|
9
|
+
|
|
10
|
+
Raw prompts and identifiers do not need to enter sketch state. Producers can
|
|
11
|
+
summarize locally and merge compatible sketches across processes or languages.
|
|
12
|
+
|
|
13
|
+
## When This Fits
|
|
14
|
+
|
|
15
|
+
Use `llm-sketchkit` inside telemetry producers and processing components when
|
|
16
|
+
exact per-key state would grow with cardinality or retain values that should not
|
|
17
|
+
enter aggregate state.
|
|
18
|
+
|
|
19
|
+
Typical uses include:
|
|
20
|
+
|
|
21
|
+
- estimating distinct prompts, users, sessions, tools, or documents without
|
|
22
|
+
keeping one counter per value;
|
|
23
|
+
- identifying token-heavy or request-heavy keys with deterministic lower and
|
|
24
|
+
upper bounds;
|
|
25
|
+
- testing approximate set membership for bounded deduplication;
|
|
26
|
+
- comparing large sets using fixed-size similarity signatures; and
|
|
27
|
+
- producing summaries in Go that can be read and merged in Python with the
|
|
28
|
+
same profiles and wire semantics.
|
|
29
|
+
|
|
30
|
+
Inputs can be canonicalized and keyed before entering sketch state. This keeps
|
|
31
|
+
raw values out of the sketch, but the resulting hashes remain pseudonymous and
|
|
32
|
+
linkable while the same secret is in use.
|
|
33
|
+
|
|
34
|
+
This is a sketch library, not a trace collector, sampling processor, storage
|
|
35
|
+
backend, dashboard, or differential-privacy system. The [FAQ](https://github.com/llm-measurement/llm-sketchkit/blob/main/docs/FAQ.md)
|
|
36
|
+
answers common questions about fit, memory, accuracy, and interoperability.
|
|
37
|
+
|
|
38
|
+
## Included Sketches
|
|
39
|
+
|
|
40
|
+
| Component | Use it for | Important property |
|
|
41
|
+
|---|---|---|
|
|
42
|
+
| HLL++ | Approximate distinct counts | Fixed memory and mergeable state |
|
|
43
|
+
| Weighted frequent-items | Heavy hitters and top items | Deterministic lower and upper bounds |
|
|
44
|
+
| Bloom filter | Set membership | No false negatives; configurable false-positive rate |
|
|
45
|
+
| MinHash | Approximate Jaccard similarity | Fixed-size mergeable signatures |
|
|
46
|
+
|
|
47
|
+
The Go and Python implementations share the same profiles, hash domains, test
|
|
48
|
+
vectors, and serialized representation.
|
|
49
|
+
|
|
50
|
+
## Evidence At A Glance
|
|
51
|
+
|
|
52
|
+
Measurements use deterministic workloads and report the least favorable of five
|
|
53
|
+
Linux benchmark runs where applicable.
|
|
54
|
+
|
|
55
|
+
- HMAC-SHA256-64 sustained at least **1.65 million 64-byte inputs/s/core** and
|
|
56
|
+
**850,340 1 KiB inputs/s/core** on an Intel Xeon Platinum 8573C with Go 1.26.5.
|
|
57
|
+
- HLL++ and weighted frequent-items updates took at most **10.58 ns/op** and
|
|
58
|
+
**145.6 ns/op**, respectively, with **0 allocations/op** in the measured paths.
|
|
59
|
+
- The HLL++ `small` profile's maximum observed relative error was **2.3301%**
|
|
60
|
+
across the characterization grid, within its **2.4375%** enforced bound.
|
|
61
|
+
- Bloom profile false-positive rates were at or below their configured targets
|
|
62
|
+
in the measured trials, with zero false negatives among inserted hashes.
|
|
63
|
+
- MinHash mean absolute error fell from **0.02845** at `k=128` to **0.02009** at
|
|
64
|
+
`k=256`, closely following the expected inverse-square-root relationship.
|
|
65
|
+
- Both weighted frequent-items oracle workloads retained **100% true top-20
|
|
66
|
+
recall** and valid no-false-positive query results in both implementations.
|
|
67
|
+
|
|
68
|
+
See the [visual scorecard](https://github.com/llm-measurement/llm-sketchkit/blob/main/reports/scorecard.md),
|
|
69
|
+
[raw measurement records](https://github.com/llm-measurement/llm-sketchkit/blob/main/reports/README.md),
|
|
70
|
+
and [DataSketches implementation rationale](https://github.com/llm-measurement/llm-sketchkit/blob/main/docs/DATASKETCHES.md)
|
|
71
|
+
for methods, limitations, and reproduction commands.
|
|
72
|
+
|
|
73
|
+
## Requirements
|
|
74
|
+
|
|
75
|
+
- Go 1.25 or newer, with the latest security patch for that release line
|
|
76
|
+
- Python 3.11 or newer
|
|
77
|
+
|
|
78
|
+
## Install
|
|
79
|
+
|
|
80
|
+
Go:
|
|
81
|
+
|
|
82
|
+
```sh
|
|
83
|
+
go get github.com/llm-measurement/llm-sketchkit@latest
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Python from a checkout:
|
|
87
|
+
|
|
88
|
+
```sh
|
|
89
|
+
git clone https://github.com/llm-measurement/llm-sketchkit.git
|
|
90
|
+
cd llm-sketchkit
|
|
91
|
+
python -m venv .venv
|
|
92
|
+
source .venv/bin/activate
|
|
93
|
+
python -m pip install --upgrade pip==26.2 setuptools==83.0.0
|
|
94
|
+
python -m pip install .
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
For development checks, install the optional tools:
|
|
98
|
+
|
|
99
|
+
```sh
|
|
100
|
+
python -m pip install -e '.[dev]'
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
## Quick Start
|
|
104
|
+
|
|
105
|
+
Generate a deployment secret and expose it to the process:
|
|
106
|
+
|
|
107
|
+
```sh
|
|
108
|
+
export LLM_SKETCHKIT_SECRET="$(python -c 'import secrets; print(secrets.token_hex(32))')"
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Go HLL++ example:
|
|
112
|
+
|
|
113
|
+
```go
|
|
114
|
+
package main
|
|
115
|
+
|
|
116
|
+
import (
|
|
117
|
+
"fmt"
|
|
118
|
+
"log"
|
|
119
|
+
|
|
120
|
+
"github.com/llm-measurement/llm-sketchkit/go/sketchkit/canon"
|
|
121
|
+
sketchhash "github.com/llm-measurement/llm-sketchkit/go/sketchkit/hash"
|
|
122
|
+
"github.com/llm-measurement/llm-sketchkit/go/sketchkit/hllpp"
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
func main() {
|
|
126
|
+
secret, err := sketchhash.SecretFromEnv("LLM_SKETCHKIT_SECRET")
|
|
127
|
+
if err != nil {
|
|
128
|
+
log.Fatal(err)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
sketch, err := hllpp.New(
|
|
132
|
+
hllpp.ProfileSmall,
|
|
133
|
+
sketchhash.PromptV1,
|
|
134
|
+
sketchhash.HMACSHA25664,
|
|
135
|
+
)
|
|
136
|
+
if err != nil {
|
|
137
|
+
log.Fatal(err)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
canonical, err := canon.CanonicalizeString(canon.TextV1, " cafe\u0301\r\n")
|
|
141
|
+
if err != nil {
|
|
142
|
+
log.Fatal(err)
|
|
143
|
+
}
|
|
144
|
+
digest, err := sketchhash.Hash64(secret, sketchhash.PromptV1, canonical)
|
|
145
|
+
if err != nil {
|
|
146
|
+
log.Fatal(err)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
sketch.AddHash(digest)
|
|
150
|
+
fmt.Printf("estimated distinct prompts: %.0f\n", sketch.Estimate())
|
|
151
|
+
}
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Python HLL++ example:
|
|
155
|
+
|
|
156
|
+
```python
|
|
157
|
+
from llm_sketchkit import PROMPT_V1, canonicalize_text_v1, hash64, hllpp
|
|
158
|
+
from llm_sketchkit import secret_from_env
|
|
159
|
+
|
|
160
|
+
secret = secret_from_env("LLM_SKETCHKIT_SECRET")
|
|
161
|
+
sketch = hllpp.new("small", PROMPT_V1)
|
|
162
|
+
|
|
163
|
+
canonical = canonicalize_text_v1(" cafe\u0301\r\n")
|
|
164
|
+
sketch.add_hash(hash64(secret, PROMPT_V1, canonical))
|
|
165
|
+
|
|
166
|
+
print(f"estimated distinct prompts: {sketch.estimate():.0f}")
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
## Merge Sketches
|
|
170
|
+
|
|
171
|
+
Sketches merge only when their kind, profile, hash domain, hash algorithm, and
|
|
172
|
+
shape metadata match. A mismatch is an error rather than an implicit conversion.
|
|
173
|
+
|
|
174
|
+
```python
|
|
175
|
+
left = hllpp.new("small", PROMPT_V1)
|
|
176
|
+
right = hllpp.new("small", PROMPT_V1)
|
|
177
|
+
|
|
178
|
+
left.add_hash(hash64(secret, PROMPT_V1, canonicalize_text_v1("alpha")))
|
|
179
|
+
right.add_hash(hash64(secret, PROMPT_V1, canonicalize_text_v1("beta")))
|
|
180
|
+
|
|
181
|
+
left.merge(right)
|
|
182
|
+
print(f"merged distinct prompts: {left.estimate():.0f}")
|
|
183
|
+
```
|
|
184
|
+
|
|
185
|
+
## Security And Privacy
|
|
186
|
+
|
|
187
|
+
- Hash inputs with a registered domain and a high-entropy secret before adding
|
|
188
|
+
them to a sketch. The built-in secret loaders require at least 16 bytes and
|
|
189
|
+
reject known placeholder values.
|
|
190
|
+
- Keyed hashes are pseudonymous, not anonymous. Anyone with the secret can test
|
|
191
|
+
candidate values, and repeated hashes remain linkable while the same secret
|
|
192
|
+
and domain are in use.
|
|
193
|
+
- Never log, serialize, or commit the hash secret. Rotate it when the trust
|
|
194
|
+
boundary changes; rotation intentionally breaks comparison with older state.
|
|
195
|
+
- Bound raw input size before canonicalization. Canonicalization operates on
|
|
196
|
+
in-memory text and intentionally leaves application-specific limits to callers.
|
|
197
|
+
- Sketches reveal bounded aggregate information and may reveal membership or
|
|
198
|
+
recurrence. They do not provide differential privacy.
|
|
199
|
+
- Treat serialized sketches as untrusted input at process boundaries. The parse
|
|
200
|
+
APIs cap input size and reject invalid profiles, domains, shapes, counters, and
|
|
201
|
+
register values.
|
|
202
|
+
|
|
203
|
+
See [SECURITY.md](https://github.com/llm-measurement/llm-sketchkit/blob/main/SECURITY.md)
|
|
204
|
+
for private vulnerability reporting.
|
|
205
|
+
|
|
206
|
+
## Wire Compatibility
|
|
207
|
+
|
|
208
|
+
Deterministic protobuf encoding is part of the compatibility surface. Go and
|
|
209
|
+
Python are checked against the same canonicalization, hashing, sketch, and
|
|
210
|
+
cross-language merge fixtures in
|
|
211
|
+
[`vectors/`](https://github.com/llm-measurement/llm-sketchkit/tree/main/vectors/).
|
|
212
|
+
|
|
213
|
+
Run all local checks:
|
|
214
|
+
|
|
215
|
+
```sh
|
|
216
|
+
go test ./... -race
|
|
217
|
+
python -m pytest -q
|
|
218
|
+
ruff check .
|
|
219
|
+
mypy --strict
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
The optional Apache DataSketches comparison checks weighted frequent-items
|
|
223
|
+
query behavior against an independent implementation:
|
|
224
|
+
|
|
225
|
+
```sh
|
|
226
|
+
python -m pip install -e '.[oracle]'
|
|
227
|
+
python scripts/datasketches_oracle.py --check
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
## Reference
|
|
231
|
+
|
|
232
|
+
- [`spec/`](https://github.com/llm-measurement/llm-sketchkit/tree/main/spec/) defines canonicalization, hashing, profiles, and wire encoding.
|
|
233
|
+
- [`vectors/`](https://github.com/llm-measurement/llm-sketchkit/tree/main/vectors/) contains executable conformance fixtures.
|
|
234
|
+
- [`reports/`](https://github.com/llm-measurement/llm-sketchkit/tree/main/reports/) contains benchmark, accuracy, and oracle results.
|
|
235
|
+
- [`bench/`](https://github.com/llm-measurement/llm-sketchkit/tree/main/bench/) contains the Go benchmark harnesses.
|
|
236
|
+
- [`docs/FAQ.md`](https://github.com/llm-measurement/llm-sketchkit/blob/main/docs/FAQ.md) answers common adoption questions.
|
|
237
|
+
- [`CHANGELOG.md`](https://github.com/llm-measurement/llm-sketchkit/blob/main/CHANGELOG.md) records release-level changes.
|
|
238
|
+
|
|
239
|
+
## Status
|
|
240
|
+
|
|
241
|
+
`llm-sketchkit` is an alpha library. The compatibility surface consists of the
|
|
242
|
+
specifications, the Go and Python APIs exercised by the vectors, and the checked-in
|
|
243
|
+
conformance fixtures.
|
|
244
|
+
|
|
245
|
+
## License
|
|
246
|
+
|
|
247
|
+
Apache-2.0.
|