Private AI meeting notes that stay on your computer. No meeting bot. Free and open source for Mac, Windows and Linux. An alternative to Granola, Otter and Fireflies.
TypeScript ai-meeting-assistantai-notetakerelectrongranola-alternativelinuxllama-cpplocal-ailocal-firstmacosmcpmeeting-minutesmeeting-notesoffline-firstopen-sourceprivacyspeech-to-texttranscriptionwhisperwhisper-cppwindows
notes: when they don't fit next to the model on a small graphics card, load it again with fewer layers there, then none (#18)
christianmat6d094bb241de
diff --git a/app/src/engine/llm.ts b/app/src/engine/llm.tsindex 2de83405..81fe29bc 100644--- a/app/src/engine/llm.ts+++ b/app/src/engine/llm.ts@@ -1,4 +1,4 @@-import { getLlama, Gemma4ChatWrapper, LlamaChatSession, type ChatHistoryItem, type Llama, type LlamaContext, type LlamaContextSequence, type LlamaEmbeddingContext, type LlamaModel } from 'node-llama-cpp'+import { getLlama, Gemma4ChatWrapper, InsufficientMemoryError, LlamaChatSession, type ChatHistoryItem, type Llama, type LlamaContext, type LlamaContextSequence, type LlamaEmbeddingContext, type LlamaModel } from 'node-llama-cpp'import { cleanAnswer, streamCleaner } from './control'import { samplingFor } from './sampling'import { holds } from './prefix'@@ -63,15 +63,26 @@ export function createLlm() {return out},/** gpu: what it runs on (cuda, vulkan, metal, or false: the processor alone), for the log */- async load(modelPath: string, contextSize: number, minContext: number): Promise<{ contextSize: number; gpu: string | false }> {+ async load(modelPath: string, contextSize: number, minContext: number): Promise<{ contextSize: number; gpu: string | false; gpuLayers: number }> {await unload()const l = await getL()- model = await l.loadModel({ modelPath })- context = await model.createContext({ contextSize: { min: Math.min(minContext, contextSize), max: contextSize }, ...threads(l) })+ const size = { min: Math.min(minContext, contextSize), max: contextSize }+ // The model fills the graphics card first and keeps room for only a tiny context; on a card with little memory the+ // notes' context then doesn't fit (InsufficientMemoryError, every summary failing). So: load again leaving room for+ // the smallest context we accept, then with nothing on the card. A computer where it fits loads once, as before.+ for (const gpuLayers of [undefined, { fitContext: { contextSize: size.min } }, 0] as const) {+ model = await l.loadModel(gpuLayers === undefined ? { modelPath } : { modelPath, gpuLayers })+ try { context = await model.createContext({ contextSize: size, ...threads(l) }); break } catch (e) {+ if (!(e instanceof InsufficientMemoryError) || gpuLayers === 0 || model.gpuLayers === 0) throw e+ console.warn('the notes did not fit next to the model on the graphics card; loading it again with fewer layers there', e.message)+ await model.dispose(); model = undefined+ }+ }+ if (!model || !context) throw new Error('The notes model did not load')main = context.getSequence()// the first evaluation pages the weights in from disk: pay it here, not on the first word of the notesawait main.evaluateWithoutGeneratingNewTokens(model.tokenize('Hello'))- return { contextSize: context.contextSize, gpu: l.gpu }+ return { contextSize: context.contextSize, gpu: model.gpuLayers === 0 ? false : l.gpu, gpuLayers: model.gpuLayers }},count(texts: string[]): number[] {if (!model) throw new Error('No model loaded')diff --git a/app/src/main/engine.ts b/app/src/main/engine.tsindex 82c421a0..7abbbfbf 100644--- a/app/src/main/engine.ts+++ b/app/src/main/engine.ts@@ -129,9 +129,9 @@ export class UtilityEngine implements Engine {}load(modelPath: string, contextSize: number, minContext: number) {console.info('NOTES', `loading the notes model ${modelPath.split(/[\\/]/).pop()}${this.opts.gpuFallback ? (this.cpuOnly ? ' without the graphics card' : ' (graphics card allowed)') : ''}`)- const { id, done } = this.call<{ contextSize: number; gpu?: string | false }>({ type: 'load', modelPath, contextSize, minContext })+ const { id, done } = this.call<{ contextSize: number; gpu?: string | false; gpuLayers?: number }>({ type: 'load', modelPath, contextSize, minContext })this.watch(id, 'loading the model', this.limits.loadMs, this.limits.cpuLoadMs)- return done.then((r) => { console.info('NOTES', `notes model loaded: context ${r.contextSize}, graphics ${r.gpu === undefined ? 'unknown' : r.gpu || 'none'}`); return r })+ return done.then((r) => { console.info('NOTES', `notes model loaded: context ${r.contextSize}, graphics ${r.gpu === undefined ? 'unknown' : r.gpu || 'none'}${r.gpu && r.gpuLayers !== undefined ? ` (${r.gpuLayers} layers)` : ''}`); return r })}async unload() { if (this.proc) await this.call({ type: 'unload' }).done }count(texts: string[]) { return this.call<number[]>({ type: 'count', texts }).done }diff --git a/app/test/llm.test.ts b/app/test/llm.test.tsindex 44c4500e..dfc42df6 100644--- a/app/test/llm.test.ts+++ b/app/test/llm.test.ts@@ -2,17 +2,23 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'import { Gemma4ChatWrapper, type LlamaChatSessionOptions } from 'node-llama-cpp'import { createLlm } from '../src/engine/llm'-const fake = vi.hoisted(() => ({ architecture: 'gemma4', sessions: [] as unknown[] }))+const fake = vi.hoisted(() => ({ architecture: 'gemma4', sessions: [] as unknown[], loads: [] as unknown[], disposed: 0, contextFails: 0, contextError: undefined as (() => Error) | undefined }))vi.mock('node-llama-cpp', async (original) => {const actual = await original<typeof import('node-llama-cpp')>()const sequence = { contextTokens: [], tokenMeter: { usedInputTokens: 0 }, evaluateWithoutGeneratingNewTokens: async () => {} }return {...actual,- getLlama: async () => ({ loadModel: async () => ({- fileInfo: { metadata: { general: { architecture: fake.architecture } } },- tokenize: () => [], dispose: async () => {},- createContext: async () => ({ contextSize: 8192, getSequence: () => sequence, dispose: async () => {} }),- }) }),+ getLlama: async () => ({ gpu: 'vulkan', loadModel: async (o: { gpuLayers?: unknown }) => {+ fake.loads.push(o.gpuLayers)+ return {+ fileInfo: { metadata: { general: { architecture: fake.architecture } } }, gpuLayers: o.gpuLayers === 0 ? 0 : 20,+ tokenize: () => [], dispose: async () => { fake.disposed++ },+ createContext: async () => {+ if (fake.contextFails > 0) { fake.contextFails--; throw fake.contextError?.() ?? new actual.InsufficientMemoryError('A context size of 8192 is too large for the available VRAM') }+ return { contextSize: 8192, getSequence: () => sequence, dispose: async () => {} }+ },+ }+ } }),LlamaChatSession: class {constructor(options: LlamaChatSessionOptions) { fake.sessions.push(options) }setChatHistory() {}@@ -23,7 +29,7 @@ vi.mock('node-llama-cpp', async (original) => {}})-beforeEach(() => { fake.sessions.length = 0 })+beforeEach(() => { fake.sessions.length = 0; fake.loads.length = 0; fake.disposed = 0; fake.contextFails = 0; fake.contextError = undefined })describe('model prompt wrapper', () => {it.each(['gemma4', 'qwen3', 'gpt-oss'])('preload and answers use the same format for %s', async (architecture) => {@@ -47,3 +53,33 @@ describe('model prompt wrapper', () => {await llm.unload()})})++describe('too little room on the graphics card for the notes', () => {+ it('loads once, as before, when the context fits', async () => {+ const llm = createLlm()+ expect(await llm.load('fixture.gguf', 32768, 8192)).toMatchObject({ gpu: 'vulkan', gpuLayers: 20 })+ expect(fake.loads).toEqual([undefined])+ })+ it('loads again with room left for the notes on the card, when the context did not fit', async () => {+ fake.contextFails = 1+ const llm = createLlm()+ await llm.load('fixture.gguf', 32768, 8192)+ expect(fake.loads).toEqual([undefined, { fitContext: { contextSize: 8192 } }])+ expect(fake.disposed).toBe(1)+ })+ it('runs on the processor alone when even that does not fit', async () => {+ fake.contextFails = 2+ const llm = createLlm()+ expect(await llm.load('fixture.gguf', 32768, 8192)).toMatchObject({ gpu: false, gpuLayers: 0 })+ expect(fake.loads).toEqual([undefined, { fitContext: { contextSize: 8192 } }, 0])+ })+ it('gives up when the processor alone does not fit either', async () => {+ fake.contextFails = 3+ await expect(createLlm().load('fixture.gguf', 32768, 8192)).rejects.toThrow(/too large/)+ })+ it('another error is not retried', async () => {+ fake.contextFails = 1; fake.contextError = () => new Error('bad file')+ await expect(createLlm().load('fixture.gguf', 32768, 8192)).rejects.toThrow('bad file')+ expect(fake.loads).toEqual([undefined])+ })+})