@nomyx/assistant
Version:
A powerful assistant library and cli for your AI projects. works with Vertex AI (Claude and Gemini)
57 lines (56 loc) • 2.02 kB
JavaScript
;
// LOCKED: TRUE
// All content in this file is locked and cannot be edited.
Object.defineProperty(exports, "__esModule", { value: true });
exports.SemanticCacheManagerImpl = void 0;
class SemanticCacheManagerImpl {
constructor(embeddingProvider) {
this.cache = new Map();
this.embeddingProvider = embeddingProvider;
}
async get(key) {
const entry = this.cache.get(key);
if (entry && (!entry.expiresAt || entry.expiresAt > Date.now())) {
return entry.value;
}
return undefined;
}
async set(key, value, ttl) {
const embedding = await this.embeddingProvider.embed(key);
const entry = {
key,
value,
embedding,
expiresAt: ttl ? Date.now() + ttl : undefined,
};
this.cache.set(key, entry);
}
async delete(key) {
this.cache.delete(key);
}
async clear() {
this.cache.clear();
}
async search(query, threshold) {
const queryEmbedding = await this.embeddingProvider.embed(query);
const results = [];
for (const entry of this.cache.values()) {
if (entry.expiresAt && entry.expiresAt <= Date.now()) {
continue;
}
const similarity = this.cosineSimilarity(queryEmbedding, entry.embedding);
if (similarity >= threshold) {
results.push(entry);
}
}
return results.sort((a, b) => this.cosineSimilarity(queryEmbedding, b.embedding) -
this.cosineSimilarity(queryEmbedding, a.embedding));
}
cosineSimilarity(a, b) {
const dotProduct = a.reduce((sum, _, i) => sum + a[i] * b[i], 0);
const magnitudeA = Math.sqrt(a.reduce((sum, val) => sum + val * val, 0));
const magnitudeB = Math.sqrt(b.reduce((sum, val) => sum + val * val, 0));
return dotProduct / (magnitudeA * magnitudeB);
}
}
exports.SemanticCacheManagerImpl = SemanticCacheManagerImpl;