pi-knowledge 0.4.0 → 0.4.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 +10 -0
- package/README.md +2 -1
- package/index.ts +1 -110
- package/package.json +1 -1
- package/src/embedding/provider.ts +5 -39
- package/src/engine.ts +2 -0
- package/src/model-worker-client.ts +146 -0
- package/src/model-worker.ts +88 -0
- package/src/search/reranker.ts +5 -35
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [0.4.1] - 2026-06-16
|
|
4
|
+
|
|
5
|
+
### Fixed
|
|
6
|
+
- Isolated local Transformers.js embedding and reranker models in a model worker process so Pi's TUI process no longer loads `onnxruntime-node`, fixing macOS arm64 `/quit` aborts after knowledge-base tool usage.
|
|
7
|
+
- Removed the custom `knowledge_search` TUI renderer and switched warning output to plain text to avoid Pi TUI line-width render crashes.
|
|
8
|
+
- Kept native ONNX idle disposal opt-in only, prioritizing stable session shutdown over aggressive memory reclamation.
|
|
9
|
+
|
|
10
|
+
### Changed
|
|
11
|
+
- Documented the native model lifecycle contract, the onnxruntime teardown pitfall, and the worker-based shutdown strategy in contributor and architecture docs.
|
|
12
|
+
|
|
3
13
|
## [0.4.0] - 2026-06-16
|
|
4
14
|
|
|
5
15
|
### Added
|
package/README.md
CHANGED
|
@@ -32,7 +32,8 @@ Unlike `pi-memory` (which manages the agent's own notes), `pi-knowledge` indexes
|
|
|
32
32
|
| Cross-session persistence | ✅ | ✅ | ✅ |
|
|
33
33
|
| Pi extension native | ✅ | N/A | ✅ |
|
|
34
34
|
| Context injection per turn | ✅ | ❌ | ✅ |
|
|
35
|
-
| TUI
|
|
35
|
+
| Search result TUI rendering | ✅ | N/A | ❌ |
|
|
36
|
+
| Full tool TUI rendering | planned | N/A | ❌ |
|
|
36
37
|
| RPC mode support | ✅ | N/A | N/A |
|
|
37
38
|
|
|
38
39
|
## Research-Backed Retrieval
|
package/index.ts
CHANGED
|
@@ -5,92 +5,6 @@ import { getActiveWatcherCount, startWatcher, stopAllWatchers } from "./src/watc
|
|
|
5
5
|
|
|
6
6
|
type Schema = Record<string, unknown> & { optional?: true };
|
|
7
7
|
type ContextMessage = { role: string; content: string };
|
|
8
|
-
type ThemeLike = {
|
|
9
|
-
fg?: (name: string, text: string) => string;
|
|
10
|
-
};
|
|
11
|
-
type RenderContextLike = {
|
|
12
|
-
lastComponent?: RenderableText;
|
|
13
|
-
};
|
|
14
|
-
type RenderOptionsLike = {
|
|
15
|
-
expanded?: boolean;
|
|
16
|
-
};
|
|
17
|
-
type ToolResultLike = {
|
|
18
|
-
content?: Array<{ type: string; text?: string }>;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
const MAX_RENDER_LINE_WIDTH = 80;
|
|
22
|
-
const ESCAPE = "\u001B";
|
|
23
|
-
|
|
24
|
-
function ansiSequenceLength(line: string, offset: number): number {
|
|
25
|
-
if (line[offset] !== ESCAPE || line[offset + 1] !== "[") return 0;
|
|
26
|
-
for (let i = offset + 2; i < line.length; i++) {
|
|
27
|
-
const codePoint = line.charCodeAt(i);
|
|
28
|
-
if (codePoint >= 0x40 && codePoint <= 0x7e) return i - offset + 1;
|
|
29
|
-
}
|
|
30
|
-
return 0;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
function charRenderWidth(char: string): number {
|
|
34
|
-
const codePoint = char.codePointAt(0);
|
|
35
|
-
if (codePoint === undefined) return 0;
|
|
36
|
-
if (
|
|
37
|
-
(codePoint >= 0x1100 && codePoint <= 0x115f) ||
|
|
38
|
-
(codePoint >= 0x2e80 && codePoint <= 0xa4cf) ||
|
|
39
|
-
(codePoint >= 0xac00 && codePoint <= 0xd7a3) ||
|
|
40
|
-
(codePoint >= 0xf900 && codePoint <= 0xfaff) ||
|
|
41
|
-
(codePoint >= 0xfe10 && codePoint <= 0xfe19) ||
|
|
42
|
-
(codePoint >= 0xfe30 && codePoint <= 0xfe6f) ||
|
|
43
|
-
(codePoint >= 0xff00 && codePoint <= 0xff60) ||
|
|
44
|
-
(codePoint >= 0xffe0 && codePoint <= 0xffe6)
|
|
45
|
-
) {
|
|
46
|
-
return 2;
|
|
47
|
-
}
|
|
48
|
-
return 1;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
function truncateRenderLine(line: string): string {
|
|
52
|
-
let visibleWidth = 0;
|
|
53
|
-
let output = "";
|
|
54
|
-
for (let i = 0; i < line.length; ) {
|
|
55
|
-
const ansiLength = ansiSequenceLength(line, i);
|
|
56
|
-
if (ansiLength > 0) {
|
|
57
|
-
output += line.slice(i, i + ansiLength);
|
|
58
|
-
i += ansiLength;
|
|
59
|
-
continue;
|
|
60
|
-
}
|
|
61
|
-
const codePoint = line.codePointAt(i);
|
|
62
|
-
if (codePoint === undefined) break;
|
|
63
|
-
const char = String.fromCodePoint(codePoint);
|
|
64
|
-
const width = charRenderWidth(char);
|
|
65
|
-
if (visibleWidth + width > MAX_RENDER_LINE_WIDTH - 1) return `${output}…`;
|
|
66
|
-
output += char;
|
|
67
|
-
visibleWidth += width;
|
|
68
|
-
i += char.length;
|
|
69
|
-
}
|
|
70
|
-
return output;
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* Minimal duck-type shim for @earendil-works/pi-tui Text component.
|
|
75
|
-
* Allows startup without Pi virtual module while implementing the render host methods this extension needs.
|
|
76
|
-
*/
|
|
77
|
-
class RenderableText {
|
|
78
|
-
private text: string;
|
|
79
|
-
|
|
80
|
-
constructor(text = "") {
|
|
81
|
-
this.text = text;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
setText(text: string): void {
|
|
85
|
-
this.text = text;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
invalidate(): void {}
|
|
89
|
-
|
|
90
|
-
render(): string[] {
|
|
91
|
-
return this.text.split("\n").map(truncateRenderLine);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
8
|
|
|
95
9
|
const Type = {
|
|
96
10
|
Object(properties: Record<string, Schema>): Schema {
|
|
@@ -128,16 +42,6 @@ const Type = {
|
|
|
128
42
|
},
|
|
129
43
|
};
|
|
130
44
|
|
|
131
|
-
function themed(theme: ThemeLike, name: string, text: string): string {
|
|
132
|
-
return theme.fg ? theme.fg(name, text) : text;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
function renderText(context: RenderContextLike, text: string): RenderableText {
|
|
136
|
-
const component = context.lastComponent ?? new RenderableText();
|
|
137
|
-
component.setText(text);
|
|
138
|
-
return component;
|
|
139
|
-
}
|
|
140
|
-
|
|
141
45
|
const engine = new KnowledgeEngine();
|
|
142
46
|
const WATCH_ENABLED = process.env.PI_KNOWLEDGE_WATCH === "true";
|
|
143
47
|
const AUTO_INJECT = process.env.PI_KNOWLEDGE_AUTO_INJECT === "true";
|
|
@@ -365,19 +269,6 @@ export default function (pi: ExtensionAPI) {
|
|
|
365
269
|
Type.Boolean({ description: "Include ranking diagnostics and mode/fallback details in the result" }),
|
|
366
270
|
),
|
|
367
271
|
}),
|
|
368
|
-
renderCall(args, theme: ThemeLike, context: RenderContextLike) {
|
|
369
|
-
const query = typeof args.query === "string" ? args.query : "";
|
|
370
|
-
const mode = typeof args.mode === "string" ? args.mode : "hybrid";
|
|
371
|
-
return renderText(
|
|
372
|
-
context,
|
|
373
|
-
`${themed(theme, "accent", "Search")}: "${query}" ${themed(theme, "muted", `(${mode})`)}`,
|
|
374
|
-
);
|
|
375
|
-
},
|
|
376
|
-
renderResult(result: ToolResultLike, options: RenderOptionsLike, _theme: ThemeLike, context: RenderContextLike) {
|
|
377
|
-
const text = result.content?.find((item) => item.type === "text")?.text ?? "";
|
|
378
|
-
const lines = options.expanded ? text : text.split("\n").slice(0, 5).join("\n");
|
|
379
|
-
return renderText(context, lines);
|
|
380
|
-
},
|
|
381
272
|
async execute(_id, params) {
|
|
382
273
|
const { query, mode, limit, kb_id, offset, file_type, diversity, diagnostics } = params;
|
|
383
274
|
const filters = file_type ? { file_type } : undefined;
|
|
@@ -399,7 +290,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
399
290
|
if (response.retry_modes?.length) output += ` — retried: ${response.retry_modes.join(", ")}`;
|
|
400
291
|
output += ":\n\n";
|
|
401
292
|
if (response.warnings?.length) {
|
|
402
|
-
output =
|
|
293
|
+
output = `Warnings:\n- ${response.warnings.join("\n- ")}\n\n${output}`;
|
|
403
294
|
}
|
|
404
295
|
output += response.results
|
|
405
296
|
.map((r, i) => {
|
package/package.json
CHANGED
|
@@ -1,12 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { getDefaultKnowledgeDir } from "../storage/sqlite.ts";
|
|
1
|
+
import { embedInModelWorker } from "../model-worker-client.ts";
|
|
3
2
|
|
|
4
|
-
type FeatureExtractionPipeline = {
|
|
5
|
-
(input: string, options: { pooling: "mean"; normalize: true }): Promise<{ data: ArrayLike<number> }>;
|
|
6
|
-
dispose(): Promise<void> | void;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
let pipelineInstance: FeatureExtractionPipeline | null = null;
|
|
10
3
|
let disposeTimer: ReturnType<typeof setTimeout> | null = null;
|
|
11
4
|
let disposePromise: Promise<void> | null = null;
|
|
12
5
|
let activeRuns = 0;
|
|
@@ -15,22 +8,7 @@ const idleWaiters: Array<() => void> = [];
|
|
|
15
8
|
|
|
16
9
|
const IDLE_TIMEOUT_MS = Number(process.env.PI_KNOWLEDGE_EMBEDDING_IDLE_MS ?? 30_000);
|
|
17
10
|
const EMBEDDING_CONFIG = process.env.PI_KNOWLEDGE_EMBEDDING ?? "local:multilingual-e5-small";
|
|
18
|
-
|
|
19
|
-
function getModelCacheDir(): string {
|
|
20
|
-
return join(getDefaultKnowledgeDir(), "models");
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
async function loadPipeline(): Promise<FeatureExtractionPipeline> {
|
|
24
|
-
if (pipelineInstance) return pipelineInstance;
|
|
25
|
-
if (disposePromise) await disposePromise;
|
|
26
|
-
const { pipeline, env } = await import("@huggingface/transformers");
|
|
27
|
-
env.cacheDir = getModelCacheDir();
|
|
28
|
-
pipelineInstance = await pipeline("feature-extraction", "Xenova/multilingual-e5-small", {
|
|
29
|
-
quantized: true,
|
|
30
|
-
dtype: "fp32",
|
|
31
|
-
});
|
|
32
|
-
return pipelineInstance;
|
|
33
|
-
}
|
|
11
|
+
const ENABLE_NATIVE_IDLE_DISPOSE = process.env.PI_KNOWLEDGE_ENABLE_NATIVE_IDLE_DISPOSE === "true";
|
|
34
12
|
|
|
35
13
|
function clearIdleTimer(): void {
|
|
36
14
|
if (disposeTimer) clearTimeout(disposeTimer);
|
|
@@ -40,6 +18,7 @@ function clearIdleTimer(): void {
|
|
|
40
18
|
function scheduleIdleDispose(): void {
|
|
41
19
|
if (activeRuns > 0 || disposeRequested) return;
|
|
42
20
|
clearIdleTimer();
|
|
21
|
+
if (!ENABLE_NATIVE_IDLE_DISPOSE) return;
|
|
43
22
|
disposeTimer = setTimeout(() => dispose(), IDLE_TIMEOUT_MS);
|
|
44
23
|
}
|
|
45
24
|
|
|
@@ -65,13 +44,7 @@ export async function dispose(): Promise<void> {
|
|
|
65
44
|
if (disposePromise) return disposePromise;
|
|
66
45
|
disposeRequested = true;
|
|
67
46
|
await waitForNoActiveRuns();
|
|
68
|
-
|
|
69
|
-
pipelineInstance = null;
|
|
70
|
-
if (!instance) {
|
|
71
|
-
disposeRequested = false;
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
disposePromise = Promise.resolve(instance.dispose()).finally(() => {
|
|
47
|
+
disposePromise = Promise.resolve().finally(() => {
|
|
75
48
|
disposePromise = null;
|
|
76
49
|
disposeRequested = false;
|
|
77
50
|
});
|
|
@@ -115,19 +88,12 @@ export async function embedTexts(
|
|
|
115
88
|
/* fallback to local */
|
|
116
89
|
}
|
|
117
90
|
}
|
|
118
|
-
const pipe = await loadPipeline();
|
|
119
91
|
beginRun();
|
|
120
|
-
const results: Float32Array[] = [];
|
|
121
92
|
try {
|
|
122
|
-
|
|
123
|
-
if (signal?.aborted) throw new Error("Cancelled");
|
|
124
|
-
const output = await pipe(`${prefix}: ${text}`, { pooling: "mean", normalize: true });
|
|
125
|
-
results.push(new Float32Array(output.data));
|
|
126
|
-
}
|
|
93
|
+
return await embedInModelWorker(texts, prefix, signal);
|
|
127
94
|
} finally {
|
|
128
95
|
endRun();
|
|
129
96
|
}
|
|
130
|
-
return results;
|
|
131
97
|
}
|
|
132
98
|
|
|
133
99
|
export async function embedQuery(text: string): Promise<Float32Array> {
|
package/src/engine.ts
CHANGED
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
type ScanOptions,
|
|
22
22
|
summarizeSkippedScan,
|
|
23
23
|
} from "./indexer/chunker.ts";
|
|
24
|
+
import { shutdownModelWorker } from "./model-worker-client.ts";
|
|
24
25
|
import { searchBM25 } from "./search/bm25.ts";
|
|
25
26
|
import { weightedScoreFusion } from "./search/fusion.ts";
|
|
26
27
|
import { normalizedQueryText, tokenizeForSearch } from "./search/query.ts";
|
|
@@ -1584,6 +1585,7 @@ export class KnowledgeEngine {
|
|
|
1584
1585
|
await prepareEmbeddingForShutdown();
|
|
1585
1586
|
await prepareRerankerForShutdown();
|
|
1586
1587
|
}
|
|
1588
|
+
shutdownModelWorker();
|
|
1587
1589
|
this.db?.close();
|
|
1588
1590
|
this.db = null;
|
|
1589
1591
|
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
import { type ChildProcess, fork } from "node:child_process";
|
|
2
|
+
import { fileURLToPath } from "node:url";
|
|
3
|
+
|
|
4
|
+
type WorkerResponse = {
|
|
5
|
+
id: number;
|
|
6
|
+
result?: unknown;
|
|
7
|
+
error?: string;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
type PendingRequest = {
|
|
11
|
+
resolve: (value: unknown) => void;
|
|
12
|
+
reject: (reason: Error) => void;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
let worker: ChildProcess | null = null;
|
|
16
|
+
let nextRequestId = 1;
|
|
17
|
+
const pending = new Map<number, PendingRequest>();
|
|
18
|
+
|
|
19
|
+
function rejectPending(error: Error): void {
|
|
20
|
+
for (const request of pending.values()) {
|
|
21
|
+
request.reject(error);
|
|
22
|
+
}
|
|
23
|
+
pending.clear();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function getWorker(): ChildProcess {
|
|
27
|
+
if (worker?.connected) return worker;
|
|
28
|
+
const workerPath = fileURLToPath(new URL("./model-worker.ts", import.meta.url));
|
|
29
|
+
worker = fork(workerPath, {
|
|
30
|
+
execArgv: ["--experimental-strip-types"],
|
|
31
|
+
stdio: ["ignore", "ignore", "ignore", "ipc"],
|
|
32
|
+
env: process.env,
|
|
33
|
+
});
|
|
34
|
+
const child = worker;
|
|
35
|
+
worker.on("message", (message: WorkerResponse) => {
|
|
36
|
+
const request = pending.get(message.id);
|
|
37
|
+
if (!request) return;
|
|
38
|
+
pending.delete(message.id);
|
|
39
|
+
if (message.error) {
|
|
40
|
+
request.reject(new Error(message.error));
|
|
41
|
+
} else {
|
|
42
|
+
request.resolve(message.result);
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
worker.on("exit", (code, signal) => {
|
|
46
|
+
if (worker !== child) return;
|
|
47
|
+
worker = null;
|
|
48
|
+
if (pending.size > 0) {
|
|
49
|
+
rejectPending(
|
|
50
|
+
new Error(`Model worker exited before responding (code ${code ?? "null"}, signal ${signal ?? "null"})`),
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
});
|
|
54
|
+
worker.on("error", (error) => {
|
|
55
|
+
if (worker !== child) return;
|
|
56
|
+
worker = null;
|
|
57
|
+
rejectPending(error);
|
|
58
|
+
});
|
|
59
|
+
return worker;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function requestModelWorker(message: Record<string, unknown>, signal?: AbortSignal): Promise<unknown> {
|
|
63
|
+
if (signal?.aborted) throw new Error("Cancelled");
|
|
64
|
+
const child = getWorker();
|
|
65
|
+
const id = nextRequestId++;
|
|
66
|
+
return new Promise((resolve, reject) => {
|
|
67
|
+
let abortHandler: (() => void) | undefined;
|
|
68
|
+
pending.set(id, { resolve, reject });
|
|
69
|
+
if (signal) {
|
|
70
|
+
abortHandler = () => {
|
|
71
|
+
pending.delete(id);
|
|
72
|
+
reject(new Error("Cancelled"));
|
|
73
|
+
shutdownModelWorker();
|
|
74
|
+
};
|
|
75
|
+
signal.addEventListener("abort", abortHandler, { once: true });
|
|
76
|
+
}
|
|
77
|
+
const cleanup = (): void => {
|
|
78
|
+
if (abortHandler) signal?.removeEventListener("abort", abortHandler);
|
|
79
|
+
};
|
|
80
|
+
const originalResolve = resolve;
|
|
81
|
+
const originalReject = reject;
|
|
82
|
+
pending.set(id, {
|
|
83
|
+
resolve(value) {
|
|
84
|
+
cleanup();
|
|
85
|
+
originalResolve(value);
|
|
86
|
+
},
|
|
87
|
+
reject(error) {
|
|
88
|
+
cleanup();
|
|
89
|
+
originalReject(error);
|
|
90
|
+
},
|
|
91
|
+
});
|
|
92
|
+
child.send({ id, ...message }, (error) => {
|
|
93
|
+
if (!error) return;
|
|
94
|
+
pending.delete(id);
|
|
95
|
+
cleanup();
|
|
96
|
+
reject(error);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export async function embedInModelWorker(
|
|
102
|
+
texts: string[],
|
|
103
|
+
prefix: "query" | "passage",
|
|
104
|
+
signal?: AbortSignal,
|
|
105
|
+
): Promise<Float32Array[]> {
|
|
106
|
+
const result = await requestModelWorker({ type: "embed", texts, prefix }, signal);
|
|
107
|
+
if (!Array.isArray(result)) throw new Error("Invalid embedding worker response");
|
|
108
|
+
return result.map((vector) => {
|
|
109
|
+
if (!Array.isArray(vector)) throw new Error("Invalid embedding vector from worker");
|
|
110
|
+
return new Float32Array(vector);
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export interface RerankWorkerCandidate {
|
|
115
|
+
chunkId: string;
|
|
116
|
+
content: string;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export async function rerankInModelWorker(
|
|
120
|
+
query: string,
|
|
121
|
+
candidates: RerankWorkerCandidate[],
|
|
122
|
+
topK: number,
|
|
123
|
+
): Promise<Array<{ chunkId: string; score: number }>> {
|
|
124
|
+
const result = await requestModelWorker({ type: "rerank", query, candidates, topK });
|
|
125
|
+
if (!Array.isArray(result)) throw new Error("Invalid reranker worker response");
|
|
126
|
+
return result.map((item) => {
|
|
127
|
+
if (
|
|
128
|
+
typeof item !== "object" ||
|
|
129
|
+
item === null ||
|
|
130
|
+
typeof (item as { chunkId?: unknown }).chunkId !== "string" ||
|
|
131
|
+
typeof (item as { score?: unknown }).score !== "number"
|
|
132
|
+
) {
|
|
133
|
+
throw new Error("Invalid reranker result from worker");
|
|
134
|
+
}
|
|
135
|
+
return item as { chunkId: string; score: number };
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export function shutdownModelWorker(): void {
|
|
140
|
+
const child = worker;
|
|
141
|
+
worker = null;
|
|
142
|
+
rejectPending(new Error("Model worker shut down"));
|
|
143
|
+
if (child && !child.killed) {
|
|
144
|
+
child.kill("SIGKILL");
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { join } from "node:path";
|
|
2
|
+
import { getDefaultKnowledgeDir } from "./storage/sqlite.ts";
|
|
3
|
+
|
|
4
|
+
type FeatureExtractionPipeline = (
|
|
5
|
+
input: string,
|
|
6
|
+
options: { pooling: "mean"; normalize: true },
|
|
7
|
+
) => Promise<{ data: ArrayLike<number> }>;
|
|
8
|
+
|
|
9
|
+
type RerankerPipeline = (input: {
|
|
10
|
+
text: string;
|
|
11
|
+
text_pair: string;
|
|
12
|
+
}) => Promise<{ score?: number } | Array<{ score?: number }>>;
|
|
13
|
+
|
|
14
|
+
type EmbedRequest = {
|
|
15
|
+
id: number;
|
|
16
|
+
type: "embed";
|
|
17
|
+
texts: string[];
|
|
18
|
+
prefix: "query" | "passage";
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
type RerankRequest = {
|
|
22
|
+
id: number;
|
|
23
|
+
type: "rerank";
|
|
24
|
+
query: string;
|
|
25
|
+
candidates: Array<{ chunkId: string; content: string }>;
|
|
26
|
+
topK: number;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
type WorkerRequest = EmbedRequest | RerankRequest;
|
|
30
|
+
|
|
31
|
+
let embeddingPipeline: FeatureExtractionPipeline | null = null;
|
|
32
|
+
let rerankerPipeline: RerankerPipeline | null = null;
|
|
33
|
+
|
|
34
|
+
function getModelCacheDir(): string {
|
|
35
|
+
return process.env.PI_KNOWLEDGE_MODEL_CACHE_DIR ?? join(getDefaultKnowledgeDir(), "models");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async function loadEmbeddingPipeline(): Promise<FeatureExtractionPipeline> {
|
|
39
|
+
if (embeddingPipeline) return embeddingPipeline;
|
|
40
|
+
const { pipeline, env } = await import("@huggingface/transformers");
|
|
41
|
+
env.cacheDir = getModelCacheDir();
|
|
42
|
+
embeddingPipeline = await pipeline("feature-extraction", "Xenova/multilingual-e5-small", {
|
|
43
|
+
quantized: true,
|
|
44
|
+
dtype: "fp32",
|
|
45
|
+
});
|
|
46
|
+
return embeddingPipeline;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function loadRerankerPipeline(): Promise<RerankerPipeline> {
|
|
50
|
+
if (rerankerPipeline) return rerankerPipeline;
|
|
51
|
+
const { pipeline, env } = await import("@huggingface/transformers");
|
|
52
|
+
env.cacheDir = getModelCacheDir();
|
|
53
|
+
rerankerPipeline = await pipeline("text-classification", "Xenova/ms-marco-MiniLM-L-4-v2");
|
|
54
|
+
return rerankerPipeline;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async function handleEmbed(request: EmbedRequest): Promise<number[][]> {
|
|
58
|
+
const pipe = await loadEmbeddingPipeline();
|
|
59
|
+
const vectors: number[][] = [];
|
|
60
|
+
for (const text of request.texts) {
|
|
61
|
+
const output = await pipe(`${request.prefix}: ${text}`, { pooling: "mean", normalize: true });
|
|
62
|
+
vectors.push(Array.from(output.data));
|
|
63
|
+
}
|
|
64
|
+
return vectors;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function handleRerank(request: RerankRequest): Promise<Array<{ chunkId: string; score: number }>> {
|
|
68
|
+
const pipe = await loadRerankerPipeline();
|
|
69
|
+
const results: Array<{ chunkId: string; score: number }> = [];
|
|
70
|
+
for (const candidate of request.candidates) {
|
|
71
|
+
const output = await pipe({ text: request.query, text_pair: candidate.content });
|
|
72
|
+
const score = Array.isArray(output) ? (output[0]?.score ?? 0) : (output?.score ?? 0);
|
|
73
|
+
results.push({ chunkId: candidate.chunkId, score });
|
|
74
|
+
}
|
|
75
|
+
results.sort((a, b) => b.score - a.score);
|
|
76
|
+
return results.slice(0, request.topK);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
process.on("message", (request: WorkerRequest) => {
|
|
80
|
+
void (async () => {
|
|
81
|
+
try {
|
|
82
|
+
const result = request.type === "embed" ? await handleEmbed(request) : await handleRerank(request);
|
|
83
|
+
process.send?.({ id: request.id, result });
|
|
84
|
+
} catch (error) {
|
|
85
|
+
process.send?.({ id: request.id, error: error instanceof Error ? error.message : String(error) });
|
|
86
|
+
}
|
|
87
|
+
})();
|
|
88
|
+
});
|
package/src/search/reranker.ts
CHANGED
|
@@ -1,27 +1,12 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { getDefaultKnowledgeDir } from "../storage/sqlite.ts";
|
|
1
|
+
import { rerankInModelWorker } from "../model-worker-client.ts";
|
|
3
2
|
|
|
4
|
-
type RerankerPipeline = {
|
|
5
|
-
(input: { text: string; text_pair: string }): Promise<{ score?: number } | Array<{ score?: number }>>;
|
|
6
|
-
dispose(): Promise<void> | void;
|
|
7
|
-
};
|
|
8
|
-
|
|
9
|
-
let rerankerPipeline: RerankerPipeline | null = null;
|
|
10
3
|
let disposeTimer: ReturnType<typeof setTimeout> | null = null;
|
|
11
4
|
let disposePromise: Promise<void> | null = null;
|
|
12
5
|
let activeRuns = 0;
|
|
13
6
|
let disposeRequested = false;
|
|
14
7
|
const idleWaiters: Array<() => void> = [];
|
|
15
8
|
const IDLE_MS = 30_000;
|
|
16
|
-
|
|
17
|
-
async function load(): Promise<RerankerPipeline> {
|
|
18
|
-
if (rerankerPipeline) return rerankerPipeline;
|
|
19
|
-
if (disposePromise) await disposePromise;
|
|
20
|
-
const { pipeline, env } = await import("@huggingface/transformers");
|
|
21
|
-
env.cacheDir = join(getDefaultKnowledgeDir(), "models");
|
|
22
|
-
rerankerPipeline = await pipeline("text-classification", "Xenova/ms-marco-MiniLM-L-4-v2");
|
|
23
|
-
return rerankerPipeline;
|
|
24
|
-
}
|
|
9
|
+
const ENABLE_NATIVE_IDLE_DISPOSE = process.env.PI_KNOWLEDGE_ENABLE_NATIVE_IDLE_DISPOSE === "true";
|
|
25
10
|
|
|
26
11
|
function clearTimer(): void {
|
|
27
12
|
if (disposeTimer) clearTimeout(disposeTimer);
|
|
@@ -31,6 +16,7 @@ function clearTimer(): void {
|
|
|
31
16
|
function scheduleDispose(): void {
|
|
32
17
|
if (activeRuns > 0 || disposeRequested) return;
|
|
33
18
|
clearTimer();
|
|
19
|
+
if (!ENABLE_NATIVE_IDLE_DISPOSE) return;
|
|
34
20
|
disposeTimer = setTimeout(() => disposeReranker(), IDLE_MS);
|
|
35
21
|
}
|
|
36
22
|
|
|
@@ -56,13 +42,7 @@ export async function disposeReranker(): Promise<void> {
|
|
|
56
42
|
if (disposePromise) return disposePromise;
|
|
57
43
|
disposeRequested = true;
|
|
58
44
|
await waitForNoActiveRuns();
|
|
59
|
-
|
|
60
|
-
rerankerPipeline = null;
|
|
61
|
-
if (!instance) {
|
|
62
|
-
disposeRequested = false;
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
disposePromise = Promise.resolve(instance.dispose()).finally(() => {
|
|
45
|
+
disposePromise = Promise.resolve().finally(() => {
|
|
66
46
|
disposePromise = null;
|
|
67
47
|
disposeRequested = false;
|
|
68
48
|
});
|
|
@@ -85,20 +65,10 @@ export async function rerank(
|
|
|
85
65
|
topK: number,
|
|
86
66
|
): Promise<Array<{ chunkId: string; score: number }>> {
|
|
87
67
|
if (candidates.length === 0) return [];
|
|
88
|
-
const pipe = await load();
|
|
89
68
|
beginRun();
|
|
90
|
-
|
|
91
|
-
const results: Array<{ chunkId: string; score: number }> = [];
|
|
92
69
|
try {
|
|
93
|
-
|
|
94
|
-
const output = await pipe({ text: query, text_pair: c.content });
|
|
95
|
-
const score = Array.isArray(output) ? (output[0]?.score ?? 0) : (output?.score ?? 0);
|
|
96
|
-
results.push({ chunkId: c.chunkId, score });
|
|
97
|
-
}
|
|
70
|
+
return await rerankInModelWorker(query, candidates, topK);
|
|
98
71
|
} finally {
|
|
99
72
|
endRun();
|
|
100
73
|
}
|
|
101
|
-
|
|
102
|
-
results.sort((a, b) => b.score - a.score);
|
|
103
|
-
return results.slice(0, topK);
|
|
104
74
|
}
|