glancey 2.8.0 → 2.8.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.
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { EmbeddingBackend, EmbeddingConfig } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* OpenAI embedding backend using the OpenAI API
|
|
4
|
+
*/
|
|
5
|
+
export declare class OpenAIBackend implements EmbeddingBackend {
|
|
6
|
+
name: string;
|
|
7
|
+
private apiKey;
|
|
8
|
+
private model;
|
|
9
|
+
private dimensions;
|
|
10
|
+
private baseUrl;
|
|
11
|
+
constructor(config: Partial<EmbeddingConfig> & {
|
|
12
|
+
apiKey: string;
|
|
13
|
+
});
|
|
14
|
+
initialize(): Promise<void>;
|
|
15
|
+
embed(text: string): Promise<number[]>;
|
|
16
|
+
embedBatch(texts: string[]): Promise<number[][]>;
|
|
17
|
+
getDimensions(): number;
|
|
18
|
+
}
|
|
19
|
+
//# sourceMappingURL=openai.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../src/embeddings/openai.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC;AAWpE;;GAEG;AACH,qBAAa,aAAc,YAAW,gBAAgB;IACpD,IAAI,SAAY;IAChB,OAAO,CAAC,MAAM,CAAS;IACvB,OAAO,CAAC,KAAK,CAAS;IACtB,OAAO,CAAC,UAAU,CAAS;IAC3B,OAAO,CAAC,OAAO,CAAS;gBAEZ,MAAM,EAAE,OAAO,CAAC,eAAe,CAAC,GAAG;QAAE,MAAM,EAAE,MAAM,CAAA;KAAE;IAO3D,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAa3B,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC;IAKtC,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,EAAE,CAAC;IA2BtD,aAAa,IAAI,MAAM;CAGxB"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { fetchWithRetry } from './retry.js';
|
|
2
|
+
const OPENAI_EMBEDDING_MODELS = {
|
|
3
|
+
'text-embedding-3-small': 1536,
|
|
4
|
+
'text-embedding-3-large': 3072,
|
|
5
|
+
'text-embedding-ada-002': 1536,
|
|
6
|
+
};
|
|
7
|
+
const DEFAULT_MODEL = 'text-embedding-3-small';
|
|
8
|
+
/**
|
|
9
|
+
* OpenAI embedding backend using the OpenAI API
|
|
10
|
+
*/
|
|
11
|
+
export class OpenAIBackend {
|
|
12
|
+
name = 'openai';
|
|
13
|
+
apiKey;
|
|
14
|
+
model;
|
|
15
|
+
dimensions;
|
|
16
|
+
baseUrl;
|
|
17
|
+
constructor(config) {
|
|
18
|
+
this.apiKey = config.apiKey;
|
|
19
|
+
this.model = config.model || DEFAULT_MODEL;
|
|
20
|
+
this.dimensions = OPENAI_EMBEDDING_MODELS[this.model] || 1536;
|
|
21
|
+
this.baseUrl = config.baseUrl || 'https://api.openai.com/v1';
|
|
22
|
+
}
|
|
23
|
+
async initialize() {
|
|
24
|
+
// Test the API key with a simple request
|
|
25
|
+
const response = await fetchWithRetry(`${this.baseUrl}/models`, {
|
|
26
|
+
headers: {
|
|
27
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
if (!response.ok) {
|
|
31
|
+
throw new Error(`OpenAI API error: ${response.status} ${response.statusText}`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
async embed(text) {
|
|
35
|
+
const embeddings = await this.embedBatch([text]);
|
|
36
|
+
return embeddings[0];
|
|
37
|
+
}
|
|
38
|
+
async embedBatch(texts) {
|
|
39
|
+
const response = await fetchWithRetry(`${this.baseUrl}/embeddings`, {
|
|
40
|
+
method: 'POST',
|
|
41
|
+
headers: {
|
|
42
|
+
Authorization: `Bearer ${this.apiKey}`,
|
|
43
|
+
'Content-Type': 'application/json',
|
|
44
|
+
},
|
|
45
|
+
body: JSON.stringify({
|
|
46
|
+
model: this.model,
|
|
47
|
+
input: texts,
|
|
48
|
+
}),
|
|
49
|
+
});
|
|
50
|
+
if (!response.ok) {
|
|
51
|
+
const error = await response.text();
|
|
52
|
+
throw new Error(`OpenAI embedding error: ${response.status} ${error}`);
|
|
53
|
+
}
|
|
54
|
+
const data = (await response.json());
|
|
55
|
+
// Sort by index to maintain order
|
|
56
|
+
const sorted = data.data.sort((a, b) => a.index - b.index);
|
|
57
|
+
return sorted.map((item) => item.embedding);
|
|
58
|
+
}
|
|
59
|
+
getDimensions() {
|
|
60
|
+
return this.dimensions;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
//# sourceMappingURL=openai.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai.js","sourceRoot":"","sources":["../../src/embeddings/openai.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAE5C,MAAM,uBAAuB,GAA2B;IACtD,wBAAwB,EAAE,IAAI;IAC9B,wBAAwB,EAAE,IAAI;IAC9B,wBAAwB,EAAE,IAAI;CAC/B,CAAC;AAEF,MAAM,aAAa,GAAG,wBAAwB,CAAC;AAE/C;;GAEG;AACH,MAAM,OAAO,aAAa;IACxB,IAAI,GAAG,QAAQ,CAAC;IACR,MAAM,CAAS;IACf,KAAK,CAAS;IACd,UAAU,CAAS;IACnB,OAAO,CAAS;IAExB,YAAY,MAAqD;QAC/D,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;QAC5B,IAAI,CAAC,KAAK,GAAG,MAAM,CAAC,KAAK,IAAI,aAAa,CAAC;QAC3C,IAAI,CAAC,UAAU,GAAG,uBAAuB,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,IAAI,CAAC;QAC9D,IAAI,CAA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