UNPKG

jorel

Version:

The easiest way to use LLMs, including streams, images, documents, tools and various agent scenarios.

93 lines (92 loc) 3.71 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.LlmDocumentCollection = void 0; const document_1 = require("./document"); const xmlDocumentToTextTemplate = "<Document id='{{id}}' type='{{type}}' title='{{title}}' source='{{source}}'{{attributes}}>{{content}}</Document>"; /** * A collection of LLM documents, like a binder or folder of documents * that can be used for grounding LLM generations (either directly or passed to agents). * * Also provides a system message representation of the documents */ class LlmDocumentCollection { constructor(documents = [], config = {}) { const _documents = documents.map((document) => document instanceof document_1.LlmDocument ? document : new document_1.LlmDocument(document)); this._documents = new Map(_documents.map((document) => [document.id, document])); this.documentToTextTemplate = config.documentToText || "xml"; } /** * The number of documents in the collection */ get length() { return this._documents.size; } /** * Get all documents in the collection (as a copy) */ get all() { return Array.from(this._documents.values()); } /** * Get the definition of all documents in the collection (e.g. for serialization) */ get definition() { return this.all.map((document) => document.definition); } /** * Get a system message representation of the documents */ get systemMessageRepresentation() { if (this._documents.size === 0) return "-"; if (this.documentToTextTemplate === "json") return JSON.stringify(this.definition); const template = this.documentToTextTemplate === "xml" ? xmlDocumentToTextTemplate : this.documentToTextTemplate.template; const rendered = this.all.map((document) => { if (!template.includes("{{id}}")) throw new Error("Document template must include '{{id}}' placeholder."); if (!template.includes("{{content}}")) throw new Error("Document template must include '{{content}}' placeholder."); const _attributes = document.attributes ? Object.entries(document.attributes) : []; const attributes = _attributes .map(([key, value]) => `${key}='${value}'`) .join(" ") .trim(); return template .replace("{{id}}", document.id) .replace("{{type}}", document.type) .replace("{{title}}", document.title) .replace("{{content}}", document.content) .replace("{{attributes}}", attributes ? ` ${attributes}` : "") .replace("{{source}}", document.source || "n/a"); }); if (this.documentToTextTemplate === "xml") return `<Documents>\n${rendered.join("\n")}\n</Documents>`; return rendered.join(this.documentToTextTemplate.separator); } /** * Create a new collection from a JSON representation */ static fromJSON(documents = []) { return new LlmDocumentCollection(documents.map((document) => new document_1.LlmDocument(document instanceof document_1.LlmDocument ? document.definition : document))); } /** * Add a document to the collection */ add(document) { this._documents.set(document.id, document); } /** * Remove a document from the collection */ remove(id) { this._documents.delete(id); } /** * Get a document by its ID */ get(id) { return this._documents.get(id); } } exports.LlmDocumentCollection = LlmDocumentCollection;