@zukhruf/embedding-cache 0.0.0-stage → 0.8.35
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.
- package/LICENSE +21 -0
- package/README.md +120 -2
- package/dist/float64.d.ts +4 -0
- package/dist/float64.d.ts.map +1 -0
- package/dist/float64.js +13 -0
- package/dist/float64.js.map +1 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +60 -0
- package/dist/index.js.map +1 -0
- package/dist/sqlite.d.ts +18 -0
- package/dist/sqlite.d.ts.map +1 -0
- package/dist/sqlite.js +44 -0
- package/dist/sqlite.js.map +1 -0
- package/dist/sqlserver.d.ts +30 -0
- package/dist/sqlserver.d.ts.map +1 -0
- package/dist/sqlserver.js +64 -0
- package/dist/sqlserver.js.map +1 -0
- package/dist/table-name.d.ts +6 -0
- package/dist/table-name.d.ts.map +1 -0
- package/dist/table-name.js +11 -0
- package/dist/table-name.js.map +1 -0
- package/package.json +62 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 ezzabuzaid
|
|
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.
|
package/README.md
CHANGED
|
@@ -1,3 +1,121 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @zukhruf/embedding-cache
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
> Ezz accepted each ADR on 2026-11-19. Built and tested, not yet on npm.
|
|
4
|
+
|
|
5
|
+
A model call is the slow and paid part of an embedding. This package wraps an AI SDK embedding model so that each `embed` and `embedMany` call through it calls the model only for text that it did not embed before. A hit gives back exactly the numbers that the model gave the first time.
|
|
6
|
+
|
|
7
|
+
The words in these documents have one meaning each. See the glossary in [CONTEXT.md](./CONTEXT.md).
|
|
8
|
+
|
|
9
|
+
## Use it
|
|
10
|
+
|
|
11
|
+
```sh
|
|
12
|
+
npm install @zukhruf/embedding-cache ai
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import { embedMany, wrapEmbeddingModel } from 'ai';
|
|
17
|
+
|
|
18
|
+
import { embeddingCacheMiddleware } from '@zukhruf/embedding-cache';
|
|
19
|
+
import { SqliteEmbeddingCacheStore } from '@zukhruf/embedding-cache/sqlite';
|
|
20
|
+
|
|
21
|
+
const model = wrapEmbeddingModel({
|
|
22
|
+
model: openai.embedding('text-embedding-3-small'),
|
|
23
|
+
middleware: embeddingCacheMiddleware({
|
|
24
|
+
store: new SqliteEmbeddingCacheStore({
|
|
25
|
+
database,
|
|
26
|
+
table: 'embedding_cache',
|
|
27
|
+
}),
|
|
28
|
+
}),
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const { embeddings } = await embedMany({ model, values: texts });
|
|
32
|
+
// Only the texts that the store does not hold reach OpenAI.
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
A model with other prompts for documents and for queries is two wrapped models with two model ids; their keys never meet ([ADR 0003](./docs/adr/0003-the-key-is-the-sha256-of-the-provider-the-model-id-the-provider-options-and-the-text.md)).
|
|
36
|
+
|
|
37
|
+
### Keep the cache in SQL Server
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { SqlServerEmbeddingCacheStore } from '@zukhruf/embedding-cache/sqlserver';
|
|
41
|
+
|
|
42
|
+
const store = new SqlServerEmbeddingCacheStore({
|
|
43
|
+
table: 'dbo.embedding_cache',
|
|
44
|
+
session: {
|
|
45
|
+
async query(sql, parameters) {
|
|
46
|
+
const request = pool.request();
|
|
47
|
+
for (const [name, value] of Object.entries(parameters))
|
|
48
|
+
request.input(name, value);
|
|
49
|
+
// Let the driver's error through as it is: the store reads its `number`
|
|
50
|
+
// (208: no table yet, so it creates one).
|
|
51
|
+
return (await request.query(sql)).recordset;
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
});
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## API
|
|
58
|
+
|
|
59
|
+
```ts
|
|
60
|
+
// @zukhruf/embedding-cache
|
|
61
|
+
function embeddingCacheMiddleware(options: {
|
|
62
|
+
store: EmbeddingCacheStore;
|
|
63
|
+
}): EmbeddingModelMiddleware;
|
|
64
|
+
|
|
65
|
+
interface EmbeddingCacheStore {
|
|
66
|
+
get(keys: readonly string[]): Promise<ReadonlyMap<string, readonly number[]>>; // only the keys it holds
|
|
67
|
+
set(entries: ReadonlyMap<string, readonly number[]>): Promise<void>;
|
|
68
|
+
}
|
|
69
|
+
class MemoryEmbeddingCacheStore implements EmbeddingCacheStore {}
|
|
70
|
+
|
|
71
|
+
// @zukhruf/embedding-cache/sqlite
|
|
72
|
+
class SqliteEmbeddingCacheStore implements EmbeddingCacheStore {
|
|
73
|
+
constructor(options: { database: DatabaseSync; table: string });
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// @zukhruf/embedding-cache/sqlserver
|
|
77
|
+
class SqlServerEmbeddingCacheStore implements EmbeddingCacheStore {
|
|
78
|
+
constructor(options: { session: SqlServerSession; table: string });
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## What a call does underneath
|
|
83
|
+
|
|
84
|
+
| Step | What happens |
|
|
85
|
+
| ------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
86
|
+
| Key | sha256 of the JSON array `[model.provider, model.modelId, params.providerOptions ?? null, params.dimensions ?? null, text]`, for each text |
|
|
87
|
+
| Read | one `store.get` with the keys of the call |
|
|
88
|
+
| Embed | one `model.doEmbed` with the misses only, each text once, with the `abortSignal`, `headers`, `providerOptions` and `dimensions` of the call. A call whose texts all hit calls no model |
|
|
89
|
+
| Write | one `store.set` with the new vectors, as the model gave them |
|
|
90
|
+
| Answer | the vectors in the order of the texts; `usage` counts the tokens of the miss call only, so a full hit reports 0 |
|
|
91
|
+
|
|
92
|
+
| Store | Where a vector lives |
|
|
93
|
+
| ------------------------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
94
|
+
| `MemoryEmbeddingCacheStore` | a `Map` in the process |
|
|
95
|
+
| `SqliteEmbeddingCacheStore` | `key TEXT PRIMARY KEY, vector BLOB`: the float64 values, little-endian |
|
|
96
|
+
| `SqlServerEmbeddingCacheStore` | `[key] VARCHAR(64) PRIMARY KEY, vector VARBINARY(MAX)`: the same bytes. Not the `VECTOR` type, which keeps float32 or float16 and would round ([ADR 0004](./docs/adr/0004-a-hit-gives-the-numbers-that-the-model-gave.md)) |
|
|
97
|
+
|
|
98
|
+
## Errors
|
|
99
|
+
|
|
100
|
+
An error of the model, of `store.get` or of `store.set` reaches the caller as it is. A failed model call writes nothing, and so does a model that answers another number of vectors than the texts it got. A SQL store answers one error itself: a read or write that finds no table creates the table and runs once more.
|
|
101
|
+
|
|
102
|
+
## The same concepts in other systems
|
|
103
|
+
|
|
104
|
+
| This package | .NET `Microsoft.Extensions.AI` | LangChain.js |
|
|
105
|
+
| -------------------------- | --------------------------------------------------------------------------------------- | ------------------------------------------------------------------------ |
|
|
106
|
+
| `embeddingCacheMiddleware` | `CachingEmbeddingGenerator<TInput, TEmbedding>`, `DistributedCachingEmbeddingGenerator` | `CacheBackedEmbeddings.fromBytesStore(underlying, store, { namespace })` |
|
|
107
|
+
| the key | `GetCacheKey(ReadOnlySpan<object?> values)` | `namespace + sha256(text)` |
|
|
108
|
+
| `EmbeddingCacheStore` | `IDistributedCache` | a byte store with `mget` and `mset` |
|
|
109
|
+
| every call is cached | every call is cached | documents only; `embedQuery` is not cached |
|
|
110
|
+
|
|
111
|
+
## Why not an npm package
|
|
112
|
+
|
|
113
|
+
Checked on 2026-11-19: npm has no embedding cache for the AI SDK (searches "ai sdk embedding cache middleware", "embedding cache wrapEmbeddingModel", "cache embeddings content hash"). LangChain's `CacheBackedEmbeddings` decorates LangChain's own `Embeddings`, not an AI SDK model.
|
|
114
|
+
|
|
115
|
+
## Who moves onto it
|
|
116
|
+
|
|
117
|
+
| Program | Its copy today | After |
|
|
118
|
+
| --------------------------- | --------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- |
|
|
119
|
+
| `@zukhruf/vector-decisions` | `src/reader/vector-cache.ts` (#4375) | the middleware and `SqliteEmbeddingCacheStore` |
|
|
120
|
+
| `@zukhruf/embeddings` | `Collection` compares the hash of `[provider, modelId, text]` (#3134) | wraps its model; dropping the hash column of its table is its own migration |
|
|
121
|
+
| `@deepagents/retrieval` | `cid`, the sha256 of a document | stays: it decides whether a document changed before it is split, and its embedder is not an AI SDK model |
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"float64.d.ts","sourceRoot":"","sources":["../src/float64.ts"],"names":[],"mappings":"AAAA,sGAAsG;AACtG,wBAAgB,KAAK,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,GAAG,MAAM,CAKvD;AAED,wBAAgB,OAAO,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,CAM7C"}
|
package/dist/float64.js
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/** The hex of the bytes of each value as a float64, little-endian: each number comes back exactly. */
|
|
2
|
+
export function toHex(vector) {
|
|
3
|
+
const bytes = new Uint8Array(vector.length * 8);
|
|
4
|
+
const view = new DataView(bytes.buffer);
|
|
5
|
+
vector.forEach((value, index) => view.setFloat64(index * 8, value, true));
|
|
6
|
+
return bytes.toHex();
|
|
7
|
+
}
|
|
8
|
+
export function fromHex(hex) {
|
|
9
|
+
const bytes = Uint8Array.fromHex(hex);
|
|
10
|
+
const view = new DataView(bytes.buffer);
|
|
11
|
+
return Array.from({ length: bytes.byteLength / 8 }, (_, index) => view.getFloat64(index * 8, true));
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=float64.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"float64.js","sourceRoot":"","sources":["../src/float64.ts"],"names":[],"mappings":"AAAA,sGAAsG;AACtG,MAAM,UAAU,KAAK,CAAC,MAAyB;IAC7C,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC;IAChD,MAAM,IAAI,GAAG,IAAI,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IACxC,MAAM,CAAC,OAAO,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,KAAK,GAAG,CAAC,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC,CAAC;IAC1E,OAAO,KAAK,CAAC,KAAK,EAAE,CAAC;AACvB,CAAC;AAED,MAAM,UAAU,OAAO,CAAC,GAAW;IACjC,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IACtC,MAAM,IAAI,GAAG,IAAI,QAAQ,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IACxC,OAAO,KAAK,CAAC,IAAI,CAAC,EAAE,MAAM,EAAE,KAAK,CAAC,UAAU,GAAG,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,KAAK,EAAE,EAAE,CAC/D,IAAI,CAAC,UAAU,CAAC,KAAK,GAAG,CAAC,EAAE,IAAI,CAAC,CACjC,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { EmbeddingModelV4Middleware } from '@ai-sdk/provider';
|
|
2
|
+
/**
|
|
3
|
+
* Where the cache keeps each key and its vector. A host can write one over
|
|
4
|
+
* its own table: the middleware needs nothing more.
|
|
5
|
+
*/
|
|
6
|
+
export interface EmbeddingCacheStore {
|
|
7
|
+
/** Gives the vector of each key that the store holds; a key it does not hold is absent. */
|
|
8
|
+
get(keys: readonly string[]): Promise<ReadonlyMap<string, readonly number[]>>;
|
|
9
|
+
/** Keeps each vector under its key. */
|
|
10
|
+
set(entries: ReadonlyMap<string, readonly number[]>): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* A middleware for `wrapEmbeddingModel` of the AI SDK. Each call reads the
|
|
14
|
+
* keys of its texts with one `store.get`, calls the model one time with the
|
|
15
|
+
* misses only, each text once, and writes their vectors with one `store.set`.
|
|
16
|
+
* A call whose texts all hit calls no model.
|
|
17
|
+
*/
|
|
18
|
+
export declare function embeddingCacheMiddleware({ store, }: {
|
|
19
|
+
store: EmbeddingCacheStore;
|
|
20
|
+
}): EmbeddingModelV4Middleware;
|
|
21
|
+
/** Keeps each vector in a `Map` of the process. */
|
|
22
|
+
export declare class MemoryEmbeddingCacheStore implements EmbeddingCacheStore {
|
|
23
|
+
#private;
|
|
24
|
+
get(keys: readonly string[]): Promise<ReadonlyMap<string, readonly number[]>>;
|
|
25
|
+
set(entries: ReadonlyMap<string, readonly number[]>): Promise<void>;
|
|
26
|
+
}
|
|
27
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAGV,0BAA0B,EAC3B,MAAM,kBAAkB,CAAC;AAE1B;;;GAGG;AACH,MAAM,WAAW,mBAAmB;IAClC,2FAA2F;IAC3F,GAAG,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,GAAG,OAAO,CAAC,WAAW,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC,CAAC,CAAC;IAC9E,uCAAuC;IACvC,GAAG,CAAC,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CACrE;AAED;;;;;GAKG;AACH,wBAAgB,wBAAwB,CAAC,EACvC,KAAK,GACN,EAAE;IACD,KAAK,EAAE,mBAAmB,CAAC;CAC5B,GAAG,0BAA0B,CAuC7B;AAmBD,mDAAmD;AACnD,qBAAa,yBAA0B,YAAW,mBAAmB;;IAG7D,GAAG,CACP,IAAI,EAAE,SAAS,MAAM,EAAE,GACtB,OAAO,CAAC,WAAW,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC,CAAC;IAS5C,GAAG,CAAC,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;CAI1E"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { hash } from 'node:crypto';
|
|
2
|
+
/**
|
|
3
|
+
* A middleware for `wrapEmbeddingModel` of the AI SDK. Each call reads the
|
|
4
|
+
* keys of its texts with one `store.get`, calls the model one time with the
|
|
5
|
+
* misses only, each text once, and writes their vectors with one `store.set`.
|
|
6
|
+
* A call whose texts all hit calls no model.
|
|
7
|
+
*/
|
|
8
|
+
export function embeddingCacheMiddleware({ store, }) {
|
|
9
|
+
return {
|
|
10
|
+
specificationVersion: 'v4',
|
|
11
|
+
async wrapEmbed({ params, model }) {
|
|
12
|
+
const keys = params.values.map((text) => cacheKey(model, params, text));
|
|
13
|
+
const hits = await store.get(keys);
|
|
14
|
+
const misses = new Map(keys.flatMap((key, index) => hits.has(key) ? [] : [[key, params.values[index]]]));
|
|
15
|
+
const inOrder = (vectors) => keys.map((key) => [...vectors.get(key)]);
|
|
16
|
+
if (misses.size === 0)
|
|
17
|
+
return {
|
|
18
|
+
embeddings: inOrder(hits),
|
|
19
|
+
usage: { tokens: 0 },
|
|
20
|
+
warnings: [],
|
|
21
|
+
};
|
|
22
|
+
const result = await model.doEmbed({
|
|
23
|
+
...params,
|
|
24
|
+
values: [...misses.values()],
|
|
25
|
+
});
|
|
26
|
+
const missKeys = [...misses.keys()];
|
|
27
|
+
if (result.embeddings.length !== missKeys.length)
|
|
28
|
+
throw new Error(`The model gave ${result.embeddings.length} vectors for ${missKeys.length} texts.`);
|
|
29
|
+
// The check above gives each vector of the model one key of a miss.
|
|
30
|
+
const fresh = new Map(result.embeddings.map((vector, index) => [missKeys[index], vector]));
|
|
31
|
+
await store.set(fresh);
|
|
32
|
+
return { ...result, embeddings: inOrder(new Map([...hits, ...fresh])) };
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function cacheKey(model, params, text) {
|
|
37
|
+
return hash('sha256', JSON.stringify([
|
|
38
|
+
model.provider,
|
|
39
|
+
model.modelId,
|
|
40
|
+
params.providerOptions ?? null,
|
|
41
|
+
params.dimensions ?? null,
|
|
42
|
+
text,
|
|
43
|
+
]));
|
|
44
|
+
}
|
|
45
|
+
/** Keeps each vector in a `Map` of the process. */
|
|
46
|
+
export class MemoryEmbeddingCacheStore {
|
|
47
|
+
#entries = new Map();
|
|
48
|
+
async get(keys) {
|
|
49
|
+
return new Map(keys.flatMap((key) => {
|
|
50
|
+
const vector = this.#entries.get(key);
|
|
51
|
+
return vector === undefined ? [] : [[key, vector]];
|
|
52
|
+
}));
|
|
53
|
+
}
|
|
54
|
+
async set(entries) {
|
|
55
|
+
for (const [key, vector] of entries)
|
|
56
|
+
if (!this.#entries.has(key))
|
|
57
|
+
this.#entries.set(key, [...vector]);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,aAAa,CAAC;AAmBnC;;;;;GAKG;AACH,MAAM,UAAU,wBAAwB,CAAC,EACvC,KAAK,GAGN;IACC,OAAO;QACL,oBAAoB,EAAE,IAAI;QAC1B,KAAK,CAAC,SAAS,CAAC,EAAE,MAAM,EAAE,KAAK,EAAE;YAC/B,MAAM,IAAI,GAAG,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,CAAC,CAAC,CAAC;YACxE,MAAM,IAAI,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;YACnC,MAAM,MAAM,GAAG,IAAI,GAAG,CACpB,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,KAAK,EAAE,EAAE,CAC1B,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,CAAC,KAAK,CAAE,CAAU,CAAC,CAC7D,CACF,CAAC;YACF,MAAM,OAAO,GAAG,CAAC,OAA+C,EAAE,EAAE,CAClE,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,GAAG,OAAO,CAAC,GAAG,CAAC,GAAG,CAAE,CAAC,CAAC,C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I;QAC9B,MAAM,CAAC,UAAU,IAAI,IAAI;QACzB,IAAI;KACL,CAAC,CACH,CAAC;AACJ,CAAC;AAED,mDAAmD;AACnD,MAAM,OAAO,yBAAyB;IAC3B,QAAQ,GAAG,IAAI,GAAG,EAA6B,CAAC;IAEzD,KAAK,CAAC,GAAG,CACP,IAAuB;QAEvB,OAAO,IAAI,GAAG,CACZ,IAAI,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;YACnB,MAAM,MAAM,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YACtC,OAAO,MAAM,KAAK,SAAS,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,EAAE,MAAM,CAAU,CAAC,CAAC;QAC9D,CAAC,CAAC,CACH,CAAC;IACJ,CAAC;IAED,KAAK,CAAC,GAAG,CAAC,OAA+C;QACvD,KAAK,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,IAAI,OAAO;YACjC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC;gBAAE,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC,GAAG,MAAM,CAAC,CAAC,CAAC;IACrE,CAAC;CACF"}
|
package/dist/sqlite.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { DatabaseSync } from 'node:sqlite';
|
|
2
|
+
import type { EmbeddingCacheStore } from './index.ts';
|
|
3
|
+
/**
|
|
4
|
+
* Keeps each vector in a SQLite table, `key TEXT PRIMARY KEY, vector BLOB`,
|
|
5
|
+
* as the bytes of its float64 values. A read or write that finds no table
|
|
6
|
+
* creates it and runs again; an existing table keeps its other columns and
|
|
7
|
+
* rows.
|
|
8
|
+
*/
|
|
9
|
+
export declare class SqliteEmbeddingCacheStore implements EmbeddingCacheStore {
|
|
10
|
+
#private;
|
|
11
|
+
constructor({ database, table }: {
|
|
12
|
+
database: DatabaseSync;
|
|
13
|
+
table: string;
|
|
14
|
+
});
|
|
15
|
+
get(keys: readonly string[]): Promise<ReadonlyMap<string, readonly number[]>>;
|
|
16
|
+
set(entries: ReadonlyMap<string, readonly number[]>): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=sqlite.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlite.d.ts","sourceRoot":"","sources":["../src/sqlite.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAGhD,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAGtD;;;;;GAKG;AACH,qBAAa,yBAA0B,YAAW,mBAAmB;;gBAIvD,EAAE,QAAQ,EAAE,KAAK,EAAE,EAAE;QAAE,QAAQ,EAAE,YAAY,CAAC;QAAC,KAAK,EAAE,MAAM,CAAA;KAAE;IAOpE,GAAG,CACP,IAAI,EAAE,SAAS,MAAM,EAAE,GACtB,OAAO,CAAC,WAAW,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC,CAAC;IAa5C,GAAG,CAAC,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;CA6B1E"}
|
package/dist/sqlite.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { fromHex, toHex } from "./float64.js";
|
|
2
|
+
import { tableNameParts } from "./table-name.js";
|
|
3
|
+
/**
|
|
4
|
+
* Keeps each vector in a SQLite table, `key TEXT PRIMARY KEY, vector BLOB`,
|
|
5
|
+
* as the bytes of its float64 values. A read or write that finds no table
|
|
6
|
+
* creates it and runs again; an existing table keeps its other columns and
|
|
7
|
+
* rows.
|
|
8
|
+
*/
|
|
9
|
+
export class SqliteEmbeddingCacheStore {
|
|
10
|
+
#database;
|
|
11
|
+
#table;
|
|
12
|
+
constructor({ database, table }) {
|
|
13
|
+
this.#database = database;
|
|
14
|
+
this.#table = tableNameParts(table)
|
|
15
|
+
.map((part) => `"${part}"`)
|
|
16
|
+
.join('.');
|
|
17
|
+
}
|
|
18
|
+
async get(keys) {
|
|
19
|
+
const rows = this.#creatingTable(() => this.#database
|
|
20
|
+
.prepare(`SELECT key, hex(vector) AS vector FROM ${this.#table} WHERE key IN (SELECT value FROM json_each(?))`)
|
|
21
|
+
.all(JSON.stringify(keys)));
|
|
22
|
+
return new Map(rows.map((row) => [String(row['key']), fromHex(String(row['vector']))]));
|
|
23
|
+
}
|
|
24
|
+
async set(entries) {
|
|
25
|
+
this.#creatingTable(() => this.#database
|
|
26
|
+
.prepare(`INSERT INTO ${this.#table} (key, vector)
|
|
27
|
+
SELECT value ->> 0, unhex(value ->> 1) FROM json_each(?) WHERE true
|
|
28
|
+
ON CONFLICT (key) DO NOTHING`)
|
|
29
|
+
.run(JSON.stringify([...entries].map(([key, vector]) => [key, toHex(vector)]))));
|
|
30
|
+
}
|
|
31
|
+
/** Runs the statement; when it finds no table, creates the table and runs it once more. */
|
|
32
|
+
#creatingTable(statement) {
|
|
33
|
+
try {
|
|
34
|
+
return statement();
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
if (!(Error.isError(error) && error.message.startsWith('no such table:')))
|
|
38
|
+
throw error;
|
|
39
|
+
this.#database.exec(`CREATE TABLE IF NOT EXISTS ${this.#table} (key TEXT PRIMARY KEY, vector BLOB NOT NULL)`);
|
|
40
|
+
return statement();
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
//# sourceMappingURL=sqlite.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlite.js","sourceRoot":"","sources":["../src/sqlite.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAE9C,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAEj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,CAAI,SAAkB;QAClC,IAAI,CAAC;YACH,OAAO,SAAS,EAAE,CAAC;QACrB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,IAAI,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,gBAAgB,CAAC,CAAC;gBACvE,MAAM,KAAK,CAAC;YACd,IAAI,CAAC,SAAS,CAAC,IAAI,CACjB,8BAA8B,IAAI,CAAC,MAAM,+CAA+C,CACzF,CAAC;YACF,OAAO,SAAS,EAAE,CAAC;QACrB,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { EmbeddingCacheStore } from './index.ts';
|
|
2
|
+
/**
|
|
3
|
+
* One connection to SQL Server: only the method that the store calls, so the
|
|
4
|
+
* package imports no driver. A host writes it over its own driver.
|
|
5
|
+
*/
|
|
6
|
+
export interface SqlServerSession {
|
|
7
|
+
/**
|
|
8
|
+
* Runs one batch with named parameters. Resolves to the rows of the batch.
|
|
9
|
+
* Rejects with the error of the driver as it is: the store reads its
|
|
10
|
+
* `number` (208 for a missing table, 2714 for a table that exists).
|
|
11
|
+
*/
|
|
12
|
+
query(sql: string, parameters: Record<string, unknown>): Promise<Record<string, unknown>[]>;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Keeps each vector in a SQL Server table, `[key] VARCHAR(64) PRIMARY KEY,
|
|
16
|
+
* vector VARBINARY(MAX)`, as the bytes of its float64 values. Not the
|
|
17
|
+
* `VECTOR` type, which keeps float32 or float16 and would round. A read or
|
|
18
|
+
* write that finds no table creates it and runs again. Each parameter and
|
|
19
|
+
* each column it reads is a string, so no driver has to map a binary type.
|
|
20
|
+
*/
|
|
21
|
+
export declare class SqlServerEmbeddingCacheStore implements EmbeddingCacheStore {
|
|
22
|
+
#private;
|
|
23
|
+
constructor({ session, table, }: {
|
|
24
|
+
session: SqlServerSession;
|
|
25
|
+
table: string;
|
|
26
|
+
});
|
|
27
|
+
get(keys: readonly string[]): Promise<ReadonlyMap<string, readonly number[]>>;
|
|
28
|
+
set(entries: ReadonlyMap<string, readonly number[]>): Promise<void>;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=sqlserver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlserver.d.ts","sourceRoot":"","sources":["../src/sqlserver.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AAGtD;;;GAGG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;;OAIG;IACH,KAAK,CACH,GAAG,EAAE,MAAM,EACX,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAClC,OAAO,CAAC,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAC,CAAC;CACvC;AAMD;;;;;;GAMG;AACH,qBAAa,4BAA6B,YAAW,mBAAmB;;gBAI1D,EACV,OAAO,EACP,KAAK,GACN,EAAE;QACD,OAAO,EAAE,gBAAgB,CAAC;QAC1B,KAAK,EAAE,MAAM,CAAC;KACf;IAOK,GAAG,CACP,IAAI,EAAE,SAAS,MAAM,EAAE,GACtB,OAAO,CAAC,WAAW,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC,CAAC;IAa5C,GAAG,CAAC,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,SAAS,MAAM,EAAE,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;CAwC1E"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { fromHex, toHex } from "./float64.js";
|
|
2
|
+
import { tableNameParts } from "./table-name.js";
|
|
3
|
+
/** SQL Server's error numbers that the store answers; any other error reaches the caller. */
|
|
4
|
+
const invalidObjectName = 208;
|
|
5
|
+
const objectExists = 2714;
|
|
6
|
+
/**
|
|
7
|
+
* Keeps each vector in a SQL Server table, `[key] VARCHAR(64) PRIMARY KEY,
|
|
8
|
+
* vector VARBINARY(MAX)`, as the bytes of its float64 values. Not the
|
|
9
|
+
* `VECTOR` type, which keeps float32 or float16 and would round. A read or
|
|
10
|
+
* write that finds no table creates it and runs again. Each parameter and
|
|
11
|
+
* each column it reads is a string, so no driver has to map a binary type.
|
|
12
|
+
*/
|
|
13
|
+
export class SqlServerEmbeddingCacheStore {
|
|
14
|
+
#session;
|
|
15
|
+
#table;
|
|
16
|
+
constructor({ session, table, }) {
|
|
17
|
+
this.#session = session;
|
|
18
|
+
this.#table = tableNameParts(table)
|
|
19
|
+
.map((part) => `[${part}]`)
|
|
20
|
+
.join('.');
|
|
21
|
+
}
|
|
22
|
+
async get(keys) {
|
|
23
|
+
const rows = await this.#creatingTable(() => this.#session.query(`SELECT [key], CONVERT(VARCHAR(MAX), vector, 2) AS vector FROM ${this.#table}
|
|
24
|
+
WHERE [key] IN (SELECT [key] FROM OPENJSON(@keys) WITH ([key] VARCHAR(MAX) '$'))`, { keys: JSON.stringify(keys) }));
|
|
25
|
+
return new Map(rows.map((row) => [String(row['key']), fromHex(String(row['vector']))]));
|
|
26
|
+
}
|
|
27
|
+
async set(entries) {
|
|
28
|
+
await this.#creatingTable(() => this.#session.query(`MERGE ${this.#table} WITH (HOLDLOCK) AS target
|
|
29
|
+
USING (
|
|
30
|
+
SELECT [key], CONVERT(VARBINARY(MAX), vector, 2) AS vector
|
|
31
|
+
FROM OPENJSON(@entries) WITH ([key] VARCHAR(MAX) '$[0]', vector VARCHAR(MAX) '$[1]')
|
|
32
|
+
) AS source
|
|
33
|
+
ON target.[key] = source.[key]
|
|
34
|
+
WHEN NOT MATCHED THEN INSERT ([key], vector) VALUES (source.[key], source.vector);`, {
|
|
35
|
+
entries: JSON.stringify([...entries].map(([key, vector]) => [key, toHex(vector)])),
|
|
36
|
+
}));
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Runs the batch; when it finds no table, creates the table and runs it
|
|
40
|
+
* once more. A create that finds the table made by a concurrent store
|
|
41
|
+
* counts as done.
|
|
42
|
+
*/
|
|
43
|
+
async #creatingTable(batch) {
|
|
44
|
+
try {
|
|
45
|
+
return await batch();
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
if (errorNumber(error) !== invalidObjectName)
|
|
49
|
+
throw error;
|
|
50
|
+
try {
|
|
51
|
+
await this.#session.query(`CREATE TABLE ${this.#table} ([key] VARCHAR(64) NOT NULL PRIMARY KEY, vector VARBINARY(MAX) NOT NULL)`, {});
|
|
52
|
+
}
|
|
53
|
+
catch (createError) {
|
|
54
|
+
if (errorNumber(createError) !== objectExists)
|
|
55
|
+
throw createError;
|
|
56
|
+
}
|
|
57
|
+
return await batch();
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function errorNumber(error) {
|
|
62
|
+
return Error.isError(error) && 'number' in error ? error.number : undefined;
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=sqlserver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sqlserver.js","sourceRoot":"","sources":["../src/sqlserver.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAE9C,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAkBjD,6FAA6F;AAC7F,MAAM,iBAAiB,GAAG,GAAG,CAAC;AAC9B,MAAM,YAAY,GAAG,IAAI,CAAC;AAE1B;;;;;;GAMG;AACH,MAAM,OAAO,4BAA4B;IAC9B,QAAQ,CAAmB;IAC3B,MAAM,CAAS;IAExB,YAAY,EACV,OAAO,EACP,KAAK,GAIN;QACC,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;QACxB,IAAI,CAAC,MAAM,GAAG,cAAc,CAAC,KAAK,CAAC;aAChC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,IAAI,GAAG,CAAC;aAC1B,IAAI,CAAC,GAA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CAAC,MAAM,2EAA2E,EACtG,EAAE,CACH,CAAC;YACJ,CAAC;YAAC,OAAO,WAAW,EAAE,CAAC;gBACrB,IAAI,WAAW,CAAC,WAAW,CAAC,KAAK,YAAY;oBAAE,MAAM,WAAW,CAAC;YACnE,CAAC;YACD,OAAO,MAAM,KAAK,EAAE,CAAC;QACvB,CAAC;IACH,CAAC;CACF;AAED,SAAS,WAAW,CAAC,KAAc;IACjC,OAAO,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,QAAQ,IAAI,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAC;AAC9E,CAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The parts of a table name, an optional schema and the name. The name goes
|
|
3
|
+
* into the SQL text, quoted, so only letters, digits and underscores pass.
|
|
4
|
+
*/
|
|
5
|
+
export declare function tableNameParts(table: string): string[];
|
|
6
|
+
//# sourceMappingURL=table-name.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"table-name.d.ts","sourceRoot":"","sources":["../src/table-name.ts"],"names":[],"mappings":"AAEA;;;GAGG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,CAMtD"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const identifier = /^[A-Za-z_][A-Za-z0-9_]*(\.[A-Za-z_][A-Za-z0-9_]*)?$/;
|
|
2
|
+
/**
|
|
3
|
+
* The parts of a table name, an optional schema and the name. The name goes
|
|
4
|
+
* into the SQL text, quoted, so only letters, digits and underscores pass.
|
|
5
|
+
*/
|
|
6
|
+
export function tableNameParts(table) {
|
|
7
|
+
if (!identifier.test(table))
|
|
8
|
+
throw new TypeError(`The table name ${JSON.stringify(table)} is not an identifier: use letters, digits and underscores, with an optional schema before a dot.`);
|
|
9
|
+
return table.split('.');
|
|
10
|
+
}
|
|
11
|
+
//# sourceMappingURL=table-name.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"table-name.js","sourceRoot":"","sources":["../src/table-name.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,GAAG,qDAAqD,CAAC;AAEzE;;;GAGG;AACH,MAAM,UAAU,cAAc,CAAC,KAAa;IAC1C,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC;QACzB,MAAM,IAAI,SAAS,CACjB,kBAAkB,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,mGAAmG,CAC3I,CAAC;IACJ,OAAO,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;AAC1B,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,64 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zukhruf/embedding-cache",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.8.35",
|
|
4
|
+
"description": "An AI SDK embedding middleware that calls the model only for new text and gives back exactly the numbers it gave, kept in memory, SQLite or SQL Server. After .NET's CachingEmbeddingGenerator.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/JanuaryLabs/zukhruf",
|
|
9
|
+
"directory": "packages/embedding-cache"
|
|
10
|
+
},
|
|
11
|
+
"bugs": {
|
|
12
|
+
"url": "https://github.com/JanuaryLabs/zukhruf/issues"
|
|
13
|
+
},
|
|
14
|
+
"homepage": "https://github.com/JanuaryLabs/zukhruf/tree/main/packages/embedding-cache#readme",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"embedding",
|
|
17
|
+
"cache",
|
|
18
|
+
"ai-sdk",
|
|
19
|
+
"middleware",
|
|
20
|
+
"sqlite",
|
|
21
|
+
"sql-server"
|
|
22
|
+
],
|
|
23
|
+
"type": "module",
|
|
24
|
+
"exports": {
|
|
25
|
+
"./package.json": "./package.json",
|
|
26
|
+
".": {
|
|
27
|
+
"@zukhruf/source": "./src/index.ts",
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"import": "./dist/index.js",
|
|
30
|
+
"default": "./dist/index.js"
|
|
31
|
+
},
|
|
32
|
+
"./sqlite": {
|
|
33
|
+
"@zukhruf/source": "./src/sqlite.ts",
|
|
34
|
+
"types": "./dist/sqlite.d.ts",
|
|
35
|
+
"import": "./dist/sqlite.js",
|
|
36
|
+
"default": "./dist/sqlite.js"
|
|
37
|
+
},
|
|
38
|
+
"./sqlserver": {
|
|
39
|
+
"@zukhruf/source": "./src/sqlserver.ts",
|
|
40
|
+
"types": "./dist/sqlserver.d.ts",
|
|
41
|
+
"import": "./dist/sqlserver.js",
|
|
42
|
+
"default": "./dist/sqlserver.js"
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
"files": [
|
|
46
|
+
"dist",
|
|
47
|
+
"!dist/spec/**",
|
|
48
|
+
"!**/*.tsbuildinfo",
|
|
49
|
+
"!**/*.test.*"
|
|
50
|
+
],
|
|
51
|
+
"publishConfig": {
|
|
52
|
+
"access": "public"
|
|
53
|
+
},
|
|
54
|
+
"dependencies": {
|
|
55
|
+
"@ai-sdk/provider": "^4.0.34"
|
|
56
|
+
},
|
|
57
|
+
"devDependencies": {
|
|
58
|
+
"@types/mssql": "^12.3.0",
|
|
59
|
+
"@zukhruf/sqlite-open": "^0.8.35",
|
|
60
|
+
"@zukhruf/testing": "^0.8.35",
|
|
61
|
+
"ai": "^7.0.154",
|
|
62
|
+
"mssql": "^12.7.14"
|
|
63
|
+
}
|
|
64
|
+
}
|