Compare commits
4 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a5ed4076b7 | |||
| 0112c92505 | |||
| 2351f590b5 | |||
| 2c2acef84e |
@@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@ztimson/ai-utils",
|
"name": "@ztimson/ai-utils",
|
||||||
"version": "0.1.12",
|
"version": "0.1.16",
|
||||||
"description": "AI Utility library",
|
"description": "AI Utility library",
|
||||||
"author": "Zak Timson",
|
"author": "Zak Timson",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|||||||
27
src/ai.ts
27
src/ai.ts
@@ -1,9 +1,9 @@
|
|||||||
import {$} from '@ztimson/node-utils';
|
|
||||||
import {createWorker} from 'tesseract.js';
|
import {createWorker} from 'tesseract.js';
|
||||||
import {LLM, LLMOptions} from './llm';
|
import {LLM, LLMOptions} from './llm';
|
||||||
import fs from 'node:fs/promises';
|
import fs from 'node:fs/promises';
|
||||||
import Path from 'node:path';
|
import Path from 'node:path';
|
||||||
import * as tf from '@tensorflow/tfjs';
|
import * as tf from '@tensorflow/tfjs';
|
||||||
|
import {spawn} from 'node:child_process';
|
||||||
|
|
||||||
export type AiOptions = LLMOptions & {
|
export type AiOptions = LLMOptions & {
|
||||||
whisper?: {
|
whisper?: {
|
||||||
@@ -13,8 +13,6 @@ export type AiOptions = LLMOptions & {
|
|||||||
model: string;
|
model: string;
|
||||||
/** Path to models */
|
/** Path to models */
|
||||||
path: string;
|
path: string;
|
||||||
/** Path to storage location for temporary files */
|
|
||||||
temp?: string;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -39,14 +37,23 @@ export class Ai {
|
|||||||
* @param model Whisper model
|
* @param model Whisper model
|
||||||
* @returns {Promise<any>} Extracted text
|
* @returns {Promise<any>} Extracted text
|
||||||
*/
|
*/
|
||||||
async asr(path: string, model: string = this.whisperModel): Promise<string | null> {
|
asr(path: string, model: string = this.whisperModel): {abort: () => void, response: Promise<string | null>} {
|
||||||
if(!this.options.whisper?.binary) throw new Error('Whisper not configured');
|
if(!this.options.whisper?.binary) throw new Error('Whisper not configured');
|
||||||
const m = await this.downloadAsrModel(model);
|
let abort: any = () => {};
|
||||||
const name = Math.random().toString(36).substring(2, 10) + '-' + path.split('/').pop() + '.txt';
|
const response = new Promise<string | null>((resolve, reject) => {
|
||||||
const output = Path.join(this.options.whisper.temp || '/tmp', name);
|
this.downloadAsrModel(model).then(m => {
|
||||||
await $`rm -f ${output} && ${this.options.whisper.binary} -nt -np -m ${m} -f ${path} -otxt -of ${output}`;
|
let output = '';
|
||||||
return fs.readFile(output, 'utf-8').then(text => text?.trim() || null)
|
const proc = spawn(<string>this.options.whisper?.binary, ['-nt', '-np', '-m', m, '-f', path], {stdio: ['ignore', 'pipe', 'ignore']});
|
||||||
.finally(() => fs.rm(output, {force: true}).catch(() => {}));
|
abort = () => proc.kill('SIGTERM');
|
||||||
|
proc.on('error', (err: Error) => reject(err));
|
||||||
|
proc.stdout.on('data', (data: Buffer) => output += data.toString());
|
||||||
|
proc.on('close', (code: number) => {
|
||||||
|
if(code === 0) resolve(output.trim() || null);
|
||||||
|
else reject(new Error(`Exit code ${code}`));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
return {response, abort};
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -13,24 +13,29 @@ export class Anthropic extends LLMProvider {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private toStandard(history: any[]): LLMMessage[] {
|
private toStandard(history: any[]): LLMMessage[] {
|
||||||
|
const merged: any[] = [];
|
||||||
for(let i = 0; i < history.length; i++) {
|
for(let i = 0; i < history.length; i++) {
|
||||||
const orgI = i;
|
const msg = history[i];
|
||||||
if(typeof history[orgI].content != 'string') {
|
if(typeof msg.content != 'string') {
|
||||||
if(history[orgI].role == 'assistant') {
|
if(msg.role == 'assistant') {
|
||||||
history[orgI].content.filter((c: any) => c.type =='tool_use').forEach((c: any) => {
|
msg.content.filter((c: any) => c.type == 'tool_use').forEach((c: any) => {
|
||||||
i++;
|
merged.push({role: 'tool', id: c.id, name: c.name, args: c.input});
|
||||||
history.splice(i, 0, {role: 'tool', id: c.id, name: c.name, args: c.input});
|
|
||||||
});
|
});
|
||||||
} else if(history[orgI].role == 'user') {
|
} else if(msg.role == 'user') {
|
||||||
history[orgI].content.filter((c: any) => c.type =='tool_result').forEach((c: any) => {
|
msg.content.filter((c: any) => c.type == 'tool_result').forEach((c: any) => {
|
||||||
const h = history.find((h: any) => h.id == c.tool_use_id);
|
const h = merged.find((h: any) => h.id == c.tool_use_id);
|
||||||
h[c.is_error ? 'error' : 'content'] = c.content;
|
if(h) h[c.is_error ? 'error' : 'content'] = c.content;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
history[orgI].content = history[orgI].content.filter((c: any) => c.type == 'text').map((c: any) => c.text).join('\n\n');
|
msg.content = msg.content.filter((c: any) => c.type == 'text').map((c: any) => c.text).join('\n\n');
|
||||||
|
}
|
||||||
|
if(msg.content) {
|
||||||
|
const last = merged.at(-1);
|
||||||
|
if(last && last.role == 'assistant' && msg.role == 'assistant') last.content += '\n\n' + msg.content;
|
||||||
|
else merged.push({role: msg.role, content: msg.content});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return history.filter(h => !!h.content);
|
return merged;
|
||||||
}
|
}
|
||||||
|
|
||||||
private fromStandard(history: LLMMessage[]): any[] {
|
private fromStandard(history: LLMMessage[]): any[] {
|
||||||
@@ -71,13 +76,15 @@ export class Anthropic extends LLMProvider {
|
|||||||
stream: !!options.stream,
|
stream: !!options.stream,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Run tool changes
|
|
||||||
let resp: any;
|
let resp: any;
|
||||||
|
let isFirstMessage = true;
|
||||||
do {
|
do {
|
||||||
resp = await this.client.messages.create(requestParams);
|
resp = await this.client.messages.create(requestParams);
|
||||||
|
|
||||||
// Streaming mode
|
|
||||||
if(options.stream) {
|
if(options.stream) {
|
||||||
|
if(!isFirstMessage) options.stream({text: '\n\n'});
|
||||||
|
isFirstMessage = false;
|
||||||
|
|
||||||
resp.content = [];
|
resp.content = [];
|
||||||
for await (const chunk of resp) {
|
for await (const chunk of resp) {
|
||||||
if(controller.signal.aborted) break;
|
if(controller.signal.aborted) break;
|
||||||
@@ -104,7 +111,6 @@ export class Anthropic extends LLMProvider {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run tools
|
|
||||||
const toolCalls = resp.content.filter((c: any) => c.type === 'tool_use');
|
const toolCalls = resp.content.filter((c: any) => c.type === 'tool_use');
|
||||||
if(toolCalls.length && !controller.signal.aborted) {
|
if(toolCalls.length && !controller.signal.aborted) {
|
||||||
history.push({role: 'assistant', content: resp.content});
|
history.push({role: 'assistant', content: resp.content});
|
||||||
@@ -122,12 +128,14 @@ export class Anthropic extends LLMProvider {
|
|||||||
requestParams.messages = history;
|
requestParams.messages = history;
|
||||||
}
|
}
|
||||||
} while (!controller.signal.aborted && resp.content.some((c: any) => c.type === 'tool_use'));
|
} while (!controller.signal.aborted && resp.content.some((c: any) => c.type === 'tool_use'));
|
||||||
|
|
||||||
if(options.stream) options.stream({done: true});
|
if(options.stream) options.stream({done: true});
|
||||||
res(this.toStandard([...history, {
|
res(this.toStandard([...history, {
|
||||||
role: 'assistant',
|
role: 'assistant',
|
||||||
content: resp.content.filter((c: any) => c.type == 'text').map((c: any) => c.text).join('\n\n')
|
content: resp.content.filter((c: any) => c.type == 'text').map((c: any) => c.text).join('\n\n')
|
||||||
}]));
|
}]));
|
||||||
});
|
});
|
||||||
|
|
||||||
return Object.assign(response, {abort: () => controller.abort()});
|
return Object.assign(response, {abort: () => controller.abort()});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user