LLM ASR
All checks were successful
Publish Library / Build NPM Project (push) Successful in 43s
Publish Library / Tag Version (push) Successful in 13s

This commit is contained in:
2026-02-22 09:29:31 -05:00
parent ca66e8e304
commit abd290246c
4 changed files with 143 additions and 95 deletions

View File

@@ -145,7 +145,7 @@ class LLM {
// Handle compression and memory extraction
if(options.compress || options.memory) {
let compressed = null;
let compressed: any = null;
if(options.compress) {
compressed = await this.ai.language.compressHistory(options.history, options.compress.max, options.compress.min, options);
options.history.splice(0, options.history.length, ...compressed.history);
@@ -164,6 +164,15 @@ class LLM {
}), {abort});
}
async code(message: string, options?: LLMRequest): Promise<any> {
const resp = await this.ask(message, {...options, system: [
options?.system,
'Return your response in a code block'
].filter(t => !!t).join(('\n'))});
const codeBlock = /```(?:.+)?\s*([\s\S]*?)```/.exec(resp);
return codeBlock ? codeBlock[1].trim() : null;
}
/**
* Compress chat history to reduce context size
* @param {LLMMessage[]} history Chatlog that will be compressed
@@ -343,14 +352,11 @@ class LLM {
* @returns {Promise<{} | {} | RegExpExecArray | null>}
*/
async json(text: string, schema: string, options?: LLMRequest): Promise<any> {
let resp = await this.ask(text, {...options, system: (options?.system ? `${options.system}\n` : '') + `Only respond using a JSON code block matching this schema:
\`\`\`json
${schema}
\`\`\``});
if(!resp) return {};
const codeBlock = /```(?:.+)?\s*([\s\S]*?)```/.exec(resp);
const jsonStr = codeBlock ? codeBlock[1].trim() : resp;
return JSONAttemptParse(jsonStr, {});
const code = await this.code(text, {...options, system: [
options?.system,
`Only respond using JSON matching this schema:\n\`\`\`json\n${schema}\n\`\`\``
].filter(t => !!t).join('\n')});
return code ? JSONAttemptParse(code, {}) : null;
}
/**