@tangleai/models 0.21.1 → 0.24.1
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/CHANGELOG.md +26 -0
- package/README.md +2 -1
- package/package.json +3 -3
- package/src/check.d.ts +9 -10
- package/src/check.js +14 -18
- package/src/client.d.ts +59 -102
- package/src/client.js +307 -351
- package/src/embed.d.ts +54 -36
- package/src/embed.js +202 -287
- package/src/embedding-vector.d.ts +9 -4
- package/src/embedding-vector.js +13 -17
- package/src/errors.d.ts +26 -9
- package/src/errors.js +22 -24
- package/src/grammar.d.ts +6 -7
- package/src/grammar.js +39 -30
- package/src/index.d.ts +10 -9
- package/src/index.js +9 -10
- package/src/providers.d.ts +41 -31
- package/src/providers.js +79 -99
- package/src/replay.d.ts +52 -37
- package/src/replay.js +31 -59
- package/src/retry.d.ts +45 -86
- package/src/retry.js +50 -105
- package/src/routing.d.ts +5 -9
- package/src/routing.js +91 -79
- package/src/sse.d.ts +10 -1
- package/src/sse.js +0 -2
- package/src/structured.d.ts +19 -17
- package/src/structured.js +95 -124
package/src/embed.d.ts
CHANGED
|
@@ -1,3 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Embeddings: the `/embeddings` wire of the same OpenAI-compatible
|
|
3
|
+
* provider family the chat client speaks, and the seam every consumer
|
|
4
|
+
* of an embedding in this package is written against —
|
|
5
|
+
*
|
|
6
|
+
* { embed(texts, { signal }) → Promise<Float32Array[]>, model, dims }
|
|
7
|
+
*
|
|
8
|
+
* — one vector per input, in input order, from a named model at a
|
|
9
|
+
* fixed width. `model` and `dims` are a vector's identity: vectors from
|
|
10
|
+
* different models are pairwise meaningless and compare into plausible
|
|
11
|
+
* garbage, so the identity travels with every embedding and a consumer
|
|
12
|
+
* refuses to mix two. Two implementations ship here.
|
|
13
|
+
* `createEmbeddingClient` is the wire — OpenRouter, Ollama, LM Studio or
|
|
14
|
+
* any OpenAI-compatible base, resolved exactly as the chat client
|
|
15
|
+
* resolves it. `createHashEmbedder` is the deterministic reference:
|
|
16
|
+
* hashed character trigrams, dependency-free and network-free — what
|
|
17
|
+
* the tests and the offline demos run on, and demo-grade by design.
|
|
18
|
+
* Anything heavier (a local transformer runtime, a native embedding
|
|
19
|
+
* library) is the host's, wired through the same three members. This
|
|
20
|
+
* package ships no model weights, no tokenizer, no download and no
|
|
21
|
+
* opinion on which embedding model is good.
|
|
22
|
+
*
|
|
23
|
+
* The one invariant the wire client exists to own: a reply is
|
|
24
|
+
* reassembled by each item's `index`, and refused unless exactly one
|
|
25
|
+
* non-empty, finite vector of the expected width arrives per input. An
|
|
26
|
+
* embedding attached to the wrong text is worse than an error, and a
|
|
27
|
+
* client that trusts reply order gets exactly that from a provider
|
|
28
|
+
* that answers a batch out of order.
|
|
29
|
+
*/
|
|
1
30
|
/**
|
|
2
31
|
* The OpenAI-compatible `/embeddings` client over the existing provider
|
|
3
32
|
* set. The endpoint is `${base}/embeddings` from the same resolved base
|
|
@@ -11,11 +40,7 @@
|
|
|
11
40
|
* a fixed model — is therefore reported after `attempts` tries; a probe
|
|
12
41
|
* that wants a fast answer sets `retry: { attempts: 1 }`.
|
|
13
42
|
*
|
|
14
|
-
* @param
|
|
15
|
-
* model?: string, dims?: number, headers?: Record<string, string>,
|
|
16
|
-
* fetch?: typeof fetch, timeoutMs?: number,
|
|
17
|
-
* retry?: import('./retry.js').RetryOptions,
|
|
18
|
-
* cache?: import('./replay.js').ReplayCache }} [options]
|
|
43
|
+
* @param [options]
|
|
19
44
|
* - `model` is required: it is half of every vector's identity.
|
|
20
45
|
* - `cache` is the replay seam (`createChatClient` documents the
|
|
21
46
|
* contract). Here it is PER TEXT: each input is keyed by the
|
|
@@ -36,10 +61,10 @@
|
|
|
36
61
|
* does.
|
|
37
62
|
* - `retry` is the chat client's option, unchanged (see
|
|
38
63
|
* `createChatClient`).
|
|
39
|
-
* @returns
|
|
64
|
+
* @returns the seam, plus the
|
|
40
65
|
* resolved provider name; `dims` is the settled width
|
|
41
66
|
*/
|
|
42
|
-
export
|
|
67
|
+
export interface EmbeddingClientOptions {
|
|
43
68
|
provider?: string;
|
|
44
69
|
baseUrl?: string;
|
|
45
70
|
apiKey?: string;
|
|
@@ -48,9 +73,17 @@ export function createEmbeddingClient(options?: {
|
|
|
48
73
|
headers?: Record<string, string>;
|
|
49
74
|
fetch?: typeof fetch;
|
|
50
75
|
timeoutMs?: number;
|
|
51
|
-
retry?: import(
|
|
52
|
-
cache?: import(
|
|
76
|
+
retry?: import('./retry.ts').RetryOptions;
|
|
77
|
+
cache?: import('./replay.ts').ReplayCache;
|
|
78
|
+
}
|
|
79
|
+
export declare function createEmbeddingClient(options: EmbeddingClientOptions & {
|
|
80
|
+
dims: number;
|
|
53
81
|
}): Embedder & {
|
|
82
|
+
dims: number;
|
|
83
|
+
provider: string;
|
|
84
|
+
};
|
|
85
|
+
export declare function createEmbeddingClient(options?: EmbeddingClientOptions): Omit<Embedder, 'dims'> & {
|
|
86
|
+
readonly dims: number | undefined;
|
|
54
87
|
provider: string;
|
|
55
88
|
};
|
|
56
89
|
/**
|
|
@@ -60,13 +93,9 @@ export function createEmbeddingClient(options?: {
|
|
|
60
93
|
* (default 5 000, as `probeProvider`). Never throws — the result object
|
|
61
94
|
* is the settings-UI contract, and the live proof that a provider
|
|
62
95
|
* really serves `/embeddings` beside `/chat/completions`.
|
|
63
|
-
* @param
|
|
64
|
-
* model?: string, dims?: number, headers?: Record<string, string>,
|
|
65
|
-
* fetch?: typeof fetch, timeoutMs?: number }} [options]
|
|
66
|
-
* @returns {Promise<{ ok: true, model: string, dims: number } |
|
|
67
|
-
* { ok: false, status?: number, error: string }>}
|
|
96
|
+
* @param [options]
|
|
68
97
|
*/
|
|
69
|
-
export function probeEmbeddings(options?: {
|
|
98
|
+
export declare function probeEmbeddings(options?: {
|
|
70
99
|
provider?: string;
|
|
71
100
|
baseUrl?: string;
|
|
72
101
|
apiKey?: string;
|
|
@@ -95,35 +124,24 @@ export function probeEmbeddings(options?: {
|
|
|
95
124
|
* thing — so it exercises retrieval mechanics (does the right memory
|
|
96
125
|
* reach the prompt?) without saying anything about embedding quality,
|
|
97
126
|
* which belongs to a real model behind the same seam.
|
|
98
|
-
* @param
|
|
127
|
+
* @param [options] - the width (default 64); the
|
|
99
128
|
* identity is `hash-trigram-<dims>`, so two widths never mix
|
|
100
|
-
* @returns {Embedder & { dims: number }}
|
|
101
129
|
*/
|
|
102
|
-
export function createHashEmbedder(options?: {
|
|
130
|
+
export declare function createHashEmbedder(options?: {
|
|
103
131
|
dims?: number;
|
|
104
132
|
}): Embedder & {
|
|
105
133
|
dims: number;
|
|
106
134
|
};
|
|
107
|
-
/**
|
|
108
|
-
* The embedder seam: what every consumer of embeddings in this package
|
|
109
|
-
* takes, and what a host implements to bring its own.
|
|
110
|
-
*/
|
|
111
135
|
export type Embedder = {
|
|
112
|
-
/**
|
|
113
|
-
* - one vector per input, in input order; rejects `AI0001` for
|
|
114
|
-
* anything but a non-empty array of strings
|
|
115
|
-
*/
|
|
116
136
|
embed: (texts: string[], options?: {
|
|
117
137
|
signal?: AbortSignal;
|
|
118
|
-
}) => Promise<Float32Array[]>;
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
* caller configured it
|
|
127
|
-
*/
|
|
138
|
+
}) => Promise<Float32Array<ArrayBufferLike>[]>; /**
|
|
139
|
+
* - the name half of a vector's identity
|
|
140
|
+
*/
|
|
141
|
+
model: string; /**
|
|
142
|
+
* - the width half; the wire client
|
|
143
|
+
* leaves it undefined until its first reply settles it, unless the
|
|
144
|
+
* caller configured it
|
|
145
|
+
*/
|
|
128
146
|
dims: number | undefined;
|
|
129
147
|
};
|