UNPKG

bilberrydb

Version:

TypeScript/JavaScript SDK for BilberryDB vector search API

153 lines 5.21 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BilberryVector = exports.BilberryClient = void 0; exports.init = init; // src/index.ts const axios_1 = __importDefault(require("axios")); const form_data_1 = __importDefault(require("form-data")); const fs_1 = __importDefault(require("fs")); const path_1 = __importDefault(require("path")); class BilberryVector { constructor(config) { this.config = config; this.client = axios_1.default.create({ baseURL: config.base_url || 'https://appbilberry.com', timeout: 30000, }); } /** * Search for similar images using an image file * @param imagePath - Path to the image file or Buffer/Uint8Array * @param options - Search options */ async search(imagePath, options = {}) { try { const formData = new form_data_1.default(); // Handle different input types if (typeof imagePath === 'string') { // File path if (!fs_1.default.existsSync(imagePath)) { throw new Error(`File not found: ${imagePath}`); } const fileStream = fs_1.default.createReadStream(imagePath); const filename = path_1.default.basename(imagePath); formData.append('file', fileStream, filename); } else if (Buffer.isBuffer(imagePath) || imagePath instanceof Uint8Array) { // Buffer or Uint8Array formData.append('file', Buffer.from(imagePath), 'image.jpg'); } else { throw new Error('Invalid image input. Use file path, Buffer, or Uint8Array'); } const response = await this.client.post('/search/imagez', formData, { headers: { ...formData.getHeaders(), }, params: { user_email: this.config.api_id, api_key: this.config.api_key, top_k: options.top_k || 5, content_type: options.content_type, }, }); // Debug: Log the actual response structure if (response.data && response.data.length > 0) { console.log('API Response sample:', JSON.stringify(response.data[0], null, 2)); } return response.data; } catch (error) { if (error.response) { throw new Error(`API Error: ${error.response.data.detail || error.response.statusText}`); } throw new Error(`Request failed: ${error.message}`); } } /** * Get all items for the user * @param content_type - Optional content type filter */ async getAllItems(content_type) { try { const response = await this.client.get('/itemz', { params: { user_email: this.config.api_id, api_key: this.config.api_key, content_type, }, }); return response.data; } catch (error) { if (error.response) { throw new Error(`API Error: ${error.response.data.detail || error.response.statusText}`); } throw new Error(`Request failed: ${error.message}`); } } /** * Download a file by ID * @param itemId - The ID of the item to download */ async downloadFile(itemId) { try { const response = await this.client.get(`/itemz/${itemId}/download`, { params: { user_email: this.config.api_id, api_key: this.config.api_key, }, responseType: 'arraybuffer', }); return Buffer.from(response.data); } catch (error) { if (error.response) { throw new Error(`API Error: ${error.response.data.detail || error.response.statusText}`); } throw new Error(`Request failed: ${error.message}`); } } } exports.BilberryVector = BilberryVector; class BilberryClient { constructor(config) { this.config = config; } /** * Get vector search instance */ get_vec() { return new BilberryVector(this.config); } /** * Alias for get_vec() for consistency */ getVector() { return this.get_vec(); } } exports.BilberryClient = BilberryClient; /** * Initialize BilberryDB client * @param config - Configuration object with api_key and api_id */ function init(config) { if (!config.api_key) { throw new Error('api_key is required'); } if (!config.api_id) { throw new Error('api_id is required'); } return new BilberryClient(config); } // Default export for CommonJS compatibility exports.default = { init, BilberryClient, BilberryVector, }; //# sourceMappingURL=index.js.map