"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: all[name]
    });
}
_export(exports, {
    ALL_OPENAI_EMBEDDING_MODELS: function() {
        return ALL_OPENAI_EMBEDDING_MODELS;
    },
    OpenAIEmbedding: function() {
        return OpenAIEmbedding;
    }
});
const _azure = require("../llm/azure.js");
const _openai = require("../llm/openai.js");
const _types = require("./types.js");
const ALL_OPENAI_EMBEDDING_MODELS = {
    "text-embedding-ada-002": {
        dimensions: 1536,
        maxTokens: 8191
    },
    "text-embedding-3-small": {
        dimensions: 1536,
        dimensionOptions: [
            512,
            1536
        ],
        maxTokens: 8191
    },
    "text-embedding-3-large": {
        dimensions: 3072,
        dimensionOptions: [
            256,
            1024,
            3072
        ],
        maxTokens: 8191
    }
};
class OpenAIEmbedding extends _types.BaseEmbedding {
    /** embeddding model. defaults to "text-embedding-ada-002" */ model;
    /** number of dimensions of the resulting vector, for models that support choosing fewer dimensions. undefined will default to model default */ dimensions;
    // OpenAI session params
    /** api key */ apiKey = undefined;
    /** maximum number of retries, default 10 */ maxRetries;
    /** timeout in ms, default 60 seconds  */ timeout;
    /** other session options for OpenAI */ additionalSessionOptions;
    /** session object */ session;
    /**
   * OpenAI Embedding
   * @param init - initial parameters
   */ constructor(init){
        super();
        this.model = init?.model ?? "text-embedding-ada-002";
        this.dimensions = init?.dimensions; // if no dimensions provided, will be undefined/not sent to OpenAI
        this.embedBatchSize = init?.embedBatchSize ?? 10;
        this.maxRetries = init?.maxRetries ?? 10;
        this.timeout = init?.timeout ?? 60 * 1000; // Default is 60 seconds
        this.additionalSessionOptions = init?.additionalSessionOptions;
        if (init?.azure || (0, _azure.shouldUseAzure)()) {
            const azureConfig = (0, _azure.getAzureConfigFromEnv)({
                ...init?.azure,
                model: (0, _azure.getAzureModel)(this.model)
            });
            if (!azureConfig.apiKey) {
                throw new Error("Azure API key is required for OpenAI Azure models. Please set the AZURE_OPENAI_KEY environment variable.");
            }
            this.apiKey = azureConfig.apiKey;
            this.session = init?.session ?? (0, _openai.getOpenAISession)({
                azure: true,
                apiKey: this.apiKey,
                baseURL: (0, _azure.getAzureBaseUrl)(azureConfig),
                maxRetries: this.maxRetries,
                timeout: this.timeout,
                defaultQuery: {
                    "api-version": azureConfig.apiVersion
                },
                ...this.additionalSessionOptions
            });
        } else {
            this.apiKey = init?.apiKey ?? undefined;
            this.session = init?.session ?? (0, _openai.getOpenAISession)({
                apiKey: this.apiKey,
                maxRetries: this.maxRetries,
                timeout: this.timeout,
                ...this.additionalSessionOptions
            });
        }
    }
    /**
   * Get embeddings for a batch of texts
   * @param texts
   * @param options
   */ async getOpenAIEmbedding(input) {
        const { data } = await this.session.openai.embeddings.create({
            model: this.model,
            dimensions: this.dimensions,
            input
        });
        return data.map((d)=>d.embedding);
    }
    /**
   * Get embeddings for a batch of texts
   * @param texts
   */ async getTextEmbeddings(texts) {
        return await this.getOpenAIEmbedding(texts);
    }
    /**
   * Get embeddings for a single text
   * @param texts
   */ async getTextEmbedding(text) {
        return (await this.getOpenAIEmbedding([
            text
        ]))[0];
    }
}
