File size: 9,784 Bytes
40d7073 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 | "use strict";
/**
* Embedding Service - Unified embedding generation and management
*
* This service provides a unified interface for generating, caching, and
* managing embeddings from various sources (local models, APIs, etc.)
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.EmbeddingService = exports.LocalNGramProvider = exports.MockEmbeddingProvider = void 0;
exports.createEmbeddingService = createEmbeddingService;
exports.getDefaultEmbeddingService = getDefaultEmbeddingService;
/**
* Simple hash function for cache keys
*/
function hashText(text) {
let hash = 0;
for (let i = 0; i < text.length; i++) {
const char = text.charCodeAt(i);
hash = ((hash << 5) - hash) + char;
hash = hash & hash;
}
return `h${hash.toString(36)}`;
}
/**
* Mock embedding provider for testing
*/
class MockEmbeddingProvider {
constructor(dimensions = 384) {
this.name = 'mock';
this.dimensions = dimensions;
}
async embed(texts) {
return texts.map(text => {
// Generate deterministic pseudo-random embeddings based on text
const embedding = [];
let seed = 0;
for (let i = 0; i < text.length; i++) {
seed = ((seed << 5) - seed + text.charCodeAt(i)) | 0;
}
for (let i = 0; i < this.dimensions; i++) {
seed = (seed * 1103515245 + 12345) | 0;
embedding.push((seed % 1000) / 1000 - 0.5);
}
// Normalize
const norm = Math.sqrt(embedding.reduce((s, v) => s + v * v, 0));
return embedding.map(v => v / (norm || 1));
});
}
getDimensions() {
return this.dimensions;
}
}
exports.MockEmbeddingProvider = MockEmbeddingProvider;
/**
* Simple local embedding using character n-grams
* This is a fallback when no external provider is available
*/
class LocalNGramProvider {
constructor(dimensions = 256, ngramSize = 3) {
this.name = 'local-ngram';
this.dimensions = dimensions;
this.ngramSize = ngramSize;
}
async embed(texts) {
return texts.map(text => this.embedSingle(text));
}
embedSingle(text) {
const embedding = new Array(this.dimensions).fill(0);
const normalized = text.toLowerCase().replace(/[^a-z0-9]/g, ' ');
// Generate n-grams and hash them into embedding dimensions
for (let i = 0; i <= normalized.length - this.ngramSize; i++) {
const ngram = normalized.slice(i, i + this.ngramSize);
const hash = this.hashNgram(ngram);
const idx = Math.abs(hash) % this.dimensions;
embedding[idx] += hash > 0 ? 1 : -1;
}
// Normalize
const norm = Math.sqrt(embedding.reduce((s, v) => s + v * v, 0));
return embedding.map(v => v / (norm || 1));
}
hashNgram(ngram) {
let hash = 0;
for (let i = 0; i < ngram.length; i++) {
hash = ((hash << 5) - hash + ngram.charCodeAt(i)) | 0;
}
return hash;
}
getDimensions() {
return this.dimensions;
}
}
exports.LocalNGramProvider = LocalNGramProvider;
/**
* Embedding service with caching and batching
*/
class EmbeddingService {
constructor(config = {}) {
this.providers = new Map();
this.cache = new Map();
this.config = {
defaultProvider: config.defaultProvider ?? 'local-ngram',
maxCacheSize: config.maxCacheSize ?? 10000,
cacheTtl: config.cacheTtl ?? 3600000, // 1 hour
batchSize: config.batchSize ?? 32,
};
// Register default providers
this.registerProvider(new LocalNGramProvider());
this.registerProvider(new MockEmbeddingProvider());
}
/**
* Register an embedding provider
*/
registerProvider(provider) {
this.providers.set(provider.name, provider);
}
/**
* Get a registered provider
*/
getProvider(name) {
const providerName = name ?? this.config.defaultProvider;
const provider = this.providers.get(providerName);
if (!provider) {
throw new Error(`Provider not found: ${providerName}`);
}
return provider;
}
/**
* Generate embeddings for texts with caching
*
* @param texts - Texts to embed
* @param provider - Provider name (uses default if not specified)
* @returns Array of embeddings
*/
async embed(texts, provider) {
const providerInstance = this.getProvider(provider);
const providerName = providerInstance.name;
const now = Date.now();
// Check cache and collect texts that need embedding
const results = new Array(texts.length).fill(null);
const uncachedIndices = [];
const uncachedTexts = [];
for (let i = 0; i < texts.length; i++) {
const cacheKey = `${providerName}:${hashText(texts[i])}`;
const cached = this.cache.get(cacheKey);
if (cached && now - cached.timestamp < this.config.cacheTtl) {
results[i] = cached.embedding;
cached.hits++;
}
else {
uncachedIndices.push(i);
uncachedTexts.push(texts[i]);
}
}
// Generate embeddings for uncached texts in batches
if (uncachedTexts.length > 0) {
const batches = [];
for (let i = 0; i < uncachedTexts.length; i += this.config.batchSize) {
batches.push(uncachedTexts.slice(i, i + this.config.batchSize));
}
let batchOffset = 0;
for (const batch of batches) {
const embeddings = await providerInstance.embed(batch);
for (let j = 0; j < embeddings.length; j++) {
const originalIndex = uncachedIndices[batchOffset + j];
results[originalIndex] = embeddings[j];
// Cache the result
const cacheKey = `${providerName}:${hashText(texts[originalIndex])}`;
this.addToCache(cacheKey, embeddings[j], now);
}
batchOffset += batch.length;
}
}
return results;
}
/**
* Generate a single embedding
*/
async embedOne(text, provider) {
const results = await this.embed([text], provider);
return results[0];
}
/**
* Add entry to cache with LRU eviction
*/
addToCache(key, embedding, timestamp) {
// Evict old entries if cache is full
if (this.cache.size >= this.config.maxCacheSize) {
// Find and remove least recently used entry
let oldestKey = '';
let oldestTime = Infinity;
let lowestHits = Infinity;
for (const [k, v] of this.cache.entries()) {
if (v.hits < lowestHits || (v.hits === lowestHits && v.timestamp < oldestTime)) {
oldestKey = k;
oldestTime = v.timestamp;
lowestHits = v.hits;
}
}
if (oldestKey) {
this.cache.delete(oldestKey);
}
}
this.cache.set(key, { embedding, timestamp, hits: 0 });
}
/**
* Compute cosine similarity between two embeddings
*/
cosineSimilarity(a, b) {
if (a.length !== b.length) {
throw new Error('Embeddings must have same dimensions');
}
let dotProduct = 0;
let normA = 0;
let normB = 0;
for (let i = 0; i < a.length; i++) {
dotProduct += a[i] * b[i];
normA += a[i] * a[i];
normB += b[i] * b[i];
}
const denom = Math.sqrt(normA) * Math.sqrt(normB);
return denom === 0 ? 0 : dotProduct / denom;
}
/**
* Find most similar texts from a corpus
*/
async findSimilar(query, corpus, k = 5, provider) {
const [queryEmbed, ...corpusEmbeds] = await this.embed([query, ...corpus], provider);
const results = corpusEmbeds.map((embed, i) => ({
text: corpus[i],
similarity: this.cosineSimilarity(queryEmbed, embed),
index: i,
}));
return results
.sort((a, b) => b.similarity - a.similarity)
.slice(0, k);
}
/**
* Get cache statistics
*/
getCacheStats() {
let totalHits = 0;
for (const entry of this.cache.values()) {
totalHits += entry.hits;
}
return {
size: this.cache.size,
maxSize: this.config.maxCacheSize,
hitRate: this.cache.size > 0 ? totalHits / this.cache.size : 0,
};
}
/**
* Clear the cache
*/
clearCache() {
this.cache.clear();
}
/**
* Get embedding dimensions for a provider
*/
getDimensions(provider) {
return this.getProvider(provider).getDimensions();
}
/**
* List available providers
*/
listProviders() {
return Array.from(this.providers.keys());
}
}
exports.EmbeddingService = EmbeddingService;
/**
* Create an embedding service instance
*/
function createEmbeddingService(config) {
return new EmbeddingService(config);
}
// Singleton instance
let defaultService = null;
/**
* Get the default embedding service instance
*/
function getDefaultEmbeddingService() {
if (!defaultService) {
defaultService = new EmbeddingService();
}
return defaultService;
}
exports.default = {
EmbeddingService,
LocalNGramProvider,
MockEmbeddingProvider,
createEmbeddingService,
getDefaultEmbeddingService,
};
|