Skip to content

The agent engineering platform.

feat(embeddings): text-embeddings-inference (#14288)

Massimiliano Pronestic215a4c9ec47
diff --git a/docs/docs/integrations/text_embedding/text_embeddings_inference.ipynb b/docs/docs/integrations/text_embedding/text_embeddings_inference.ipynb
new file mode 100644
index 0000000000..dafedba496
--- /dev/null
+++ b/docs/docs/integrations/text_embedding/text_embeddings_inference.ipynb
@@ -0,0 +1,171 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "id": "ceabf1eb-ca96-4791-90ad-e9acb31edf5c",
+ "metadata": {},
+ "source": [
+ "# Text Embeddings Inference\n",
+ "\n",
+ "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.\n",
+ "\n",
+ "To use it within langchain, first install `huggingface-hub`."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 2,
+ "id": "579f0677-aa06-4ad8-a816-3520c8d6923c",
+ "metadata": {
+ "tags": []
+ },
+ "outputs": [],
+ "source": [
+ "!pip install huggingface-hub -q"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "7c6b1015-bc3f-4283-93d5-11387be1b98d",
+ "metadata": {},
+ "source": [
+ "Then expose an embedding model using TEI. For instance, using Docker, you can serve `BAAI/bge-large-en-v1.5` as follows:\n",
+ "\n",
+ "```bash\n",
+ "model=BAAI/bge-large-en-v1.5\n",
+ "revision=refs/pr/5\n",
+ "volume=$PWD/data # share a volume with the Docker container to avoid downloading weights every run\n",
+ "\n",
+ "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\n",
+ "```"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "48eebefc-a631-48dd-9bde-4a987f81aa20",
+ "metadata": {},
+ "source": [
+ "Finally, instantiate the client and embed your texts."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 3,
+ "id": "22b09777-5ba3-4fbe-81cf-a702a55df9c4",
+ "metadata": {
+ "tags": []
+ },
+ "outputs": [],
+ "source": [
+ "from langchain.embeddings import HuggingFaceHubEmbeddings"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 4,
+ "id": "c26fca9f-cfdb-45e5-a0bd-f677ff8b9d92",
+ "metadata": {
+ "tags": []
+ },
+ "outputs": [
+ {
+ "name": "stdin",
+ "output_type": "stream",
+ "text": [
+ "Enter your HF API Key:\n",
+ "\n",
+ " ········\n"
+ ]
+ }
+ ],
+ "source": [
+ "from getpass import getpass\n",
+ "\n",
+ "huggingfacehub_api_token = getpass(\"Enter your HF API Key:\\n\\n\")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "id": "f9a92970-16f4-458c-b186-2a83e9f7d840",
+ "metadata": {
+ "tags": []
+ },
+ "outputs": [],
+ "source": [
+ "embeddings = HuggingFaceHubEmbeddings(\n",
+ " model=\"http://localhost:8080\", huggingfacehub_api_token=huggingfacehub_api_token\n",
+ ")"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "id": "42105438-9fee-460a-9c52-b7c595722758",
+ "metadata": {
+ "tags": []
+ },
+ "outputs": [],
+ "source": [
+ "text = \"What is deep learning?\""
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 8,
+ "id": "20167762-0988-4205-bbd4-1f20fd9dd247",
+ "metadata": {
+ "tags": []
+ },
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[0.018113142, 0.00302585, -0.049911194]"
+ ]
+ },
+ "execution_count": 8,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "query_result = embeddings.embed_query(text)\n",
+ "query_result[:3]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "id": "54b87cf6-86ad-46f5-b2cd-17eb43cb4d0b",
+ "metadata": {
+ "tags": []
+ },
+ "outputs": [],
+ "source": [
+ "doc_result = embeddings.embed_documents([text])"
+ ]
+ }
+ ],
+ "metadata": {
+ "kernelspec": {
+ "display_name": "conda_python3",
+ "language": "python",
+ "name": "conda_python3"
+ },
+ "language_info": {
+ "codemirror_mode": {
+ "name": "ipython",
+ "version": 3
+ },
+ "file_extension": ".py",
+ "mimetype": "text/x-python",
+ "name": "python",
+ "nbconvert_exporter": "python",
+ "pygments_lexer": "ipython3",
+ "version": "3.10.13"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 5
+}
diff --git a/libs/langchain/langchain/embeddings/huggingface_hub.py b/libs/langchain/langchain/embeddings/huggingface_hub.py
index c864d673d9..60b7808dbe 100644
--- a/libs/langchain/langchain/embeddings/huggingface_hub.py
+++ b/libs/langchain/langchain/embeddings/huggingface_hub.py
@@ -1,3 +1,4 @@
+import json
from typing import Any, Dict, List, Optional
from langchain_core.embeddings import Embeddings
@@ -5,7 +6,7 @@ from langchain_core.pydantic_v1 import BaseModel, Extra, root_validator
from langchain.utils import get_from_dict_or_env
-DEFAULT_REPO_ID = "sentence-transformers/all-mpnet-base-v2"
+DEFAULT_MODEL = "sentence-transformers/all-mpnet-base-v2"
VALID_TASKS = ("feature-extraction",)
@@ -20,17 +21,19 @@ class HuggingFaceHubEmbeddings(BaseModel, Embeddings):
.. code-block:: python
from langchain.embeddings import HuggingFaceHubEmbeddings
- repo_id = "sentence-transformers/all-mpnet-base-v2"
+ model = "sentence-transformers/all-mpnet-base-v2"
hf = HuggingFaceHubEmbeddings(
- repo_id=repo_id,
+ model=model,
task="feature-extraction",
huggingfacehub_api_token="my-api-key",
)
"""
client: Any #: :meta private:
- repo_id: str = DEFAULT_REPO_ID
+ model: Optional[str] = None
"""Model name to use."""
+ repo_id: Optional[str] = None
+ """Huggingfacehub repository id, for backward compatibility."""
task: Optional[str] = "feature-extraction"
"""Task to call the model with."""
model_kwargs: Optional[dict] = None
@@ -50,22 +53,23 @@ class HuggingFaceHubEmbeddings(BaseModel, Embeddings):
values, "huggingfacehub_api_token", "HUGGINGFACEHUB_API_TOKEN"
)
try:
- from huggingface_hub.inference_api import InferenceApi
-
- repo_id = values["repo_id"]
- if not repo_id.startswith("sentence-transformers"):
- raise ValueError(
- "Currently only 'sentence-transformers' embedding models "
- f"are supported. Got invalid 'repo_id' {repo_id}."
- )
- client = InferenceApi(
- repo_id=repo_id,
+ from huggingface_hub import InferenceClient
+
+ if values["model"]:
+ values["repo_id"] = values["model"]
+ elif values["repo_id"]:
+ values["model"] = values["repo_id"]
+ else:
+ values["model"] = DEFAULT_MODEL
+ values["repo_id"] = DEFAULT_MODEL
+
+ client = InferenceClient(
+ model=values["model"],
token=huggingfacehub_api_token,
- task=values.get("task"),
)
- if client.task not in VALID_TASKS:
+ if values["task"] not in VALID_TASKS:
raise ValueError(
- f"Got invalid task {client.task}, "
+ f"Got invalid task {values['task']}, "
f"currently only {VALID_TASKS} are supported"
)
values["client"] = client
@@ -88,8 +92,10 @@ class HuggingFaceHubEmbeddings(BaseModel, Embeddings):
# replace newlines, which can negatively affect performance.
texts = [text.replace("\n", " ") for text in texts]
_model_kwargs = self.model_kwargs or {}
- responses = self.client(inputs=texts, params=_model_kwargs)
- return responses
+ responses = self.client.post(
+ json={"inputs": texts, "parameters": _model_kwargs, "task": self.task}
+ )
+ return json.loads(responses.decode())
def embed_query(self, text: str) -> List[float]:
"""Call out to HuggingFaceHub's embedding endpoint for embedding query text.