@remit/search-service 0.0.8 → 0.0.9
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/package.json +1 -1
- package/src/embeddings.test.ts +41 -1
- package/src/embeddings.ts +44 -7
- package/src/index.ts +1 -0
package/package.json
CHANGED
package/src/embeddings.test.ts
CHANGED
|
@@ -1,6 +1,24 @@
|
|
|
1
1
|
import assert from "node:assert/strict";
|
|
2
2
|
import { describe, it } from "node:test";
|
|
3
|
-
import {
|
|
3
|
+
import type { pipeline as PipelineFn } from "@huggingface/transformers";
|
|
4
|
+
import {
|
|
5
|
+
EmbeddingModelUnavailableError,
|
|
6
|
+
LocalEmbeddingService,
|
|
7
|
+
} from "./embeddings.js";
|
|
8
|
+
|
|
9
|
+
// Exercises the model-load path without the network: the pipeline factory
|
|
10
|
+
// rejects the way a failed `from_pretrained` (HuggingFace fetch failed) does.
|
|
11
|
+
class FailingLoadService extends LocalEmbeddingService {
|
|
12
|
+
importCount = 0;
|
|
13
|
+
protected importTransformers(): Promise<{ pipeline: typeof PipelineFn }> {
|
|
14
|
+
this.importCount++;
|
|
15
|
+
const pipeline = (() =>
|
|
16
|
+
Promise.reject(
|
|
17
|
+
new TypeError("fetch failed"),
|
|
18
|
+
)) as unknown as typeof PipelineFn;
|
|
19
|
+
return Promise.resolve({ pipeline });
|
|
20
|
+
}
|
|
21
|
+
}
|
|
4
22
|
|
|
5
23
|
describe("LocalEmbeddingService", () => {
|
|
6
24
|
it("keeps the historical embeddingId when dtype is unset", () => {
|
|
@@ -25,4 +43,26 @@ describe("LocalEmbeddingService", () => {
|
|
|
25
43
|
"local:Xenova/paraphrase-multilingual-MiniLM-L12-v2:q8@384",
|
|
26
44
|
);
|
|
27
45
|
});
|
|
46
|
+
|
|
47
|
+
it("raises a typed EmbeddingModelUnavailableError when the model cannot be loaded", async () => {
|
|
48
|
+
const service = new FailingLoadService({ dimensions: 8 });
|
|
49
|
+
await assert.rejects(
|
|
50
|
+
() => service.embed(["hello"]),
|
|
51
|
+
(error: unknown) => {
|
|
52
|
+
assert.ok(error instanceof EmbeddingModelUnavailableError);
|
|
53
|
+
assert.equal(
|
|
54
|
+
(error as { code?: string }).code,
|
|
55
|
+
"ERR_EMBEDDING_MODEL_UNAVAILABLE",
|
|
56
|
+
);
|
|
57
|
+
return true;
|
|
58
|
+
},
|
|
59
|
+
);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("clears the memoized pipeline on a load failure so a later call retries", async () => {
|
|
63
|
+
const service = new FailingLoadService({ dimensions: 8 });
|
|
64
|
+
await assert.rejects(() => service.embed(["hello"]));
|
|
65
|
+
await assert.rejects(() => service.embed(["hello"]));
|
|
66
|
+
assert.equal(service.importCount, 2);
|
|
67
|
+
});
|
|
28
68
|
});
|
package/src/embeddings.ts
CHANGED
|
@@ -91,6 +91,24 @@ export interface LocalEmbeddingConfig {
|
|
|
91
91
|
const DEFAULT_LOCAL_MODEL_ID = "Xenova/all-MiniLM-L6-v2";
|
|
92
92
|
const DEFAULT_LOCAL_DIMENSIONS = 384;
|
|
93
93
|
|
|
94
|
+
/**
|
|
95
|
+
* The local embedder could not load its model. Raised when Transformers.js fails
|
|
96
|
+
* to resolve the model files — the common cause is `from_pretrained` lazily
|
|
97
|
+
* fetching the weights from HuggingFace and the network fetch failing
|
|
98
|
+
* (`TypeError: fetch failed`), which otherwise surfaces as an opaque 500.
|
|
99
|
+
*
|
|
100
|
+
* The `code` is the contract consumers match on (mirroring Node's own error
|
|
101
|
+
* codes) to degrade the semantic path to empty results instead of crashing,
|
|
102
|
+
* without importing this class or string-matching undici internals.
|
|
103
|
+
*/
|
|
104
|
+
export class EmbeddingModelUnavailableError extends Error {
|
|
105
|
+
readonly code = "ERR_EMBEDDING_MODEL_UNAVAILABLE";
|
|
106
|
+
constructor(modelId: string, options?: { cause?: unknown }) {
|
|
107
|
+
super(`Embedding model "${modelId}" could not be loaded`, options);
|
|
108
|
+
this.name = "EmbeddingModelUnavailableError";
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
94
112
|
type TransformersModule = { pipeline: typeof PipelineFn };
|
|
95
113
|
|
|
96
114
|
/**
|
|
@@ -122,15 +140,34 @@ export class LocalEmbeddingService implements EmbeddingService {
|
|
|
122
140
|
|
|
123
141
|
private getPipeline = async (): Promise<FeatureExtractionPipeline> => {
|
|
124
142
|
if (this.pipelinePromise) return this.pipelinePromise;
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
143
|
+
const promise = this.loadPipeline();
|
|
144
|
+
this.pipelinePromise = promise;
|
|
145
|
+
try {
|
|
146
|
+
return await promise;
|
|
147
|
+
} catch (error) {
|
|
148
|
+
// A failed load (module absent, or the model could not be fetched) must
|
|
149
|
+
// not poison the instance: clear the memo so a later call retries rather
|
|
150
|
+
// than returning the same rejected promise for the process lifetime.
|
|
151
|
+
if (this.pipelinePromise === promise) this.pipelinePromise = null;
|
|
152
|
+
throw error;
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
// Import boundary kept overridable so a load failure can be exercised without
|
|
157
|
+
// the network; the default resolves Transformers.js lazily via runtimeImport.
|
|
158
|
+
protected importTransformers(): Promise<TransformersModule> {
|
|
159
|
+
return runtimeImport<TransformersModule>("@huggingface/transformers");
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
private loadPipeline = async (): Promise<FeatureExtractionPipeline> => {
|
|
163
|
+
const { pipeline } = await this.importTransformers();
|
|
164
|
+
try {
|
|
165
|
+
return await pipeline("feature-extraction", this.modelId, {
|
|
130
166
|
dtype: this.dtype,
|
|
131
167
|
});
|
|
132
|
-
}
|
|
133
|
-
|
|
168
|
+
} catch (error) {
|
|
169
|
+
throw new EmbeddingModelUnavailableError(this.modelId, { cause: error });
|
|
170
|
+
}
|
|
134
171
|
};
|
|
135
172
|
|
|
136
173
|
embed = async (texts: string[]): Promise<number[][]> => {
|