import { expect, test } from 'vitest'; import { FlagEmbedding, EmbeddingModel } from './fastembed.js'; test('BGEBaseENV15: init', async () => { const model = await FlagEmbedding.init({ model: EmbeddingModel.BGEBaseENV15, }); expect(model).toBeDefined(); }, 60_000); test('BGEBaseENV15: embed single', async () => { const flagEmbedding = await FlagEmbedding.init({ model: EmbeddingModel.BGEBaseENV15, maxLength: 512, }); const embeddings = (await flagEmbedding.embed(['This is a test']).next()).value!; expect(embeddings).toBeDefined(); expect(embeddings.length).toBe(1); }, 60_000); test('BGEBaseENV15: embed batch', async () => { const flagEmbedding = await FlagEmbedding.init({ model: EmbeddingModel.BGEBaseENV15, maxLength: 512, }); const embeddingsBatch = flagEmbedding.embed([ 'This is a test', 'Some text', 'Some more test', 'This is a test', 'Some text', 'Some more test', ]); for await (const embeddings of embeddingsBatch) { expect(embeddings).toBeDefined(); expect(embeddings.length).toBe(6); expect(embeddings[0].length).toBe(768); } }, 60_000); test('BGEBaseENV15: embed small batch', async () => { const flagEmbedding = await FlagEmbedding.init({ model: EmbeddingModel.BGEBaseENV15, maxLength: 512, }); const embeddingsBatch = flagEmbedding.embed( ['This is a test', 'Some text', 'Some more test', 'This is a test', 'Some text', 'Some more test'], 1, ); for await (const embeddings of embeddingsBatch) { expect(embeddings).toBeDefined(); expect(embeddings.length).toBe(1); expect(embeddings[0].length).toBe(768); } }, 60_000); test('BGEBaseENV15: queryEmbed', async () => { const flagEmbedding = await FlagEmbedding.init({ model: EmbeddingModel.BGEBaseENV15, maxLength: 512, }); const embeddings = await flagEmbedding.queryEmbed('This is a test'); expect(embeddings).toBeDefined(); expect(embeddings.length).toBe(768); }, 60_000); test('BGEBaseENV15: passageEmbed', async () => { const flagEmbedding = await FlagEmbedding.init({ model: EmbeddingModel.BGEBaseENV15, maxLength: 512, }); const embeddings = (await flagEmbedding.passageEmbed(['This is a test']).next()).value!; expect(embeddings).toBeDefined(); expect(embeddings.length).toBe(1); }, 60_000); test('BGEBaseENV15: canonical values', async () => { const flagEmbedding = await FlagEmbedding.init({ model: EmbeddingModel.BGEBaseENV15, maxLength: 512, }); const expected = [0.01129394, 0.05493144, 0.02615099, 0.00328772, 0.02996045]; const embeddings = (await flagEmbedding.embed(['hello world']).next()).value!; expect(embeddings).toBeDefined(); for (let i = 0; i < expected.length; i++) { expect(embeddings[0][i]).toBeCloseTo(expected[i], 3); } }, 60_000);