Skip to content

Instantly share code, notes, and snippets.

@hsm207
Created May 31, 2023 15:17
Show Gist options
  • Select an option

  • Save hsm207/d054d385bcca2f55b094590198678eaa to your computer and use it in GitHub Desktop.

Select an option

Save hsm207/d054d385bcca2f55b094590198678eaa to your computer and use it in GitHub Desktop.

Revisions

  1. hsm207 created this gist May 31, 2023.
    146 changes: 146 additions & 0 deletions multidim.ipynb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,146 @@
    {
    "cells": [
    {
    "cell_type": "code",
    "execution_count": 1,
    "metadata": {},
    "outputs": [],
    "source": [
    "import weaviate\n",
    "from weaviate.embedded import EmbeddedOptions\n",
    "import numpy as np"
    ]
    },
    {
    "attachments": {},
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "Instantiate a weaviate instance:"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 2,
    "metadata": {},
    "outputs": [
    {
    "name": "stdout",
    "output_type": "stream",
    "text": [
    "Started /root/.cache/weaviate-embedded: process ID 6011\n"
    ]
    },
    {
    "name": "stderr",
    "output_type": "stream",
    "text": [
    "{\"action\":\"startup\",\"default_vectorizer_module\":\"none\",\"level\":\"info\",\"msg\":\"the default vectorizer modules is set to \\\"none\\\", as a result all new schema classes without an explicit vectorizer setting, will use this vectorizer\",\"time\":\"2023-05-31T15:16:27Z\"}\n",
    "{\"action\":\"startup\",\"auto_schema_enabled\":true,\"level\":\"info\",\"msg\":\"auto schema enabled setting is set to \\\"true\\\"\",\"time\":\"2023-05-31T15:16:27Z\"}\n",
    "{\"action\":\"hnsw_vector_cache_prefill\",\"count\":3000,\"index_id\":\"image_UNRDEZyQjeKA\",\"level\":\"info\",\"limit\":1000000000000,\"msg\":\"prefilled vector cache\",\"time\":\"2023-05-31T15:16:27Z\",\"took\":41666}\n",
    "{\"level\":\"warning\",\"msg\":\"Multiple vector spaces are present, GraphQL Explore and REST API list objects endpoint module include params has been disabled as a result.\",\"time\":\"2023-05-31T15:16:27Z\"}\n",
    "{\"action\":\"grpc_startup\",\"level\":\"info\",\"msg\":\"grpc server listening at [::]:50051\",\"time\":\"2023-05-31T15:16:27Z\"}\n",
    "{\"action\":\"restapi_management\",\"level\":\"info\",\"msg\":\"Serving weaviate at http://127.0.0.1:6666\",\"time\":\"2023-05-31T15:16:27Z\"}\n",
    "/usr/local/lib/python3.11/subprocess.py:1125: ResourceWarning: subprocess 6011 is still running\n",
    " _warn(\"subprocess %s is still running\" % self.pid,\n",
    "ResourceWarning: Enable tracemalloc to get the object allocation traceback\n"
    ]
    }
    ],
    "source": [
    "client = weaviate.Client(\n",
    " embedded_options=EmbeddedOptions()\n",
    ")"
    ]
    },
    {
    "attachments": {},
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "Uploading a 1-dimensional array is fine:"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 3,
    "metadata": {},
    "outputs": [
    {
    "data": {
    "text/plain": [
    "'952b1d76-1f2c-4535-b518-63be66ff5048'"
    ]
    },
    "execution_count": 3,
    "metadata": {},
    "output_type": "execute_result"
    }
    ],
    "source": [
    "data_obj = {\n",
    " \"image\": np.random.rand(3).tolist()\n",
    "}\n",
    "\n",
    "client.data_object.create(data_obj, \"Image\")\n",
    "\n"
    ]
    },
    {
    "attachments": {},
    "cell_type": "markdown",
    "metadata": {},
    "source": [
    "Uploading N-dimensional array will throw an error:"
    ]
    },
    {
    "cell_type": "code",
    "execution_count": 4,
    "metadata": {},
    "outputs": [
    {
    "ename": "UnexpectedStatusCodeException",
    "evalue": "Creating object! Unexpected status code: 422, with response body: {'error': [{'message': \"invalid object: invalid number array property 'image' on class 'Image': invalid integer array value: [[[0.09662202892002447 0.26587363850560963 0.2526249753249421] [0.5997894079991571 0.04508543602843351 0.33456907195307883] [0.2994446041482661 0.38215130732254976 0.9951704047699703]] [[0.10272631763565154 0.10046049202936658 0.34985564198291563] [0.1638687659078779 0.2759878216812781 0.5558858020132917] [0.2679400036874593 0.8761890340851821 0.8882664174625479]] [[0.07289126419364189 0.33216443156345987 0.2612265651805187] [0.4410875537145824 0.7701331990444771 0.4107703234139375] [0.8238581460532814 0.13519572755080067 0.39933404046125975]]]\"}]}.",
    "output_type": "error",
    "traceback": [
    "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
    "\u001b[0;31mUnexpectedStatusCodeException\u001b[0m Traceback (most recent call last)",
    "Cell \u001b[0;32mIn[4], line 5\u001b[0m\n\u001b[1;32m 1\u001b[0m data_obj \u001b[39m=\u001b[39m {\n\u001b[1;32m 2\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mimage\u001b[39m\u001b[39m\"\u001b[39m: np\u001b[39m.\u001b[39mrandom\u001b[39m.\u001b[39mrand(\u001b[39m3\u001b[39m,\u001b[39m3\u001b[39m,\u001b[39m3\u001b[39m)\u001b[39m.\u001b[39mtolist()\n\u001b[1;32m 3\u001b[0m }\n\u001b[0;32m----> 5\u001b[0m client\u001b[39m.\u001b[39;49mdata_object\u001b[39m.\u001b[39;49mcreate(data_obj, \u001b[39m\"\u001b[39;49m\u001b[39mImage\u001b[39;49m\u001b[39m\"\u001b[39;49m)\n",
    "File \u001b[0;32m/usr/local/lib/python3.11/site-packages/weaviate/data/crud_data.py:154\u001b[0m, in \u001b[0;36mDataObject.create\u001b[0;34m(self, data_object, class_name, uuid, vector, consistency_level)\u001b[0m\n\u001b[1;32m 152\u001b[0m \u001b[39mif\u001b[39;00m object_does_already_exist:\n\u001b[1;32m 153\u001b[0m \u001b[39mraise\u001b[39;00m ObjectAlreadyExistsException(\u001b[39mstr\u001b[39m(uuid))\n\u001b[0;32m--> 154\u001b[0m \u001b[39mraise\u001b[39;00m UnexpectedStatusCodeException(\u001b[39m\"\u001b[39m\u001b[39mCreating object\u001b[39m\u001b[39m\"\u001b[39m, response)\n",
    "\u001b[0;31mUnexpectedStatusCodeException\u001b[0m: Creating object! Unexpected status code: 422, with response body: {'error': [{'message': \"invalid object: invalid number array property 'image' on class 'Image': invalid integer array value: [[[0.09662202892002447 0.26587363850560963 0.2526249753249421] [0.5997894079991571 0.04508543602843351 0.33456907195307883] [0.2994446041482661 0.38215130732254976 0.9951704047699703]] [[0.10272631763565154 0.10046049202936658 0.34985564198291563] [0.1638687659078779 0.2759878216812781 0.5558858020132917] [0.2679400036874593 0.8761890340851821 0.8882664174625479]] [[0.07289126419364189 0.33216443156345987 0.2612265651805187] [0.4410875537145824 0.7701331990444771 0.4107703234139375] [0.8238581460532814 0.13519572755080067 0.39933404046125975]]]\"}]}."
    ]
    }
    ],
    "source": [
    "data_obj = {\n",
    " \"image\": np.random.rand(3,3,3).tolist()\n",
    "}\n",
    "\n",
    "client.data_object.create(data_obj, \"Image\")\n"
    ]
    }
    ],
    "metadata": {
    "kernelspec": {
    "display_name": "Python 3",
    "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.11.3"
    },
    "orig_nbformat": 4
    },
    "nbformat": 4,
    "nbformat_minor": 2
    }