| 27 | let embedCounter = 0; |
| 28 | |
| 29 | function mockEmbedding() { |
| 30 | embedCounter = 0; |
| 31 | const original = Ollama.prototype.embed; |
| 32 | Ollama.prototype.embed = async function ({ input }) { |
| 33 | const batch = Array.isArray(input) ? input : [input]; |
| 34 | return { |
| 35 | embeddings: batch.map((text) => { |
| 36 | embedCounter++; |
| 37 | const vec = new Array(64).fill(0); |
| 38 | for (let i = 0; i < Math.min(text.length, 64); i++) { |
| 39 | vec[i] = (text.charCodeAt(i) % 100) / 100; |
| 40 | } |
| 41 | const norm = Math.sqrt(vec.reduce((s, v) => s + v * v, 0)); |
| 42 | return norm > 0 ? vec.map((v) => v / norm) : vec; |
| 43 | }), |
| 44 | }; |
| 45 | }; |
| 46 | return () => { Ollama.prototype.embed = original; }; |
| 47 | } |
| 48 | |
| 49 | before(async () => { |
| 50 | await rm(FIXTURE, { recursive: true, force: true }); |