@nodetool-ai/vectorstore 0.7.0-rc.11
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/dist/chroma-client.d.ts +25 -0
- package/dist/chroma-client.d.ts.map +1 -0
- package/dist/chroma-client.js +130 -0
- package/dist/chroma-client.js.map +1 -0
- package/dist/embedding.d.ts +106 -0
- package/dist/embedding.d.ts.map +1 -0
- package/dist/embedding.js +386 -0
- package/dist/embedding.js.map +1 -0
- package/dist/index.d.ts +18 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +56 -0
- package/dist/index.js.map +1 -0
- package/dist/sqlite-vec-store.d.ts +129 -0
- package/dist/sqlite-vec-store.d.ts.map +1 -0
- package/dist/sqlite-vec-store.js +507 -0
- package/dist/sqlite-vec-store.js.map +1 -0
- package/package.json +51 -0
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Document splitting utilities.
|
|
3
|
+
*
|
|
4
|
+
* Kept from the original ChromaDB client module. The ChromaDB client
|
|
5
|
+
* has been replaced by sqlite-vec-store.ts.
|
|
6
|
+
*/
|
|
7
|
+
export interface TextChunk {
|
|
8
|
+
text: string;
|
|
9
|
+
source_id: string;
|
|
10
|
+
start_index: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Split text into chunks using a recursive character text splitter.
|
|
14
|
+
*
|
|
15
|
+
* This is a pure-TS implementation that mirrors LangChain's
|
|
16
|
+
* `RecursiveCharacterTextSplitter` behaviour used in the Python codebase.
|
|
17
|
+
*
|
|
18
|
+
* @param text The text to split.
|
|
19
|
+
* @param sourceId An identifier for the source document.
|
|
20
|
+
* @param chunkSize Maximum chunk size in characters (default 2000).
|
|
21
|
+
* @param chunkOverlap Overlap between consecutive chunks (default 1000).
|
|
22
|
+
* @param separators Ordered list of separators to try.
|
|
23
|
+
*/
|
|
24
|
+
export declare function splitDocument(text: string, sourceId: string, chunkSize?: number, chunkOverlap?: number, separators?: string[]): TextChunk[];
|
|
25
|
+
//# sourceMappingURL=chroma-client.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chroma-client.d.ts","sourceRoot":"","sources":["../src/chroma-client.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAMH,MAAM,WAAW,SAAS;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;CACrB;AAID;;;;;;;;;;;GAWG;AACH,wBAAgB,aAAa,CAC3B,IAAI,EAAE,MAAM,EACZ,QAAQ,EAAE,MAAM,EAChB,SAAS,SAAO,EAChB,YAAY,SAAO,EACnB,UAAU,GAAE,MAAM,EAAuB,GACxC,SAAS,EAAE,CAUb"}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Document splitting utilities.
|
|
3
|
+
*
|
|
4
|
+
* Kept from the original ChromaDB client module. The ChromaDB client
|
|
5
|
+
* has been replaced by sqlite-vec-store.ts.
|
|
6
|
+
*/
|
|
7
|
+
const DEFAULT_SEPARATORS = ["\n\n", "\n", "."];
|
|
8
|
+
/**
|
|
9
|
+
* Split text into chunks using a recursive character text splitter.
|
|
10
|
+
*
|
|
11
|
+
* This is a pure-TS implementation that mirrors LangChain's
|
|
12
|
+
* `RecursiveCharacterTextSplitter` behaviour used in the Python codebase.
|
|
13
|
+
*
|
|
14
|
+
* @param text The text to split.
|
|
15
|
+
* @param sourceId An identifier for the source document.
|
|
16
|
+
* @param chunkSize Maximum chunk size in characters (default 2000).
|
|
17
|
+
* @param chunkOverlap Overlap between consecutive chunks (default 1000).
|
|
18
|
+
* @param separators Ordered list of separators to try.
|
|
19
|
+
*/
|
|
20
|
+
export function splitDocument(text, sourceId, chunkSize = 2000, chunkOverlap = 1000, separators = DEFAULT_SEPARATORS) {
|
|
21
|
+
if (!text || !text.trim())
|
|
22
|
+
return [];
|
|
23
|
+
const chunks = recursiveSplit(text, separators, chunkSize, chunkOverlap);
|
|
24
|
+
return chunks.map((chunk) => ({
|
|
25
|
+
text: chunk.text,
|
|
26
|
+
source_id: sourceId,
|
|
27
|
+
start_index: chunk.start
|
|
28
|
+
}));
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Recursive text splitting — tries each separator in order, falling back
|
|
32
|
+
* to character-level splitting when no separator produces small enough chunks.
|
|
33
|
+
*/
|
|
34
|
+
function recursiveSplit(text, separators, chunkSize, chunkOverlap) {
|
|
35
|
+
if (text.length <= chunkSize) {
|
|
36
|
+
return [{ text, start: 0 }];
|
|
37
|
+
}
|
|
38
|
+
// Try each separator
|
|
39
|
+
for (let i = 0; i < separators.length; i++) {
|
|
40
|
+
const sep = separators[i];
|
|
41
|
+
if (!text.includes(sep))
|
|
42
|
+
continue;
|
|
43
|
+
const parts = splitBySeparator(text, sep);
|
|
44
|
+
const merged = mergeSplits(parts, sep, chunkSize, chunkOverlap);
|
|
45
|
+
// Check if all chunks are within the size limit
|
|
46
|
+
const allFit = merged.every((c) => c.text.length <= chunkSize);
|
|
47
|
+
if (allFit)
|
|
48
|
+
return merged;
|
|
49
|
+
// Some chunks are still too large — recursively split them with remaining separators
|
|
50
|
+
const remainingSeparators = separators.slice(i + 1);
|
|
51
|
+
const result = [];
|
|
52
|
+
for (const chunk of merged) {
|
|
53
|
+
if (chunk.text.length <= chunkSize) {
|
|
54
|
+
result.push(chunk);
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
const subChunks = recursiveSplit(chunk.text, remainingSeparators, chunkSize, chunkOverlap);
|
|
58
|
+
for (const sub of subChunks) {
|
|
59
|
+
result.push({ text: sub.text, start: chunk.start + sub.start });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return result;
|
|
64
|
+
}
|
|
65
|
+
// No separator worked — split by character boundaries
|
|
66
|
+
return splitByCharacters(text, chunkSize, chunkOverlap);
|
|
67
|
+
}
|
|
68
|
+
function splitBySeparator(text, sep) {
|
|
69
|
+
const parts = [];
|
|
70
|
+
let pos = 0;
|
|
71
|
+
while (true) {
|
|
72
|
+
const next = text.indexOf(sep, pos);
|
|
73
|
+
if (next === -1) {
|
|
74
|
+
const remaining = text.slice(pos);
|
|
75
|
+
if (remaining.length > 0) {
|
|
76
|
+
parts.push({ text: remaining, offset: pos });
|
|
77
|
+
}
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
const segment = text.slice(pos, next + sep.length);
|
|
81
|
+
if (segment.length > 0) {
|
|
82
|
+
parts.push({ text: segment, offset: pos });
|
|
83
|
+
}
|
|
84
|
+
pos = next + sep.length;
|
|
85
|
+
}
|
|
86
|
+
return parts;
|
|
87
|
+
}
|
|
88
|
+
function mergeSplits(parts, _sep, chunkSize, chunkOverlap) {
|
|
89
|
+
const chunks = [];
|
|
90
|
+
let current = "";
|
|
91
|
+
let currentStart = 0;
|
|
92
|
+
for (const part of parts) {
|
|
93
|
+
const candidate = current.length === 0 ? part.text : current + part.text;
|
|
94
|
+
if (candidate.length > chunkSize && current.length > 0) {
|
|
95
|
+
// Emit current chunk
|
|
96
|
+
chunks.push({ text: current.trimEnd(), start: currentStart });
|
|
97
|
+
// Compute overlap: keep the tail of the current chunk
|
|
98
|
+
if (chunkOverlap > 0 && current.length > chunkOverlap) {
|
|
99
|
+
const overlapText = current.slice(current.length - chunkOverlap);
|
|
100
|
+
current = overlapText + part.text;
|
|
101
|
+
currentStart = part.offset - overlapText.length;
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
current = part.text;
|
|
105
|
+
currentStart = part.offset;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
if (current.length === 0)
|
|
110
|
+
currentStart = part.offset;
|
|
111
|
+
current = candidate;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (current.trim().length > 0) {
|
|
115
|
+
chunks.push({ text: current.trimEnd(), start: currentStart });
|
|
116
|
+
}
|
|
117
|
+
return chunks;
|
|
118
|
+
}
|
|
119
|
+
function splitByCharacters(text, chunkSize, chunkOverlap) {
|
|
120
|
+
const chunks = [];
|
|
121
|
+
const step = Math.max(1, chunkSize - chunkOverlap);
|
|
122
|
+
let pos = 0;
|
|
123
|
+
while (pos < text.length) {
|
|
124
|
+
const end = Math.min(pos + chunkSize, text.length);
|
|
125
|
+
chunks.push({ text: text.slice(pos, end), start: pos });
|
|
126
|
+
pos += step;
|
|
127
|
+
}
|
|
128
|
+
return chunks;
|
|
129
|
+
}
|
|
130
|
+
//# sourceMappingURL=chroma-client.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chroma-client.js","sourceRoot":"","sources":["../src/chroma-client.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAYH,MAAM,kBAAkB,GAAG,CAAC,MAAM,EAAE,IAAI,EAAE,GAAG,CAAC,CAAC;AAE/C;;;;;;;;;;;GAWG;AACH,MAAM,UAAU,aAAa,CAC3B,IAAY,EACZ,QAAgB,EAChB,SAAS,GAAG,IAAI,EAChB,YAAY,GAAG,IAAI,EACnB,aAAuB,kBAAkB;IAEzC,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,IAAI,EAAE;QAAE,OAAO,EAAE,CAAC;IAErC,MAAM,MAAM,GAAG,cAAc,CAAC,IAAI,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,CAAC,CAAC;IAEzE,OAAO,MAAM,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC;QAC5B,IAAI,EAAE,KAAK,CAAC,IAAI;QAChB,SAAS,EAAE,QAAQ;QACnB,WAAW,EAAE,KAAK,CAAC,KAAK;KACzB,CAAC,CAAC,CAAC;AACN,CAAC;AAOD;;;GAGG;AACH,SAAS,cAAc,CACrB,IAAY,EACZ,UAAoB,EACpB,SAAiB,EACjB,YAAoB;IAEpB,IAAI,IAAI,CAAC,MAAM,IAAI,SAAS,EAAE,CAAC;QAC7B,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YAAY,EAAE,CAAC,CAAC;IAChE,CAAC;IAED,OAAO,MAAM,CAAC;AAChB,CAAC;AAED,SAAS,iBAAiB,CACxB,IAAY,EACZ,SAAiB,EACjB,YAAoB;IAEpB,MAAM,MAAM,GAAe,EAAE,CAAC;IAC9B,MAAM,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,SAAS,GAAG,YAAY,CAAC,CAAC;IACnD,IAAI,GAAG,GAAG,CAAC,CAAC;IACZ,OAAO,GAAG,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC;QACzB,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,GAAG,SAAS,EAAE,IAAI,CAAC,MAAM,CAAC,CAAC;QACnD,MAAM,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,KAAK,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,KAAK,EAAE,GAAG,EAAE,CAAC,CAAC;QACxD,GAAG,IAAI,IAAI,CAAC;IACd,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider-based embedding functions.
|
|
3
|
+
*
|
|
4
|
+
* Implements the EmbeddingFunction interface using provider APIs
|
|
5
|
+
* (OpenAI, Ollama, Gemini, Mistral, Cohere, Voyage AI, Jina AI).
|
|
6
|
+
*/
|
|
7
|
+
import type { EmbeddingFunction } from "./sqlite-vec-store.js";
|
|
8
|
+
export type EmbeddingProvider = "openai" | "ollama" | "gemini" | "mistral" | "cohere" | "voyage" | "jina";
|
|
9
|
+
export interface ProviderEmbeddingOptions {
|
|
10
|
+
provider: EmbeddingProvider;
|
|
11
|
+
model: string;
|
|
12
|
+
dimensions?: number;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Embedding function that calls a remote provider API.
|
|
16
|
+
*
|
|
17
|
+
* Lazily resolves API keys from secrets / environment on first call.
|
|
18
|
+
*/
|
|
19
|
+
export declare class ProviderEmbeddingFunction implements EmbeddingFunction {
|
|
20
|
+
readonly name: string;
|
|
21
|
+
private provider;
|
|
22
|
+
private model;
|
|
23
|
+
private dimensions?;
|
|
24
|
+
private _apiKey;
|
|
25
|
+
private _keyResolved;
|
|
26
|
+
constructor(opts: ProviderEmbeddingOptions);
|
|
27
|
+
private resolveApiKey;
|
|
28
|
+
generate(texts: string[]): Promise<number[][]>;
|
|
29
|
+
private _generateOpenAI;
|
|
30
|
+
private _generateOllama;
|
|
31
|
+
private _generateGemini;
|
|
32
|
+
private _generateMistral;
|
|
33
|
+
private _generateCohere;
|
|
34
|
+
private _generateVoyage;
|
|
35
|
+
private _generateJina;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* OpenAI embedding function.
|
|
39
|
+
*
|
|
40
|
+
* @param model Defaults to `text-embedding-3-small`.
|
|
41
|
+
* @param dimensions Optional output dimensions for text-embedding-3-* models.
|
|
42
|
+
*/
|
|
43
|
+
export declare class OpenAIEmbeddingFunction extends ProviderEmbeddingFunction {
|
|
44
|
+
constructor(model?: string, dimensions?: number);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Ollama embedding function.
|
|
48
|
+
*
|
|
49
|
+
* @param model Defaults to `nomic-embed-text`.
|
|
50
|
+
*/
|
|
51
|
+
export declare class OllamaEmbeddingFunction extends ProviderEmbeddingFunction {
|
|
52
|
+
constructor(model?: string);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Gemini embedding function.
|
|
56
|
+
*
|
|
57
|
+
* @param model Defaults to `text-embedding-004`.
|
|
58
|
+
*/
|
|
59
|
+
export declare class GeminiEmbeddingFunction extends ProviderEmbeddingFunction {
|
|
60
|
+
constructor(model?: string);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Mistral embedding function.
|
|
64
|
+
*
|
|
65
|
+
* @param model Defaults to `mistral-embed`.
|
|
66
|
+
*/
|
|
67
|
+
export declare class MistralEmbeddingFunction extends ProviderEmbeddingFunction {
|
|
68
|
+
constructor(model?: string);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Cohere embedding function.
|
|
72
|
+
*
|
|
73
|
+
* @param model Defaults to `embed-v4.0`.
|
|
74
|
+
*/
|
|
75
|
+
export declare class CohereEmbeddingFunction extends ProviderEmbeddingFunction {
|
|
76
|
+
constructor(model?: string, dimensions?: number);
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Voyage AI embedding function.
|
|
80
|
+
*
|
|
81
|
+
* @param model Defaults to `voyage-3.5`.
|
|
82
|
+
*/
|
|
83
|
+
export declare class VoyageEmbeddingFunction extends ProviderEmbeddingFunction {
|
|
84
|
+
constructor(model?: string, dimensions?: number);
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Jina AI embedding function.
|
|
88
|
+
*
|
|
89
|
+
* @param model Defaults to `jina-embeddings-v3`.
|
|
90
|
+
*/
|
|
91
|
+
export declare class JinaEmbeddingFunction extends ProviderEmbeddingFunction {
|
|
92
|
+
constructor(model?: string, dimensions?: number);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Resolve an embedding function from a model name and optional provider.
|
|
96
|
+
*
|
|
97
|
+
* When provider is omitted, the model name is used to infer the provider:
|
|
98
|
+
* - `text-embedding-*` → OpenAI
|
|
99
|
+
* - Otherwise → Ollama (if OLLAMA_API_URL is set)
|
|
100
|
+
* - Fallback → null (caller should handle)
|
|
101
|
+
*
|
|
102
|
+
* @param embeddingModel Model identifier.
|
|
103
|
+
* @param provider Optional explicit provider name.
|
|
104
|
+
*/
|
|
105
|
+
export declare function getProviderEmbeddingFunction(embeddingModel: string, provider?: string | null): EmbeddingFunction | null;
|
|
106
|
+
//# sourceMappingURL=embedding.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"embedding.d.ts","sourceRoot":"","sources":["../src/embedding.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAI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AAE,MAAM;CAGtD;AAED;;;;GAIG;AACH,qBAAa,uBAAwB,SAAQ,yBAAyB;gBACxD,KAAK,SAAe,EAAE,UAAU,CAAC,EAAE,MAAM;CAGtD;AAED;;;;GAIG;AACH,qBAAa,qBAAsB,SAAQ,yBAAyB;gBACtD,KAAK,SAAuB,EAAE,UAAU,CAAC,EAAE,MAAM;CAG9D;AAMD;;;;;;;;;;GAUG;AACH,wBAAgB,4BAA4B,CAC1C,cAAc,EAAE,MAAM,EACtB,QAAQ,CAAC,EAAE,MAAM,GAAG,IAAI,GACvB,iBAAiB,GAAG,IAAI,CA+C1B"}
|
|
@@ -0,0 +1,386 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Provider-based embedding functions.
|
|
3
|
+
*
|
|
4
|
+
* Implements the EmbeddingFunction interface using provider APIs
|
|
5
|
+
* (OpenAI, Ollama, Gemini, Mistral, Cohere, Voyage AI, Jina AI).
|
|
6
|
+
*/
|
|
7
|
+
import { createLogger } from "@nodetool-ai/config";
|
|
8
|
+
import { getSecret } from "@nodetool-ai/models";
|
|
9
|
+
const log = createLogger("nodetool.vectorstore.embedding");
|
|
10
|
+
// ---------------------------------------------------------------------------
|
|
11
|
+
// Provider embedding function
|
|
12
|
+
// ---------------------------------------------------------------------------
|
|
13
|
+
/**
|
|
14
|
+
* Embedding function that calls a remote provider API.
|
|
15
|
+
*
|
|
16
|
+
* Lazily resolves API keys from secrets / environment on first call.
|
|
17
|
+
*/
|
|
18
|
+
export class ProviderEmbeddingFunction {
|
|
19
|
+
name;
|
|
20
|
+
provider;
|
|
21
|
+
model;
|
|
22
|
+
dimensions;
|
|
23
|
+
_apiKey = null;
|
|
24
|
+
_keyResolved = false;
|
|
25
|
+
constructor(opts) {
|
|
26
|
+
this.provider = opts.provider;
|
|
27
|
+
this.model = opts.model;
|
|
28
|
+
this.dimensions = opts.dimensions;
|
|
29
|
+
this.name = `${opts.provider}/${opts.model}`;
|
|
30
|
+
}
|
|
31
|
+
async resolveApiKey() {
|
|
32
|
+
if (this._keyResolved)
|
|
33
|
+
return this._apiKey;
|
|
34
|
+
this._keyResolved = true;
|
|
35
|
+
const envKeyMap = {
|
|
36
|
+
openai: "OPENAI_API_KEY",
|
|
37
|
+
ollama: "OLLAMA_API_URL",
|
|
38
|
+
gemini: "GEMINI_API_KEY",
|
|
39
|
+
mistral: "MISTRAL_API_KEY",
|
|
40
|
+
cohere: "COHERE_API_KEY",
|
|
41
|
+
voyage: "VOYAGE_API_KEY",
|
|
42
|
+
jina: "JINA_API_KEY"
|
|
43
|
+
};
|
|
44
|
+
const envKey = envKeyMap[this.provider];
|
|
45
|
+
this._apiKey =
|
|
46
|
+
(await getSecret(envKey, "1").catch(() => null)) ??
|
|
47
|
+
process.env[envKey] ??
|
|
48
|
+
null;
|
|
49
|
+
return this._apiKey;
|
|
50
|
+
}
|
|
51
|
+
async generate(texts) {
|
|
52
|
+
if (texts.length === 0)
|
|
53
|
+
return [];
|
|
54
|
+
switch (this.provider) {
|
|
55
|
+
case "openai":
|
|
56
|
+
return this._generateOpenAI(texts);
|
|
57
|
+
case "ollama":
|
|
58
|
+
return this._generateOllama(texts);
|
|
59
|
+
case "gemini":
|
|
60
|
+
return this._generateGemini(texts);
|
|
61
|
+
case "mistral":
|
|
62
|
+
return this._generateMistral(texts);
|
|
63
|
+
case "cohere":
|
|
64
|
+
return this._generateCohere(texts);
|
|
65
|
+
case "voyage":
|
|
66
|
+
return this._generateVoyage(texts);
|
|
67
|
+
case "jina":
|
|
68
|
+
return this._generateJina(texts);
|
|
69
|
+
default:
|
|
70
|
+
throw new Error(`Unsupported embedding provider: ${this.provider}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
// ── OpenAI ───────────────────────────────────────────────────────────
|
|
74
|
+
async _generateOpenAI(texts) {
|
|
75
|
+
const apiKey = await this.resolveApiKey();
|
|
76
|
+
if (!apiKey)
|
|
77
|
+
throw new Error("OPENAI_API_KEY not configured");
|
|
78
|
+
const body = {
|
|
79
|
+
model: this.model,
|
|
80
|
+
input: texts
|
|
81
|
+
};
|
|
82
|
+
if (this.dimensions)
|
|
83
|
+
body.dimensions = this.dimensions;
|
|
84
|
+
const resp = await fetch("https://api.openai.com/v1/embeddings", {
|
|
85
|
+
method: "POST",
|
|
86
|
+
headers: {
|
|
87
|
+
"Content-Type": "application/json",
|
|
88
|
+
Authorization: `Bearer ${apiKey}`
|
|
89
|
+
},
|
|
90
|
+
body: JSON.stringify(body)
|
|
91
|
+
});
|
|
92
|
+
if (!resp.ok) {
|
|
93
|
+
const err = await resp.text().catch(() => "");
|
|
94
|
+
throw new Error(`OpenAI embedding failed (${resp.status}): ${err}`);
|
|
95
|
+
}
|
|
96
|
+
const data = (await resp.json());
|
|
97
|
+
// Sort by index to guarantee order
|
|
98
|
+
return data.data
|
|
99
|
+
.sort((a, b) => (a.index ?? 0) - (b.index ?? 0))
|
|
100
|
+
.map((d) => d.embedding);
|
|
101
|
+
}
|
|
102
|
+
// ── Ollama ───────────────────────────────────────────────────────────
|
|
103
|
+
async _generateOllama(texts) {
|
|
104
|
+
const baseUrl = (await this.resolveApiKey()) || "http://127.0.0.1:11434";
|
|
105
|
+
const resp = await fetch(`${baseUrl}/api/embed`, {
|
|
106
|
+
method: "POST",
|
|
107
|
+
headers: { "Content-Type": "application/json" },
|
|
108
|
+
body: JSON.stringify({ model: this.model, input: texts })
|
|
109
|
+
});
|
|
110
|
+
if (!resp.ok) {
|
|
111
|
+
const err = await resp.text().catch(() => "");
|
|
112
|
+
throw new Error(`Ollama embedding failed (${resp.status}): ${err}`);
|
|
113
|
+
}
|
|
114
|
+
const data = (await resp.json());
|
|
115
|
+
return data.embeddings;
|
|
116
|
+
}
|
|
117
|
+
// ── Gemini ───────────────────────────────────────────────────────────
|
|
118
|
+
async _generateGemini(texts) {
|
|
119
|
+
const apiKey = await this.resolveApiKey();
|
|
120
|
+
if (!apiKey)
|
|
121
|
+
throw new Error("GEMINI_API_KEY not configured");
|
|
122
|
+
// Gemini embedContent only handles one text at a time
|
|
123
|
+
const embeddings = [];
|
|
124
|
+
for (const text of texts) {
|
|
125
|
+
const url = `https://generativelanguage.googleapis.com/v1beta/models/${this.model}:embedContent?key=${apiKey}`;
|
|
126
|
+
const resp = await fetch(url, {
|
|
127
|
+
method: "POST",
|
|
128
|
+
headers: { "Content-Type": "application/json" },
|
|
129
|
+
body: JSON.stringify({
|
|
130
|
+
content: { parts: [{ text }] }
|
|
131
|
+
})
|
|
132
|
+
});
|
|
133
|
+
if (!resp.ok) {
|
|
134
|
+
const err = await resp.text().catch(() => "");
|
|
135
|
+
throw new Error(`Gemini embedding failed (${resp.status}): ${err}`);
|
|
136
|
+
}
|
|
137
|
+
const data = (await resp.json());
|
|
138
|
+
embeddings.push(data.embedding?.values ?? []);
|
|
139
|
+
}
|
|
140
|
+
return embeddings;
|
|
141
|
+
}
|
|
142
|
+
// ── Mistral ──────────────────────────────────────────────────────────
|
|
143
|
+
async _generateMistral(texts) {
|
|
144
|
+
const apiKey = await this.resolveApiKey();
|
|
145
|
+
if (!apiKey)
|
|
146
|
+
throw new Error("MISTRAL_API_KEY not configured");
|
|
147
|
+
const resp = await fetch("https://api.mistral.ai/v1/embeddings", {
|
|
148
|
+
method: "POST",
|
|
149
|
+
headers: {
|
|
150
|
+
"Content-Type": "application/json",
|
|
151
|
+
Authorization: `Bearer ${apiKey}`
|
|
152
|
+
},
|
|
153
|
+
body: JSON.stringify({
|
|
154
|
+
model: this.model,
|
|
155
|
+
input: texts
|
|
156
|
+
})
|
|
157
|
+
});
|
|
158
|
+
if (!resp.ok) {
|
|
159
|
+
const err = await resp.text().catch(() => "");
|
|
160
|
+
throw new Error(`Mistral embedding failed (${resp.status}): ${err}`);
|
|
161
|
+
}
|
|
162
|
+
const data = (await resp.json());
|
|
163
|
+
return data.data
|
|
164
|
+
.sort((a, b) => (a.index ?? 0) - (b.index ?? 0))
|
|
165
|
+
.map((d) => d.embedding);
|
|
166
|
+
}
|
|
167
|
+
// ── Cohere ───────────────────────────────────────────────────────────
|
|
168
|
+
async _generateCohere(texts) {
|
|
169
|
+
const apiKey = await this.resolveApiKey();
|
|
170
|
+
if (!apiKey)
|
|
171
|
+
throw new Error("COHERE_API_KEY not configured");
|
|
172
|
+
const body = {
|
|
173
|
+
model: this.model,
|
|
174
|
+
texts,
|
|
175
|
+
input_type: "search_document",
|
|
176
|
+
embedding_types: ["float"]
|
|
177
|
+
};
|
|
178
|
+
if (this.dimensions)
|
|
179
|
+
body.output_dimension = this.dimensions;
|
|
180
|
+
const resp = await fetch("https://api.cohere.com/v2/embed", {
|
|
181
|
+
method: "POST",
|
|
182
|
+
headers: {
|
|
183
|
+
"Content-Type": "application/json",
|
|
184
|
+
Authorization: `Bearer ${apiKey}`
|
|
185
|
+
},
|
|
186
|
+
body: JSON.stringify(body)
|
|
187
|
+
});
|
|
188
|
+
if (!resp.ok) {
|
|
189
|
+
const err = await resp.text().catch(() => "");
|
|
190
|
+
throw new Error(`Cohere embedding failed (${resp.status}): ${err}`);
|
|
191
|
+
}
|
|
192
|
+
const data = (await resp.json());
|
|
193
|
+
if (Array.isArray(data.embeddings))
|
|
194
|
+
return data.embeddings;
|
|
195
|
+
const floats = data.embeddings?.float;
|
|
196
|
+
if (!Array.isArray(floats)) {
|
|
197
|
+
throw new Error("Cohere embedding response missing float embeddings");
|
|
198
|
+
}
|
|
199
|
+
return floats;
|
|
200
|
+
}
|
|
201
|
+
// ── Voyage AI ────────────────────────────────────────────────────────
|
|
202
|
+
async _generateVoyage(texts) {
|
|
203
|
+
const apiKey = await this.resolveApiKey();
|
|
204
|
+
if (!apiKey)
|
|
205
|
+
throw new Error("VOYAGE_API_KEY not configured");
|
|
206
|
+
const body = {
|
|
207
|
+
model: this.model,
|
|
208
|
+
input: texts,
|
|
209
|
+
input_type: "document"
|
|
210
|
+
};
|
|
211
|
+
if (this.dimensions)
|
|
212
|
+
body.output_dimension = this.dimensions;
|
|
213
|
+
const resp = await fetch("https://api.voyageai.com/v1/embeddings", {
|
|
214
|
+
method: "POST",
|
|
215
|
+
headers: {
|
|
216
|
+
"Content-Type": "application/json",
|
|
217
|
+
Authorization: `Bearer ${apiKey}`
|
|
218
|
+
},
|
|
219
|
+
body: JSON.stringify(body)
|
|
220
|
+
});
|
|
221
|
+
if (!resp.ok) {
|
|
222
|
+
const err = await resp.text().catch(() => "");
|
|
223
|
+
throw new Error(`Voyage embedding failed (${resp.status}): ${err}`);
|
|
224
|
+
}
|
|
225
|
+
const data = (await resp.json());
|
|
226
|
+
return data.data
|
|
227
|
+
.sort((a, b) => (a.index ?? 0) - (b.index ?? 0))
|
|
228
|
+
.map((d) => d.embedding);
|
|
229
|
+
}
|
|
230
|
+
// ── Jina AI ──────────────────────────────────────────────────────────
|
|
231
|
+
async _generateJina(texts) {
|
|
232
|
+
const apiKey = await this.resolveApiKey();
|
|
233
|
+
if (!apiKey)
|
|
234
|
+
throw new Error("JINA_API_KEY not configured");
|
|
235
|
+
const body = {
|
|
236
|
+
model: this.model,
|
|
237
|
+
input: texts
|
|
238
|
+
};
|
|
239
|
+
if (this.model.startsWith("jina-embeddings-v3")) {
|
|
240
|
+
body.task = "retrieval.passage";
|
|
241
|
+
}
|
|
242
|
+
if (this.dimensions)
|
|
243
|
+
body.dimensions = this.dimensions;
|
|
244
|
+
const resp = await fetch("https://api.jina.ai/v1/embeddings", {
|
|
245
|
+
method: "POST",
|
|
246
|
+
headers: {
|
|
247
|
+
"Content-Type": "application/json",
|
|
248
|
+
Authorization: `Bearer ${apiKey}`
|
|
249
|
+
},
|
|
250
|
+
body: JSON.stringify(body)
|
|
251
|
+
});
|
|
252
|
+
if (!resp.ok) {
|
|
253
|
+
const err = await resp.text().catch(() => "");
|
|
254
|
+
throw new Error(`Jina embedding failed (${resp.status}): ${err}`);
|
|
255
|
+
}
|
|
256
|
+
const data = (await resp.json());
|
|
257
|
+
return data.data
|
|
258
|
+
.sort((a, b) => (a.index ?? 0) - (b.index ?? 0))
|
|
259
|
+
.map((d) => d.embedding);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
// ---------------------------------------------------------------------------
|
|
263
|
+
// Convenience classes
|
|
264
|
+
// ---------------------------------------------------------------------------
|
|
265
|
+
/**
|
|
266
|
+
* OpenAI embedding function.
|
|
267
|
+
*
|
|
268
|
+
* @param model Defaults to `text-embedding-3-small`.
|
|
269
|
+
* @param dimensions Optional output dimensions for text-embedding-3-* models.
|
|
270
|
+
*/
|
|
271
|
+
export class OpenAIEmbeddingFunction extends ProviderEmbeddingFunction {
|
|
272
|
+
constructor(model = "text-embedding-3-small", dimensions) {
|
|
273
|
+
super({ provider: "openai", model, dimensions });
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
/**
|
|
277
|
+
* Ollama embedding function.
|
|
278
|
+
*
|
|
279
|
+
* @param model Defaults to `nomic-embed-text`.
|
|
280
|
+
*/
|
|
281
|
+
export class OllamaEmbeddingFunction extends ProviderEmbeddingFunction {
|
|
282
|
+
constructor(model = "nomic-embed-text") {
|
|
283
|
+
super({ provider: "ollama", model });
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Gemini embedding function.
|
|
288
|
+
*
|
|
289
|
+
* @param model Defaults to `text-embedding-004`.
|
|
290
|
+
*/
|
|
291
|
+
export class GeminiEmbeddingFunction extends ProviderEmbeddingFunction {
|
|
292
|
+
constructor(model = "text-embedding-004") {
|
|
293
|
+
super({ provider: "gemini", model });
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Mistral embedding function.
|
|
298
|
+
*
|
|
299
|
+
* @param model Defaults to `mistral-embed`.
|
|
300
|
+
*/
|
|
301
|
+
export class MistralEmbeddingFunction extends ProviderEmbeddingFunction {
|
|
302
|
+
constructor(model = "mistral-embed") {
|
|
303
|
+
super({ provider: "mistral", model });
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Cohere embedding function.
|
|
308
|
+
*
|
|
309
|
+
* @param model Defaults to `embed-v4.0`.
|
|
310
|
+
*/
|
|
311
|
+
export class CohereEmbeddingFunction extends ProviderEmbeddingFunction {
|
|
312
|
+
constructor(model = "embed-v4.0", dimensions) {
|
|
313
|
+
super({ provider: "cohere", model, dimensions });
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Voyage AI embedding function.
|
|
318
|
+
*
|
|
319
|
+
* @param model Defaults to `voyage-3.5`.
|
|
320
|
+
*/
|
|
321
|
+
export class VoyageEmbeddingFunction extends ProviderEmbeddingFunction {
|
|
322
|
+
constructor(model = "voyage-3.5", dimensions) {
|
|
323
|
+
super({ provider: "voyage", model, dimensions });
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Jina AI embedding function.
|
|
328
|
+
*
|
|
329
|
+
* @param model Defaults to `jina-embeddings-v3`.
|
|
330
|
+
*/
|
|
331
|
+
export class JinaEmbeddingFunction extends ProviderEmbeddingFunction {
|
|
332
|
+
constructor(model = "jina-embeddings-v3", dimensions) {
|
|
333
|
+
super({ provider: "jina", model, dimensions });
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
// ---------------------------------------------------------------------------
|
|
337
|
+
// Factory
|
|
338
|
+
// ---------------------------------------------------------------------------
|
|
339
|
+
/**
|
|
340
|
+
* Resolve an embedding function from a model name and optional provider.
|
|
341
|
+
*
|
|
342
|
+
* When provider is omitted, the model name is used to infer the provider:
|
|
343
|
+
* - `text-embedding-*` → OpenAI
|
|
344
|
+
* - Otherwise → Ollama (if OLLAMA_API_URL is set)
|
|
345
|
+
* - Fallback → null (caller should handle)
|
|
346
|
+
*
|
|
347
|
+
* @param embeddingModel Model identifier.
|
|
348
|
+
* @param provider Optional explicit provider name.
|
|
349
|
+
*/
|
|
350
|
+
export function getProviderEmbeddingFunction(embeddingModel, provider) {
|
|
351
|
+
if (provider) {
|
|
352
|
+
return new ProviderEmbeddingFunction({
|
|
353
|
+
provider: provider,
|
|
354
|
+
model: embeddingModel
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
// Auto-detect from model name — check Gemini-specific patterns before generic OpenAI prefix
|
|
358
|
+
if (embeddingModel.startsWith("text-embedding-004") ||
|
|
359
|
+
embeddingModel.startsWith("gemini-embedding-")) {
|
|
360
|
+
return new GeminiEmbeddingFunction(embeddingModel);
|
|
361
|
+
}
|
|
362
|
+
if (embeddingModel.startsWith("text-embedding-")) {
|
|
363
|
+
return new OpenAIEmbeddingFunction(embeddingModel);
|
|
364
|
+
}
|
|
365
|
+
if (embeddingModel.startsWith("mistral-embed")) {
|
|
366
|
+
return new MistralEmbeddingFunction(embeddingModel);
|
|
367
|
+
}
|
|
368
|
+
if (embeddingModel.startsWith("embed-")) {
|
|
369
|
+
return new CohereEmbeddingFunction(embeddingModel);
|
|
370
|
+
}
|
|
371
|
+
if (embeddingModel.startsWith("voyage-")) {
|
|
372
|
+
return new VoyageEmbeddingFunction(embeddingModel);
|
|
373
|
+
}
|
|
374
|
+
if (embeddingModel.startsWith("jina-")) {
|
|
375
|
+
return new JinaEmbeddingFunction(embeddingModel);
|
|
376
|
+
}
|
|
377
|
+
// Default to Ollama for local models (nomic-embed-text, all-minilm, mxbai-embed-large, etc.)
|
|
378
|
+
const ollamaUrl = process.env.OLLAMA_API_URL;
|
|
379
|
+
if (ollamaUrl) {
|
|
380
|
+
return new OllamaEmbeddingFunction(embeddingModel);
|
|
381
|
+
}
|
|
382
|
+
log.warn(`Could not determine provider for embedding model '${embeddingModel}'. ` +
|
|
383
|
+
`Set OLLAMA_API_URL or pass an explicit provider.`);
|
|
384
|
+
return null;
|
|
385
|
+
}
|
|
386
|
+
//# sourceMappingURL=embedding.js.map
|