@abhishek944/pi-embeddings 0.5.0 → 0.6.0
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/README.md +26 -5
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/providers.d.ts +17 -1
- package/dist/providers.js +435 -18
- package/dist/response.d.ts +23 -0
- package/dist/response.js +115 -0
- package/dist/schemas.d.ts +846 -23
- package/dist/schemas.js +203 -2
- package/dist/settings.js +20 -2
- package/dist/tool-contracts.d.ts +387 -9
- package/package.json +8 -3
- package/src/index.ts +29 -0
- package/src/providers.ts +519 -14
- package/src/response.ts +152 -0
- package/src/schemas.ts +265 -2
- package/src/settings.ts +26 -1
package/src/providers.ts
CHANGED
|
@@ -6,22 +6,45 @@ import {
|
|
|
6
6
|
EMBEDDING_FORMAT_VERSION,
|
|
7
7
|
EMBEDDING_NORMALIZATION_VERSION,
|
|
8
8
|
CLOUDFLARE_EMBEDDING_MODELS,
|
|
9
|
+
COHERE_EMBEDDING_DIMENSIONS,
|
|
10
|
+
COHERE_EMBEDDING_MODELS,
|
|
11
|
+
DASHSCOPE_EMBEDDING_DIMENSIONS,
|
|
12
|
+
DASHSCOPE_EMBEDDING_MODELS,
|
|
13
|
+
DEFAULT_COHERE_DIMENSIONS,
|
|
14
|
+
DEFAULT_DASHSCOPE_DIMENSIONS,
|
|
9
15
|
DEFAULT_GEMINI_DIMENSIONS,
|
|
10
16
|
DEFAULT_GEMINI_TASK_FORMAT,
|
|
17
|
+
DEFAULT_VOYAGE_DIMENSIONS,
|
|
11
18
|
GEMINI_EMBEDDING_MODELS,
|
|
12
19
|
GEMINI_TASK_PREFIXES,
|
|
20
|
+
HETZNER_EMBEDDING_DIMENSIONS,
|
|
21
|
+
HETZNER_EMBEDDING_MODELS,
|
|
22
|
+
JINA_EMBEDDING_DIMENSIONS,
|
|
23
|
+
JINA_EMBEDDING_MODELS,
|
|
24
|
+
OPENAI_EMBEDDING_DIMENSIONS,
|
|
25
|
+
OPENAI_EMBEDDING_MODELS,
|
|
13
26
|
OPENCODE_EMBEDDING_MODELS,
|
|
14
27
|
OPENCODE_GO_EMBEDDING_MODELS,
|
|
15
28
|
OPENROUTER_EMBEDDING_MODELS,
|
|
29
|
+
VOYAGE_EMBEDDING_DIMENSIONS,
|
|
30
|
+
VOYAGE_EMBEDDING_MODELS,
|
|
31
|
+
type CohereEmbeddingDimension,
|
|
32
|
+
type CohereEmbeddingModel,
|
|
33
|
+
type DashScopeEmbeddingDimension,
|
|
16
34
|
type EmbeddingContext,
|
|
17
35
|
type EmbeddingRoute,
|
|
18
36
|
type EmbeddingSettings,
|
|
19
37
|
type EmbeddingSpace,
|
|
20
38
|
type GeminiEmbeddingDimension,
|
|
21
39
|
type GeminiEmbeddingTaskFormat,
|
|
40
|
+
type JinaEmbeddingModel,
|
|
41
|
+
type OpenAiEmbeddingModel,
|
|
22
42
|
type OpenCodeEmbeddingModel,
|
|
23
43
|
type OpenCodeGoEmbeddingModel,
|
|
24
44
|
type OpenCodeRouteId,
|
|
45
|
+
type OpenRouterEmbeddingModel,
|
|
46
|
+
type VoyageEmbeddingDimension,
|
|
47
|
+
type VoyageEmbeddingModel,
|
|
25
48
|
} from './schemas.js';
|
|
26
49
|
import { fixedOrigin, headerValueIsSafe, requestJson } from './provider-http.js';
|
|
27
50
|
import { acquireLocalRuntime, LOCAL_EMBEDDING_SPACE, type AcquiredLocalRuntime } from './local-runtime.js';
|
|
@@ -34,18 +57,123 @@ import type {
|
|
|
34
57
|
import {
|
|
35
58
|
parseBatchResponse,
|
|
36
59
|
parseCloudflareBatchResponse,
|
|
60
|
+
parseCohereBatchResponse,
|
|
37
61
|
parseGeminiCountTokensResponse,
|
|
38
62
|
parseGeminiEmbeddingResponse,
|
|
39
63
|
parseOpenCodeBatchResponse,
|
|
64
|
+
parseOptionalUsageBatchResponse,
|
|
65
|
+
parseVoyageBatchResponse,
|
|
40
66
|
} from './response.js';
|
|
41
67
|
|
|
42
|
-
export const
|
|
43
|
-
|
|
44
|
-
|
|
68
|
+
export const openaiEmbeddingSpace = (model: OpenAiEmbeddingModel): EmbeddingSpace => {
|
|
69
|
+
const common = {
|
|
70
|
+
provider: 'openai' as const,
|
|
71
|
+
revision: null,
|
|
72
|
+
quantization: null,
|
|
73
|
+
runtime: null,
|
|
74
|
+
format: EMBEDDING_FORMAT_VERSION,
|
|
75
|
+
prefix: EMBEDDING_DOCUMENT_PREFIX,
|
|
76
|
+
normalization: EMBEDDING_NORMALIZATION_VERSION,
|
|
77
|
+
} as const;
|
|
78
|
+
// Dimension is bound to the model so the descriptor cannot record an impossible pair.
|
|
79
|
+
if (model === 'text-embedding-3-large')
|
|
80
|
+
return Object.freeze({
|
|
81
|
+
...common,
|
|
82
|
+
model: 'text-embedding-3-large' as const,
|
|
83
|
+
dimensions: OPENAI_EMBEDDING_DIMENSIONS['text-embedding-3-large'],
|
|
84
|
+
});
|
|
85
|
+
return Object.freeze({
|
|
86
|
+
...common,
|
|
87
|
+
model: 'text-embedding-3-small' as const,
|
|
88
|
+
dimensions: OPENAI_EMBEDDING_DIMENSIONS['text-embedding-3-small'],
|
|
89
|
+
});
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export const OPENAI_EMBEDDING_SPACE: EmbeddingSpace = openaiEmbeddingSpace('text-embedding-3-small');
|
|
93
|
+
|
|
94
|
+
export const voyageEmbeddingSpace = (config: {
|
|
95
|
+
model: VoyageEmbeddingModel;
|
|
96
|
+
dimensions: VoyageEmbeddingDimension;
|
|
97
|
+
}): EmbeddingSpace =>
|
|
98
|
+
Object.freeze({
|
|
99
|
+
provider: 'voyage',
|
|
100
|
+
model: config.model,
|
|
101
|
+
revision: null,
|
|
102
|
+
quantization: null,
|
|
103
|
+
runtime: 'voyage-api-v1',
|
|
104
|
+
dimensions: config.dimensions,
|
|
105
|
+
inputType: 'document',
|
|
106
|
+
format: EMBEDDING_FORMAT_VERSION,
|
|
107
|
+
prefix: EMBEDDING_DOCUMENT_PREFIX,
|
|
108
|
+
normalization: EMBEDDING_NORMALIZATION_VERSION,
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
export const cohereEmbeddingSpace = (config: {
|
|
112
|
+
model: CohereEmbeddingModel;
|
|
113
|
+
dimensions: CohereEmbeddingDimension;
|
|
114
|
+
}): EmbeddingSpace =>
|
|
115
|
+
Object.freeze({
|
|
116
|
+
provider: 'cohere',
|
|
117
|
+
model: config.model,
|
|
118
|
+
revision: null,
|
|
119
|
+
quantization: null,
|
|
120
|
+
runtime: 'cohere-api-v2',
|
|
121
|
+
dimensions: config.dimensions,
|
|
122
|
+
inputType: 'search_document',
|
|
123
|
+
format: EMBEDDING_FORMAT_VERSION,
|
|
124
|
+
prefix: EMBEDDING_DOCUMENT_PREFIX,
|
|
125
|
+
normalization: EMBEDDING_NORMALIZATION_VERSION,
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
/** Jina's declared default is the largest documented Matryoshka size for that model. */
|
|
129
|
+
const jinaDefaultDimensions = (model: JinaEmbeddingModel): number => {
|
|
130
|
+
const documented = JINA_EMBEDDING_DIMENSIONS[model];
|
|
131
|
+
const largest = documented[documented.length - 1];
|
|
132
|
+
if (largest === undefined) throw new EmbeddingsError('configuration-invalid');
|
|
133
|
+
return largest;
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
/** Jina documents per-model Matryoshka sizes; an undeclared size is a configuration error. */
|
|
137
|
+
export const jinaEmbeddingSpace = (config: {
|
|
138
|
+
model: JinaEmbeddingModel;
|
|
139
|
+
dimensions: number;
|
|
140
|
+
}): EmbeddingSpace => {
|
|
141
|
+
if (!JINA_EMBEDDING_DIMENSIONS[config.model].includes(config.dimensions))
|
|
142
|
+
throw new EmbeddingsError('configuration-invalid');
|
|
143
|
+
return Object.freeze({
|
|
144
|
+
provider: 'jina',
|
|
145
|
+
model: config.model,
|
|
146
|
+
revision: null,
|
|
147
|
+
quantization: null,
|
|
148
|
+
runtime: 'jina-api-v1',
|
|
149
|
+
dimensions: config.dimensions,
|
|
150
|
+
task: 'retrieval.passage',
|
|
151
|
+
format: EMBEDDING_FORMAT_VERSION,
|
|
152
|
+
prefix: EMBEDDING_DOCUMENT_PREFIX,
|
|
153
|
+
normalization: EMBEDDING_NORMALIZATION_VERSION,
|
|
154
|
+
});
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
export const dashScopeEmbeddingSpace = (dimensions: DashScopeEmbeddingDimension): EmbeddingSpace =>
|
|
158
|
+
Object.freeze({
|
|
159
|
+
provider: 'dashscope',
|
|
160
|
+
model: DASHSCOPE_EMBEDDING_MODELS[0],
|
|
161
|
+
revision: null,
|
|
162
|
+
quantization: null,
|
|
163
|
+
runtime: 'dashscope-compatible-mode-v1',
|
|
164
|
+
dimensions,
|
|
165
|
+
format: EMBEDDING_FORMAT_VERSION,
|
|
166
|
+
prefix: EMBEDDING_DOCUMENT_PREFIX,
|
|
167
|
+
normalization: EMBEDDING_NORMALIZATION_VERSION,
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
export const HETZNER_EMBEDDING_SPACE: EmbeddingSpace = Object.freeze({
|
|
171
|
+
provider: 'hetzner',
|
|
172
|
+
model: HETZNER_EMBEDDING_MODELS[0],
|
|
45
173
|
revision: null,
|
|
46
174
|
quantization: null,
|
|
47
|
-
runtime:
|
|
48
|
-
dimensions:
|
|
175
|
+
runtime: 'hetzner-inference-v1',
|
|
176
|
+
dimensions: HETZNER_EMBEDDING_DIMENSIONS,
|
|
49
177
|
format: EMBEDDING_FORMAT_VERSION,
|
|
50
178
|
prefix: EMBEDDING_DOCUMENT_PREFIX,
|
|
51
179
|
normalization: EMBEDDING_NORMALIZATION_VERSION,
|
|
@@ -114,6 +242,24 @@ const OPENAI_MAX_INPUT_TOKENS = 8192;
|
|
|
114
242
|
const OPENAI_MAX_REQUEST_INPUTS = 2048;
|
|
115
243
|
const OPENAI_MAX_REQUEST_TOKENS = 300000;
|
|
116
244
|
|
|
245
|
+
/** Voyage documents a 1000-input list ceiling; aggregate caps are provider-enforced only. */
|
|
246
|
+
const VOYAGE_ORIGIN = 'https://api.voyageai.com';
|
|
247
|
+
const VOYAGE_MAX_REQUEST_INPUTS = 1000;
|
|
248
|
+
/** Cohere documents a maximum of 96 texts per call. */
|
|
249
|
+
const COHERE_ORIGIN = 'https://api.cohere.com';
|
|
250
|
+
const COHERE_MAX_REQUEST_INPUTS = 96;
|
|
251
|
+
/** Jina publishes RPM/TPM rate limits but no per-request input count ceiling. */
|
|
252
|
+
const JINA_EMBEDDINGS_URL = 'https://api.jina.ai/v1/embeddings';
|
|
253
|
+
/** Model Studio documents at most 20 rows per request for qwen3.7-text-embedding. */
|
|
254
|
+
const DASHSCOPE_EMBEDDINGS_PATH = '/compatible-mode/v1/embeddings';
|
|
255
|
+
const DASHSCOPE_MAX_REQUEST_INPUTS = 20;
|
|
256
|
+
/** Hetzner publishes a 32 MiB body ceiling and about 1M recommended tokens, no count cap. */
|
|
257
|
+
const HETZNER_EMBEDDINGS_URL = 'https://inference.hetzner.com/v1/embeddings';
|
|
258
|
+
|
|
259
|
+
const VOYAGE_INPUT_TYPE = 'document' as const;
|
|
260
|
+
const COHERE_INPUT_TYPE = 'search_document' as const;
|
|
261
|
+
const JINA_TASK = 'retrieval.passage' as const;
|
|
262
|
+
|
|
117
263
|
const isOneOf = <T extends string>(values: readonly T[], value: string): value is T =>
|
|
118
264
|
values.some((candidate) => candidate === value);
|
|
119
265
|
|
|
@@ -244,6 +390,33 @@ export function routeDiagnostics(
|
|
|
244
390
|
if (settings.providers?.vertex === undefined) add('vertex-configuration-unconfigured');
|
|
245
391
|
// Provider-free diagnostics cannot establish ADC, IAM, quota or live model availability.
|
|
246
392
|
break;
|
|
393
|
+
case 'voyage':
|
|
394
|
+
if (trim(settings.providers?.voyage?.apiKey) === undefined && trim(process.env.VOYAGE_API_KEY) === undefined)
|
|
395
|
+
add('voyage-key-unconfigured');
|
|
396
|
+
break;
|
|
397
|
+
case 'cohere':
|
|
398
|
+
if (trim(settings.providers?.cohere?.apiKey) === undefined && trim(process.env.COHERE_API_KEY) === undefined)
|
|
399
|
+
add('cohere-key-unconfigured');
|
|
400
|
+
break;
|
|
401
|
+
case 'jina':
|
|
402
|
+
if (trim(settings.providers?.jina?.apiKey) === undefined && trim(process.env.JINA_API_KEY) === undefined)
|
|
403
|
+
add('jina-key-unconfigured');
|
|
404
|
+
break;
|
|
405
|
+
case 'dashscope': {
|
|
406
|
+
const dashscope = settings.providers?.dashscope;
|
|
407
|
+
if (trim(dashscope?.baseUrl) === undefined && trim(process.env.DASHSCOPE_BASE_URL) === undefined)
|
|
408
|
+
add('dashscope-base-url-unconfigured');
|
|
409
|
+
if (trim(dashscope?.apiKey) === undefined && trim(process.env.DASHSCOPE_API_KEY) === undefined)
|
|
410
|
+
add('dashscope-key-unconfigured');
|
|
411
|
+
break;
|
|
412
|
+
}
|
|
413
|
+
case 'hetzner':
|
|
414
|
+
if (
|
|
415
|
+
trim(settings.providers?.hetzner?.apiKey) === undefined &&
|
|
416
|
+
trim(process.env.HETZNER_INFERENCE_TOKEN) === undefined
|
|
417
|
+
)
|
|
418
|
+
add('hetzner-key-unconfigured');
|
|
419
|
+
break;
|
|
247
420
|
}
|
|
248
421
|
return diagnostics;
|
|
249
422
|
}
|
|
@@ -311,16 +484,21 @@ const providerTexts = (inputs: readonly PreparedInput[]): string[] =>
|
|
|
311
484
|
return input.text;
|
|
312
485
|
});
|
|
313
486
|
|
|
487
|
+
const openRouterCanonicalModel = (model: OpenRouterEmbeddingModel): OpenAiEmbeddingModel =>
|
|
488
|
+
model === 'openai/text-embedding-3-large' ? 'text-embedding-3-large' : 'text-embedding-3-small';
|
|
489
|
+
|
|
314
490
|
class OpenAiProvider implements EmbeddingsProvider {
|
|
315
491
|
readonly route = 'openai' as const;
|
|
316
|
-
readonly model
|
|
317
|
-
readonly space: EmbeddingSpace
|
|
492
|
+
readonly model: OpenAiEmbeddingModel;
|
|
493
|
+
readonly space: EmbeddingSpace;
|
|
318
494
|
readonly maxRequestInputs = OPENAI_MAX_REQUEST_INPUTS;
|
|
319
495
|
readonly maxRequestTokens = OPENAI_MAX_REQUEST_TOKENS;
|
|
320
496
|
readonly #encoding = getEncoding('cl100k_base');
|
|
321
497
|
readonly #key: string;
|
|
322
498
|
|
|
323
|
-
constructor(key: string) {
|
|
499
|
+
constructor(model: OpenAiEmbeddingModel, key: string) {
|
|
500
|
+
this.model = model;
|
|
501
|
+
this.space = openaiEmbeddingSpace(model);
|
|
324
502
|
this.#key = key;
|
|
325
503
|
}
|
|
326
504
|
|
|
@@ -520,14 +698,16 @@ class GatewayProvider implements EmbeddingsProvider {
|
|
|
520
698
|
|
|
521
699
|
class OpenRouterProvider implements EmbeddingsProvider {
|
|
522
700
|
readonly route = 'openrouter' as const;
|
|
523
|
-
readonly model
|
|
524
|
-
readonly space: EmbeddingSpace
|
|
701
|
+
readonly model: OpenRouterEmbeddingModel;
|
|
702
|
+
readonly space: EmbeddingSpace;
|
|
525
703
|
readonly maxRequestInputs = OPENAI_MAX_REQUEST_INPUTS;
|
|
526
704
|
readonly maxRequestTokens = OPENAI_MAX_REQUEST_TOKENS;
|
|
527
705
|
readonly #encoding = getEncoding('cl100k_base');
|
|
528
706
|
readonly #key: string;
|
|
529
707
|
|
|
530
|
-
constructor(key: string) {
|
|
708
|
+
constructor(model: OpenRouterEmbeddingModel, key: string) {
|
|
709
|
+
this.model = model;
|
|
710
|
+
this.space = openaiEmbeddingSpace(openRouterCanonicalModel(model));
|
|
531
711
|
this.#key = key;
|
|
532
712
|
}
|
|
533
713
|
|
|
@@ -562,6 +742,228 @@ class OpenRouterProvider implements EmbeddingsProvider {
|
|
|
562
742
|
}
|
|
563
743
|
}
|
|
564
744
|
|
|
745
|
+
class VoyageProvider implements EmbeddingsProvider {
|
|
746
|
+
readonly route = 'voyage' as const;
|
|
747
|
+
readonly model: VoyageEmbeddingModel;
|
|
748
|
+
readonly space: EmbeddingSpace;
|
|
749
|
+
// Voyage documents a 1000-input list ceiling; aggregate token caps stay provider-enforced.
|
|
750
|
+
readonly maxRequestInputs = VOYAGE_MAX_REQUEST_INPUTS;
|
|
751
|
+
readonly maxRequestTokens = Number.POSITIVE_INFINITY;
|
|
752
|
+
readonly #key: string;
|
|
753
|
+
|
|
754
|
+
constructor(config: {
|
|
755
|
+
model: VoyageEmbeddingModel;
|
|
756
|
+
dimensions: VoyageEmbeddingDimension;
|
|
757
|
+
key: string;
|
|
758
|
+
}) {
|
|
759
|
+
this.model = config.model;
|
|
760
|
+
this.space = voyageEmbeddingSpace({ model: config.model, dimensions: config.dimensions });
|
|
761
|
+
this.#key = config.key;
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
async prepare(text: string, signal: AbortSignal): Promise<PreparedInput> {
|
|
765
|
+
signal.throwIfAborted();
|
|
766
|
+
return { kind: 'text', text: EMBEDDING_DOCUMENT_PREFIX + text, tokenCount: null };
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
async embed(inputs: readonly PreparedInput[], signal: AbortSignal): Promise<ProviderBatchResult> {
|
|
770
|
+
const response = await requestJson({
|
|
771
|
+
url: new URL('/v1/embeddings', VOYAGE_ORIGIN),
|
|
772
|
+
signal,
|
|
773
|
+
headers: { authorization: `Bearer ${this.#key}` },
|
|
774
|
+
body: {
|
|
775
|
+
model: this.model,
|
|
776
|
+
input: providerTexts(inputs),
|
|
777
|
+
input_type: VOYAGE_INPUT_TYPE,
|
|
778
|
+
output_dimension: this.space.dimensions,
|
|
779
|
+
// This package never truncates, so an over-length input must fail instead of being cut.
|
|
780
|
+
truncation: false,
|
|
781
|
+
},
|
|
782
|
+
});
|
|
783
|
+
return parseVoyageBatchResponse({
|
|
784
|
+
response,
|
|
785
|
+
space: this.space,
|
|
786
|
+
expectedModels: [this.model],
|
|
787
|
+
expectedCount: inputs.length,
|
|
788
|
+
});
|
|
789
|
+
}
|
|
790
|
+
|
|
791
|
+
async close(): Promise<void> {
|
|
792
|
+
// Native fetch owns no long-lived resource.
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
class CohereProvider implements EmbeddingsProvider {
|
|
797
|
+
readonly route = 'cohere' as const;
|
|
798
|
+
readonly model: CohereEmbeddingModel;
|
|
799
|
+
readonly space: EmbeddingSpace;
|
|
800
|
+
/** Cohere rejects more than 96 texts in one call. */
|
|
801
|
+
readonly maxRequestInputs = COHERE_MAX_REQUEST_INPUTS;
|
|
802
|
+
readonly maxRequestTokens = Number.POSITIVE_INFINITY;
|
|
803
|
+
readonly #key: string;
|
|
804
|
+
|
|
805
|
+
constructor(config: {
|
|
806
|
+
model: CohereEmbeddingModel;
|
|
807
|
+
dimensions: CohereEmbeddingDimension;
|
|
808
|
+
key: string;
|
|
809
|
+
}) {
|
|
810
|
+
this.model = config.model;
|
|
811
|
+
this.space = cohereEmbeddingSpace({ model: config.model, dimensions: config.dimensions });
|
|
812
|
+
this.#key = config.key;
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
async prepare(text: string, signal: AbortSignal): Promise<PreparedInput> {
|
|
816
|
+
signal.throwIfAborted();
|
|
817
|
+
return { kind: 'text', text: EMBEDDING_DOCUMENT_PREFIX + text, tokenCount: null };
|
|
818
|
+
}
|
|
819
|
+
|
|
820
|
+
async embed(inputs: readonly PreparedInput[], signal: AbortSignal): Promise<ProviderBatchResult> {
|
|
821
|
+
const response = await requestJson({
|
|
822
|
+
url: new URL('/v2/embed', COHERE_ORIGIN),
|
|
823
|
+
signal,
|
|
824
|
+
headers: { authorization: `Bearer ${this.#key}` },
|
|
825
|
+
body: {
|
|
826
|
+
model: this.model,
|
|
827
|
+
input_type: COHERE_INPUT_TYPE,
|
|
828
|
+
texts: providerTexts(inputs),
|
|
829
|
+
embedding_types: ['float'],
|
|
830
|
+
output_dimension: this.space.dimensions,
|
|
831
|
+
// Default is END; refuse instead of silently shortening an over-length input.
|
|
832
|
+
truncate: 'NONE',
|
|
833
|
+
},
|
|
834
|
+
});
|
|
835
|
+
return parseCohereBatchResponse({ response, space: this.space, expectedCount: inputs.length });
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
async close(): Promise<void> {
|
|
839
|
+
// Native fetch owns no long-lived resource.
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
class JinaProvider implements EmbeddingsProvider {
|
|
844
|
+
readonly route = 'jina' as const;
|
|
845
|
+
readonly model: JinaEmbeddingModel;
|
|
846
|
+
readonly space: EmbeddingSpace;
|
|
847
|
+
// Jina publishes RPM/TPM limits but no per-request input count ceiling.
|
|
848
|
+
readonly maxRequestInputs = Number.POSITIVE_INFINITY;
|
|
849
|
+
readonly maxRequestTokens = Number.POSITIVE_INFINITY;
|
|
850
|
+
readonly #key: string;
|
|
851
|
+
|
|
852
|
+
constructor(config: { model: JinaEmbeddingModel; dimensions: number; key: string }) {
|
|
853
|
+
this.model = config.model;
|
|
854
|
+
this.space = jinaEmbeddingSpace({ model: config.model, dimensions: config.dimensions });
|
|
855
|
+
this.#key = config.key;
|
|
856
|
+
}
|
|
857
|
+
|
|
858
|
+
async prepare(text: string, signal: AbortSignal): Promise<PreparedInput> {
|
|
859
|
+
signal.throwIfAborted();
|
|
860
|
+
return { kind: 'text', text: EMBEDDING_DOCUMENT_PREFIX + text, tokenCount: null };
|
|
861
|
+
}
|
|
862
|
+
|
|
863
|
+
async embed(inputs: readonly PreparedInput[], signal: AbortSignal): Promise<ProviderBatchResult> {
|
|
864
|
+
const response = await requestJson({
|
|
865
|
+
url: new URL(JINA_EMBEDDINGS_URL),
|
|
866
|
+
signal,
|
|
867
|
+
headers: { authorization: `Bearer ${this.#key}` },
|
|
868
|
+
body: {
|
|
869
|
+
model: this.model,
|
|
870
|
+
input: providerTexts(inputs),
|
|
871
|
+
task: JINA_TASK,
|
|
872
|
+
dimensions: this.space.dimensions,
|
|
873
|
+
embedding_type: 'float',
|
|
874
|
+
// The package owns normalization and never truncates, so both are disabled explicitly.
|
|
875
|
+
normalized: false,
|
|
876
|
+
truncate: false,
|
|
877
|
+
},
|
|
878
|
+
});
|
|
879
|
+
return parseBatchResponse({ response, space: this.space, expectedModels: [this.model] });
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
async close(): Promise<void> {
|
|
883
|
+
// Native fetch owns no long-lived resource.
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
class DashScopeProvider implements EmbeddingsProvider {
|
|
888
|
+
readonly route = 'dashscope' as const;
|
|
889
|
+
readonly model = DASHSCOPE_EMBEDDING_MODELS[0];
|
|
890
|
+
readonly space: EmbeddingSpace;
|
|
891
|
+
/** Model Studio documents at most 20 rows per request for this model. */
|
|
892
|
+
readonly maxRequestInputs = DASHSCOPE_MAX_REQUEST_INPUTS;
|
|
893
|
+
readonly maxRequestTokens = Number.POSITIVE_INFINITY;
|
|
894
|
+
readonly #url: URL;
|
|
895
|
+
readonly #key: string;
|
|
896
|
+
|
|
897
|
+
constructor(config: { origin: URL; dimensions: DashScopeEmbeddingDimension; key: string }) {
|
|
898
|
+
this.#url = new URL(DASHSCOPE_EMBEDDINGS_PATH, config.origin);
|
|
899
|
+
this.#key = config.key;
|
|
900
|
+
this.space = dashScopeEmbeddingSpace(config.dimensions);
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
async prepare(text: string, signal: AbortSignal): Promise<PreparedInput> {
|
|
904
|
+
signal.throwIfAborted();
|
|
905
|
+
return { kind: 'text', text: EMBEDDING_DOCUMENT_PREFIX + text, tokenCount: null };
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
async embed(inputs: readonly PreparedInput[], signal: AbortSignal): Promise<ProviderBatchResult> {
|
|
909
|
+
const response = await requestJson({
|
|
910
|
+
url: this.#url,
|
|
911
|
+
signal,
|
|
912
|
+
headers: { authorization: `Bearer ${this.#key}` },
|
|
913
|
+
body: {
|
|
914
|
+
model: this.model,
|
|
915
|
+
input: providerTexts(inputs),
|
|
916
|
+
dimensions: this.space.dimensions,
|
|
917
|
+
encoding_format: 'float',
|
|
918
|
+
},
|
|
919
|
+
});
|
|
920
|
+
return parseBatchResponse({ response, space: this.space, expectedModels: [this.model] });
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
async close(): Promise<void> {
|
|
924
|
+
// Native fetch owns no long-lived resource.
|
|
925
|
+
}
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
class HetznerProvider implements EmbeddingsProvider {
|
|
929
|
+
readonly route = 'hetzner' as const;
|
|
930
|
+
readonly model = HETZNER_EMBEDDING_MODELS[0];
|
|
931
|
+
readonly space: EmbeddingSpace = HETZNER_EMBEDDING_SPACE;
|
|
932
|
+
// Hetzner documents a 32 MiB body ceiling and no fixed input count.
|
|
933
|
+
readonly maxRequestInputs = Number.POSITIVE_INFINITY;
|
|
934
|
+
readonly maxRequestTokens = Number.POSITIVE_INFINITY;
|
|
935
|
+
readonly #key: string;
|
|
936
|
+
|
|
937
|
+
constructor(key: string) {
|
|
938
|
+
this.#key = key;
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
async prepare(text: string, signal: AbortSignal): Promise<PreparedInput> {
|
|
942
|
+
signal.throwIfAborted();
|
|
943
|
+
return { kind: 'text', text: EMBEDDING_DOCUMENT_PREFIX + text, tokenCount: null };
|
|
944
|
+
}
|
|
945
|
+
|
|
946
|
+
async embed(inputs: readonly PreparedInput[], signal: AbortSignal): Promise<ProviderBatchResult> {
|
|
947
|
+
const response = await requestJson({
|
|
948
|
+
url: new URL(HETZNER_EMBEDDINGS_URL),
|
|
949
|
+
signal,
|
|
950
|
+
headers: { authorization: `Bearer ${this.#key}` },
|
|
951
|
+
// Hetzner documents `float` and `base64` for this endpoint; float is the value this package records.
|
|
952
|
+
body: { model: this.model, input: providerTexts(inputs), encoding_format: 'float' },
|
|
953
|
+
});
|
|
954
|
+
return parseOptionalUsageBatchResponse({
|
|
955
|
+
response,
|
|
956
|
+
space: this.space,
|
|
957
|
+
expectedModels: [this.model],
|
|
958
|
+
expectedCount: inputs.length,
|
|
959
|
+
});
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
async close(): Promise<void> {
|
|
963
|
+
// Native fetch owns no long-lived resource.
|
|
964
|
+
}
|
|
965
|
+
}
|
|
966
|
+
|
|
565
967
|
class CloudflareProvider implements EmbeddingsProvider {
|
|
566
968
|
readonly route = 'cloudflare' as const;
|
|
567
969
|
readonly model = CLOUDFLARE_EMBEDDING_MODELS[0];
|
|
@@ -723,13 +1125,17 @@ export async function acquireProvider({
|
|
|
723
1125
|
);
|
|
724
1126
|
return { provider: acquired.provider, release: acquired.release };
|
|
725
1127
|
}
|
|
726
|
-
case 'openai':
|
|
1128
|
+
case 'openai': {
|
|
1129
|
+
if (!isOneOf<OpenAiEmbeddingModel>(OPENAI_EMBEDDING_MODELS, route.model))
|
|
1130
|
+
throw new EmbeddingsError('configuration-invalid');
|
|
727
1131
|
return {
|
|
728
1132
|
provider: new OpenAiProvider(
|
|
1133
|
+
route.model,
|
|
729
1134
|
credential(settings.providers?.openai?.apiKey, process.env.OPENAI_API_KEY)
|
|
730
1135
|
),
|
|
731
1136
|
release: noRelease,
|
|
732
1137
|
};
|
|
1138
|
+
}
|
|
733
1139
|
case 'sarvam':
|
|
734
1140
|
return {
|
|
735
1141
|
provider: new SarvamProvider(
|
|
@@ -755,13 +1161,17 @@ export async function acquireProvider({
|
|
|
755
1161
|
release: noRelease,
|
|
756
1162
|
};
|
|
757
1163
|
}
|
|
758
|
-
case 'openrouter':
|
|
1164
|
+
case 'openrouter': {
|
|
1165
|
+
if (!isOneOf<OpenRouterEmbeddingModel>(OPENROUTER_EMBEDDING_MODELS, route.model))
|
|
1166
|
+
throw new EmbeddingsError('configuration-invalid');
|
|
759
1167
|
return {
|
|
760
1168
|
provider: new OpenRouterProvider(
|
|
1169
|
+
route.model,
|
|
761
1170
|
headerCredential(settings.providers?.openrouter?.apiKey, process.env.OPENROUTER_API_KEY)
|
|
762
1171
|
),
|
|
763
1172
|
release: noRelease,
|
|
764
1173
|
};
|
|
1174
|
+
}
|
|
765
1175
|
case 'cloudflare': {
|
|
766
1176
|
const configured = settings.providers?.cloudflare;
|
|
767
1177
|
const accountId = trim(configured?.accountId) ?? trim(process.env.CLOUDFLARE_ACCOUNT_ID);
|
|
@@ -817,6 +1227,66 @@ export async function acquireProvider({
|
|
|
817
1227
|
release: noRelease,
|
|
818
1228
|
};
|
|
819
1229
|
}
|
|
1230
|
+
case 'voyage': {
|
|
1231
|
+
const configured = settings.providers?.voyage;
|
|
1232
|
+
if (!isOneOf<VoyageEmbeddingModel>(VOYAGE_EMBEDDING_MODELS, route.model))
|
|
1233
|
+
throw new EmbeddingsError('configuration-invalid');
|
|
1234
|
+
return {
|
|
1235
|
+
provider: new VoyageProvider({
|
|
1236
|
+
model: route.model,
|
|
1237
|
+
dimensions: configured?.dimensions ?? DEFAULT_VOYAGE_DIMENSIONS,
|
|
1238
|
+
key: headerCredential(configured?.apiKey, process.env.VOYAGE_API_KEY),
|
|
1239
|
+
}),
|
|
1240
|
+
release: noRelease,
|
|
1241
|
+
};
|
|
1242
|
+
}
|
|
1243
|
+
case 'cohere': {
|
|
1244
|
+
const configured = settings.providers?.cohere;
|
|
1245
|
+
if (!isOneOf<CohereEmbeddingModel>(COHERE_EMBEDDING_MODELS, route.model))
|
|
1246
|
+
throw new EmbeddingsError('configuration-invalid');
|
|
1247
|
+
return {
|
|
1248
|
+
provider: new CohereProvider({
|
|
1249
|
+
model: route.model,
|
|
1250
|
+
dimensions: configured?.dimensions ?? DEFAULT_COHERE_DIMENSIONS,
|
|
1251
|
+
key: headerCredential(configured?.apiKey, process.env.COHERE_API_KEY),
|
|
1252
|
+
}),
|
|
1253
|
+
release: noRelease,
|
|
1254
|
+
};
|
|
1255
|
+
}
|
|
1256
|
+
case 'jina': {
|
|
1257
|
+
const configured = settings.providers?.jina;
|
|
1258
|
+
if (!isOneOf<JinaEmbeddingModel>(JINA_EMBEDDING_MODELS, route.model))
|
|
1259
|
+
throw new EmbeddingsError('configuration-invalid');
|
|
1260
|
+
const model = route.model;
|
|
1261
|
+
return {
|
|
1262
|
+
provider: new JinaProvider({
|
|
1263
|
+
model,
|
|
1264
|
+
dimensions: configured?.dimensions ?? jinaDefaultDimensions(model),
|
|
1265
|
+
key: headerCredential(configured?.apiKey, process.env.JINA_API_KEY),
|
|
1266
|
+
}),
|
|
1267
|
+
release: noRelease,
|
|
1268
|
+
};
|
|
1269
|
+
}
|
|
1270
|
+
case 'dashscope': {
|
|
1271
|
+
const configured = settings.providers?.dashscope;
|
|
1272
|
+
const baseUrl = trim(configured?.baseUrl) ?? trim(process.env.DASHSCOPE_BASE_URL);
|
|
1273
|
+
if (baseUrl === undefined) throw new EmbeddingsError('configuration-invalid');
|
|
1274
|
+
return {
|
|
1275
|
+
provider: new DashScopeProvider({
|
|
1276
|
+
origin: fixedOrigin(baseUrl),
|
|
1277
|
+
dimensions: configured?.dimensions ?? DEFAULT_DASHSCOPE_DIMENSIONS,
|
|
1278
|
+
key: headerCredential(configured?.apiKey, process.env.DASHSCOPE_API_KEY),
|
|
1279
|
+
}),
|
|
1280
|
+
release: noRelease,
|
|
1281
|
+
};
|
|
1282
|
+
}
|
|
1283
|
+
case 'hetzner':
|
|
1284
|
+
return {
|
|
1285
|
+
provider: new HetznerProvider(
|
|
1286
|
+
headerCredential(settings.providers?.hetzner?.apiKey, process.env.HETZNER_INFERENCE_TOKEN)
|
|
1287
|
+
),
|
|
1288
|
+
release: noRelease,
|
|
1289
|
+
};
|
|
820
1290
|
}
|
|
821
1291
|
}
|
|
822
1292
|
|
|
@@ -828,9 +1298,44 @@ export const spaceForRoute = (
|
|
|
828
1298
|
case 'local':
|
|
829
1299
|
return LOCAL_EMBEDDING_SPACE;
|
|
830
1300
|
case 'openai':
|
|
1301
|
+
if (!isOneOf<OpenAiEmbeddingModel>(OPENAI_EMBEDDING_MODELS, route.model))
|
|
1302
|
+
throw new EmbeddingsError('configuration-invalid');
|
|
1303
|
+
return openaiEmbeddingSpace(route.model);
|
|
831
1304
|
case 'gateway':
|
|
832
|
-
case 'openrouter':
|
|
833
1305
|
return OPENAI_EMBEDDING_SPACE;
|
|
1306
|
+
case 'openrouter':
|
|
1307
|
+
if (!isOneOf<OpenRouterEmbeddingModel>(OPENROUTER_EMBEDDING_MODELS, route.model))
|
|
1308
|
+
throw new EmbeddingsError('configuration-invalid');
|
|
1309
|
+
return openaiEmbeddingSpace(openRouterCanonicalModel(route.model));
|
|
1310
|
+
case 'voyage':
|
|
1311
|
+
if (!isOneOf<VoyageEmbeddingModel>(VOYAGE_EMBEDDING_MODELS, route.model))
|
|
1312
|
+
throw new EmbeddingsError('configuration-invalid');
|
|
1313
|
+
return voyageEmbeddingSpace({
|
|
1314
|
+
model: route.model,
|
|
1315
|
+
dimensions: settings?.providers?.voyage?.dimensions ?? DEFAULT_VOYAGE_DIMENSIONS,
|
|
1316
|
+
});
|
|
1317
|
+
case 'cohere':
|
|
1318
|
+
if (!isOneOf<CohereEmbeddingModel>(COHERE_EMBEDDING_MODELS, route.model))
|
|
1319
|
+
throw new EmbeddingsError('configuration-invalid');
|
|
1320
|
+
return cohereEmbeddingSpace({
|
|
1321
|
+
model: route.model,
|
|
1322
|
+
dimensions: settings?.providers?.cohere?.dimensions ?? DEFAULT_COHERE_DIMENSIONS,
|
|
1323
|
+
});
|
|
1324
|
+
case 'jina': {
|
|
1325
|
+
if (!isOneOf<JinaEmbeddingModel>(JINA_EMBEDDING_MODELS, route.model))
|
|
1326
|
+
throw new EmbeddingsError('configuration-invalid');
|
|
1327
|
+
const model = route.model;
|
|
1328
|
+
return jinaEmbeddingSpace({
|
|
1329
|
+
model,
|
|
1330
|
+
dimensions: settings?.providers?.jina?.dimensions ?? jinaDefaultDimensions(model),
|
|
1331
|
+
});
|
|
1332
|
+
}
|
|
1333
|
+
case 'dashscope':
|
|
1334
|
+
return dashScopeEmbeddingSpace(
|
|
1335
|
+
settings?.providers?.dashscope?.dimensions ?? DEFAULT_DASHSCOPE_DIMENSIONS
|
|
1336
|
+
);
|
|
1337
|
+
case 'hetzner':
|
|
1338
|
+
return HETZNER_EMBEDDING_SPACE;
|
|
834
1339
|
case 'sarvam':
|
|
835
1340
|
return SARVAM_EMBEDDING_SPACE;
|
|
836
1341
|
case 'cloudflare':
|