prompt templates : contexts

This commit is contained in:
lovebird 2025-03-26 17:51:53 +01:00
parent f63efb2784
commit a867769a0e
2 changed files with 106 additions and 69 deletions

View File

@ -43,12 +43,19 @@ interface TemplateMap {
const LLMConfigSchema = z.object({
templates: z.record(TemplateConfigSchema),
instructions: InstructionSetSchema,
defaults: z.record(z.array(z.string()))
instructions: InstructionSetSchema.optional(),
defaults: z.record(z.array(z.string())).optional()
});
type LLMConfig = z.infer<typeof LLMConfigSchema>;
// Context enum for template switching
export const enum TemplateContext {
COMMONS = 'commons',
HOWTO = 'howto',
MARKETPLACE = 'marketplace'
}
// Default configuration
export const DEFAULT_CONFIG: LLMConfig = {
templates: {},
@ -70,7 +77,8 @@ const DEFAULT_TEMPLATE_OPTIONS = {
} as const;
// Load configuration from JSON file
const loadConfig = (configPath: string = "./src/config/llm.json"): LLMConfig => {
const loadConfig = (context: TemplateContext = TemplateContext.COMMONS): LLMConfig => {
const configPath = `./src/config/templates/${context}.json`;
if (exists(configPath)) {
try {
const content = read(configPath);
@ -79,7 +87,7 @@ const loadConfig = (configPath: string = "./src/config/llm.json"): LLMConfig =>
return LLMConfigSchema.parse(parsed);
}
} catch (error) {
console.error("Error loading LLM config:", error);
console.error(`Error loading ${context} config:`, error);
}
}
return DEFAULT_CONFIG;
@ -156,8 +164,8 @@ const createTemplate = (config: LLMConfig, name: string, defaults: Partial<Templ
return (opts: Partial<TemplateConfig> = {}) => {
const template = config.templates[name] || defaults;
const prompt = promptKey ? renderPrompt(DEFAULT_PROMPTS[promptKey], opts.variables) : buildPrompt(
config.instructions,
config.defaults,
config.instructions || {},
config.defaults || {},
opts.overrides || template.overrides
);
@ -170,66 +178,84 @@ const createTemplate = (config: LLMConfig, name: string, defaults: Partial<Templ
};
};
export const createTemplates = (configPath?: string) => {
const config = loadConfig(configPath);
export const createTemplates = (context: TemplateContext = TemplateContext.COMMONS) => {
const config = loadConfig(context);
return {
simple: createTemplate(config, 'simple', {
router: "openai",
model: "gpt-4o",
...DEFAULT_TEMPLATE_OPTIONS
}),
keywords: createTemplate(config, 'keywords', {
_router: "openai",
model: "google/gemini-exp-1206:free",
...DEFAULT_TEMPLATE_OPTIONS
}, 'keywords'),
references: createTemplate(config, 'references', {
_router: "openai",
model: "google/gemini-exp-1206:free",
...DEFAULT_TEMPLATE_OPTIONS
}, 'references'),
codeSimple: createTemplate(config, 'codeSimple', {
model: "gpt-4o",
...DEFAULT_TEMPLATE_OPTIONS
}),
research: createTemplate(config, 'research', {
router: "openai",
model: "gpt-4.5-preview",
...DEFAULT_TEMPLATE_OPTIONS
}),
toolsAndHardware: createTemplate(config, 'toolsAndHardware', {
router: "openai",
model: "perplexity/sonar-deep-research",
...DEFAULT_TEMPLATE_OPTIONS
}, 'toolsAndHardware'),
learnedSkills: createTemplate(config, 'learnedSkills', {
router: "openai",
model: "gpt-4o",
...DEFAULT_TEMPLATE_OPTIONS
}, 'learnedSkills'),
local: createTemplate(config, 'local', {
model: "perplexity/sonar-deep-research",
...DEFAULT_TEMPLATE_OPTIONS
}, 'local'),
howto: createTemplate(config, 'howto', {
router: "openai",
model: "gpt-4o-mini",
...DEFAULT_TEMPLATE_OPTIONS
})
};
switch (context) {
case TemplateContext.COMMONS:
return {
simple: createTemplate(config, 'simple', {
router: "openai",
model: "gpt-4o",
...DEFAULT_TEMPLATE_OPTIONS
}),
codeSimple: createTemplate(config, 'codeSimple', {
model: "gpt-4o",
...DEFAULT_TEMPLATE_OPTIONS
}),
research: createTemplate(config, 'research', {
router: "openai",
model: "gpt-4.5-preview",
...DEFAULT_TEMPLATE_OPTIONS
})
};
case TemplateContext.HOWTO:
return {
keywords: createTemplate(config, 'keywords', {
_router: "openai",
model: "google/gemini-exp-1206:free",
...DEFAULT_TEMPLATE_OPTIONS
}, 'keywords'),
references: createTemplate(config, 'references', {
_router: "openai",
model: "google/gemini-exp-1206:free",
...DEFAULT_TEMPLATE_OPTIONS
}, 'references'),
toolsAndHardware: createTemplate(config, 'toolsAndHardware', {
router: "openai",
model: "perplexity/sonar-deep-research",
...DEFAULT_TEMPLATE_OPTIONS
}, 'toolsAndHardware'),
requiredSkills: createTemplate(config, 'requiredSkills', {
router: "openai",
model: "gpt-4o",
...DEFAULT_TEMPLATE_OPTIONS
}),
learnedSkills: createTemplate(config, 'learnedSkills', {
router: "openai",
model: "gpt-4o",
...DEFAULT_TEMPLATE_OPTIONS
}, 'learnedSkills'),
local: createTemplate(config, 'local', {
model: "perplexity/sonar-deep-research",
...DEFAULT_TEMPLATE_OPTIONS
}, 'local')
};
case TemplateContext.MARKETPLACE:
return {
productDescription: createTemplate(config, 'productDescription', {
router: "openai",
model: "gpt-4o",
...DEFAULT_TEMPLATE_OPTIONS
}),
pricingAnalysis: createTemplate(config, 'pricingAnalysis', {
router: "openai",
model: "gpt-4o",
...DEFAULT_TEMPLATE_OPTIONS
}),
marketResearch: createTemplate(config, 'marketResearch', {
router: "openai",
model: "perplexity/sonar-deep-research",
...DEFAULT_TEMPLATE_OPTIONS
})
};
default:
return {};
}
};
// Export default templates with default config
export const templates = createTemplates();
// Export default templates with default config (commons context)
export const templates = createTemplates(TemplateContext.COMMONS);
// Export types
export type { TemplateConfig, LLMConfig, Prompt, PromptRegistry };

View File

@ -6,23 +6,34 @@ import {
templates as templateRegistry,
TemplateProps,
DEFAULT_CONFIG,
buildPrompt
buildPrompt,
TemplateContext,
createTemplates
} from "./kbot-templates.js";
export interface Props extends TemplateProps {}
export interface Props extends TemplateProps {
context?: TemplateContext;
}
export const filter = async (content: string, tpl: string = 'howto', opts: any = {}) => {
if (!content || content.length < 20 || !templateRegistry[tpl]) {
export const filter = async (content: string, tpl: string = 'howto', opts: Props = {}) => {
if (!content || content.length < 20) {
return content;
}
const template = typeof templateRegistry[tpl] === 'function' ? templateRegistry[tpl]() : templateRegistry[tpl];
const context = opts.context || TemplateContext.COMMONS;
const templates = createTemplates(context);
if (!templates[tpl]) {
return content;
}
const template = typeof templates[tpl] === 'function' ? templates[tpl]() : templates[tpl];
const options = getFilterOptions(content, template, opts);
const result = await run(options);
return result[0] as string;
};
export const getFilterOptions = (content: string, template: any, opts: any = {}) => {
export const getFilterOptions = (content: string, template: any, opts: Props = {}) => {
return OptionsSchema().parse({
...template,
prompt: `${template.prompt || ""} : ${content}`,