@anvia/transformers
v0.1.2
Published
Transformers.js embedding model adapter for Anvia.
Readme
@anvia/transformers
Transformers.js embedding model adapter for Anvia.
Use this package when you want local embedding generation through @huggingface/transformers, especially for development or lightweight RAG workflows.
Installation
pnpm add @anvia/transformers @anvia/core @huggingface/transformersIn this monorepo, the package is available through the workspace:
pnpm --filter @anvia/transformers buildUsage
import { embedDocuments, InMemoryVectorStore } from "@anvia/core";
import { createTransformersEmbeddingModel } from "@anvia/transformers";
const embeddingModel = await createTransformersEmbeddingModel();
const documents = await embedDocuments(
embeddingModel,
[
{
id: "password-reset",
title: "Password reset policy",
body: "Password reset links expire after 30 minutes.",
},
{
id: "priority-support",
title: "Priority support",
body: "Enterprise customers receive priority support.",
},
],
{
id: (document) => document.id,
content: (document) => `${document.title}\n${document.body}`,
},
);
const store = InMemoryVectorStore.fromDocuments(documents);
const index = store.index(embeddingModel);
const results = await index.search({
query: "How long does a password reset link last?",
topK: 3,
});
console.log(results);Default Model
The default embedding model is:
Xenova/all-MiniLM-L6-v2You can pass another feature-extraction model:
const embeddingModel = await createTransformersEmbeddingModel({
model: "Xenova/all-MiniLM-L6-v2",
pooling: "mean",
normalize: true,
maxBatchSize: 16,
});Exports
TransformersEmbeddingModelcreateTransformersEmbeddingModelDEFAULT_TRANSFORMERS_EMBEDDING_MODELTransformersEmbeddingModelOptionsTransformersPooling
Development
pnpm --filter @anvia/transformers typecheck
pnpm --filter @anvia/transformers test
pnpm --filter @anvia/transformers build