"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.MetadataTagger = void 0;
exports.createMetadataTagger = createMetadataTagger;
exports.createMetadataTaggerFromZod = createMetadataTaggerFromZod;
const documents_1 = require("@langchain/core/documents");
const openai_1 = require("@langchain/openai");
const json_schema_1 = require("@langchain/core/utils/json_schema");
const index_js_1 = require("../chains/openai_functions/index.cjs");
/**
 * A transformer that tags metadata to a document using a tagging chain.
 */
class MetadataTagger extends documents_1.MappingDocumentTransformer {
    static lc_name() {
        return "MetadataTagger";
    }
    constructor(fields) {
        super();
        Object.defineProperty(this, "taggingChain", {
            enumerable: true,
            configurable: true,
            writable: true,
            value: void 0
        });
        this.taggingChain = fields.taggingChain;
        if (this.taggingChain.inputKeys.length !== 1) {
            throw new Error("Invalid input chain. The input chain must have exactly one input.");
        }
        if (this.taggingChain.outputKeys.length !== 1) {
            throw new Error("Invalid input chain. The input chain must have exactly one output.");
        }
    }
    async _transformDocument(document) {
        const taggingChainResponse = await this.taggingChain.call({
            [this.taggingChain.inputKeys[0]]: document.pageContent,
        });
        const extractedMetadata = taggingChainResponse[this.taggingChain.outputKeys[0]];
        return new documents_1.Document({
            pageContent: document.pageContent,
            metadata: { ...extractedMetadata, ...document.metadata },
        });
    }
}
exports.MetadataTagger = MetadataTagger;
function createMetadataTagger(schema, options) {
    const { llm = new openai_1.ChatOpenAI({ model: "gpt-3.5-turbo-0613" }), ...rest } = options;
    const taggingChain = (0, index_js_1.createTaggingChain)(schema, llm, rest);
    return new MetadataTagger({ taggingChain });
}
function createMetadataTaggerFromZod(schema, options) {
    return createMetadataTagger((0, json_schema_1.toJsonSchema)(schema), options);
}
