HuggingFace Transformers
The TransformerEmbeddings
class uses the Transformers.js package to generate embeddings for a given text.
It runs locally and even works directly in the browser, allowing you to create web apps with built-in embeddings.
Setup
You'll need to install the @xenova/transformers package as a peer dependency:
- npm
- Yarn
- pnpm
npm install @xenova/transformers
yarn add @xenova/transformers
pnpm add @xenova/transformers
- npm
- Yarn
- pnpm
npm install @langchain/community @langchain/core
yarn add @langchain/community @langchain/core
pnpm add @langchain/community @langchain/core
Example
Note that if you're using in a browser context, you'll likely want to put all inference-related code in a web worker to avoid blocking the main thread.
See this guide and the other resources in the Transformers.js docs for an idea of how to set up your project.
import { HuggingFaceTransformersEmbeddings } from "@langchain/community/embeddings/hf_transformers";
const model = new HuggingFaceTransformersEmbeddings({
model: "Xenova/all-MiniLM-L6-v2",
});
/* Embed queries */
const res = await model.embedQuery(
"What would be a good company name for a company that makes colorful socks?"
);
console.log({ res });
/* Embed documents */
const documentRes = await model.embedDocuments(["Hello world", "Bye bye"]);
console.log({ documentRes });
API Reference:
- HuggingFaceTransformersEmbeddings from
@langchain/community/embeddings/hf_transformers
Related
- Embedding model conceptual guide
- Embedding model how-to guides