返回模型
说明文档
Xenova/bge-base-en-v1.5
使用方法 (Transformers.js)
如果还没有安装,可以从 NPM 安装 Transformers.js JavaScript 库:
npm i @huggingface/transformers
然后可以按如下方式使用模型计算嵌入向量:
import { pipeline } from '@huggingface/transformers';
// 创建特征提取管道
const extractor = await pipeline('feature-extraction', 'Xenova/bge-base-en-v1.5');
// 计算句子嵌入向量
const texts = ['Hello world.', 'Example sentence.'];
const embeddings = await extractor(texts, { pooling: 'mean', normalize: true });
console.log(embeddings);
// Tensor {
// dims: [ 2, 768 ],
// type: 'float32',
// data: Float32Array(1536) [ 0.019079938530921936, 0.041718777269124985, ... ],
// size: 1536
// }
console.log(embeddings.tolist()); // 将嵌入向量转换为 JavaScript 列表
// [
// [ 0.019079938530921936, 0.041718777269124985, 0.037672195583581924, ... ],
// [ 0.020936904475092888, 0.020080938935279846, -0.00787576474249363, ... ]
// ]
你也可以使用该模型进行检索。例如:
import { pipeline, cos_sim } from '@huggingface/transformers';
// 创建特征提取管道
const extractor = await pipeline('feature-extraction', 'Xenova/bge-base-en-v1.5');
// 要嵌入的文档列表
const texts = [
'Hello world.',
'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.',
'I love pandas so much!',
];
// 计算句子嵌入向量
const embeddings = await extractor(texts, { pooling: 'mean', normalize: true });
// 预先添加推荐的查询指令用于检索
const query_prefix = 'Represent this sentence for searching relevant passages: '
const query = query_prefix + 'What is a panda?';
const query_embeddings = await extractor(query, { pooling: 'mean', normalize: true });
// 按余弦相似度分数排序
const scores = embeddings.tolist().map(
(embedding, i) => ({
id: i,
score: cos_sim(query_embeddings.data, embedding),
text: texts[i],
})
).sort((a, b) => b.score - a.score);
console.log(scores);
// [
// { id: 1, score: 0.7787772374597298, text: 'The giant panda (Ailuropoda melanoleuca), sometimes called a panda bear or simply panda, is a bear species endemic to China.' },
// { id: 2, score: 0.7071589521880506, text: 'I love pandas so much!' },
// { id: 0, score: 0.4252782730390429, text: 'Hello world.' }
// ]
注意:为 ONNX 权重创建单独的仓库是一个临时解决方案,直到 WebML 获得更多关注。如果你希望使模型能够支持 Web 端,我们建议使用 🤗 Optimum 转换为 ONNX 格式,并像此仓库这样构建(将 ONNX 权重存放在名为 onnx 的子文件夹中)。
Xenova/bge-base-en-v1.5
作者 Xenova
feature-extraction
transformers.js
↓ 1.6M
♥ 9
创建时间: 2023-09-13 15:48:03+00:00
更新时间: 2025-07-29 21:44:26+00:00
在 Hugging Face 上查看文件 (16)
.gitattributes
README.md
config.json
onnx/model.onnx
ONNX
onnx/model_bnb4.onnx
ONNX
onnx/model_fp16.onnx
ONNX
onnx/model_int8.onnx
ONNX
onnx/model_q4.onnx
ONNX
onnx/model_q4f16.onnx
ONNX
onnx/model_quantized.onnx
ONNX
onnx/model_uint8.onnx
ONNX
quantize_config.json
special_tokens_map.json
tokenizer.json
tokenizer_config.json
vocab.txt