Updated LLM config and added read_webpage
All checks were successful
Publish Library / Build NPM Project (push) Successful in 46s
Publish Library / Tag Version (push) Successful in 6s

This commit is contained in:
2026-02-01 13:16:08 -05:00
parent 28904cddbe
commit 7b57a0ded1
11 changed files with 840 additions and 384 deletions

View File

@@ -1,7 +1,6 @@
import {JSONAttemptParse} from '@ztimson/utils';
import {AbortablePromise, Ai} from './ai.ts';
import {Anthropic} from './antrhopic.ts';
import {Ollama} from './ollama.ts';
import {OpenAi} from './open-ai.ts';
import {LLMProvider} from './provider.ts';
import {AiTool} from './tools.ts';
@@ -9,6 +8,10 @@ import {Worker} from 'worker_threads';
import {fileURLToPath} from 'url';
import {dirname, join} from 'path';
export type AnthropicConfig = {proto: 'anthropic', token: string};
export type OllamaConfig = {proto: 'ollama', host: string};
export type OpenAiConfig = {proto: 'openai', host?: string, token: string};
export type LLMMessage = {
/** Message originator */
role: 'assistant' | 'system' | 'user';
@@ -33,32 +36,6 @@ export type LLMMessage = {
timestamp?: number;
}
export type LLMOptions = {
/** Anthropic settings */
anthropic?: {
/** API Token */
token: string;
/** Default model */
model: string;
},
/** Ollama settings */
ollama?: {
/** connection URL */
host: string;
/** Default model */
model: string;
},
/** Open AI settings */
openAi?: {
/** API Token */
token: string;
/** Default model */
model: string;
},
/** Default provider & model */
model: string | [string, string];
} & Omit<LLMRequest, 'model'>;
export type LLMRequest = {
/** System prompt */
system?: string;
@@ -71,7 +48,7 @@ export type LLMRequest = {
/** Available tools */
tools?: AiTool[];
/** LLM model */
model?: string | [string, string];
model?: string;
/** Stream response */
stream?: (chunk: {text?: string, tool?: string, done?: true}) => any;
/** Compress old messages in the chat to free up context */
@@ -87,8 +64,8 @@ export class LLM {
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} = {};
private models: {[model: string]: LLMProvider} = {};
private defaultModel!: string;
constructor(public readonly ai: Ai) {
this.embedWorker = new Worker(join(dirname(fileURLToPath(import.meta.url)), 'embedder.js'));
@@ -100,9 +77,13 @@ export class LLM {
}
});
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);
if(!ai.options.llm?.models) return;
Object.entries(ai.options.llm.models).forEach(([model, config]) => {
if(!this.defaultModel) this.defaultModel = model;
if(config.proto == 'anthropic') this.models[model] = new Anthropic(this.ai, config.token, model);
else if(config.proto == 'ollama') this.models[model] = new OpenAi(this.ai, config.host, 'not-needed', model);
else if(config.proto == 'openai') this.models[model] = new OpenAi(this.ai, config.host || null, config.token, model);
});
}
/**
@@ -112,17 +93,9 @@ export class LLM {
* @returns {{abort: () => void, response: Promise<LLMMessage[]>}} Function to abort response and chat history
*/
ask(message: string, options: LLMRequest = {}): AbortablePromise<LLMMessage[]> {
let model: any = [null, null];
if(options.model) {
if(typeof options.model == 'object') model = options.model;
else model = [options.model, (<any>this.ai.options)[options.model]?.model];
}
if(!options.model || model[1] == null) {
if(typeof this.ai.options.model == 'object') model = this.ai.options.model;
else model = [this.ai.options.model, (<any>this.ai.options)[this.ai.options.model]?.model];
}
if(!model[0] || !model[1]) throw new Error(`Unknown LLM provider or model: ${model[0]} / ${model[1]}`);
return this.providers[model[0]].ask(message, {...options, model: model[1]});
const m = options.model || this.defaultModel;
if(!this.models[m]) throw new Error(`Model does not exist: ${m}`);
return this.models[m].ask(message, options);
}
/**