Skip to main content

HuggingFace Transformers

TransformerEmbeddings 类使用 Transformers.js 包为给定文本生成嵌入向量。

¥The TransformerEmbeddings class uses the Transformers.js package to generate embeddings for a given text.

它在本地运行,甚至可以直接在浏览器中运行,允许你创建带有内置嵌入的 Web 应用。

¥It runs locally and even works directly in the browser, allowing you to create web apps with built-in embeddings.

设置

¥Setup

你需要将 @huggingface/transformers 包安装为对等依赖:

¥You'll need to install the @huggingface/transformers package as a peer dependency:

Compatibility

如果你使用的社区版本早于 0.3.21,请安装旧版 @xenova/transformers 包,并从下方的 "@langchain/community/embeddings/hf_transformers" 中导入嵌入。

¥If you are using a version of community older than 0.3.21, install the older @xenova/transformers package and import the embeddings from "@langchain/community/embeddings/hf_transformers" below.

npm install @huggingface/transformers
npm install @langchain/community @langchain/core

示例

¥Example

请注意,如果你在浏览器环境中使用,你可能需要将所有与推断相关的代码放入 Web Worker 中,以避免阻塞主线程。

¥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.

请参阅 此指南 和 Transformers.js 文档中的其他资源,了解如何设置你的项目。

¥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/huggingface_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:

¥Related