{
 "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
}
