From b3da60538c6d3cd92b097b79bf36ef7cfab41482 Mon Sep 17 00:00:00 2001 From: babayaga Date: Wed, 26 Mar 2025 17:58:41 +0100 Subject: [PATCH] prompts : json --- src/base/kbot-templates.ts | 9 +++++++++ src/base/kbot.ts | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/base/kbot-templates.ts b/src/base/kbot-templates.ts index 3692a0e..8c6fc78 100644 --- a/src/base/kbot-templates.ts +++ b/src/base/kbot-templates.ts @@ -128,6 +128,10 @@ const PromptRegistrySchema = z.record(PromptSchema); type PromptRegistry = z.infer; const DEFAULT_PROMPTS: PromptRegistry = { + seo: { + template: "Analyze the following content and return a JSON object with these fields: keywords (array of max 10 strings), title (string), description (string), tags (array of max 5 strings). Format as valid JSON only.", + format: 'json' + }, keywords: { template: "Return a list of max. 10 keywords that can be used for SEO purposes, separated by commas (dont comment, just the list) : ", format: 'text' @@ -184,6 +188,11 @@ export const createTemplates = (context: TemplateContext = TemplateContext.COMMO switch (context) { case TemplateContext.COMMONS: return { + seo: createTemplate(config, 'seo', { + router: "openai", + model: "gpt-4o", + ...DEFAULT_TEMPLATE_OPTIONS + }, 'seo'), simple: createTemplate(config, 'simple', { router: "openai", model: "gpt-4o", diff --git a/src/base/kbot.ts b/src/base/kbot.ts index 17d33df..6a4460f 100644 --- a/src/base/kbot.ts +++ b/src/base/kbot.ts @@ -30,6 +30,17 @@ export const filter = async (content: string, tpl: string = 'howto', opts: Props const template = typeof templates[tpl] === 'function' ? templates[tpl]() : templates[tpl]; const options = getFilterOptions(content, template, opts); const result = await run(options); + + // Handle JSON format responses + if (template.format === 'json') { + try { + return JSON.parse(result[0] as string); + } catch (e) { + console.error('Failed to parse JSON response:', e); + return result[0]; + } + } + return result[0] as string; };