💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
Add additional example to 4.0 notebook #188
davidmezzettia454e99dd8eb
diff --git a/examples/24_Whats_new_in_txtai_4_0.ipynb b/examples/24_Whats_new_in_txtai_4_0.ipynbindex 13de0b14..682dee3f 100644--- a/examples/24_Whats_new_in_txtai_4_0.ipynb+++ b/examples/24_Whats_new_in_txtai_4_0.ipynb@@ -63,7 +63,7 @@"%%capture\n","!pip install git+https://github.com/neuml/txtai"],- "execution_count": 21,+ "execution_count": 1,"outputs": []},{@@ -90,7 +90,7 @@"colab": {"base_uri": "https://localhost:8080/"},- "outputId": "c37d3b1d-1096-4518-92fa-1950cb69b183"+ "outputId": "efd1cbe0-f27c-4f78-ed06-ba2eb04e8902"},"source": ["from txtai.embeddings import Embeddings\n",@@ -119,7 +119,7 @@" # Print text\n"," print(\"%-20s %s\" % (query, text))"],- "execution_count": 22,+ "execution_count": 2,"outputs": [{"output_type": "stream",@@ -182,9 +182,9 @@"base_uri": "https://localhost:8080/"},"id": "aPH-dnV2ZuL1",- "outputId": "7946a782-8b9c-4ee5-abcc-ce0f45858349"+ "outputId": "01879767-0a84-4f1d-ee1f-5e1d2442087b"},- "execution_count": 23,+ "execution_count": 3,"outputs": [{"output_type": "stream",@@ -251,9 +251,9 @@"base_uri": "https://localhost:8080/","height": 420},- "outputId": "3dfe3593-6a07-4277-b388-4983e81c5e8a"+ "outputId": "8947bcc7-8e22-4197-a9ce-7359cb91b72a"},- "execution_count": 24,+ "execution_count": 4,"outputs": [{"output_type": "execute_result",@@ -268,7 +268,7 @@"width": 600}},- "execution_count": 24+ "execution_count": 4}]},@@ -302,9 +302,9 @@"base_uri": "https://localhost:8080/"},"id": "fAz7Jv6SjR6a",- "outputId": "0ea984bf-a9ab-42fc-fa18-3f59bd8ef2b9"+ "outputId": "30696b75-c726-4ed7-9f8f-985da2b512d5"},- "execution_count": 25,+ "execution_count": 5,"outputs": [{"output_type": "stream",@@ -313,7 +313,7 @@"{\n"," \"backend\": \"faiss\",\n"," \"build\": {\n",- " \"create\": \"2021-12-29T00:42:17Z\",\n",+ " \"create\": \"2022-01-05T01:44:13Z\",\n"," \"python\": \"3.7.12\",\n"," \"settings\": {\n"," \"components\": \"IDMap,Flat\"\n",@@ -331,7 +331,7 @@"{\n"," \"backend\": \"faiss\",\n"," \"build\": {\n",- " \"create\": \"2021-12-29T00:42:29Z\",\n",+ " \"create\": \"2022-01-05T01:44:19Z\",\n"," \"python\": \"3.7.12\",\n"," \"settings\": {\n"," \"components\": \"IDMap,Flat\"\n",@@ -383,18 +383,18 @@"base_uri": "https://localhost:8080/"},"id": "0oOC8ToG1pyn",- "outputId": "bd42dbbe-0de1-4fe3-97b9-602992bdca5a"+ "outputId": "1c992108-cc8f-4b24-a4d9-474de2c12b31"},- "execution_count": 26,+ "execution_count": 6,"outputs": [{"output_type": "stream","name": "stdout","text": [- "drwx------ root/root 0 2021-12-29 00:42 ./\n",- "-rw-r--r-- root/root 290 2021-12-29 00:42 ./config\n",- "-rw-r--r-- root/root 77824 2021-12-29 00:42 ./documents\n",- "-rw-r--r-- root/root 10898 2021-12-29 00:42 ./embeddings\n",+ "drwx------ root/root 0 2022-01-05 01:44 ./\n",+ "-rw-r--r-- root/root 290 2022-01-05 01:44 ./config\n",+ "-rw-r--r-- root/root 77824 2022-01-05 01:44 ./documents\n",+ "-rw-r--r-- root/root 10898 2022-01-05 01:44 ./embeddings\n","\n","Strms Blocks Compressed Uncompressed Ratio Check Filename\n"," 1 1 45.8 KiB 100.0 KiB 0.458 CRC64 index.tar.xz\n",@@ -411,7 +411,7 @@]},"metadata": {},- "execution_count": 26+ "execution_count": 6}]},@@ -431,12 +431,84 @@"\n","txtai supports generating vectors with [Hugging Face Transformers](https://github.com/huggingface/transformers), [PyTorch](https://github.com/pytorch/pytorch), [ONNX](https://github.com/microsoft/onnxruntime) and [Word Vector](https://github.com/neuml/magnitude) models.\n","\n",- "This release adds support for pre-computed vectors using external models. External models may be an API, custom library and/or another way to vectorize data. The example below uses [spaCy](https://github.com/explosion/spaCy) to build vectors and then loads them into txtai. "+ "This release adds support for pre-computed vectors using external models. External models may be an API, custom library and/or another way to vectorize data. This adds flexibility given the high computation cost in building embeddings vectors. Embeddings generation could be outsourced or consolidated to a group of servers with GPUs, leaving index servers to run on lower resourced machines. \n",+ "\n",+ "The example below uses the [Hugging Face Inference API](https://huggingface.co/inference-api) to build embeddings vectors. We'll load the [exact model as in the first example](#scrollTo=0p3WCDniUths) and produce the same results."],"metadata": {"id": "oAISaBYVg60i"}},+ {+ "cell_type": "code",+ "source": [+ "import numpy as np\n",+ "import requests\n",+ "import time\n",+ "\n",+ "def transform(text):\n",+ " if isinstance(text, tuple):\n",+ " text = text[1]\n",+ "\n",+ " response = requests.post(\"https://api-inference.huggingface.co/pipeline/feature-extraction/sentence-transformers/nli-mpnet-base-v2\",\n",+ " json={\"inputs\": text})\n",+ "\n",+ " return np.array(response.json(), dtype=np.float32)\n",+ "\n",+ "# Batch encode data\n",+ "vectors = transform(data)\n",+ "\n",+ "# Index data using vectors from Inference API\n",+ "embeddings = Embeddings({\"method\": \"external\", \"transform\": transform, \"content\": True})\n",+ "embeddings.index([(x, {\"object\": vectors[x], \"row\": row}, None) for x, row in enumerate(data)])\n",+ "\n",+ "print(\"%-20s %s\" % (\"Query\", \"Best Match\"))\n",+ "print(\"-\" * 50)\n",+ "\n",+ "# Run an embeddings search for each query\n",+ "for query in (\"feel good story\", \"climate change\", \"public health story\", \"war\", \"wildlife\", \"asia\", \"lucky\", \"dishonest junk\"):\n",+ " # Extract text field from result\n",+ " text = embeddings.search(f\"select id, row as text, score from txtai where similar('{query}')\", 1)[0][\"text\"]\n",+ "\n",+ " # Print text\n",+ " print(\"%-20s %s\" % (query, text))"+ ],+ "metadata": {+ "colab": {+ "base_uri": "https://localhost:8080/"+ },+ "id": "d6dbB3nMk29O",+ "outputId": "fb779c00-4088-4e2e-ee3d-29715fa98a2f"+ },+ "execution_count": 7,+ "outputs": [+ {+ "output_type": "stream",+ "name": "stdout",+ "text": [+ "Query Best Match\n",+ "--------------------------------------------------\n",+ "feel good story Maine man wins $1M from $25 lottery ticket\n",+ "climate change Canada's last fully intact ice shelf has suddenly collapsed, forming a Manhattan-sized iceberg\n",+ "public health story US tops 5 million confirmed virus cases\n",+ "war Beijing mobilises invasion craft along coast as Taiwan tensions escalate\n",+ "wildlife The National Park Service warns against sacrificing slower friends in a bear attack\n",+ "asia Beijing mobilises invasion craft along coast as Taiwan tensions escalate\n",+ "lucky Maine man wins $1M from $25 lottery ticket\n",+ "dishonest junk Make huge profits without work, earn up to $100,000 a day\n"+ ]+ }+ ]+ },+ {+ "cell_type": "markdown",+ "source": [+ "The next example uses [spaCy](https://github.com/explosion/spaCy) to build vectors and then loads them into txtai. The vectors with this model are much faster to generate at the expense of accuracy."+ ],+ "metadata": {+ "id": "_kq_fbjd4g74"+ }+ },{"cell_type": "code","source": [@@ -447,7 +519,7 @@"metadata": {"id": "IIGlel3ShyGP"},- "execution_count": 27,+ "execution_count": 8,"outputs": []},{@@ -473,9 +545,9 @@"base_uri": "https://localhost:8080/"},"id": "nikKybxEkhjC",- "outputId": "84950459-8d2b-4570-b7ed-ad468c783901"+ "outputId": "65a0a547-820b-42ea-b88c-82e994b89e7f"},- "execution_count": 28,+ "execution_count": 9,"outputs": [{"output_type": "stream",@@ -504,4 +576,4 @@]}]-}\ No newline at end of file+}