{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Infinity\n",
    "\n",
    "`Infinity` allows to create `Embeddings` using a MIT-licensed Embedding Server. \n",
    "\n",
    "This notebook goes over how to use Langchain with Embeddings with the [Infinity Github Project](https://github.com/michaelfeil/infinity).\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Imports"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.embeddings import InfinityEmbeddings"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Optional: Make sure to start the Infinity instance\n",
    "\n",
    "To install infinity use the following command. For further details check out the [Docs on Github](https://github.com/michaelfeil/infinity).\n",
    "```bash\n",
    "pip install infinity_emb[all]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: infinity_emb[cli] in /home/michi/langchain/.venv/lib/python3.10/site-packages (0.0.8)\n",
      "\u001b[33mWARNING: infinity-emb 0.0.8 does not provide the extra 'cli'\u001b[0m\u001b[33m\n",
      "\u001b[0mRequirement already satisfied: numpy>=1.20.0 in /home/michi/langchain/.venv/lib/python3.10/site-packages (from infinity_emb[cli]) (1.24.4)\n",
      "\u001b[33mWARNING: There was an error checking the latest version of pip.\u001b[0m\u001b[33m\n",
      "\u001b[0m"
     ]
    }
   ],
   "source": [
    "# Install the infinity package\n",
    "!pip install infinity_emb[cli,torch]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Start up the server - best to be done from a separate terminal, not inside Jupyter Notebook\n",
    "\n",
    "```bash\n",
    "model=sentence-transformers/all-MiniLM-L6-v2\n",
    "port=7797\n",
    "infinity_emb --port $port --model-name-or-path $model\n",
    "```\n",
    "\n",
    "or alternativley just use docker:\n",
    "```bash\n",
    "model=sentence-transformers/all-MiniLM-L6-v2\n",
    "port=7797\n",
    "docker run -it --gpus all -p $port:$port michaelf34/infinity:latest --model-name-or-path $model --port $port\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Embed your documents using your Infinity instance "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "documents = [\n",
    "    \"Baguette is a dish.\",\n",
    "    \"Paris is the capital of France.\",\n",
    "    \"numpy is a lib for linear algebra\",\n",
    "    \"You escaped what I've escaped - You'd be in Paris getting fucked up too\",\n",
    "]\n",
    "query = \"Where is Paris?\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "embeddings created successful\n"
     ]
    }
   ],
   "source": [
    "#\n",
    "infinity_api_url = \"http://localhost:7797/v1\"\n",
    "# model is currently not validated.\n",
    "embeddings = InfinityEmbeddings(\n",
    "    model=\"sentence-transformers/all-MiniLM-L6-v2\", infinity_api_url=infinity_api_url\n",
    ")\n",
    "try:\n",
    "    documents_embedded = embeddings.embed_documents(documents)\n",
    "    query_result = embeddings.embed_query(query)\n",
    "    print(\"embeddings created successful\")\n",
    "except Exception as ex:\n",
    "    print(\n",
    "        \"Make sure the infinity instance is running. Verify by clicking on \"\n",
    "        f\"{infinity_api_url.replace('v1','docs')} Exception: {ex}. \"\n",
    "    )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'Baguette is a dish.': 0.31344215908661155,\n",
       " 'Paris is the capital of France.': 0.8148670296896388,\n",
       " 'numpy is a lib for linear algebra': 0.004429399861302009,\n",
       " \"You escaped what I've escaped - You'd be in Paris getting fucked up too\": 0.5088476180154582}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# (demo) compute similarity\n",
    "import numpy as np\n",
    "\n",
    "scores = np.array(documents_embedded) @ np.array(query_result).T\n",
    "dict(zip(documents, scores))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "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.12"
  },
  "vscode": {
   "interpreter": {
    "hash": "a0a0263b650d907a3bfe41c0f8d6a63a071b884df3cfdc1579f00cdc1aed6b03"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
