Added JSON / Summary LLM safeguard
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/ai-utils",
|
"name": "@ztimson/ai-utils",
|
||||||
"version": "0.8.13",
|
"version": "0.8.14",
|
||||||
"description": "AI Utility library",
|
"description": "AI Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|||||||
15
src/llm.ts
15
src/llm.ts
@@ -358,7 +358,7 @@ class LLM {
|
|||||||
* @returns {Promise<{} | {} | RegExpExecArray | null>}
|
* @returns {Promise<{} | {} | RegExpExecArray | null>}
|
||||||
*/
|
*/
|
||||||
async json(text: string, schema: string, options?: LLMRequest): Promise<any> {
|
async json(text: string, schema: string, options?: LLMRequest): Promise<any> {
|
||||||
let system = `Your job is to convert input to JSON. Call the \`submit\` tool exactly once with JSON matching this schema:\n\`\`\`json\n${schema}\n\`\`\``;
|
let system = `Your job is to convert input to JSON using tool calls. Call the \`submit\` tool at least once with JSON matching this schema:\n\`\`\`json\n${schema}\n\`\`\`\n\nResponses are ignored`;
|
||||||
if(options?.system) system += '\n\n' + options.system;
|
if(options?.system) system += '\n\n' + options.system;
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
let done = false;
|
let done = false;
|
||||||
@@ -376,11 +376,11 @@ class LLM {
|
|||||||
resolve(json);
|
resolve(json);
|
||||||
done = true;
|
done = true;
|
||||||
} catch { return 'Invalid JSON'; }
|
} catch { return 'Invalid JSON'; }
|
||||||
return 'Done';
|
return 'Saved';
|
||||||
}
|
}
|
||||||
}, ...(options?.tools || [])],
|
}, ...(options?.tools || [])],
|
||||||
});
|
});
|
||||||
if(!done) reject(`AI failed to create summary: ${resp}`);
|
if(!done) reject(`AI failed to create JSON:\n${resp}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -392,7 +392,7 @@ class LLM {
|
|||||||
* @returns {Promise<string>} Summary
|
* @returns {Promise<string>} Summary
|
||||||
*/
|
*/
|
||||||
async summarize(text: string, tokens: number = 500, options?: LLMRequest): Promise<string | null> {
|
async summarize(text: string, tokens: number = 500, options?: LLMRequest): Promise<string | null> {
|
||||||
let system = `Your job is to summarize the users message. Call the \`submit\` tool exactly once with the shortest summary possible that's <= ${tokens} tokens. Output nothing else`;
|
let system = `Your job is to summarize the users message using tool calls. Call the \`submit\` tool at least once with the shortest summary possible that's <= ${tokens} tokens. The tool call will respond with the token count. Responses are ignored`;
|
||||||
if(options?.system) system += '\n\n' + options.system;
|
if(options?.system) system += '\n\n' + options.system;
|
||||||
return new Promise(async (resolve, reject) => {
|
return new Promise(async (resolve, reject) => {
|
||||||
let done = false;
|
let done = false;
|
||||||
@@ -405,13 +405,16 @@ class LLM {
|
|||||||
description: 'Submit summary',
|
description: 'Submit summary',
|
||||||
args: {summary: {type: 'string', description: 'Text summarization', required: true}},
|
args: {summary: {type: 'string', description: 'Text summarization', required: true}},
|
||||||
fn: (args) => {
|
fn: (args) => {
|
||||||
|
if(!args.summary) return 'No summary provided';
|
||||||
|
const count = this.estimateTokens(args.summary);
|
||||||
|
if(count > tokens) return `Summary is too long (${count} tokens)`;
|
||||||
done = true;
|
done = true;
|
||||||
resolve(args.summary || null);
|
resolve(args.summary || null);
|
||||||
return 'Done';
|
return `Saved (${count} tokens)`;
|
||||||
}
|
}
|
||||||
}, ...(options?.tools || [])],
|
}, ...(options?.tools || [])],
|
||||||
});
|
});
|
||||||
if(!done) reject(`AI failed to create summary: ${resp}`);
|
if(!done) reject(`AI failed to create summary:\n${resp}`);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user