UNPKG

ai-utils.js

Version:

Build AI applications, chatbots, and agents with JavaScript and TypeScript.

64 lines (63 loc) 2.29 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MemoryVectorIndex = void 0; const zod_1 = __importDefault(require("zod")); const cosineSimilarity_js_1 = require("../../util/cosineSimilarity.cjs"); /** * A very simple vector index that stores all entries in memory. Useful when you only have * a small number of entries and don't want to set up a real database, e.g. for conversational memory * that does not need to be persisted. */ class MemoryVectorIndex { constructor() { Object.defineProperty(this, "entries", { enumerable: true, configurable: true, writable: true, value: new Map() }); } static async deserialize({ serializedData, schema, }) { let json = JSON.parse(serializedData); if (schema != null) { json = zod_1.default .array(zod_1.default.object({ id: zod_1.default.string(), vector: zod_1.default.array(zod_1.default.number()), data: schema, })) .parse(json); } const vectorIndex = new MemoryVectorIndex(); vectorIndex.upsertMany(json); return vectorIndex; } async upsertMany(data) { for (const entry of data) { this.entries.set(entry.id, entry); } } async queryByVector({ queryVector, similarityThreshold, maxResults, }) { const results = [...this.entries.values()] .map((entry) => ({ id: entry.id, similarity: (0, cosineSimilarity_js_1.cosineSimilarity)(entry.vector, queryVector), data: entry.data, })) .filter((entry) => similarityThreshold == undefined || entry.similarity == undefined || entry.similarity > similarityThreshold); results.sort((a, b) => b.similarity - a.similarity); return results.slice(0, maxResults); } serialize() { return JSON.stringify([...this.entries.values()]); } asIndex() { return this; } } exports.MemoryVectorIndex = MemoryVectorIndex;