crewai-ts
Version:
TypeScript port of crewAI for agent-based workflows
148 lines • 4.2 kB
TypeScript
/**
* OptimizedEmbeddingStorage implementation
* Provides memory-efficient storage for vector embeddings with various precision options
*/
/**
* Types of embedding precision
*/
export type EmbeddingPrecision = 'high' | 'standard' | 'reduced' | 'quantized';
/**
* Metadata for stored embeddings
*/
export interface EmbeddingMetadata {
dimensions: number;
precision: EmbeddingPrecision;
createdAt: number;
lastAccessedAt: number;
source?: string;
modelName?: string;
sizeBytes: number;
}
/**
* Options for embedding quantization
*/
export interface QuantizationOptions {
/**
* Quantization method
* @default 'minmax'
*/
method?: 'minmax' | 'centered' | 'logarithmic';
/**
* Whether to store normalization parameters for dequantization
* @default true
*/
storeParams?: boolean;
}
/**
* Options for configuring OptimizedEmbeddingStorage
*/
export interface OptimizedEmbeddingStorageOptions {
/**
* Default precision for embeddings if not specified during storage
* @default 'standard'
*/
defaultPrecision?: EmbeddingPrecision;
/**
* Whether to normalize vectors by default (unit vectors)
* @default false
*/
normalize?: boolean;
/**
* Maximum dimensions for embeddings (helps pre-allocate memory efficiently)
* @default 1536 (typical for large language models)
*/
maxDimensions?: number;
/**
* Options for quantization when 'quantized' precision is used
*/
quantizationOptions?: QuantizationOptions;
/**
* Whether to track memory usage statistics
* @default true
*/
trackStats?: boolean;
}
/**
* OptimizedEmbeddingStorage class
* Provides highly memory-efficient storage for vector embeddings
* with support for various precision levels and quantization
*/
export declare class OptimizedEmbeddingStorage {
private float32Embeddings;
private float64Embeddings;
private quantizedEmbeddings;
private defaultPrecision;
private normalize;
private maxDimensions;
private quantizationOptions;
private trackStats;
private stats;
constructor(options?: OptimizedEmbeddingStorageOptions);
/**
* Store an embedding with specified precision
*/
storeEmbedding(id: string, embedding: number[], precision?: EmbeddingPrecision, metadata?: Partial<Omit<EmbeddingMetadata, 'dimensions' | 'precision' | 'createdAt' | 'lastAccessedAt' | 'sizeBytes'>>): void;
/**
* Retrieve an embedding by ID
*/
getEmbedding(id: string): number[] | null;
/**
* Get embedding metadata without loading the full embedding
*/
getEmbeddingMetadata(id: string): EmbeddingMetadata | null;
/**
* Get typed array directly (for efficient similarity calculations)
*/
getEmbeddingArray(id: string): Float32Array | Float64Array | null;
/**
* Remove an embedding from storage
*/
removeEmbedding(id: string): boolean;
/**
* Clear all embeddings from storage
*/
clear(): void;
/**
* Get storage statistics
*/
getStats(): typeof this.stats;
/**
* Check if an embedding exists
*/
hasEmbedding(id: string): boolean;
/**
* Get all embedding IDs
*/
getEmbeddingIds(): string[];
/**
* Calculate vector similarity (cosine similarity)
* Optimized to work directly with stored embeddings
*/
calculateSimilarity(id1: string, id2: string): number | null;
/**
* Create embedding metadata
*/
private createMetadata;
/**
* Normalize a vector to unit length
*/
private normalizeVector;
/**
* Reduce precision of float32 values to simulate 16-bit storage
*/
private reduceFloat32Precision;
/**
* Quantize a vector to 8-bit integers
*/
private quantizeVector;
/**
* Dequantize a vector from 8-bit integers back to floating point
*/
private dequantizeVector;
/**
* Calculate cosine similarity between two vectors
* Optimized implementation for TypedArrays
*/
private cosineSimilarity;
}
//# sourceMappingURL=OptimizedEmbeddingStorage.d.ts.map