promptly-ai
Version:
A universal template-based prompt management system for LLM applications
111 lines • 3.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Promptly = void 0;
const template_loader_1 = require("./template-loader");
const template_renderer_1 = require("./template-renderer");
const utils_1 = require("../utils");
class Promptly {
constructor(config) {
this.adapters = new Map();
this.templateLoader = new template_loader_1.TemplateLoader(config.templatesPath, config.cache ?? true);
this.templateRenderer = new template_renderer_1.TemplateRenderer();
this.defaultModel = config.defaultModel;
// Register LLM adapters
for (const adapter of config.adapters) {
this.adapters.set(adapter.name, adapter);
}
// Register Handlebars helpers
(0, utils_1.registerHandlebarsHelpers)();
}
/**
* Set validation schema for templates
*/
setValidationSchema(schema) {
this.validationSchema = schema;
}
/**
* Render a template with context
*/
async renderTemplate(templateName, context = {}) {
// Validate context if schema is provided
if (this.validationSchema?.[templateName]) {
(0, utils_1.validateContext)(context, this.validationSchema[templateName]);
}
const template = await this.templateLoader.loadTemplate(templateName);
return this.templateRenderer.renderTemplate(template, context, this.defaultModel);
}
/**
* Generate content using a template
*/
async generate(templateName, context = {}) {
const renderedPrompt = await this.renderTemplate(templateName, context);
const adapter = this.findAdapter(renderedPrompt.config.model);
if (!adapter) {
throw new Error(`No adapter found for model "${renderedPrompt.config.model}". Available adapters: ${Array.from(this.adapters.keys()).join(', ')}`);
}
return adapter.generate(renderedPrompt.config);
}
/**
* Find appropriate adapter for a model
*/
findAdapter(model) {
for (const adapter of this.adapters.values()) {
if (adapter.supportsModel(model)) {
return adapter;
}
}
return undefined;
}
/**
* Add a new LLM adapter
*/
addAdapter(adapter) {
this.adapters.set(adapter.name, adapter);
}
/**
* Remove an LLM adapter
*/
removeAdapter(name) {
this.adapters.delete(name);
}
/**
* Get list of available templates
*/
async getAvailableTemplates() {
return this.templateLoader.getAvailableTemplates();
}
/**
* Check if template exists
*/
async templateExists(templateName) {
return this.templateLoader.templateExists(templateName);
}
/**
* Clear template cache
*/
clearCache() {
this.templateLoader.clearCache();
}
/**
* Get registered adapters
*/
getAdapters() {
return Array.from(this.adapters.keys());
}
/**
* Get supported models for all adapters
*/
getSupportedModels() {
const result = {};
// This is a simplified version - in practice, you might want to
// have adapters expose their supported models more explicitly
for (const [name, adapter] of this.adapters) {
// For now, we'll just indicate the adapter name
// In a real implementation, adapters could expose a getSupportedModels() method
result[name] = [`${name} models (pattern-based matching)`];
}
return result;
}
}
exports.Promptly = Promptly;
//# sourceMappingURL=promptly.js.map