text-embeddings-inference documentation

快速入门

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

快速入门

设置

使用 TEI 最简单的方法是采用其中一个官方 Docker 容器(请参阅 支持的模型和硬件 以选择合适的容器)。

因此,您需要按照其安装说明安装 Docker。

TEI 支持在 GPU 和 CPU 上进行推理。如果您计划使用 GPU,请通过查阅此表格确认您的硬件是否受支持。 接下来,安装NVIDIA Container Toolkit。您设备上的 NVIDIA 驱动程序需要与 CUDA 12.2 或更高版本兼容。

部署

接下来,是时候部署你的模型了。假设你想使用 Qwen/Qwen3-Embedding-0.6B。以下是操作方法:

model=Qwen/Qwen3-Embedding-0.6B
volume=$PWD/data

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

我们还建议与 Docker 容器共享一个卷(volume=$PWD/data),以避免每次运行时都重新下载权重。

推理

推理可以通过 3 种方式执行:使用 cURL,或通过 InferenceClient 或 OpenAI Python SDK。

cURL

要使用 cURL 向 TEI 端点发送 POST 请求,您可以运行以下命令:

curl 127.0.0.1:8080/embed \
    -X POST \
    -d '{"inputs":"What is Deep Learning?"}' \
    -H 'Content-Type: application/json'

Python

要在 Python 中运行推理,您可以使用 huggingface_hub Python SDK(推荐)或 openai Python SDK。

huggingface_hub

您可以通过 pip install --upgrade --quiet huggingface_hub 使用 pip 安装它,然后运行:

from huggingface_hub import InferenceClient

client = InferenceClient()

embedding = client.feature_extraction("What is deep learning?",
                                      model="http://localhost:8080/embed")
print(len(embedding[0]))

OpenAI

要向通过 OpenAI Python SDK 暴露的 Text Embeddings Inference (TEI) 上的 OpenAI Embeddings API 发送请求,您可以将其安装为 pip install --upgrade openai,然后运行以下代码片段:

import os
from openai import OpenAI

client = OpenAI(base_url="http://localhost:8080/v1", api_key= "-")

response = client.embeddings.create(
    model="text-embeddings-inference",
    input="What is Deep Learning?",
)

print(response.data[0].embedding)

或者,您也可以使用 cURL 发送请求,如下所示:

curl http://localhost:8080/v1/embeddings \
  -H "Content-Type: application/json" \
  -d '{
    "input": "What is Deep Learning?",
    "model": "text-embeddings-inference",
    "encoding_format": "float"
  }'

重排序模型与序列分类

TEI 还支持重排序模型和经典序列分类模型。

重排序器

重排序器(也称为交叉编码器)是一种单类别的序列分类模型,用于对查询与文本之间的相似度进行评分。请参阅 LlamaIndex 团队撰写的这篇博客文章,了解如何在 RAG 管道中使用重排序器模型以提升下游性能。

假设你想使用 BAAI/bge-reranker-large。首先,你可以这样部署它:

model=BAAI/bge-reranker-large
volume=$PWD/data

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

部署模型后,您可以使用 rerank 端点来对查询与一组文本之间的相似度进行排序。使用 cURL 时,操作方式如下:

curl 127.0.0.1:8080/rerank \
    -X POST \
    -d '{"query":"What is Deep Learning?", "texts": ["Deep Learning is not...", "Deep learning is..."], "raw_scores": false}' \
    -H 'Content-Type: application/json'

序列分类模型

您还可以使用经典的序列分类模型,例如 SamLowe/roberta-base-go_emotions:

model=SamLowe/roberta-base-go_emotions
volume=$PWD/data

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

部署模型后,您可以使用 predict 端点来获取与输入最相关的情绪:

curl 127.0.0.1:8080/predict \
    -X POST \
    -d '{"inputs":"I like you."}' \
    -H 'Content-Type: application/json'

对于在 raw_scores: false 下进行的多元分类预测,非有限模型分数(NaN 或无穷大)会引发推理错误,而不是生成 softmax 概率。

批处理

您可以在一个批次中发送多个输入。例如,对于嵌入:

curl 127.0.0.1:8080/embed \
    -X POST \
    -d '{"inputs":["Today is a nice day", "I like you"]}' \
    -H 'Content-Type: application/json'

而对于序列分类:

curl 127.0.0.1:8080/predict \
    -X POST \
    -d '{"inputs":[["I like you."], ["I hate pineapples"]]}' \
    -H 'Content-Type: application/json'

air gapped deployment

要在隔离环境中部署 Text Embeddings Inference,请先下载权重,然后使用卷将其挂载到容器内部。

例如:

# (Optional) create a `models` directory
mkdir models
cd models

# Make sure you have git-lfs installed (https://git-lfs.com)
git lfs install
git clone https://huggingface.co/Alibaba-NLP/gte-base-en-v1.5

# Set the models directory as the volume path
volume=$PWD

# Mount the models directory inside the container with a volume and set the model ID
docker run --gpus all -p 8080:80 -v $volume:/data --pull always ghcr.io/huggingface/text-embeddings-inference:cuda-1.9 --model-id /data/gte-base-en-v1.5
Update on GitHub