vecbox 0.2.2 → 0.2.3

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.
@@ -1,4 +1,4 @@
1
- import { access, constants } from 'fs/promises';
1
+ import { access, constants, readFile as fsReadFile } from 'fs/promises';
2
2
  import { join, resolve } from 'path';
3
3
  import { EmbeddingProvider } from '@providers/base/EmbeddingProvider';
4
4
  import type { EmbedConfig, EmbedInput, EmbedResult, BatchEmbedResult } from '@src/types/index';
@@ -13,8 +13,28 @@ import * as http from 'http';
13
13
  // Try to import native module
14
14
  let nativeModule: any = null;
15
15
  try {
16
- nativeModule = require('../../native');
17
- logger.info('Using native Llama.cpp module');
16
+ // Try different paths for native module
17
+ const possiblePaths = [
18
+ '../../native', // Development
19
+ 'vecbox/native', // Installed as dependency
20
+ './native', // Same directory
21
+ '../native', // One level up
22
+ '../vecbox/native' // When installed via npm
23
+ ];
24
+
25
+ for (const path of possiblePaths) {
26
+ try {
27
+ nativeModule = require(path);
28
+ logger.info(`Using native Llama.cpp module from: ${path}`);
29
+ break;
30
+ } catch (e) {
31
+ // Continue to next path
32
+ }
33
+ }
34
+
35
+ if (!nativeModule) {
36
+ throw new Error('Native module not found in any path');
37
+ }
18
38
  } catch (error) {
19
39
  logger.warn('Native module not available, falling back to HTTP');
20
40
  }
@@ -88,25 +108,6 @@ export class LlamaCppProvider extends EmbeddingProvider {
88
108
  }
89
109
  }
90
110
 
91
- private async loadGGUFModel(modelPath: string): Promise<Buffer> {
92
- try {
93
- logger.debug(`Loading GGUF model from: ${modelPath}`);
94
-
95
- // Read model file
96
- const modelBuffer = await fs.readFile(modelPath);
97
-
98
- if (!modelBuffer) {
99
- throw new Error(`Failed to read model file: ${modelPath}`);
100
- }
101
-
102
- logger.debug(`Model file loaded, size: ${modelBuffer.length} bytes`);
103
- return modelBuffer;
104
- } catch (error) {
105
- logger.error(`Failed to load GGUF model: ${error instanceof Error ? error.message : String(error)}`);
106
- throw error;
107
- }
108
- }
109
-
110
111
  private generateEmbedding(modelBuffer: Buffer, text: string): number[] {
111
112
  // Use the loaded model to generate embedding
112
113
  logger.debug(`Generating embedding with model buffer (${modelBuffer.length} bytes)`);
@@ -142,8 +143,8 @@ export class LlamaCppProvider extends EmbeddingProvider {
142
143
  };
143
144
  }
144
145
 
145
- // TODO: Implement direct Llama.cpp core usage in future
146
- throw new Error('Direct Llama.cpp core integration not yet implemented. Please use HTTP fallback or wait for next version.');
146
+ // Fallback: return error if native module not available
147
+ throw new Error('Direct Llama.cpp integration requires native module. Please ensure native module is properly compiled.');
147
148
  } catch (error: unknown) {
148
149
  logger.error(`Llama.cpp embedding failed: ${(error instanceof Error ? error.message : String(error))}`);
149
150
  throw error;
@@ -178,42 +179,8 @@ export class LlamaCppProvider extends EmbeddingProvider {
178
179
  };
179
180
  }
180
181
 
181
- // Fallback to HTTP batch processing
182
- const texts = [];
183
- for (const input of inputs) {
184
- const text = await this.readInput(input);
185
- if (text.trim()) {
186
- texts.push(text);
187
- }
188
- }
189
-
190
- if (texts.length === 0) {
191
- throw new Error('No valid texts to embed');
192
- }
193
-
194
- // For batch processing, use HTTP API
195
- const modelPath = await this.getModelPath();
196
- const requests = inputs.map((input, v) => ({
197
- input: input.text || '',
198
- model: modelPath,
199
- pooling: 'mean',
200
- normalize: 2
201
- }));
202
-
203
- // Execute batch requests (for now, do individual requests)
204
- const embeddings: number[][] = [];
205
- for (const request of requests) {
206
- const result = await this.executeLlamaEmbedding([JSON.stringify(request)]);
207
- const embedding = this.parseRawOutput(result.stdout);
208
- embeddings.push(embedding);
209
- }
210
-
211
- return {
212
- embeddings,
213
- dimensions: embeddings[0]?.length || 0,
214
- model: this.getModel(),
215
- provider: 'llamacpp',
216
- };
182
+ // Fallback: return error if native module not available
183
+ throw new Error('Direct Llama.cpp integration requires native module. Please ensure native module is properly compiled.');
217
184
  } catch (error: unknown) {
218
185
  logger.error(`Llama.cpp batch embedding failed: ${(error instanceof Error ? error.message : String(error))}`);
219
186
  throw error;
@@ -237,4 +204,31 @@ export class LlamaCppProvider extends EmbeddingProvider {
237
204
  protected getModel(): string {
238
205
  return this.modelPath;
239
206
  }
207
+
208
+ private async getModelPath(): Promise<string> {
209
+ // If modelPath is already absolute, return as-is
210
+ if (this.modelPath.startsWith('/') || this.modelPath.startsWith('./')) {
211
+ return this.modelPath;
212
+ }
213
+
214
+ // Try to resolve model path relative to current directory
215
+ const possiblePaths = [
216
+ resolve(this.modelPath), // Current directory
217
+ join('core/models', this.modelPath), // core/models subdirectory
218
+ join('models', this.modelPath), // models subdirectory
219
+ join(__dirname, '../../core/models', this.modelPath), // Relative to dist
220
+ ];
221
+
222
+ for (const path of possiblePaths) {
223
+ try {
224
+ await access(path, constants.F_OK);
225
+ return path;
226
+ } catch (e) {
227
+ // Continue to next path
228
+ }
229
+ }
230
+
231
+ // Return original path if none found (will fail later with proper error)
232
+ return this.modelPath;
233
+ }
240
234
  }
@@ -8,6 +8,10 @@ const logger = Logger.createModuleLogger('mistral');
8
8
  export class MistralProvider extends EmbeddingProvider {
9
9
  private client: Mistral;
10
10
 
11
+ protected getModel(): string {
12
+ return this.config.model || 'mistral-embed';
13
+ }
14
+
11
15
  constructor(config: EmbedConfig) {
12
16
  super(config);
13
17