@yaks/embedding 0.0.0-stage → 0.2.36
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 +202 -0
- package/README.md +549 -2
- package/_dist/compile.d.ts +34 -0
- package/_dist/compile.d.ts.map +1 -0
- package/_dist/ddl.d.ts +31 -0
- package/_dist/ddl.d.ts.map +1 -0
- package/_dist/embedder.d.ts +55 -0
- package/_dist/embedder.d.ts.map +1 -0
- package/_dist/fields.d.ts +70 -0
- package/_dist/fields.d.ts.map +1 -0
- package/_dist/graph.d.ts +63 -0
- package/_dist/graph.d.ts.map +1 -0
- package/_dist/held.d.ts +16 -0
- package/_dist/held.d.ts.map +1 -0
- package/_dist/image.d.ts +20 -0
- package/_dist/image.d.ts.map +1 -0
- package/_dist/mod.d.ts +105 -0
- package/_dist/mod.d.ts.map +1 -0
- package/_dist/native.d.ts +20 -0
- package/_dist/native.d.ts.map +1 -0
- package/_dist/near.d.ts +35 -0
- package/_dist/near.d.ts.map +1 -0
- package/_dist/neighbours.d.ts +29 -0
- package/_dist/neighbours.d.ts.map +1 -0
- package/_dist/options.d.ts +37 -0
- package/_dist/options.d.ts.map +1 -0
- package/_dist/outage.d.ts +64 -0
- package/_dist/outage.d.ts.map +1 -0
- package/_dist/owed.d.ts +79 -0
- package/_dist/owed.d.ts.map +1 -0
- package/_dist/remote.d.ts +116 -0
- package/_dist/remote.d.ts.map +1 -0
- package/_dist/score.d.ts +10 -0
- package/_dist/score.d.ts.map +1 -0
- package/_dist/search.d.ts +24 -0
- package/_dist/search.d.ts.map +1 -0
- package/_dist/service.d.ts +23 -0
- package/_dist/service.d.ts.map +1 -0
- package/_dist/sleeper.d.ts +37 -0
- package/_dist/sleeper.d.ts.map +1 -0
- package/_dist/sweep.d.ts +71 -0
- package/_dist/sweep.d.ts.map +1 -0
- package/_dist/tools.d.ts +14 -0
- package/_dist/tools.d.ts.map +1 -0
- package/_dist/vector.d.ts +30 -0
- package/_dist/vector.d.ts.map +1 -0
- package/_dist/vocab.d.ts +6 -0
- package/_dist/vocab.d.ts.map +1 -0
- package/coded.js +98 -0
- package/coded.js.map +1 -0
- package/coded.ts +85 -0
- package/compile.js +145 -0
- package/compile.js.map +1 -0
- package/compile.ts +206 -0
- package/ddl.js +476 -0
- package/ddl.js.map +1 -0
- package/ddl.ts +346 -0
- package/deno.json +1 -0
- package/embedder.js +85 -0
- package/embedder.js.map +1 -0
- package/embedder.ts +113 -0
- package/fields.js +178 -0
- package/fields.js.map +1 -0
- package/fields.ts +259 -0
- package/fill.js +177 -0
- package/fill.js.map +1 -0
- package/fill.ts +144 -0
- package/graph.js +113 -0
- package/graph.js.map +1 -0
- package/graph.ts +167 -0
- package/held.js +680 -0
- package/held.js.map +1 -0
- package/held.ts +726 -0
- package/image.js +202 -0
- package/image.js.map +1 -0
- package/image.ts +185 -0
- package/mod.js +105 -0
- package/mod.js.map +1 -0
- package/mod.ts +113 -0
- package/native.js +236 -0
- package/native.js.map +1 -0
- package/native.ts +288 -0
- package/near.js +123 -0
- package/near.js.map +1 -0
- package/near.ts +192 -0
- package/neighbours.js +223 -0
- package/neighbours.js.map +1 -0
- package/neighbours.ts +303 -0
- package/options.js +187 -0
- package/options.js.map +1 -0
- package/options.ts +269 -0
- package/outage.js +128 -0
- package/outage.js.map +1 -0
- package/outage.ts +132 -0
- package/owed.js +287 -0
- package/owed.js.map +1 -0
- package/owed.ts +346 -0
- package/package.json +45 -4
- package/remote.js +264 -0
- package/remote.js.map +1 -0
- package/remote.ts +391 -0
- package/score.js +46 -0
- package/score.js.map +1 -0
- package/score.ts +60 -0
- package/search.js +65 -0
- package/search.js.map +1 -0
- package/search.ts +118 -0
- package/service.js +107 -0
- package/service.js.map +1 -0
- package/service.ts +133 -0
- package/sleeper.js +107 -0
- package/sleeper.js.map +1 -0
- package/sleeper.ts +166 -0
- package/sparse.js +36 -0
- package/sparse.js.map +1 -0
- package/sparse.ts +49 -0
- package/sweep.js +374 -0
- package/sweep.js.map +1 -0
- package/sweep.ts +446 -0
- package/tools.js +57 -0
- package/tools.js.map +1 -0
- package/tools.ts +88 -0
- package/vector.js +78 -0
- package/vector.js.map +1 -0
- package/vector.ts +96 -0
- package/visibility.js +83 -0
- package/visibility.js.map +1 -0
- package/visibility.ts +111 -0
- package/vocab.js +22 -0
- package/vocab.js.map +1 -0
- package/vocab.json +19 -0
- package/vocab.ts +22 -0
package/neighbours.ts
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
// What a call just made is near: the `reply` the rules export gives the host
|
|
2
|
+
// (./graph.ts), which a direct tool call's answer carries beside the tool's
|
|
3
|
+
// own (@yaks/tools `Opts.reply`).
|
|
4
|
+
//
|
|
5
|
+
// An entity a call made — a task, a memory, a comment — comes back with two
|
|
6
|
+
// short lists. The first is the few existing entities of its kind nearest to
|
|
7
|
+
// it (`hit.near`), so whoever wrote it sees at once whether the graph already
|
|
8
|
+
// holds it, and keeps the one that stands instead of two. The second is the
|
|
9
|
+
// few nearest of any kind that is a resource (`hit.related`): the memories,
|
|
10
|
+
// tasks, designs and topics it bears on, and the words a person wrote. A
|
|
11
|
+
// one-off record (wearing a component the vocabulary marks `notice`: a knock,
|
|
12
|
+
// a transcript entry) is in neither list unless a person wrote it, even where
|
|
13
|
+
// it also wears the kind: a run's receipt is a comment and a knock at once.
|
|
14
|
+
// Nor is anything the calling session wrote itself, which it already holds:
|
|
15
|
+
// a helper commenting twice is not told of its first comment. A comment is
|
|
16
|
+
// shown with the id of what it is on (`hit.target`), since a comment's words
|
|
17
|
+
// alone do not say which task they are about. A call that made nothing is
|
|
18
|
+
// answered as it was.
|
|
19
|
+
//
|
|
20
|
+
// Made means created, or marked as a kind for the first time: memory_save
|
|
21
|
+
// keeps a person's words where they were said, by marking the transcript entry
|
|
22
|
+
// or comment that holds them a `memory`, and that memory is as new as a
|
|
23
|
+
// created one.
|
|
24
|
+
//
|
|
25
|
+
// A new entity has no vector yet: the sweep (./sweep.ts) makes them off the
|
|
26
|
+
// write path, seconds later. So this makes the vector now, for the few
|
|
27
|
+
// entities a caller is waiting on, and stores it as the sweep would, which
|
|
28
|
+
// then finds it made and calls no model. The neighbours are then a query,
|
|
29
|
+
// `.near` among entities of the same kind, answered like any other. A model
|
|
30
|
+
// that does not answer within WAIT, or a config naming none yet, leaves the
|
|
31
|
+
// entity with no vector, and the fallback is its first line as words
|
|
32
|
+
// (`.order=search`): a twin that says the same thing in the same words.
|
|
33
|
+
//
|
|
34
|
+
// Nothing here fails a write. The write committed before a reply is asked
|
|
35
|
+
// for, and a model out of reach only means fewer neighbours.
|
|
36
|
+
|
|
37
|
+
import { type Bundle, type Comp, type Eid, type Graph, who } from '@yaks/graph'
|
|
38
|
+
|
|
39
|
+
import { called } from '@yaks/graph/hit'
|
|
40
|
+
import { became, type Reply } from '@yaks/tools'
|
|
41
|
+
import {
|
|
42
|
+
absent,
|
|
43
|
+
and,
|
|
44
|
+
type Clause,
|
|
45
|
+
every,
|
|
46
|
+
limit,
|
|
47
|
+
ne,
|
|
48
|
+
near,
|
|
49
|
+
or,
|
|
50
|
+
order,
|
|
51
|
+
present,
|
|
52
|
+
text,
|
|
53
|
+
} from '@yaks/query'
|
|
54
|
+
import {
|
|
55
|
+
as,
|
|
56
|
+
col,
|
|
57
|
+
type Derived,
|
|
58
|
+
oneOf,
|
|
59
|
+
select,
|
|
60
|
+
type Statements,
|
|
61
|
+
statements,
|
|
62
|
+
table,
|
|
63
|
+
} from '@yaks/sql'
|
|
64
|
+
import type { Vocab } from 'jsr:@yaks/vocab@0.2.36'
|
|
65
|
+
import { type Embedder, hash } from './embedder.js'
|
|
66
|
+
import { type Field, fields, resolved, searched } from './fields.js'
|
|
67
|
+
import { type Lent, type Options, ready } from './options.js'
|
|
68
|
+
import { excerpt } from './search.js'
|
|
69
|
+
import { put, type Source, sources } from './sweep.js'
|
|
70
|
+
import { SIMILAR } from './compile.js'
|
|
71
|
+
|
|
72
|
+
// The ordering @yaks/fts gives a text search: closest match first.
|
|
73
|
+
let SEARCH = 'search'
|
|
74
|
+
|
|
75
|
+
/** How many neighbours of each list a new entity is answered with. */
|
|
76
|
+
export let FEW = 3
|
|
77
|
+
|
|
78
|
+
/** How many of one call's new entities are looked at: a plan of twenty
|
|
79
|
+
* tasks is answered about its first few, not with sixty lines. */
|
|
80
|
+
export let MOST = 5
|
|
81
|
+
|
|
82
|
+
/** How long an answer waits for the model, in milliseconds, before it falls
|
|
83
|
+
* back to words. */
|
|
84
|
+
export let WAIT = 1000
|
|
85
|
+
|
|
86
|
+
/** What a reply reads: the graph, its vocabulary, and the database the
|
|
87
|
+
* vectors are kept in. */
|
|
88
|
+
export type Host = {
|
|
89
|
+
storage: { statements?: Statements }
|
|
90
|
+
vocab: Vocab
|
|
91
|
+
graph: Graph
|
|
92
|
+
derived?: Derived
|
|
93
|
+
/** the embedders lent by name (@yaks/host `embedders`) */
|
|
94
|
+
embedders?: Lent
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
let comps = (b: Bundle): Comp[] =>
|
|
98
|
+
Object.entries(b).flatMap(([k, v]) =>
|
|
99
|
+
k != 'entity' && v && typeof v == 'object' ? [v as Comp] : []
|
|
100
|
+
)
|
|
101
|
+
|
|
102
|
+
// The first string a bundle's components hold under `name`: a task's
|
|
103
|
+
// `doc.title`, its computed `task.status`.
|
|
104
|
+
let said = (b: Bundle, name: string): string | undefined =>
|
|
105
|
+
comps(b).map((c) => c[name]).find((v): v is string => typeof v == 'string')
|
|
106
|
+
|
|
107
|
+
// An entity's text, read off its bundle the way ./fields.ts reads it off the
|
|
108
|
+
// tables: each embedded field in order, joined.
|
|
109
|
+
let textOf = (b: Bundle, prose: Field[]): string =>
|
|
110
|
+
prose.map((f) => (b[f.comp] as Comp | undefined)?.[f.prop])
|
|
111
|
+
.filter((v): v is string => typeof v == 'string' && !!v.trim())
|
|
112
|
+
.join('\n')
|
|
113
|
+
|
|
114
|
+
// Which list a neighbour is in: `near`, a possible twin of the same kind, or
|
|
115
|
+
// `related`, a resource of any kind.
|
|
116
|
+
type List = 'near' | 'related'
|
|
117
|
+
|
|
118
|
+
// One neighbour, as a search answers one (@yaks/cli, @yaks/tools `worded`): a
|
|
119
|
+
// `hit` naming the id it is called by (@yaks/graph/hit `called`), its kind,
|
|
120
|
+
// its title and its status where it has them, the id of what a comment is on,
|
|
121
|
+
// whether meaning or words found it, and, under its list's name, the new
|
|
122
|
+
// entity it was found near. An entity with no title is shown by an excerpt of
|
|
123
|
+
// its text instead.
|
|
124
|
+
let hit = (
|
|
125
|
+
vocab: Vocab,
|
|
126
|
+
b: Bundle,
|
|
127
|
+
of: Eid,
|
|
128
|
+
list: List,
|
|
129
|
+
source: 'meaning' | 'text',
|
|
130
|
+
prose: Field[],
|
|
131
|
+
on?: string,
|
|
132
|
+
): Bundle => {
|
|
133
|
+
let title = said(b, 'title')
|
|
134
|
+
let status = said(b, 'status')
|
|
135
|
+
return {
|
|
136
|
+
entity: b.entity,
|
|
137
|
+
hit: {
|
|
138
|
+
id: called(vocab)(b),
|
|
139
|
+
kind: vocab.kindOf(b),
|
|
140
|
+
...title ? { title } : {},
|
|
141
|
+
...on ? { target: on } : {},
|
|
142
|
+
snippet: title ? '' : excerpt(textOf(b, prose)),
|
|
143
|
+
source,
|
|
144
|
+
[list]: of,
|
|
145
|
+
...status ? { status } : {},
|
|
146
|
+
},
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// The ids the comments among these are on, by the comment's eid, each as a
|
|
151
|
+
// line names it (its human id where it has one).
|
|
152
|
+
let targets = async (
|
|
153
|
+
host: Host,
|
|
154
|
+
found: Bundle[],
|
|
155
|
+
): Promise<Map<Eid, string>> => {
|
|
156
|
+
let on = new Map<Eid, Eid>()
|
|
157
|
+
for (let b of found) {
|
|
158
|
+
let target = (b.comment as Comp | undefined)?.target
|
|
159
|
+
if (typeof target == 'string') on.set(b.entity.eid, target)
|
|
160
|
+
}
|
|
161
|
+
if (!on.size) return new Map()
|
|
162
|
+
let rows = await host.graph.get([...new Set(on.values())], host.vocab.kinds)
|
|
163
|
+
let name = called(host.vocab)
|
|
164
|
+
let named = new Map(rows.map((r) => [r.entity.eid, name(r)]))
|
|
165
|
+
return new Map([...on].map(([c, t]) => [c, named.get(t) ?? t]))
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// The integer ids these eids' rows are keyed by, for the ones this database
|
|
169
|
+
// holds: a rehearsed create (`check: true`) holds none.
|
|
170
|
+
let owners = (db: Statements, eids: Eid[]): number[] =>
|
|
171
|
+
db.query(select({
|
|
172
|
+
cols: [as(col('id'), 'id')],
|
|
173
|
+
from: table('entity'),
|
|
174
|
+
where: oneOf(col('eid'), eids),
|
|
175
|
+
})).map((r) => Number(r.id))
|
|
176
|
+
|
|
177
|
+
// A promise's value, or undefined once WAIT passes or it fails: an answer
|
|
178
|
+
// waits this long for a model and never longer.
|
|
179
|
+
let within = <T>(p: Promise<T>): Promise<T | undefined> =>
|
|
180
|
+
new Promise((done) => {
|
|
181
|
+
let t = setTimeout(() => done(undefined), WAIT)
|
|
182
|
+
p.then(
|
|
183
|
+
(v) => (clearTimeout(t), done(v)),
|
|
184
|
+
() => (clearTimeout(t), done(undefined)),
|
|
185
|
+
)
|
|
186
|
+
})
|
|
187
|
+
|
|
188
|
+
// The sources that have a vector for their text: the ones the sweep already
|
|
189
|
+
// made, and the rest made now and stored as the sweep stores them, where the
|
|
190
|
+
// model answers in time.
|
|
191
|
+
let vectored = async (
|
|
192
|
+
db: Statements,
|
|
193
|
+
embedder: Embedder,
|
|
194
|
+
made: Source[],
|
|
195
|
+
): Promise<Set<Eid>> => {
|
|
196
|
+
let owed = made.filter((s) => s.had != hash(embedder.model, s.text))
|
|
197
|
+
let got = await Promise.all(
|
|
198
|
+
owed.map((s) =>
|
|
199
|
+
within(Promise.resolve().then(() => embedder.embed(s.text)))
|
|
200
|
+
),
|
|
201
|
+
)
|
|
202
|
+
let have = new Set(made.filter((s) => !owed.includes(s)).map((s) => s.entity))
|
|
203
|
+
await db.atomic(() => {
|
|
204
|
+
owed.forEach((s, i) => {
|
|
205
|
+
let v = got[i]
|
|
206
|
+
if (v && put(db, s, embedder.model, v)) have.add(s.entity)
|
|
207
|
+
})
|
|
208
|
+
})
|
|
209
|
+
return have
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
// The words a twin would share: the first line's words (a document's title),
|
|
213
|
+
// the short ones left out, since every word must match.
|
|
214
|
+
let words = (s: string): string[] =>
|
|
215
|
+
(s.split('\n')[0].match(/[\p{L}\p{N}]{3,}/gu) ?? []).slice(0, 8)
|
|
216
|
+
|
|
217
|
+
/** What a neighbour in either list is: wearing no notice, or written by a
|
|
218
|
+
* person, where the vocabulary knows people. */
|
|
219
|
+
export let resources = (vocab: Vocab): Clause[] => {
|
|
220
|
+
let notices = vocab.all.filter((c) => vocab.comp(c)?.notice)
|
|
221
|
+
if (!notices.length) return []
|
|
222
|
+
let none = and(...notices.map(absent))
|
|
223
|
+
return [vocab.comp('person') ? or(none, present('created.by.person')) : none]
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* The reply: for each entity the call's write made, its nearest few existing
|
|
228
|
+
* entities of the same kind, then its nearest few resources of any kind, as
|
|
229
|
+
* `hit` bundles.
|
|
230
|
+
*/
|
|
231
|
+
export let neighbours = (host: Host, options: Options = {}): Reply => {
|
|
232
|
+
let db = statements(host.storage)
|
|
233
|
+
// Words are a query only where the vocabulary indexes words at all.
|
|
234
|
+
let indexed = fields(host.vocab, searched).length > 0
|
|
235
|
+
let kept = resources(host.vocab)
|
|
236
|
+
return async (call, _answer, wrote) => {
|
|
237
|
+
let kinds = new Map<Eid, string>()
|
|
238
|
+
for (let b of wrote) {
|
|
239
|
+
let kind = became(host.vocab, b)
|
|
240
|
+
if (kind) kinds.set(b.entity.eid, kind)
|
|
241
|
+
}
|
|
242
|
+
let fresh = new Set(kinds.keys())
|
|
243
|
+
let born = [...kinds].filter(([, k]) => k != 'entity').map(([e]) => e)
|
|
244
|
+
if (!born.length) return []
|
|
245
|
+
let now = await ready(host.vocab, options, host.graph, host.embedders)
|
|
246
|
+
let prose = resolved(now.text, host.derived)
|
|
247
|
+
// The first few that have text: a plan's links come back beside its tasks.
|
|
248
|
+
let made = sources(
|
|
249
|
+
db,
|
|
250
|
+
prose,
|
|
251
|
+
owners(db, born),
|
|
252
|
+
).slice(0, MOST)
|
|
253
|
+
if (!made.length) return []
|
|
254
|
+
let meant = now.embedder
|
|
255
|
+
? await vectored(db, now.embedder, made)
|
|
256
|
+
: new Set<Eid>()
|
|
257
|
+
// What the calling session wrote is its own, and no news to it. `!=`
|
|
258
|
+
// holds where `created.via` is absent too (@yaks/sql `ne`), and spelling
|
|
259
|
+
// that out as an OR made the screen a union over every `created` row:
|
|
260
|
+
// over two minutes on the box's graph.
|
|
261
|
+
let via = who(call)?.via
|
|
262
|
+
let others = via ? [ne('created.via', via)] : []
|
|
263
|
+
let out: Bundle[] = []
|
|
264
|
+
for (let s of made) {
|
|
265
|
+
let meaning = meant.has(s.entity)
|
|
266
|
+
let terms = words(s.text)
|
|
267
|
+
if (!meaning && !(indexed && terms.length)) continue
|
|
268
|
+
let source: 'meaning' | 'text' = meaning ? 'meaning' : 'text'
|
|
269
|
+
let shown = new Set(fresh)
|
|
270
|
+
// The nearest few matching `by`, past the ones already shown: room for
|
|
271
|
+
// them, which are not neighbours yet.
|
|
272
|
+
let nearest = async (list: List, by: Clause[]) => {
|
|
273
|
+
let room = limit(FEW + shown.size)
|
|
274
|
+
let ranked = meaning
|
|
275
|
+
? [near(s.entity), order(SIMILAR)]
|
|
276
|
+
: [...terms.map(text), order(SEARCH)]
|
|
277
|
+
let found = (await host.graph.read(
|
|
278
|
+
and(...by, ...kept, ...others, ...ranked, room, every()),
|
|
279
|
+
))
|
|
280
|
+
.filter((b) => !shown.has(b.entity.eid))
|
|
281
|
+
.slice(0, FEW)
|
|
282
|
+
found.forEach((b) => shown.add(b.entity.eid))
|
|
283
|
+
let on = await targets(host, found)
|
|
284
|
+
out.push(
|
|
285
|
+
...found.map((b) =>
|
|
286
|
+
hit(
|
|
287
|
+
host.vocab,
|
|
288
|
+
b,
|
|
289
|
+
s.entity,
|
|
290
|
+
list,
|
|
291
|
+
source,
|
|
292
|
+
prose,
|
|
293
|
+
on.get(b.entity.eid),
|
|
294
|
+
)
|
|
295
|
+
),
|
|
296
|
+
)
|
|
297
|
+
}
|
|
298
|
+
await nearest('near', [present(kinds.get(s.entity)!)])
|
|
299
|
+
await nearest('related', [])
|
|
300
|
+
}
|
|
301
|
+
return out
|
|
302
|
+
}
|
|
303
|
+
}
|
package/options.js
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
// What a config declares to this plugin, and the embedder it names. "The
|
|
2
|
+
// server" below means whichever process opened the graph and loaded this
|
|
3
|
+
// package.
|
|
4
|
+
//
|
|
5
|
+
// The vectors are this package's business; the model is not. So the config
|
|
6
|
+
// names one beside the plugin, the way a session names what serves it: a
|
|
7
|
+
// provider and a model, two choices. `./graph` needs the vector space the
|
|
8
|
+
// model names (a stored vector is only comparable with others in the same
|
|
9
|
+
// space), `./service` needs the embedding function itself:
|
|
10
|
+
//
|
|
11
|
+
// ```json
|
|
12
|
+
// { "use": "@yaks/embedding",
|
|
13
|
+
// "with": { "embedder": { "provider": "ollama",
|
|
14
|
+
// "model": "granite-embedding-30m-english" },
|
|
15
|
+
// "text": ["doc.title", "doc.body"] } }
|
|
16
|
+
// ```
|
|
17
|
+
//
|
|
18
|
+
// A provider reached over HTTP is a row in the graph, as it is for a chat
|
|
19
|
+
// (@yaks/model): `provider{name, base, api}` says where it answers and which
|
|
20
|
+
// API it speaks, and its `serves` edge to the `model` row says what it calls
|
|
21
|
+
// that model. Nothing here knows Ollama beyond the shape of its API
|
|
22
|
+
// (./remote.ts). The space is the model's own name, whoever serves it, so the
|
|
23
|
+
// same model moved to another provider keeps its vectors. A provider that runs
|
|
24
|
+
// in this process needs no row: `hash`, the offline embedder shipped here
|
|
25
|
+
// (instant, deterministic, and no model at all, which is what a development
|
|
26
|
+
// machine and a test want), or one a plugin beside this one lends its host by
|
|
27
|
+
// name (@yaks/model2vec lends `model2vec`), which this package knows nothing
|
|
28
|
+
// about beyond {@link Lent}.
|
|
29
|
+
//
|
|
30
|
+
// Missing config never prevents startup. A server that composes this plugin
|
|
31
|
+
// and has not been given a key, a provider row or a model yet is a server
|
|
32
|
+
// waiting for one: it starts, it stores no vectors, its check reports what it
|
|
33
|
+
// is waiting for, and the first pass after it arrives is the one that embeds.
|
|
34
|
+
// So nothing here throws: a config and the rows it names amount to a
|
|
35
|
+
// {@link Ready} value, read on every pass rather than once when the plugin is
|
|
36
|
+
// composed, which is what lets a key or a row written into the graph start the
|
|
37
|
+
// sweep without a restart.
|
|
38
|
+
import { dead } from "@yaks/graph/bundle";
|
|
39
|
+
import { identityEid } from "@yaks/graph/identity";
|
|
40
|
+
import { edgeEid } from "@yaks/edge";
|
|
41
|
+
import { MODEL, PROVIDER } from "@yaks/model";
|
|
42
|
+
import { hashEmbedder } from "./embedder.js";
|
|
43
|
+
import { fields, searched, unembedded } from "./fields.js";
|
|
44
|
+
import { remote } from "./remote.js";
|
|
45
|
+
// The provider this package runs in its own process.
|
|
46
|
+
let HASH = 'hash';
|
|
47
|
+
// The APIs ./remote.ts speaks.
|
|
48
|
+
let APIS = [
|
|
49
|
+
'ollama',
|
|
50
|
+
'openai'
|
|
51
|
+
];
|
|
52
|
+
// The lent embedder a config names, made from its model and width.
|
|
53
|
+
let lentOf = (said, lent)=>said.model && Object.hasOwn(lent ?? {}, said.provider) ? lent[said.provider]({
|
|
54
|
+
model: said.model,
|
|
55
|
+
dim: said.dim
|
|
56
|
+
}) : undefined;
|
|
57
|
+
/** The space a config's vectors live in, from the config alone: the model,
|
|
58
|
+
* and its width when it is cut. `undefined` where the config names none. */ export let spaceOf = (said, lent)=>!said?.provider ? undefined : said.provider == HASH ? hashEmbedder(said.dim).model : !said.model ? undefined : lentOf(said, lent)?.model ?? (said.dim ? `${said.model}#${said.dim}` : said.model);
|
|
59
|
+
let live = (b, comp)=>b && !dead(b) ? b[comp] : undefined;
|
|
60
|
+
// A provider row, the model row and the edge between them, read as an
|
|
61
|
+
// embedder, or what is missing.
|
|
62
|
+
let served = async (said, rows)=>{
|
|
63
|
+
let p = identityEid(PROVIDER, [
|
|
64
|
+
said.provider
|
|
65
|
+
]);
|
|
66
|
+
let m = identityEid(MODEL, [
|
|
67
|
+
said.model
|
|
68
|
+
]);
|
|
69
|
+
let e = edgeEid(p, 'serves', m);
|
|
70
|
+
let got = new Map((await rows.get([
|
|
71
|
+
p,
|
|
72
|
+
m,
|
|
73
|
+
e
|
|
74
|
+
])).map((b)=>[
|
|
75
|
+
b.entity.eid,
|
|
76
|
+
b
|
|
77
|
+
]));
|
|
78
|
+
let provider = live(got.get(p), PROVIDER);
|
|
79
|
+
if (!provider) return `no provider called ${JSON.stringify(said.provider)}`;
|
|
80
|
+
let api = provider.api;
|
|
81
|
+
if (!api || !APIS.includes(api)) {
|
|
82
|
+
return `${said.provider} names no API this package speaks (` + `${APIS.join(', ')}) in \`provider.api\``;
|
|
83
|
+
}
|
|
84
|
+
if (typeof provider.base != 'string' || !provider.base) {
|
|
85
|
+
return `${said.provider} says nowhere to reach it in \`provider.base\``;
|
|
86
|
+
}
|
|
87
|
+
let serves = live(got.get(e), 'serves');
|
|
88
|
+
if (!live(got.get(m), MODEL) || !serves) {
|
|
89
|
+
return `${said.provider} serves no model called ${JSON.stringify(said.model)}`;
|
|
90
|
+
}
|
|
91
|
+
return remote({
|
|
92
|
+
...said,
|
|
93
|
+
api,
|
|
94
|
+
base: provider.base,
|
|
95
|
+
model: typeof serves.name == 'string' ? serves.name : said.model,
|
|
96
|
+
space: spaceOf(said)
|
|
97
|
+
});
|
|
98
|
+
};
|
|
99
|
+
/** The embedder a config and the rows it names amount to, or what it is
|
|
100
|
+
* waiting for. Missing config means waiting: the server starts with no
|
|
101
|
+
* vectors and begins embedding the moment what is missing appears. */ export let embedderOf = async (options, rows, lent)=>{
|
|
102
|
+
let said = options.embedder;
|
|
103
|
+
let model = spaceOf(said, lent);
|
|
104
|
+
if (!said?.provider) {
|
|
105
|
+
return {
|
|
106
|
+
waiting: 'no `embedder` is named — `{"provider": "hash"}` is the offline one, and a provider and a model are named beside the plugin'
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
if (!model) {
|
|
110
|
+
return {
|
|
111
|
+
waiting: `${said.provider} is named with no \`model\``
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
if (said.provider == HASH) return {
|
|
115
|
+
model,
|
|
116
|
+
embedder: hashEmbedder(said.dim)
|
|
117
|
+
};
|
|
118
|
+
let own = lentOf(said, lent);
|
|
119
|
+
if (own) return {
|
|
120
|
+
model,
|
|
121
|
+
embedder: own
|
|
122
|
+
};
|
|
123
|
+
// A config that names a key but has no value for it is waiting: the
|
|
124
|
+
// environment does not have one yet, and every request until it does would
|
|
125
|
+
// be a 401 paid for once per entity. A config that names no key never
|
|
126
|
+
// wanted one — a machine on your own network — and goes straight through.
|
|
127
|
+
if ('key' in said && said.key == null) {
|
|
128
|
+
return {
|
|
129
|
+
model,
|
|
130
|
+
waiting: `waiting for a key: ${said.provider} is named with one the environment has not got`
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
let made = await served(said, rows);
|
|
134
|
+
return typeof made == 'string' ? {
|
|
135
|
+
model,
|
|
136
|
+
waiting: made
|
|
137
|
+
} : {
|
|
138
|
+
model,
|
|
139
|
+
embedder: made
|
|
140
|
+
};
|
|
141
|
+
};
|
|
142
|
+
/** The fields a config chose, or every searched one. A name the vocabulary
|
|
143
|
+
* does not declare is an error rather than a field that silently embeds
|
|
144
|
+
* nothing. */ export let chosen = (vocab, options)=>{
|
|
145
|
+
if (!options.text) return fields(vocab, searched);
|
|
146
|
+
return options.text.map((said)=>{
|
|
147
|
+
let [comp, prop, ...rest] = said.split('.');
|
|
148
|
+
if (!prop || rest.length || !vocab.prop(comp, prop)) {
|
|
149
|
+
throw new Error(`@yaks/embedding: \`text\` names ${JSON.stringify(said)}, which is not a declared comp.prop`);
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
comp,
|
|
153
|
+
prop,
|
|
154
|
+
...unembedded(vocab)
|
|
155
|
+
};
|
|
156
|
+
});
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* Everything a pass needs, as the config and the rows stand at this moment:
|
|
160
|
+
* the text a vector is made from, the embedder that makes it, and the message
|
|
161
|
+
* to report when there is none.
|
|
162
|
+
*
|
|
163
|
+
* Read it on every pass rather than once when the plugin is composed. That is
|
|
164
|
+
* what makes a key or a provider arriving late a server that starts embedding
|
|
165
|
+
* instead of one that has to be restarted: @yaks/cli resolves `{"secret": …}`
|
|
166
|
+
* each time it is asked for, so a key written into the graph is seen on the
|
|
167
|
+
* next pass, and the rows are read again with it.
|
|
168
|
+
*
|
|
169
|
+
* A `text` name the vocabulary does not declare is the one thing here that
|
|
170
|
+
* cannot wait: it is reported, and this plugin does nothing, which degrades the
|
|
171
|
+
* same way a key that has not arrived does.
|
|
172
|
+
*/ export let ready = async (vocab, options, rows, lent)=>{
|
|
173
|
+
let text;
|
|
174
|
+
try {
|
|
175
|
+
text = chosen(vocab, options);
|
|
176
|
+
} catch (error) {
|
|
177
|
+
return {
|
|
178
|
+
text: [],
|
|
179
|
+
waiting: error.message
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
return {
|
|
183
|
+
text,
|
|
184
|
+
...await embedderOf(options, rows, lent)
|
|
185
|
+
};
|
|
186
|
+
};
|
|
187
|
+
//# sourceMappingURL=options.js.map
|
package/options.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.js","sources":["./options.ts"],"names":[],"mappings":"AAAA,yEAAyE;AACzE,yEAAyE;AACzE,WAAW;AACX,EAAE;AACF,2EAA2E;AAC3E,yEAAyE;AACzE,0EAA0E;AAC1E,0EAA0E;AAC1E,2DAA2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