Skip to main content
Open In ColabOpen on GitHub

文本嵌入推理

Hugging Face Text Embeddings Inference (TEI) is a toolkit for deploying and serving open-source text embeddings and sequence classification models. TEI enables high-performance extraction for the most popular models, including FlagEmbedding, Ember, GTE and E5.

要在 LangChain 中使用它,首先安装 huggingface-hub

%pip install --upgrade huggingface-hub

然后使用TEI公开一个嵌入模型。例如,使用Docker,您可以按如下方式提供BAAI/bge-large-en-v1.5

model=BAAI/bge-large-en-v1.5
revision=refs/pr/5
volume=$PWD/data # share a volume with the Docker container to avoid downloading weights every run

docker run --gpus all -p 8080:80 -v $volume:/data --pull always ghcr.io/huggingface/text-embeddings-inference:0.6 --model-id $model --revision $revision

使用 Docker 的具体细节可能会因底层硬件而异。例如,要在 Intel Gaudi/Gaudi2 硬件上运行模型,请参考 tei-gaudi 仓库 中的相关 docker 运行命令。

最后,实例化客户端并嵌入您的文本。

from langchain_huggingface.embeddings import HuggingFaceEndpointEmbeddings
embeddings = HuggingFaceEndpointEmbeddings(model="http://localhost:8080")
text = "What is deep learning?"
query_result = embeddings.embed_query(text)
query_result[:3]
[0.018113142, 0.00302585, -0.049911194]
doc_result = embeddings.embed_documents([text])
doc_result[0][:3]
[0.018113142, 0.00302585, -0.049911194]