3 Commits
0.2.0 ... 0.2.2

Author SHA1 Message Date
ca5a2334bb bump 2.2.0
All checks were successful
Publish Library / Build NPM Project (push) Successful in 43s
Publish Library / Tag Version (push) Successful in 11s
2025-12-22 11:02:53 -05:00
3cd7b12f5f Configure model path for all libraries
Some checks failed
Publish Library / Tag Version (push) Has been cancelled
Publish Library / Build NPM Project (push) Has been cancelled
2025-12-22 11:02:24 -05:00
bb6933f0d5 Optimized cosineSimilarity
All checks were successful
Publish Library / Build NPM Project (push) Successful in 42s
Publish Library / Tag Version (push) Successful in 7s
2025-12-19 15:22:06 -05:00
5 changed files with 19 additions and 18 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@ztimson/ai-utils",
"version": "0.2.0",
"version": "0.2.2",
"description": "AI Utility library",
"author": "Zak Timson",
"license": "MIT",

View File

@@ -8,9 +8,9 @@ export type AiOptions = LLMOptions & {
binary: string;
/** Model: `ggml-base.en.bin` */
model: string;
/** Path to models */
path: string;
}
/** Path to models */
path: string;
}
export class Ai {
@@ -25,6 +25,7 @@ export class Ai {
vision!: Vision;
constructor(public readonly options: AiOptions) {
process.env.TRANSFORMERS_CACHE = options.path;
this.audio = new Audio(this);
this.language = new LLM(this);
this.vision = new Vision(this);

View File

@@ -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.whisper.path, model);
const p = Path.join(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}`)

View File

@@ -136,6 +136,18 @@ export class LLM {
return [{role: 'assistant', content: `Conversation Summary: ${summary}`, timestamp: Date.now()}, ...recent];
}
cosineSimilarity(v1: number[], v2: number[]): number {
if (v1.length !== v2.length) throw new Error('Vectors must be same length');
let dotProduct = 0, normA = 0, normB = 0;
for (let i = 0; i < v1.length; i++) {
dotProduct += v1[i] * v2[i];
normA += v1[i] * v1[i];
normB += v2[i] * v2[i];
}
const denominator = Math.sqrt(normA) * Math.sqrt(normB);
return denominator === 0 ? 0 : dotProduct / denominator;
}
embedding(target: object | string, maxTokens = 500, overlapTokens = 50) {
const objString = (obj: any, path = ''): string[] => {
if(obj === null || obj === undefined) return [];
@@ -205,24 +217,12 @@ export class LLM {
*/
fuzzyMatch(target: string, ...searchTerms: string[]) {
if(searchTerms.length < 2) throw new Error('Requires at least 2 strings to compare');
const vector = (text: string, dimensions: number = 10): number[] => {
return text.toLowerCase().split('').map((char, index) =>
(char.charCodeAt(0) * (index + 1)) % dimensions / dimensions).slice(0, dimensions);
}
const cosineSimilarity = (v1: number[], v2: number[]): number => {
if (v1.length !== v2.length) throw new Error('Vectors must be same length');
const tensor1 = tf.tensor1d(v1), tensor2 = tf.tensor1d(v2)
const dotProduct = tf.dot(tensor1, tensor2)
const magnitude1 = tf.norm(tensor1)
const magnitude2 = tf.norm(tensor2)
if(magnitude1.dataSync()[0] === 0 || magnitude2.dataSync()[0] === 0) return 0
return dotProduct.dataSync()[0] / (magnitude1.dataSync()[0] * magnitude2.dataSync()[0])
}
const v = vector(target);
const similarities = searchTerms.map(t => vector(t)).map(refVector => cosineSimilarity(v, refVector))
const similarities = searchTerms.map(t => vector(t)).map(refVector => this.cosineSimilarity(v, refVector))
return {avg: similarities.reduce((acc, s) => acc + s, 0) / similarities.length, max: Math.max(...similarities), similarities}
}

View File

@@ -15,7 +15,7 @@ export class Vision {
return {
abort: () => { worker?.terminate(); },
response: new Promise(async res => {
worker = await createWorker('eng');
worker = await createWorker('eng', 1, {langPath: this.ai.options.path});
const {data} = await worker.recognize(path);
await worker.terminate();
res(data.text.trim() || null);