本篇通過將句子數組轉換為句子的向量表示,并通過平均池化和歸一化處理,生成適合機器學習或深度學習任務使用的特征向量為例,演示通過NodeJS 的方式調用Xenova/all-MiniLM-L6-v2 的過程。
關于 all-MiniLM-L6-v2 的介紹,可以參照上一篇:
一篇吃透模型:all-MiniLM-L6-v2
可以訪問 Hugging Face的狀況
Hugging 的站點是:
https://huggingface.co/
如果可以訪問該站點的話,則直接安裝 Hugging Face 的 Transformers 庫。
npm i @huggingface/transformers
之后就可以編寫代碼:
import { pipeline } from '@huggingface/transformers';// Create a feature-extraction pipeline
const extractor = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2');// Compute sentence embeddings
const sentences = ['This is an example sentence', 'Each sentence is converted'];
const output = await extractor(sentences, { pooling: 'mean', normalize: true });
console.log(output);
上面代碼作用就是加載模型,將語句轉換為向量,細節的部分下面再介紹,執行的效果如下:
到此,就成功的運行了。
但是如果無法訪問Hugging Face 的化, 運行就會出現如下錯誤:
PS D:\devworkspace\vs\ai_ency\ai_nodejs_ency> node .\src\llm\minilm\huggingface-minilm.js
node:internal/deps/undici/undici:12502Error.captureStackTrace(err, this);^TypeError: fetch failedat node:internal/deps/undici/undici:12502:13at processTicksAndRejections (node:internal/process/task_queues:95:5)at runNextTicks (node:internal/process/task_queues:64:3)at process.processImmediate (node:internal/timers:449:9)at async getModelFile (file:///D:/devworkspace/vs/ai_ency/ai_nodejs_ency/node_modules/@huggingface/transformers/dist/transformers.mjs:31325:24)at async getModelJSON (file:///D:/devworkspace/vs/ai_ency/ai_nodejs_ency/node_modules/@huggingface/transformers/dist/transformers.mjs:31427:18)at async Promise.all (index 0)at async loadTokenizer (file:///D:/devworkspace/vs/ai_ency/ai_nodejs_ency/node_modules/@huggingface/transformers/dist/transformers.mjs:24731:18)at async AutoTokenizer.from_pretrained (file:///D:/devworkspace/vs/ai_ency/ai_nodejs_ency/node_modules/@huggingface/transformers/dist/transformers.mjs:29020:50)at async Promise.all (index 0) {[cause]: ConnectTimeoutError: Connect Timeout Errorat onConnectTimeout (node:internal/deps/undici/undici:6635:28)at node:internal/deps/undici/undici:6587:50at Immediate._onImmediate (node:internal/deps/undici/undici:6617:37)at process.processImmediate (node:internal/timers:478:21) {code: 'UND_ERR_CONNECT_TIMEOUT'}
}
因為默認會從Hugging Face下載模型文件,無法下載,報上面的fetch 獲取錯誤, 有解決方法嗎?
答案就是從ModelScope 下載模型文件。
從 ModelScope 下載模型文件
ModelScope上面也維護了 Xenova/all-MiniLM-L6-v2 的模型文件, 地址是:
https://www.modelscope.cn/models/Xenova/all-MiniLM-L6-v2/files
但是,ModelScope提供了Python的庫,卻沒有NodeJS的庫,所以無法直接通過ModelScope的來下載,但是可以下載模型的文件,下載方式就是先安裝ModelScope,然后用ModelScope 的命令行下載模型文件:
modelscope download --model Xenova/all-MiniLM-L6-v2
下載后的文件目錄如下圖:
下載后,將這個目錄中的內容復制到NodeJS 項目的node_modules 目錄下的 node_modules@huggingface\transformers.cache 目錄下, 這也是Hugging Face下載模型文件的目錄,結構如下圖:
復制完成之后,就可以正常運行了。
補充介紹: Transformers 是什么?
Transformers 是由 Hugging Face 團隊開發的開源 庫,專注于提供基于 Transformer 架構 的預訓練模型和工具。它簡化了自然語言處理(NLP)任務的實現流程,支持文本生成、翻譯、分類、問答等場景,并兼容 PyTorch、TensorFlow 等深度學習框架。
Transformers 有Python 和 NodeJS 兩個版本。
如果Hugging Face無法訪問
- Python 版本可以使用ModelScope提供的庫
- NodeJS
補充介紹: 代碼詳細解釋
const extractor = await pipeline(...)
是使用 Hugging Face Transformers.js 庫的核心方法之一,用于創建一個 預訓練模型的推理管道。通過 pipeline
,可以輕松加載模型并執行各種任務(如文本分類、特征提取、問答等)。
pipeline
的作用是:
- 加載模型:從本地或遠程加載預訓練模型和分詞器。
- 封裝推理邏輯:將模型的輸入預處理、推理和后處理邏輯封裝成一個簡單的接口。
- 執行任務:根據任務類型(如
feature-extraction
、text-classification
等),對輸入數據進行處理并返回結果。
參數詳解
pipeline
方法的完整簽名如下:
const pipeline = await transformers.pipeline(task, model, options);
1. task
(必需)
指定要執行的任務類型。常見的任務包括:
feature-extraction
:特征提取(生成句子或詞的嵌入向量)。text-classification
:文本分類。question-answering
:問答任務。translation
:翻譯任務。text-generation
:文本生成。- 其他任務:如
summarization
、fill-mask
等。
示例:
const extractor = await pipeline('feature-extraction');
2. model
(可選)
指定要加載的模型。可以是以下之一:
- 模型名稱:從 Hugging Face Hub 加載的模型名稱(如
Xenova/all-MiniLM-L6-v2
)。 - 本地路徑:本地模型文件的路徑(如
./custom_model
)。 - 未指定:如果不提供,庫會加載默認模型。
示例:
// 從 Hugging Face Hub 加載模型
const extractor = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2');// 從本地路徑加載模型
const extractor = await pipeline('feature-extraction', './custom_model');
3. options
(可選)
一個配置對象,用于自定義模型加載和推理行為。常見選項包括:
選項 | 類型 | 描述 |
---|---|---|
local_files_only | boolean | 是否僅從本地加載模型(默認 false )。設置為 true 可禁用網絡請求。 |
revision | string | 模型版本(如 main 或特定 commit hash)。 |
cache_dir | string | 緩存目錄路徑。設置為 null 可禁用緩存。 |
quantized | boolean | 是否加載量化模型(默認 false )。 |
progress_callback | function | 加載模型時的進度回調函數。 |
device | string | 推理設備(如 cpu 或 gpu )。 |
pooling | string | 特征提取時的池化方式(如 mean 、max )。 |
normalize | boolean | 是否對特征向量進行歸一化(默認 false )。 |
示例:
const extractor = await pipeline('feature-extraction', 'Xenova/all-MiniLM-L6-v2', {local_files_only: true, // 僅從本地加載revision: 'main', // 使用主分支版本pooling: 'mean', // 使用均值池化normalize: true, // 對輸出向量歸一化
});
返回值
pipeline
返回一個 推理函數,可以直接用于處理輸入數據。具體返回值類型取決于任務類型。
示例:
// 特征提取任務
const extractor = await pipeline('feature-extraction');// 使用推理函數
const sentences = ['This is an example sentence'];
const embeddings = await extractor(sentences, { pooling: 'mean' });
console.log(embeddings);