|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "# JSON load, Extraction and Ingest with ELSER Example\n", |
| 8 | + "[](https://colab.research.google.com/github/elastic/elasticsearch-labs/blob/main/notebooks/ingestion/json-chunking-ingest.ipynb)\n", |
| 9 | + "\n", |
| 10 | + "This workbook demonstrates how to load a JSON file, create passages and ingest into Elasticsearch. \n", |
| 11 | + "\n", |
| 12 | + "In this example we will:\n", |
| 13 | + "- load the JSON using jq\n", |
| 14 | + "- chunk the text with LangChain document splitter\n", |
| 15 | + "- ingest into Elasticsearch with LangChain Elasticsearch Vectorstore. \n", |
| 16 | + "\n", |
| 17 | + "We will also setup your Elasticsearch cluster with ELSER model, so we can use it to embed the passages." |
| 18 | + ] |
| 19 | + }, |
| 20 | + { |
| 21 | + "cell_type": "code", |
| 22 | + "execution_count": null, |
| 23 | + "metadata": { |
| 24 | + "colab": { |
| 25 | + "base_uri": "https://localhost:8080/" |
| 26 | + }, |
| 27 | + "id": "zQlYpYkI46Ff", |
| 28 | + "outputId": "83677846-8a6a-4b49-fde0-16d473778814" |
| 29 | + }, |
| 30 | + "outputs": [], |
| 31 | + "source": [ |
| 32 | + "!pip install -qU langchain_community langchain elasticsearch tiktoken langchain-elasticsearch jq" |
| 33 | + ] |
| 34 | + }, |
| 35 | + { |
| 36 | + "cell_type": "markdown", |
| 37 | + "metadata": { |
| 38 | + "id": "GCZR7-zK810e" |
| 39 | + }, |
| 40 | + "source": [ |
| 41 | + "## Connecting to Elasticsearch" |
| 42 | + ] |
| 43 | + }, |
| 44 | + { |
| 45 | + "cell_type": "code", |
| 46 | + "execution_count": 2, |
| 47 | + "metadata": { |
| 48 | + "id": "DofNZ2w25nIr" |
| 49 | + }, |
| 50 | + "outputs": [], |
| 51 | + "source": [ |
| 52 | + "from elasticsearch import Elasticsearch\n", |
| 53 | + "from getpass import getpass\n", |
| 54 | + "\n", |
| 55 | + "# https://www.elastic.co/search-labs/tutorials/install-elasticsearch/elastic-cloud#finding-your-cloud-id\n", |
| 56 | + "ELASTIC_CLOUD_ID = getpass(\"Elastic Cloud ID: \")\n", |
| 57 | + "\n", |
| 58 | + "# https://www.elastic.co/search-labs/tutorials/install-elasticsearch/elastic-cloud#creating-an-api-key\n", |
| 59 | + "ELASTIC_API_KEY = getpass(\"Elastic Api Key: \")\n", |
| 60 | + "\n", |
| 61 | + "client = Elasticsearch(\n", |
| 62 | + " # For local development\n", |
| 63 | + " # \"http://localhost:9200\",\n", |
| 64 | + " # basic_auth=(\"elastic\", \"changeme\")\n", |
| 65 | + " cloud_id=ELASTIC_CLOUD_ID,\n", |
| 66 | + " api_key=ELASTIC_API_KEY,\n", |
| 67 | + ")" |
| 68 | + ] |
| 69 | + }, |
| 70 | + { |
| 71 | + "cell_type": "markdown", |
| 72 | + "metadata": { |
| 73 | + "id": "zv6hKYWr8-Mg" |
| 74 | + }, |
| 75 | + "source": [ |
| 76 | + "## Deploying ELSER" |
| 77 | + ] |
| 78 | + }, |
| 79 | + { |
| 80 | + "cell_type": "code", |
| 81 | + "execution_count": null, |
| 82 | + "metadata": { |
| 83 | + "id": "1U4ffD2K9BkJ" |
| 84 | + }, |
| 85 | + "outputs": [], |
| 86 | + "source": [ |
| 87 | + "import time\n", |
| 88 | + "\n", |
| 89 | + "model = \".elser_model_2\"\n", |
| 90 | + "\n", |
| 91 | + "try:\n", |
| 92 | + " client.ml.put_trained_model(model_id=model, input={\"field_names\": [\"text_field\"]})\n", |
| 93 | + "except:\n", |
| 94 | + " pass\n", |
| 95 | + "\n", |
| 96 | + "while True:\n", |
| 97 | + " status = client.ml.get_trained_models(model_id=model, include=\"definition_status\")\n", |
| 98 | + "\n", |
| 99 | + " if status[\"trained_model_configs\"][0][\"fully_defined\"]:\n", |
| 100 | + " print(model + \" is downloaded and ready to be deployed.\")\n", |
| 101 | + " break\n", |
| 102 | + " else:\n", |
| 103 | + " print(model + \" is downloading or not ready to be deployed.\")\n", |
| 104 | + " time.sleep(5)\n", |
| 105 | + "\n", |
| 106 | + "client.ml.start_trained_model_deployment(\n", |
| 107 | + " model_id=model, number_of_allocations=1, wait_for=\"starting\"\n", |
| 108 | + ")\n", |
| 109 | + "\n", |
| 110 | + "while True:\n", |
| 111 | + " status = client.ml.get_trained_models_stats(\n", |
| 112 | + " model_id=model,\n", |
| 113 | + " )\n", |
| 114 | + " if status[\"trained_model_stats\"][0][\"deployment_stats\"][\"state\"] == \"started\":\n", |
| 115 | + " print(model + \" has been successfully deployed.\")\n", |
| 116 | + " break\n", |
| 117 | + " else:\n", |
| 118 | + " print(model + \" is currently being deployed.\")\n", |
| 119 | + " time.sleep(5)" |
| 120 | + ] |
| 121 | + }, |
| 122 | + { |
| 123 | + "cell_type": "markdown", |
| 124 | + "metadata": { |
| 125 | + "id": "wqYXqJxn9JsA" |
| 126 | + }, |
| 127 | + "source": [ |
| 128 | + "## Loading a JSON file, creating chunks into docs\n", |
| 129 | + "This will load the webpage from the url provided, and then chunk the html text into passage docs." |
| 130 | + ] |
| 131 | + }, |
| 132 | + { |
| 133 | + "cell_type": "code", |
| 134 | + "execution_count": 7, |
| 135 | + "metadata": { |
| 136 | + "id": "7bN32vunqIk2" |
| 137 | + }, |
| 138 | + "outputs": [], |
| 139 | + "source": [ |
| 140 | + "from langchain_community.document_loaders import JSONLoader\n", |
| 141 | + "from langchain.text_splitter import RecursiveCharacterTextSplitter\n", |
| 142 | + "from urllib.request import urlopen\n", |
| 143 | + "import json\n", |
| 144 | + "\n", |
| 145 | + "# Change the URL to the desired dataset\n", |
| 146 | + "url = \"https://raw.githubusercontent.com/elastic/elasticsearch-labs/main/datasets/workplace-documents.json\"\n", |
| 147 | + "\n", |
| 148 | + "response = urlopen(url)\n", |
| 149 | + "data = json.load(response)\n", |
| 150 | + "\n", |
| 151 | + "with open(\"temp.json\", \"w\") as json_file:\n", |
| 152 | + " json.dump(data, json_file)\n", |
| 153 | + "\n", |
| 154 | + "\n", |
| 155 | + "# Metadata function to extract metadata from the record\n", |
| 156 | + "def metadata_func(record: dict, metadata: dict) -> dict:\n", |
| 157 | + " metadata[\"name\"] = record.get(\"name\")\n", |
| 158 | + " metadata[\"summary\"] = record.get(\"summary\")\n", |
| 159 | + " metadata[\"url\"] = record.get(\"url\")\n", |
| 160 | + " metadata[\"category\"] = record.get(\"category\")\n", |
| 161 | + " metadata[\"updated_at\"] = record.get(\"updated_at\")\n", |
| 162 | + "\n", |
| 163 | + " return metadata\n", |
| 164 | + "\n", |
| 165 | + "\n", |
| 166 | + "# For more loaders https://python.langchain.com/docs/modules/data_connection/document_loaders/\n", |
| 167 | + "# And 3rd party loaders https://python.langchain.com/docs/modules/data_connection/document_loaders/#third-party-loaders\n", |
| 168 | + "loader = JSONLoader(\n", |
| 169 | + " file_path=\"temp.json\",\n", |
| 170 | + " jq_schema=\".[]\",\n", |
| 171 | + " content_key=\"content\",\n", |
| 172 | + " metadata_func=metadata_func,\n", |
| 173 | + ")\n", |
| 174 | + "\n", |
| 175 | + "text_splitter = RecursiveCharacterTextSplitter.from_tiktoken_encoder(\n", |
| 176 | + " chunk_size=512, chunk_overlap=256\n", |
| 177 | + ")\n", |
| 178 | + "docs = loader.load_and_split(text_splitter=text_splitter)" |
| 179 | + ] |
| 180 | + }, |
| 181 | + { |
| 182 | + "cell_type": "markdown", |
| 183 | + "metadata": {}, |
| 184 | + "source": [ |
| 185 | + "## Ingesting the passages into Elasticsearch\n", |
| 186 | + "This will ingest the passage docs into the Elasticsearch index, under the specified INDEX_NAME." |
| 187 | + ] |
| 188 | + }, |
| 189 | + { |
| 190 | + "cell_type": "code", |
| 191 | + "execution_count": null, |
| 192 | + "metadata": { |
| 193 | + "id": "0xtdeIJI9N9-" |
| 194 | + }, |
| 195 | + "outputs": [], |
| 196 | + "source": [ |
| 197 | + "from langchain_elasticsearch import ElasticsearchStore\n", |
| 198 | + "\n", |
| 199 | + "INDEX_NAME = \"json_chunked_index\"\n", |
| 200 | + "\n", |
| 201 | + "ElasticsearchStore.from_documents(\n", |
| 202 | + " docs,\n", |
| 203 | + " es_connection=client,\n", |
| 204 | + " index_name=INDEX_NAME,\n", |
| 205 | + " strategy=ElasticsearchStore.SparseVectorRetrievalStrategy(model_id=model),\n", |
| 206 | + " bulk_kwargs={\n", |
| 207 | + " \"request_timeout\": 180,\n", |
| 208 | + " },\n", |
| 209 | + ")" |
| 210 | + ] |
| 211 | + } |
| 212 | + ], |
| 213 | + "metadata": { |
| 214 | + "colab": { |
| 215 | + "include_colab_link": true, |
| 216 | + "provenance": [] |
| 217 | + }, |
| 218 | + "kernelspec": { |
| 219 | + "display_name": "Python 3", |
| 220 | + "name": "python3" |
| 221 | + }, |
| 222 | + "language_info": { |
| 223 | + "codemirror_mode": { |
| 224 | + "name": "ipython", |
| 225 | + "version": 3 |
| 226 | + }, |
| 227 | + "file_extension": ".py", |
| 228 | + "mimetype": "text/x-python", |
| 229 | + "name": "python", |
| 230 | + "nbconvert_exporter": "python", |
| 231 | + "pygments_lexer": "ipython3", |
| 232 | + "version": "3.10.3" |
| 233 | + } |
| 234 | + }, |
| 235 | + "nbformat": 4, |
| 236 | + "nbformat_minor": 0 |
| 237 | +} |
0 commit comments