Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| eda4eed87d | |||
| 7f88c2d1d0 | |||
| 5eae84f6cf |
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/ai-utils",
|
||||
"version": "0.8.11",
|
||||
"version": "0.8.14",
|
||||
"description": "AI Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
||||
57
src/llm.ts
57
src/llm.ts
@@ -1,3 +1,4 @@
|
||||
import {sum} from '@tensorflow/tfjs';
|
||||
import {JSONAttemptParse} from '@ztimson/utils';
|
||||
import {AbortablePromise, Ai} from './ai.ts';
|
||||
import {Anthropic} from './antrhopic.ts';
|
||||
@@ -357,11 +358,30 @@ class LLM {
|
||||
* @returns {Promise<{} | {} | RegExpExecArray | null>}
|
||||
*/
|
||||
async json(text: string, schema: string, options?: LLMRequest): Promise<any> {
|
||||
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;
|
||||
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;
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let done = false;
|
||||
const resp = await this.ask(text, {
|
||||
temperature: 0.3,
|
||||
...options,
|
||||
system,
|
||||
tools: [{
|
||||
name: 'submit',
|
||||
description: 'Submit JSON',
|
||||
args: {json: {type: 'string', description: 'Javascript parsable JSON string', required: true}},
|
||||
fn: (args) => {
|
||||
try {
|
||||
const json = JSON.parse(args.json);
|
||||
resolve(json);
|
||||
done = true;
|
||||
} catch { return 'Invalid JSON'; }
|
||||
return 'Saved';
|
||||
}
|
||||
}, ...(options?.tools || [])],
|
||||
});
|
||||
if(!done) reject(`AI failed to create JSON:\n${resp}`);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -371,8 +391,31 @@ class LLM {
|
||||
* @param options LLM request options
|
||||
* @returns {Promise<string>} Summary
|
||||
*/
|
||||
summarize(text: string, tokens: number = 500, options?: LLMRequest): Promise<string | null> {
|
||||
return this.ask(text, {system: `Generate the shortest summary possible <= ${tokens} tokens. Output nothing else`, temperature: 0.3, ...options});
|
||||
async summarize(text: string, tokens: number = 500, options?: LLMRequest): Promise<string | null> {
|
||||
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;
|
||||
return new Promise(async (resolve, reject) => {
|
||||
let done = false;
|
||||
const resp = await this.ask(text, {
|
||||
temperature: 0.3,
|
||||
...options,
|
||||
system,
|
||||
tools: [{
|
||||
name: 'submit',
|
||||
description: 'Submit summary',
|
||||
args: {summary: {type: 'string', description: 'Text summarization', required: true}},
|
||||
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;
|
||||
resolve(args.summary || null);
|
||||
return `Saved (${count} tokens)`;
|
||||
}
|
||||
}, ...(options?.tools || [])],
|
||||
});
|
||||
if(!done) reject(`AI failed to create summary:\n${resp}`);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user