Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cb60a0b0c5 | |||
| 1c59379c7d | |||
| 6dce0e8954 |
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@ztimson/ai-utils",
|
||||
"version": "0.2.3",
|
||||
"version": "0.2.6",
|
||||
"description": "AI Utility library",
|
||||
"author": "Zak Timson",
|
||||
"license": "MIT",
|
||||
|
||||
15
src/ai.ts
15
src/ai.ts
@@ -1,22 +1,26 @@
|
||||
import * as os from 'node:os';
|
||||
import {LLM, LLMOptions} from './llm';
|
||||
import { Audio } from './audio.ts';
|
||||
import {Vision} from './vision.ts';
|
||||
|
||||
export type AiOptions = LLMOptions & {
|
||||
/** Path to models */
|
||||
path?: string;
|
||||
/** Whisper ASR configuration */
|
||||
whisper?: {
|
||||
/** Whisper binary location */
|
||||
binary: string;
|
||||
/** Model: `ggml-base.en.bin` */
|
||||
model: string;
|
||||
}
|
||||
/** Path to models */
|
||||
path: string;
|
||||
/** Tesseract OCR configuration */
|
||||
tesseract?: {
|
||||
/** Model: eng, eng_best, eng_fast */
|
||||
model?: string;
|
||||
}
|
||||
}
|
||||
|
||||
export class Ai {
|
||||
private downloads: {[key: string]: Promise<string>} = {};
|
||||
private whisperModel!: string;
|
||||
|
||||
/** Audio processing AI */
|
||||
audio!: Audio;
|
||||
/** Language processing AI */
|
||||
@@ -25,6 +29,7 @@ export class Ai {
|
||||
vision!: Vision;
|
||||
|
||||
constructor(public readonly options: AiOptions) {
|
||||
if(!options.path) options.path = os.tmpdir();
|
||||
process.env.TRANSFORMERS_CACHE = options.path;
|
||||
this.audio = new Audio(this);
|
||||
this.language = new LLM(this);
|
||||
|
||||
@@ -54,12 +54,14 @@ export class Anthropic extends LLMProvider {
|
||||
let history = this.fromStandard([...options.history || [], {role: 'user', content: message, timestamp: Date.now()}]);
|
||||
const original = deepCopy(history);
|
||||
if(options.compress) history = await this.ai.language.compressHistory(<any>history, options.compress.max, options.compress.min, options);
|
||||
|
||||
const tools = options.tools || this.ai.options.tools || [];
|
||||
const requestParams: any = {
|
||||
model: options.model || this.model,
|
||||
max_tokens: options.max_tokens || this.ai.options.max_tokens || 4096,
|
||||
system: options.system || this.ai.options.system || '',
|
||||
temperature: options.temperature || this.ai.options.temperature || 0.7,
|
||||
tools: (options.tools || this.ai.options.tools || []).map(t => ({
|
||||
tools: tools.map(t => ({
|
||||
name: t.name,
|
||||
description: t.description,
|
||||
input_schema: {
|
||||
@@ -76,7 +78,10 @@ export class Anthropic extends LLMProvider {
|
||||
let resp: any, isFirstMessage = true;
|
||||
const assistantMessages: string[] = [];
|
||||
do {
|
||||
resp = await this.client.messages.create(requestParams);
|
||||
resp = await this.client.messages.create(requestParams).catch(err => {
|
||||
err.message += `\n\nMessages:\n${JSON.stringify(history, null, 2)}`;
|
||||
throw err;
|
||||
});
|
||||
|
||||
// Streaming mode
|
||||
if(options.stream) {
|
||||
@@ -114,7 +119,7 @@ export class Anthropic extends LLMProvider {
|
||||
history.push({role: 'assistant', content: resp.content});
|
||||
original.push({role: 'assistant', content: resp.content});
|
||||
const results = await Promise.all(toolCalls.map(async (toolCall: any) => {
|
||||
const tool = options.tools?.find(findByProp('name', toolCall.name));
|
||||
const tool = tools.find(findByProp('name', toolCall.name));
|
||||
if(!tool) return {tool_use_id: toolCall.id, is_error: true, content: 'Tool not found'};
|
||||
try {
|
||||
const result = await tool.fn(toolCall.input, this.ai);
|
||||
|
||||
@@ -48,7 +48,7 @@ export class Audio {
|
||||
async downloadAsrModel(model: string = this.whisperModel): Promise<string> {
|
||||
if(!this.ai.options.whisper?.binary) throw new Error('Whisper not configured');
|
||||
if(!model.endsWith('.bin')) model += '.bin';
|
||||
const p = Path.join(this.ai.options.path, model);
|
||||
const p = Path.join(<string>this.ai.options.path, model);
|
||||
if(await fs.stat(p).then(() => true).catch(() => false)) return p;
|
||||
if(!!this.downloads[model]) return this.downloads[model];
|
||||
this.downloads[model] = fetch(`https://huggingface.co/ggerganov/whisper.cpp/resolve/main/${model}`)
|
||||
|
||||
11
src/embedder.ts
Normal file
11
src/embedder.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { pipeline } from '@xenova/transformers';
|
||||
import { parentPort } from 'worker_threads';
|
||||
|
||||
let model: any;
|
||||
|
||||
parentPort?.on('message', async ({ id, text }) => {
|
||||
if(!model) model = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2');
|
||||
const output = await model(text, { pooling: 'mean', normalize: true });
|
||||
const embedding = Array.from(output.data);
|
||||
parentPort?.postMessage({ id, embedding });
|
||||
});
|
||||
32
src/llm.ts
32
src/llm.ts
@@ -1,4 +1,3 @@
|
||||
import {pipeline} from '@xenova/transformers';
|
||||
import {JSONAttemptParse} from '@ztimson/utils';
|
||||
import {Ai} from './ai.ts';
|
||||
import {Anthropic} from './antrhopic.ts';
|
||||
@@ -6,7 +5,9 @@ import {Ollama} from './ollama.ts';
|
||||
import {OpenAi} from './open-ai.ts';
|
||||
import {AbortablePromise, LLMProvider} from './provider.ts';
|
||||
import {AiTool} from './tools.ts';
|
||||
import * as tf from '@tensorflow/tfjs';
|
||||
import {Worker} from 'worker_threads';
|
||||
import {fileURLToPath} from 'url';
|
||||
import {dirname, join} from 'path';
|
||||
|
||||
export type LLMMessage = {
|
||||
/** Message originator */
|
||||
@@ -83,11 +84,22 @@ export type LLMRequest = {
|
||||
}
|
||||
|
||||
export class LLM {
|
||||
private embedModel: any;
|
||||
private embedWorker: Worker | null = null;
|
||||
private embedQueue = new Map<number, { resolve: (value: number[]) => void; reject: (error: any) => void }>();
|
||||
private embedId = 0;
|
||||
private providers: {[key: string]: LLMProvider} = {};
|
||||
|
||||
|
||||
constructor(public readonly ai: Ai) {
|
||||
this.embedModel = pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2');
|
||||
this.embedWorker = new Worker(join(dirname(fileURLToPath(import.meta.url)), 'embedder.js'));
|
||||
this.embedWorker.on('message', ({ id, embedding }) => {
|
||||
const pending = this.embedQueue.get(id);
|
||||
if (pending) {
|
||||
pending.resolve(embedding);
|
||||
this.embedQueue.delete(id);
|
||||
}
|
||||
});
|
||||
|
||||
if(ai.options.anthropic?.token) this.providers.anthropic = new Anthropic(this.ai, ai.options.anthropic.token, ai.options.anthropic.model);
|
||||
if(ai.options.ollama?.host) this.providers.ollama = new Ollama(this.ai, ai.options.ollama.host, ai.options.ollama.model);
|
||||
if(ai.options.openAi?.token) this.providers.openAi = new OpenAi(this.ai, ai.options.openAi.token, ai.options.openAi.model);
|
||||
@@ -159,10 +171,12 @@ export class LLM {
|
||||
});
|
||||
};
|
||||
|
||||
const embed = async (text: string): Promise<number[]> => {
|
||||
const model = await this.embedModel;
|
||||
const output = await model(text, {pooling: 'mean', normalize: true});
|
||||
return Array.from(output.data);
|
||||
const embed = (text: string): Promise<number[]> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const id = this.embedId++;
|
||||
this.embedQueue.set(id, { resolve, reject });
|
||||
this.embedWorker?.postMessage({ id, text });
|
||||
});
|
||||
};
|
||||
|
||||
// Tokenize
|
||||
@@ -188,7 +202,7 @@ export class LLM {
|
||||
const cleanText = text.replace(/\s*\n\s*/g, '\n').trim();
|
||||
if(cleanText) chunks.push(cleanText);
|
||||
start = end - overlapTokens;
|
||||
if (start <= end - tokens.length + end) start = end; // Safety: prevent infinite loop
|
||||
if(start <= end - tokens.length + end) start = end;
|
||||
}
|
||||
|
||||
return Promise.all(chunks.map(async (text, index) => ({
|
||||
|
||||
@@ -49,6 +49,7 @@ export class Ollama extends LLMProvider {
|
||||
if(options.compress) history = await this.ai.language.compressHistory(<any>history, options.compress.max, options.compress.min);
|
||||
if(options.system) history.unshift({role: 'system', content: system})
|
||||
|
||||
const tools = options.tools || this.ai.options.tools || [];
|
||||
const requestParams: any = {
|
||||
model: options.model || this.model,
|
||||
messages: history,
|
||||
@@ -58,7 +59,7 @@ export class Ollama extends LLMProvider {
|
||||
temperature: options.temperature || this.ai.options.temperature || 0.7,
|
||||
num_predict: options.max_tokens || this.ai.options.max_tokens || 4096,
|
||||
},
|
||||
tools: (options.tools || this.ai.options.tools || []).map(t => ({
|
||||
tools: tools.map(t => ({
|
||||
type: 'function',
|
||||
function: {
|
||||
name: t.name,
|
||||
@@ -74,7 +75,11 @@ export class Ollama extends LLMProvider {
|
||||
|
||||
let resp: any, isFirstMessage = true;
|
||||
do {
|
||||
resp = await this.client.chat(requestParams);
|
||||
resp = await this.client.chat(requestParams).catch(err => {
|
||||
err.message += `\n\nMessages:\n${JSON.stringify(history, null, 2)}`;
|
||||
throw err;
|
||||
});
|
||||
|
||||
if(options.stream) {
|
||||
if(!isFirstMessage) options.stream({text: '\n\n'});
|
||||
else isFirstMessage = false;
|
||||
@@ -93,7 +98,7 @@ export class Ollama extends LLMProvider {
|
||||
if(resp.message?.tool_calls?.length && !controller.signal.aborted) {
|
||||
history.push(resp.message);
|
||||
const results = await Promise.all(resp.message.tool_calls.map(async (toolCall: any) => {
|
||||
const tool = (options.tools || this.ai.options.tools)?.find(findByProp('name', toolCall.function.name));
|
||||
const tool = tools.find(findByProp('name', toolCall.function.name));
|
||||
if(!tool) return {role: 'tool', tool_name: toolCall.function.name, content: '{"error": "Tool not found"}'};
|
||||
const args = typeof toolCall.function.arguments === 'string' ? JSONAttemptParse(toolCall.function.arguments, {}) : toolCall.function.arguments;
|
||||
try {
|
||||
|
||||
@@ -67,13 +67,14 @@ export class OpenAi extends LLMProvider {
|
||||
let history = this.fromStandard([...options.history || [], {role: 'user', content: message, timestamp: Date.now()}]);
|
||||
if(options.compress) history = await this.ai.language.compressHistory(<any>history, options.compress.max, options.compress.min, options);
|
||||
|
||||
const tools = options.tools || this.ai.options.tools || [];
|
||||
const requestParams: any = {
|
||||
model: options.model || this.model,
|
||||
messages: history,
|
||||
stream: !!options.stream,
|
||||
max_tokens: options.max_tokens || this.ai.options.max_tokens || 4096,
|
||||
temperature: options.temperature || this.ai.options.temperature || 0.7,
|
||||
tools: (options.tools || this.ai.options.tools || []).map(t => ({
|
||||
tools: tools.map(t => ({
|
||||
type: 'function',
|
||||
function: {
|
||||
name: t.name,
|
||||
@@ -89,7 +90,11 @@ export class OpenAi extends LLMProvider {
|
||||
|
||||
let resp: any, isFirstMessage = true;
|
||||
do {
|
||||
resp = await this.client.chat.completions.create(requestParams);
|
||||
resp = await this.client.chat.completions.create(requestParams).catch(err => {
|
||||
err.message += `\n\nMessages:\n${JSON.stringify(history, null, 2)}`;
|
||||
throw err;
|
||||
});
|
||||
|
||||
if(options.stream) {
|
||||
if(!isFirstMessage) options.stream({text: '\n\n'});
|
||||
else isFirstMessage = false;
|
||||
@@ -110,7 +115,7 @@ export class OpenAi extends LLMProvider {
|
||||
if(toolCalls.length && !controller.signal.aborted) {
|
||||
history.push(resp.choices[0].message);
|
||||
const results = await Promise.all(toolCalls.map(async (toolCall: any) => {
|
||||
const tool = options.tools?.find(findByProp('name', toolCall.function.name));
|
||||
const tool = tools?.find(findByProp('name', toolCall.function.name));
|
||||
if(!tool) return {role: 'tool', tool_call_id: toolCall.id, content: '{"error": "Tool not found"}'};
|
||||
try {
|
||||
const args = JSONAttemptParse(toolCall.function.arguments, {});
|
||||
|
||||
@@ -15,7 +15,7 @@ export class Vision {
|
||||
return {
|
||||
abort: () => { worker?.terminate(); },
|
||||
response: new Promise(async res => {
|
||||
worker = await createWorker('eng', 1, {cachePath: this.ai.options.path});
|
||||
worker = await createWorker(this.ai.options.tesseract?.model || 'eng', 2, {cachePath: this.ai.options.path});
|
||||
const {data} = await worker.recognize(path);
|
||||
await worker.terminate();
|
||||
res(data.text.trim() || null);
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
import {defineConfig} from 'vite';
|
||||
import dts from 'vite-plugin-dts';
|
||||
import {resolve} from 'path';
|
||||
|
||||
export default defineConfig({
|
||||
build: {
|
||||
lib: {
|
||||
entry: './src/index.ts',
|
||||
entry: {
|
||||
index: './src/index.ts',
|
||||
embedder: './src/embedder.ts',
|
||||
},
|
||||
name: 'utils',
|
||||
fileName: (format) => (format === 'es' ? 'index.mjs' : 'index.js'),
|
||||
fileName: (format, entryName) => {
|
||||
if (entryName === 'embedder') return 'embedder.js';
|
||||
return format === 'es' ? 'index.mjs' : 'index.js';
|
||||
},
|
||||
},
|
||||
ssr: true,
|
||||
emptyOutDir: true,
|
||||
|
||||
Reference in New Issue
Block a user