|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "a3ce962e", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "## Loading Data into Weaviate with `unstructured`\n", |
| 9 | + "\n", |
| 10 | + "This notebook shows a basic workflow for uploading document elements into Weaviate using the `unstructured` library. To get started with this notebook, first install the dependencies with `pip install -r requirements.txt` and start the Weaviate docker container with `docker-compose up`." |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "code", |
| 15 | + "execution_count": 1, |
| 16 | + "id": "5d9ffc17", |
| 17 | + "metadata": {}, |
| 18 | + "outputs": [], |
| 19 | + "source": [ |
| 20 | + "import json\n", |
| 21 | + "\n", |
| 22 | + "import tqdm\n", |
| 23 | + "from unstructured.partition.pdf import partition_pdf\n", |
| 24 | + "from unstructured.staging.weaviate import create_unstructured_weaviate_class, stage_for_weaviate\n", |
| 25 | + "import weaviate\n", |
| 26 | + "from weaviate.util import generate_uuid5" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "markdown", |
| 31 | + "id": "673715e9", |
| 32 | + "metadata": {}, |
| 33 | + "source": [ |
| 34 | + "The first step is to partition the document using the `unstructured` library. In the following example, we partition a PDF with `partition_pdf`. You can also partition over a dozen document types with the `partition` function." |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": 2, |
| 40 | + "id": "f9fc0cf9", |
| 41 | + "metadata": {}, |
| 42 | + "outputs": [], |
| 43 | + "source": [ |
| 44 | + "filename = \"../../example-docs/layout-parser-paper-fast.pdf\"\n", |
| 45 | + "elements = partition_pdf(filename=filename, strategy=\"fast\")" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "markdown", |
| 50 | + "id": "3ae76364", |
| 51 | + "metadata": {}, |
| 52 | + "source": [ |
| 53 | + "Next, we'll create a schema for our Weaviate database using the `create_unstructured_weaviate_class` helper function from the `unstructured` library. The helper function generates a schema that includes all of the elements in the `ElementMetadata` object from `unstructured`. This includes information such as the filename and the page number of the document element. After specifying the schema, we create a connection to the database with the Weaviate client library and create the schema. You can change the name of the class by updating the `unstructured_class_name` variable." |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
| 58 | + "execution_count": 3, |
| 59 | + "id": "91057cb1", |
| 60 | + "metadata": {}, |
| 61 | + "outputs": [], |
| 62 | + "source": [ |
| 63 | + "unstructured_class_name = \"UnstructuredDocument\"" |
| 64 | + ] |
| 65 | + }, |
| 66 | + { |
| 67 | + "cell_type": "code", |
| 68 | + "execution_count": 4, |
| 69 | + "id": "78e804bb", |
| 70 | + "metadata": {}, |
| 71 | + "outputs": [], |
| 72 | + "source": [ |
| 73 | + "unstructured_class = create_unstructured_weaviate_class(unstructured_class_name)\n", |
| 74 | + "schema = {\"classes\": [unstructured_class]} " |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + "cell_type": "code", |
| 79 | + "execution_count": 5, |
| 80 | + "id": "3e317a2d", |
| 81 | + "metadata": {}, |
| 82 | + "outputs": [], |
| 83 | + "source": [ |
| 84 | + "client = weaviate.Client(\"http://localhost:8080\")" |
| 85 | + ] |
| 86 | + }, |
| 87 | + { |
| 88 | + "cell_type": "code", |
| 89 | + "execution_count": 6, |
| 90 | + "id": "0c508784", |
| 91 | + "metadata": {}, |
| 92 | + "outputs": [], |
| 93 | + "source": [ |
| 94 | + "client.schema.create(schema)" |
| 95 | + ] |
| 96 | + }, |
| 97 | + { |
| 98 | + "cell_type": "markdown", |
| 99 | + "id": "024ae133", |
| 100 | + "metadata": {}, |
| 101 | + "source": [ |
| 102 | + "Next, we stage the elements for Weaviate using the `stage_for_weaviate` function and batch upload the results to Weaviate. `stage_for_weaviate` outputs a dictionary that conforms to the schema we created earlier. Once that data is stage, we can use the Weaviate client library to batch upload the results to Weaviate." |
| 103 | + ] |
| 104 | + }, |
| 105 | + { |
| 106 | + "cell_type": "code", |
| 107 | + "execution_count": 7, |
| 108 | + "id": "a7018bb1", |
| 109 | + "metadata": {}, |
| 110 | + "outputs": [], |
| 111 | + "source": [ |
| 112 | + "data_objects = stage_for_weaviate(elements)" |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "cell_type": "code", |
| 117 | + "execution_count": 8, |
| 118 | + "id": "af712d8e", |
| 119 | + "metadata": {}, |
| 120 | + "outputs": [ |
| 121 | + { |
| 122 | + "name": "stderr", |
| 123 | + "output_type": "stream", |
| 124 | + "text": [ |
| 125 | + "100%|██████████████████████████████████████████████████████████████████████| 28/28 [00:46<00:00, 1.66s/it]\n" |
| 126 | + ] |
| 127 | + } |
| 128 | + ], |
| 129 | + "source": [ |
| 130 | + "with client.batch(batch_size=10) as batch:\n", |
| 131 | + " for data_object in tqdm.tqdm(data_objects):\n", |
| 132 | + " batch.add_data_object(\n", |
| 133 | + " data_object,\n", |
| 134 | + " unstructured_class_name,\n", |
| 135 | + " uuid=generate_uuid5(data_object),\n", |
| 136 | + " )" |
| 137 | + ] |
| 138 | + }, |
| 139 | + { |
| 140 | + "cell_type": "markdown", |
| 141 | + "id": "dac10bf5", |
| 142 | + "metadata": {}, |
| 143 | + "source": [ |
| 144 | + "Now that the documents are in Weaviate, we're able to run queries against Weaviate!" |
| 145 | + ] |
| 146 | + }, |
| 147 | + { |
| 148 | + "cell_type": "code", |
| 149 | + "execution_count": 9, |
| 150 | + "id": "14098434", |
| 151 | + "metadata": {}, |
| 152 | + "outputs": [ |
| 153 | + { |
| 154 | + "name": "stdout", |
| 155 | + "output_type": "stream", |
| 156 | + "text": [ |
| 157 | + "{\n", |
| 158 | + " \"data\": {\n", |
| 159 | + " \"Get\": {\n", |
| 160 | + " \"UnstructuredDocument\": [\n", |
| 161 | + " {\n", |
| 162 | + " \"text\": \"Deep Learning(DL)-based approaches are the state-of-the-art for a wide range of document image analysis (DIA) tasks including document image classi\\ufb01cation [11,\"\n", |
| 163 | + " }\n", |
| 164 | + " ]\n", |
| 165 | + " }\n", |
| 166 | + " }\n", |
| 167 | + "}\n" |
| 168 | + ] |
| 169 | + } |
| 170 | + ], |
| 171 | + "source": [ |
| 172 | + "near_text = {\"concepts\": [\"document understanding\"]}\n", |
| 173 | + "\n", |
| 174 | + "result = (\n", |
| 175 | + " client.query\n", |
| 176 | + " .get(\"UnstructuredDocument\", [\"text\"])\n", |
| 177 | + " .with_near_text(near_text)\n", |
| 178 | + " .with_limit(1)\n", |
| 179 | + " .do()\n", |
| 180 | + ")\n", |
| 181 | + "\n", |
| 182 | + "print(json.dumps(result, indent=4))" |
| 183 | + ] |
| 184 | + }, |
| 185 | + { |
| 186 | + "cell_type": "code", |
| 187 | + "execution_count": null, |
| 188 | + "id": "c191217c", |
| 189 | + "metadata": {}, |
| 190 | + "outputs": [], |
| 191 | + "source": [] |
| 192 | + } |
| 193 | + ], |
| 194 | + "metadata": { |
| 195 | + "kernelspec": { |
| 196 | + "display_name": "Python 3 (ipykernel)", |
| 197 | + "language": "python", |
| 198 | + "name": "python3" |
| 199 | + }, |
| 200 | + "language_info": { |
| 201 | + "codemirror_mode": { |
| 202 | + "name": "ipython", |
| 203 | + "version": 3 |
| 204 | + }, |
| 205 | + "file_extension": ".py", |
| 206 | + "mimetype": "text/x-python", |
| 207 | + "name": "python", |
| 208 | + "nbconvert_exporter": "python", |
| 209 | + "pygments_lexer": "ipython3", |
| 210 | + "version": "3.8.13" |
| 211 | + } |
| 212 | + }, |
| 213 | + "nbformat": 4, |
| 214 | + "nbformat_minor": 5 |
| 215 | +} |
0 commit comments