UNPKG

@genkit-ai/ai

Version:

Genkit AI framework generative AI APIs.

159 lines 4.6 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var document_exports = {}; __export(document_exports, { Document: () => Document, DocumentDataSchema: () => DocumentDataSchema, checkUniqueDocuments: () => checkUniqueDocuments }); module.exports = __toCommonJS(document_exports); var import_core = require("@genkit-ai/core"); var import_parts = require("./parts.js"); const DocumentDataSchema = import_core.z.object({ content: import_core.z.array(import_parts.PartSchema), metadata: import_core.z.record(import_core.z.string(), import_core.z.any()).optional() }); function deepCopy(value) { if (value === void 0) { return value; } return JSON.parse(JSON.stringify(value)); } class Document { content; metadata; constructor(data) { this.content = deepCopy(data.content); this.metadata = deepCopy(data.metadata); } static fromText(text, metadata) { return new Document({ content: [{ text }], metadata }); } // Construct a Document from a single media item static fromMedia(url, contentType, metadata) { return new Document({ content: [ { media: { contentType, url } } ], metadata }); } // Construct a Document from content static fromData(data, dataType, metadata) { if (dataType === "text") { return this.fromText(data, metadata); } return this.fromMedia(data, dataType, metadata); } /** * Concatenates all `text` parts present in the document with no delimiter. * @returns A string of all concatenated text parts. */ get text() { return this.content.map((part) => part.text || "").join(""); } /** * Media array getter. * @returns the array of media parts. */ get media() { return this.content.filter((part) => part.media && !part.text).map((part) => part.media); } /** * Gets the first item in the document. Either text or media url. */ get data() { if (this.text) { return this.text; } if (this.media) { return this.media[0].url; } return ""; } /** * Gets the contentType of the data that is returned by data() */ get dataType() { if (this.text) { return "text"; } if (this.media && this.media[0].contentType) { return this.media[0].contentType; } return void 0; } toJSON() { return { content: deepCopy(this.content), metadata: deepCopy(this.metadata) }; } /** * Embedders may return multiple embeddings for a single document. * But storage still requires a 1:1 relationship. So we create an * array of Documents from a single document - one per embedding. * @param embeddings The embeddings to create the documents from. * @returns an array of documents based on this document and the embeddings. */ getEmbeddingDocuments(embeddings) { const documents = []; for (const embedding of embeddings) { const jsonDoc = this.toJSON(); if (embedding.metadata) { if (!jsonDoc.metadata) { jsonDoc.metadata = {}; } jsonDoc.metadata.embedMetadata = embedding.metadata; } documents.push(new Document(jsonDoc)); } checkUniqueDocuments(documents); return documents; } } function checkUniqueDocuments(documents) { const seen = /* @__PURE__ */ new Set(); for (const doc of documents) { const serialized = JSON.stringify(doc); if (seen.has(serialized)) { console.warn( "Warning: embedding documents are not unique. Are you missing embed metadata?" ); return false; } seen.add(serialized); } return true; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { Document, DocumentDataSchema, checkUniqueDocuments }); //# sourceMappingURL=document.js.map