howto : resources:)

This commit is contained in:
2025-03-27 20:40:30 +01:00
parent 218c8df7cd
commit 28e3524297
10 changed files with 59 additions and 41 deletions
+10 -3
View File
@@ -18,6 +18,7 @@ export interface TemplateProps extends IKBotTask {
disabled?: boolean;
template?: string;
renderer?: string;
}
const TemplateConfigSchema = z.object({
@@ -196,7 +197,8 @@ export const createTemplates = (context: TemplateContext = TemplateContext.COMMO
case TemplateContext.HOWTO:
return {
keywords: createTemplate(config, 'keywords', {
model: "google/gemini-exp-1206:free",
router: "openai",
model: "gpt-4o",
...DEFAULT_TEMPLATE_OPTIONS
}, 'keywords'),
references: createTemplate(config, 'references', {
@@ -249,5 +251,10 @@ export const createTemplates = (context: TemplateContext = TemplateContext.COMMO
export const templates = createTemplates(TemplateContext.COMMONS);
// Export types
export type { TemplateConfig, LLMConfig, Prompt, PromptRegistry };
export { InstructionSchema, InstructionSetSchema, TemplateConfigSchema, LLMConfigSchema, PromptSchema, PromptRegistrySchema };
export type { TemplateConfig, LLMConfig, Prompt, PromptRegistry }
export { InstructionSchema,
InstructionSetSchema,
TemplateConfigSchema,
LLMConfigSchema,
PromptSchema,
PromptRegistrySchema };
+12 -3
View File
@@ -41,12 +41,20 @@ export const filter = async (content: string, tpl: string = 'howto', opts: Props
tools: []
};
const ca_options = JSON.parse(JSON.stringify(removeEmpty(cache_key_obj)));
const cached = await get_cached_object({ ca_options }, 'kbot');
let cached = null
try {
cached = await get_cached_object({ ca_options }, 'kbot');
} catch (e) {
logger.error(`Failed to get cached object for ${content.substring(0, 20)}`, e);
}
if (cached) {
logger.debug(`Cache hit for : ${content.substring(0, 20)}`)
return cached;
}
const result = await run(options);
if(!result || !result[0]){
logger.error(`No result for ${content.substring(0, 20)}`)
return content;
}
if (template.format === 'json') {
try {
const jsonResult = JSON.parse(result[0] as string);
@@ -76,12 +84,13 @@ export const template_filter = async (text: string, template: string, context: T
])
);
const resolvedText = resolveVariables(text, true);
return await filter(resolvedText, template, {
const ret = await filter(resolvedText, template, {
context,
...resolvedTemplate,
prompt: `${resolvedTemplate.prompt}\n\nText to process:\n${resolvedText}`,
variables: environment.variables
});
return ret;
};
export const getFilterOptions = (content: string, template: any, opts: Props = {}) => {
+8 -23
View File
@@ -38,20 +38,9 @@ model_files = model_files.map((f) =>
forward_slash(`${howto.slug}/${path.relative(path.resolve(howto_abs), f)}`),
);
const content = async (str: string) => {
const ret = await translate(
await applyFilters(str),
I18N_SOURCE_LANGUAGE,
Astro.currentLocale,
);
return ret;
};
const component = async (str: string) =>
await createMarkdownComponent(await content(str));
const componentHTML = async (str: string) =>
await createHTMLComponent(await content(str));
const content = async (str: string) => await translate(str,I18N_SOURCE_LANGUAGE, Astro.currentLocale)
const component = async (str: string) => await createMarkdownComponent(await content(str));
const componentHTML = async (str: string) => await createHTMLComponent(await content(str));
const stepsWithFilteredMarkdown = await pMap(
howto.steps,
@@ -71,7 +60,6 @@ const authorGeo = howto?.user?.geo || {
const authorLinks = (howto?.user?.data.urls || []).filter(
(l) => !l.url.includes("one_army") && !l.url.includes("bazar"),
);
//////////////////////////////////////////////////////////////
//
// Resources
@@ -84,24 +72,21 @@ let howto_resources = exists(howto_resources_path)
? read(howto_resources_path, "string") || howto_resources_default
: howto_resources_default;
const contentAll = `${howto.title} \n Description : ${howto.description} \n Steps: ${howto.steps.map((s) => s.text).join("\n")} \n`;
if (HOWTO_ADD_HARDWARE) {
const references_extra = await filter(contentAll,'tools_and_hardware')
howto_resources = `${howto_resources}\n\n ${references_extra}`
}
const contentAll = `${howto.content}`;
if (HOWTO_COMPLETE_RESOURCES) {
const keywords = (await filter(contentAll,'keywords')) as string;
const references_extra = await filter(keywords,'references');
howto_resources = `${howto_resources}\n${references_extra}`;
}
if (HOWTO_ADD_HARDWARE) {
const references_extra = await filter(contentAll,'tools_and_hardware')
howto_resources = `${howto_resources}\n\n ${references_extra}`
}
if (HOWTO_COMPLETE_SKILLS) {
const references_extra = await filter(contentAll,'learned_skills')
howto_resources = `${howto_resources}\n\n ${references_extra}`
}
if (HOWTO_LOCAL_RESOURCES && howto.user && howto.user.geo) {
const references_extra = await filter(`Location: ${authorGeo.countryName}`,'local')
howto_resources = `${howto_resources}\n\n ${references_extra}`
+1 -1
View File
@@ -25,7 +25,7 @@ export const FILES_WEB = 'https://files.polymech.io/files/machines/howtos/'
export const HOWTO_FILTER_LLM = true
export const HOWTO_ANNOTATIONS = false
export const HOWTO_ANNOTATIONS_CACHE = false
export const HOWTO_COMPLETE_RESOURCES = false
export const HOWTO_COMPLETE_RESOURCES = true
export const HOWTO_ADD_HARDWARE = false
export const HOWTO_COMPLETE_SKILLS = false
export const HOWTO_LOCAL_RESOURCES = false
+3 -5
View File
@@ -1,22 +1,20 @@
{
"templates": {
"keywords": {
"_router": "openai",
"model": "google/gemini-exp-1206:free",
"router": "openai",
"model": "gpt-4o",
"preferences": "none",
"mode": "completion",
"prompt": "Return a list of max. 10 keywords that can be used for SEO purposes, separated by commas (dont comment, just the list) : "
},
"references": {
"_router": "openai",
"model": "google/gemini-exp-1206:free",
"model": "perplexity/sonar-deep-research",
"preferences": "none",
"mode": "completion",
"prompt": "Return a list of useful references (only with links), as Markdown, grouped : Articles, Books, Papers, Youtube, Opensource Designs, ... Dont comment !",
"filters": "code"
},
"toolsAndHardware": {
"router_": "openai",
"model": "perplexity/sonar-deep-research",
"preferences": "none",
"mode": "completion",
+6
View File
@@ -123,4 +123,10 @@ export interface IHowto {
version?: string
// Added content field (description + steps)
content?: string
// Added content field : keywords
keywords?: string
// Added content field : skills
skills?: string
// Added content field : resources
resources?: string
}
+15 -2
View File
@@ -27,12 +27,16 @@ import {
HOWTO_FILES_WEB,
HOWTO_FILES_ABS,
HOWTO_FILTER_LLM,
HOWTO_COMPLETE_RESOURCES,
default_image,
HOWTO_ROOT,
HOWTO_GLOB,
HOWTO_MIGRATION,
HOWTO_ANNOTATIONS
HOWTO_ANNOTATIONS,
HOWTO_COMPLETE_RESOURCES,
HOWTO_ADD_HARDWARE,
HOWTO_COMPLETE_SKILLS,
HOWTO_LOCAL_RESOURCES
} from "config/config.js"
import { env, logger } from '@/base/index.js'
@@ -99,6 +103,7 @@ export const downloadFiles = async (dst: string, howto: IHowto) => {
}
}, { concurrency: 1 })
}
export const asset_local_rel = async (item: IHowto, asset: IImage) => {
const sanitizedFilename = sanitizeFilename(asset.name).toLowerCase()
const asset_path = path.join(HOWTO_ROOT(), item.slug, sanitizedFilename)
@@ -109,6 +114,7 @@ export const asset_local_rel = async (item: IHowto, asset: IImage) => {
}
return default_image().src
}
export const raw = async () => {
const src = HOWTO_MIGRATION()
const data = read(src, 'json') as any;
@@ -167,6 +173,7 @@ const to_github = async (item: IHowto) => {
`category: ${item.category?.label || 'uncategorized'}`,
`difficulty: ${item.difficulty_level}`,
`time: ${item.time}`,
`keywords: ${item.keywords || ''}`,
`location: ${item.user?.geo ? `${item.user.geo.city ? `${item.user.geo.city}, ` : ''}${item.user.geo.countryName || ''}` : ''}`,
'---',
'',
@@ -316,6 +323,12 @@ const complete = async (item: IHowto) => {
...item.steps.map(step => step.text)
].filter(Boolean).join('\n\n')
// Generate keywords using the keywords template
if(HOWTO_COMPLETE_RESOURCES){
item.keywords = await template_filter(item.content, 'keywords', TemplateContext.HOWTO);
item.resources = await template_filter(item.content, 'toolsAndHardware', TemplateContext.HOWTO);
}
await to_github(item)
await to_mdx(item)
await to_astro(item)