Spaces:
Sleeping
Sleeping
File size: 54,740 Bytes
93e1b64 4ae75ec 93e1b64 4ae75ec 93e1b64 4ae75ec 93e1b64 b0c103a 93e1b64 4ae75ec 93e1b64 4ae75ec 93e1b64 4ae75ec 93e1b64 b0c103a 93e1b64 4ae75ec 93e1b64 b0c103a 93e1b64 b0c103a 93e1b64 27d40b9 93e1b64 27d40b9 93e1b64 4ae75ec 93e1b64 4ae75ec 93e1b64 4ae75ec 93e1b64 4ae75ec 93e1b64 4ae75ec 93e1b64 4ae75ec 93e1b64 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Vector Search "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os, pandas as pd\n",
"from sqlalchemy import create_engine, text"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"username = 'demo'\n",
"password = 'demo'\n",
"hostname = os.getenv('IRIS_HOSTNAME', 'localhost')\n",
"port = '1972' \n",
"namespace = 'USER'\n",
"CONNECTION_STRING = f\"iris://{username}:{password}@{hostname}:{port}/{namespace}\"\n",
"\n",
"engine = create_engine(CONNECTION_STRING)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"# Load knowledge graph\n",
"entity_embeddings = pd.read_csv('./entity_embeddings.csv', index_col=0)\n",
"entity_embeddings[\"embedding\"] = entity_embeddings[\"embedding\"].apply(\n",
" lambda x: x[1:-1])\n",
"\n",
"len_label = entity_embeddings['label'].str.len().max()\n",
"len_uri = entity_embeddings['uri'].str.len().max()\n",
"# TODO: set varchar length dynamically as above\n",
"with engine.connect() as conn:\n",
" with conn.begin(): \n",
" result = conn.execute(text('DROP TABLE IF EXISTS Test.EntityEmbeddings'))\n",
" sql = f\"\"\"\n",
" CREATE TABLE Test.EntityEmbeddings (\n",
" embedding VECTOR(DOUBLE, 50),\n",
" label VARCHAR({len_label}),\n",
" uri VARCHAR({len_uri})\n",
" )\n",
" \"\"\"\n",
" result = conn.execute(text(sql))\n",
"\n",
"with engine.connect() as conn:\n",
" with conn.begin():\n",
" for index, row in entity_embeddings.iterrows():\n",
" sql = text(\"\"\"\n",
" INSERT INTO Test.EntityEmbeddings \n",
" (embedding, label, uri) \n",
" VALUES (TO_VECTOR(:embedding), :label, :uri)\n",
" \"\"\")\n",
" conn.execute(sql, {\n",
" 'embedding': str(row['embedding']),\n",
" 'label': row['label'], \n",
" 'uri': row['uri']\n",
" })\n"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Exception during reset or similar\n",
"Traceback (most recent call last):\n",
" File \"/var/folders/g5/qwphv0rn5tvflncj75xsxvxw0000gn/T/ipykernel_77641/1077196279.py\", line 11, in <module>\n",
" result = conn.execute(text(sql))\n",
" ^^^^^^^^^^^^^^^^^^^^^^^\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py\", line 1416, in execute\n",
" return meth(\n",
" ^^^^^\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/sql/elements.py\", line 517, in _execute_on_connection\n",
" return connection._execute_clauseelement(\n",
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py\", line 1639, in _execute_clauseelement\n",
" ret = self._execute_context(\n",
" ^^^^^^^^^^^^^^^^^^^^^^\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py\", line 1848, in _execute_context\n",
" return self._exec_single_context(\n",
" ^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py\", line 1988, in _exec_single_context\n",
" self._handle_dbapi_exception(\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py\", line 2347, in _handle_dbapi_exception\n",
" raise exc_info[1].with_traceback(exc_info[2])\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py\", line 1969, in _exec_single_context\n",
" self.dialect.do_execute(\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy_iris/base.py\", line 1060, in do_execute\n",
" cursor.execute(query, params)\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/dbapi/_DBAPI.py\", line 268, in execute\n",
" self._execute()\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/dbapi/_DBAPI.py\", line 528, in _execute\n",
" return exec_func()\n",
" ^^^^^^^^^^^\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/dbapi/_DBAPI.py\", line 574, in _execute_query\n",
" self._send_direct_query_request()\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/dbapi/_DBAPI.py\", line 1159, in _send_direct_query_request\n",
" self._in_message._read_message_sql(sequence_number, self._statement_id, _InStream.FETCH_DATA, [100])\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/_InStream.py\", line 46, in _read_message_sql\n",
" is_for_gateway = self.__read_message_internal(expected_message_id, expected_statement_id, type)\n",
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/_InStream.py\", line 59, in __read_message_internal\n",
" self.__read_buffer(header.buffer, 0, _MessageHeader.HEADER_SIZE)\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/_InStream.py\", line 146, in __read_buffer\n",
" raise Exception(\"Server unexpectedly closing communication device\")\n",
"Exception: Server unexpectedly closing communication device\n",
"\n",
"During handling of the above exception, another exception occurred:\n",
"\n",
"Traceback (most recent call last):\n",
" File \"/var/folders/g5/qwphv0rn5tvflncj75xsxvxw0000gn/T/ipykernel_77641/1077196279.py\", line 3, in <module>\n",
" with conn.begin():\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/util.py\", line 161, in __exit__\n",
" self.rollback()\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py\", line 2599, in rollback\n",
" self._do_rollback()\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py\", line 2718, in _do_rollback\n",
" self._close_impl(try_deactivate=True)\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py\", line 2701, in _close_impl\n",
" self._connection_rollback_impl()\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py\", line 2693, in _connection_rollback_impl\n",
" self.connection._rollback_impl()\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py\", line 1121, in _rollback_impl\n",
" self._handle_dbapi_exception(e, None, None, None, None)\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py\", line 2347, in _handle_dbapi_exception\n",
" raise exc_info[1].with_traceback(exc_info[2])\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py\", line 1119, in _rollback_impl\n",
" self.engine.dialect.do_rollback(self.connection)\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy_iris/base.py\", line 1072, in do_rollback\n",
" connection.rollback()\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/_IRISConnection.py\", line 494, in rollback\n",
" self._in_message._read_message_sql(sequence_number, -1, 0, [0])\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/_InStream.py\", line 46, in _read_message_sql\n",
" is_for_gateway = self.__read_message_internal(expected_message_id, expected_statement_id, type)\n",
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/_InStream.py\", line 59, in __read_message_internal\n",
" self.__read_buffer(header.buffer, 0, _MessageHeader.HEADER_SIZE)\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/_InStream.py\", line 138, in __read_buffer\n",
" data = self._device.recv(length)\n",
" ^^^^^^^^^^^^^^^^^^^^^^^^^\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/_Device.py\", line 40, in recv\n",
" return self._socket.recv(len)\n",
" ^^^^^^^^^^^^^^^^^^^^^^\n",
"ConnectionResetError: [Errno 54] Connection reset by peer\n",
"\n",
"During handling of the above exception, another exception occurred:\n",
"\n",
"Traceback (most recent call last):\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/pool/base.py\", line 988, in _finalize_fairy\n",
" fairy._reset(\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/pool/base.py\", line 1438, in _reset\n",
" pool._dialect.do_rollback(self)\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy_iris/base.py\", line 1072, in do_rollback\n",
" connection.rollback()\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/_IRISConnection.py\", line 493, in rollback\n",
" self._out_message._send(sequence_number)\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/_OutStream.py\", line 18, in _send\n",
" self._device.sendall(self.wire._get_buffer())\n",
" File \"/Users/aldan.creo/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/_Device.py\", line 37, in sendall\n",
" return self._socket.sendall(buffer)\n",
" ^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n",
"BrokenPipeError: [Errno 32] Broken pipe\n"
]
},
{
"ename": "ConnectionResetError",
"evalue": "[Errno 54] Connection reset by peer",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mException\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[19], line 11\u001b[0m\n\u001b[1;32m 4\u001b[0m sql \u001b[38;5;241m=\u001b[39m \u001b[38;5;124mf\u001b[39m\u001b[38;5;124m\"\"\"\u001b[39m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;124m SELECT TOP 10 e1.uri AS uri1, e2.uri AS uri2, e1.label AS label1, e2.label AS label2,\u001b[39m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;124m VECTOR_COSINE(e1.embedding, e2.embedding) AS distance\u001b[39m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[38;5;124m ORDER BY distance DESC\u001b[39m\n\u001b[1;32m 10\u001b[0m \u001b[38;5;124m \u001b[39m\u001b[38;5;124m\"\"\"\u001b[39m\n\u001b[0;32m---> 11\u001b[0m result \u001b[38;5;241m=\u001b[39m \u001b[43mconn\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexecute\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtext\u001b[49m\u001b[43m(\u001b[49m\u001b[43msql\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 12\u001b[0m data \u001b[38;5;241m=\u001b[39m result\u001b[38;5;241m.\u001b[39mfetchall()\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py:1416\u001b[0m, in \u001b[0;36mConnection.execute\u001b[0;34m(self, statement, parameters, execution_options)\u001b[0m\n\u001b[1;32m 1415\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 1416\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mmeth\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1417\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1418\u001b[0m \u001b[43m \u001b[49m\u001b[43mdistilled_parameters\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1419\u001b[0m \u001b[43m \u001b[49m\u001b[43mexecution_options\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;129;43;01mor\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mNO_OPTIONS\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1420\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/sql/elements.py:517\u001b[0m, in \u001b[0;36mClauseElement._execute_on_connection\u001b[0;34m(self, connection, distilled_params, execution_options)\u001b[0m\n\u001b[1;32m 516\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(\u001b[38;5;28mself\u001b[39m, Executable)\n\u001b[0;32m--> 517\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mconnection\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_execute_clauseelement\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 518\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mdistilled_params\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mexecution_options\u001b[49m\n\u001b[1;32m 519\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 520\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py:1639\u001b[0m, in \u001b[0;36mConnection._execute_clauseelement\u001b[0;34m(self, elem, distilled_parameters, execution_options)\u001b[0m\n\u001b[1;32m 1631\u001b[0m compiled_sql, extracted_params, cache_hit \u001b[38;5;241m=\u001b[39m elem\u001b[38;5;241m.\u001b[39m_compile_w_cache(\n\u001b[1;32m 1632\u001b[0m dialect\u001b[38;5;241m=\u001b[39mdialect,\n\u001b[1;32m 1633\u001b[0m compiled_cache\u001b[38;5;241m=\u001b[39mcompiled_cache,\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 1637\u001b[0m linting\u001b[38;5;241m=\u001b[39m\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mdialect\u001b[38;5;241m.\u001b[39mcompiler_linting \u001b[38;5;241m|\u001b[39m compiler\u001b[38;5;241m.\u001b[39mWARN_LINTING,\n\u001b[1;32m 1638\u001b[0m )\n\u001b[0;32m-> 1639\u001b[0m ret \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_execute_context\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1640\u001b[0m \u001b[43m \u001b[49m\u001b[43mdialect\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1641\u001b[0m \u001b[43m \u001b[49m\u001b[43mdialect\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexecution_ctx_cls\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_init_compiled\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1642\u001b[0m \u001b[43m \u001b[49m\u001b[43mcompiled_sql\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1643\u001b[0m \u001b[43m \u001b[49m\u001b[43mdistilled_parameters\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1644\u001b[0m \u001b[43m \u001b[49m\u001b[43mexecution_options\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1645\u001b[0m \u001b[43m \u001b[49m\u001b[43mcompiled_sql\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1646\u001b[0m \u001b[43m \u001b[49m\u001b[43mdistilled_parameters\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1647\u001b[0m \u001b[43m \u001b[49m\u001b[43melem\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1648\u001b[0m \u001b[43m \u001b[49m\u001b[43mextracted_params\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1649\u001b[0m \u001b[43m \u001b[49m\u001b[43mcache_hit\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43mcache_hit\u001b[49m\u001b[43m,\u001b[49m\n\u001b[1;32m 1650\u001b[0m \u001b[43m\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1651\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m has_events:\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py:1848\u001b[0m, in \u001b[0;36mConnection._execute_context\u001b[0;34m(self, dialect, constructor, statement, parameters, execution_options, *args, **kw)\u001b[0m\n\u001b[1;32m 1847\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m-> 1848\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_exec_single_context\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1849\u001b[0m \u001b[43m \u001b[49m\u001b[43mdialect\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcontext\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstatement\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mparameters\u001b[49m\n\u001b[1;32m 1850\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py:1988\u001b[0m, in \u001b[0;36mConnection._exec_single_context\u001b[0;34m(self, dialect, context, statement, parameters)\u001b[0m\n\u001b[1;32m 1987\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[0;32m-> 1988\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_handle_dbapi_exception\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1989\u001b[0m \u001b[43m \u001b[49m\u001b[43me\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstr_statement\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43meffective_parameters\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcursor\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcontext\u001b[49m\n\u001b[1;32m 1990\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1992\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m result\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py:2347\u001b[0m, in \u001b[0;36mConnection._handle_dbapi_exception\u001b[0;34m(self, e, statement, parameters, cursor, context, is_sub_exec)\u001b[0m\n\u001b[1;32m 2346\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m exc_info[\u001b[38;5;241m1\u001b[39m] \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m-> 2347\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exc_info[\u001b[38;5;241m1\u001b[39m]\u001b[38;5;241m.\u001b[39mwith_traceback(exc_info[\u001b[38;5;241m2\u001b[39m])\n\u001b[1;32m 2348\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py:1969\u001b[0m, in \u001b[0;36mConnection._exec_single_context\u001b[0;34m(self, dialect, context, statement, parameters)\u001b[0m\n\u001b[1;32m 1968\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m evt_handled:\n\u001b[0;32m-> 1969\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdialect\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdo_execute\u001b[49m\u001b[43m(\u001b[49m\n\u001b[1;32m 1970\u001b[0m \u001b[43m \u001b[49m\u001b[43mcursor\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mstr_statement\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43meffective_parameters\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mcontext\u001b[49m\n\u001b[1;32m 1971\u001b[0m \u001b[43m \u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1973\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_has_events \u001b[38;5;129;01mor\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mengine\u001b[38;5;241m.\u001b[39m_has_events:\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy_iris/base.py:1060\u001b[0m, in \u001b[0;36mIRISDialect.do_execute\u001b[0;34m(self, cursor, query, params, context)\u001b[0m\n\u001b[1;32m 1059\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_debug(query, params)\n\u001b[0;32m-> 1060\u001b[0m \u001b[43mcursor\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexecute\u001b[49m\u001b[43m(\u001b[49m\u001b[43mquery\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mparams\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/dbapi/_DBAPI.py:268\u001b[0m, in \u001b[0;36m_BaseCursor.execute\u001b[0;34m(self, operation, params)\u001b[0m\n\u001b[1;32m 266\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_cursor_type \u001b[38;5;241m=\u001b[39m CursorType\u001b[38;5;241m.\u001b[39mDEFAULT\n\u001b[0;32m--> 268\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_execute\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 269\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_rowcount\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/dbapi/_DBAPI.py:528\u001b[0m, in \u001b[0;36m_BaseCursor._execute\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 527\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[0;32m--> 528\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[43mexec_func\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/dbapi/_DBAPI.py:574\u001b[0m, in \u001b[0;36m_BaseCursor._execute_query\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 572\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 573\u001b[0m \u001b[38;5;66;03m# not found in client side cache - send DQ message\u001b[39;00m\n\u001b[0;32m--> 574\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_send_direct_query_request\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 575\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/dbapi/_DBAPI.py:1159\u001b[0m, in \u001b[0;36mCursor._send_direct_query_request\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 1158\u001b[0m \u001b[38;5;66;03m# retrieve data\u001b[39;00m\n\u001b[0;32m-> 1159\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_in_message\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_read_message_sql\u001b[49m\u001b[43m(\u001b[49m\u001b[43msequence_number\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_statement_id\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m_InStream\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mFETCH_DATA\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m100\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1160\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_sqlcode \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_in_message\u001b[38;5;241m.\u001b[39mwire\u001b[38;5;241m.\u001b[39mheader\u001b[38;5;241m.\u001b[39m_get_function_code()\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/_InStream.py:46\u001b[0m, in \u001b[0;36m_InStream._read_message_sql\u001b[0;34m(self, expected_message_id, expected_statement_id, type, allowedErrors)\u001b[0m\n\u001b[1;32m 45\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[0;32m---> 46\u001b[0m is_for_gateway \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m__read_message_internal\u001b[49m\u001b[43m(\u001b[49m\u001b[43mexpected_message_id\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mexpected_statement_id\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mtype\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 47\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_for_gateway:\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/_InStream.py:59\u001b[0m, in \u001b[0;36m_InStream.__read_message_internal\u001b[0;34m(self, expected_message_id, expected_statement_id, call_type)\u001b[0m\n\u001b[1;32m 58\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m high_bit \u001b[38;5;241m!=\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m---> 59\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m__read_buffer\u001b[49m\u001b[43m(\u001b[49m\u001b[43mheader\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbuffer\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m_MessageHeader\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mHEADER_SIZE\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 60\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_log_stream \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/_InStream.py:146\u001b[0m, in \u001b[0;36m_InStream.__read_buffer\u001b[0;34m(self, buffer, offset, length)\u001b[0m\n\u001b[1;32m 145\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(data) \u001b[38;5;241m==\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m--> 146\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mServer unexpectedly closing communication device\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 147\u001b[0m buffer[offset\u001b[38;5;241m+\u001b[39mcb:offset\u001b[38;5;241m+\u001b[39mcb\u001b[38;5;241m+\u001b[39m\u001b[38;5;28mlen\u001b[39m(data)] \u001b[38;5;241m=\u001b[39m data\n",
"\u001b[0;31mException\u001b[0m: Server unexpectedly closing communication device",
"\nDuring handling of the above exception, another exception occurred:\n",
"\u001b[0;31mConnectionResetError\u001b[0m Traceback (most recent call last)",
"Cell \u001b[0;32mIn[19], line 3\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[38;5;66;03m# Calculate distance between entities\u001b[39;00m\n\u001b[1;32m 2\u001b[0m \u001b[38;5;28;01mwith\u001b[39;00m engine\u001b[38;5;241m.\u001b[39mconnect() \u001b[38;5;28;01mas\u001b[39;00m conn:\n\u001b[0;32m----> 3\u001b[0m \u001b[43m \u001b[49m\u001b[38;5;28;43;01mwith\u001b[39;49;00m\u001b[43m \u001b[49m\u001b[43mconn\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbegin\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\u001b[43m:\u001b[49m\n\u001b[1;32m 4\u001b[0m \u001b[43m \u001b[49m\u001b[43msql\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m \u001b[49m\u001b[38;5;124;43mf\u001b[39;49m\u001b[38;5;124;43m\"\"\"\u001b[39;49m\n\u001b[1;32m 5\u001b[0m \u001b[38;5;124;43m SELECT TOP 10 e1.uri AS uri1, e2.uri AS uri2, e1.label AS label1, e2.label AS label2,\u001b[39;49m\n\u001b[1;32m 6\u001b[0m \u001b[38;5;124;43m VECTOR_COSINE(e1.embedding, e2.embedding) AS distance\u001b[39;49m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[38;5;124;43m ORDER BY distance DESC\u001b[39;49m\n\u001b[1;32m 10\u001b[0m \u001b[38;5;124;43m \u001b[39;49m\u001b[38;5;124;43m\"\"\"\u001b[39;49m\n\u001b[1;32m 11\u001b[0m \u001b[43m \u001b[49m\u001b[43mresult\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[43m \u001b[49m\u001b[43mconn\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mexecute\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtext\u001b[49m\u001b[43m(\u001b[49m\u001b[43msql\u001b[49m\u001b[43m)\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/util.py:161\u001b[0m, in \u001b[0;36mTransactionalContext.__exit__\u001b[0;34m(self, type_, value, traceback)\u001b[0m\n\u001b[1;32m 159\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 160\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_rollback_can_be_called():\n\u001b[0;32m--> 161\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrollback\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 162\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m 163\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m out_of_band_exit:\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py:2599\u001b[0m, in \u001b[0;36mTransaction.rollback\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 2582\u001b[0m \u001b[38;5;250m\u001b[39m\u001b[38;5;124;03m\"\"\"Roll back this :class:`.Transaction`.\u001b[39;00m\n\u001b[1;32m 2583\u001b[0m \n\u001b[1;32m 2584\u001b[0m \u001b[38;5;124;03mThe implementation of this may vary based on the type of transaction in\u001b[39;00m\n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 2596\u001b[0m \n\u001b[1;32m 2597\u001b[0m \u001b[38;5;124;03m\"\"\"\u001b[39;00m\n\u001b[1;32m 2598\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m-> 2599\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_do_rollback\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2600\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m 2601\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mis_active\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py:2718\u001b[0m, in \u001b[0;36mRootTransaction._do_rollback\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 2717\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_do_rollback\u001b[39m(\u001b[38;5;28mself\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m-> 2718\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_close_impl\u001b[49m\u001b[43m(\u001b[49m\u001b[43mtry_deactivate\u001b[49m\u001b[38;5;241;43m=\u001b[39;49m\u001b[38;5;28;43;01mTrue\u001b[39;49;00m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py:2701\u001b[0m, in \u001b[0;36mRootTransaction._close_impl\u001b[0;34m(self, try_deactivate)\u001b[0m\n\u001b[1;32m 2699\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[1;32m 2700\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mis_active:\n\u001b[0;32m-> 2701\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_connection_rollback_impl\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 2703\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mconnection\u001b[38;5;241m.\u001b[39m_nested_transaction:\n\u001b[1;32m 2704\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mconnection\u001b[38;5;241m.\u001b[39m_nested_transaction\u001b[38;5;241m.\u001b[39m_cancel()\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py:2693\u001b[0m, in \u001b[0;36mRootTransaction._connection_rollback_impl\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 2692\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_connection_rollback_impl\u001b[39m(\u001b[38;5;28mself\u001b[39m) \u001b[38;5;241m-\u001b[39m\u001b[38;5;241m>\u001b[39m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[0;32m-> 2693\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mconnection\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_rollback_impl\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py:1121\u001b[0m, in \u001b[0;36mConnection._rollback_impl\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 1119\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mengine\u001b[38;5;241m.\u001b[39mdialect\u001b[38;5;241m.\u001b[39mdo_rollback(\u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39mconnection)\n\u001b[1;32m 1120\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[0;32m-> 1121\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_handle_dbapi_exception\u001b[49m\u001b[43m(\u001b[49m\u001b[43me\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43;01mNone\u001b[39;49;00m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py:2347\u001b[0m, in \u001b[0;36mConnection._handle_dbapi_exception\u001b[0;34m(self, e, statement, parameters, cursor, context, is_sub_exec)\u001b[0m\n\u001b[1;32m 2345\u001b[0m \u001b[38;5;28;01melse\u001b[39;00m:\n\u001b[1;32m 2346\u001b[0m \u001b[38;5;28;01massert\u001b[39;00m exc_info[\u001b[38;5;241m1\u001b[39m] \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m\n\u001b[0;32m-> 2347\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m exc_info[\u001b[38;5;241m1\u001b[39m]\u001b[38;5;241m.\u001b[39mwith_traceback(exc_info[\u001b[38;5;241m2\u001b[39m])\n\u001b[1;32m 2348\u001b[0m \u001b[38;5;28;01mfinally\u001b[39;00m:\n\u001b[1;32m 2349\u001b[0m \u001b[38;5;28;01mdel\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_reentrant_error\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy/engine/base.py:1119\u001b[0m, in \u001b[0;36mConnection._rollback_impl\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 1117\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_log_info(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mROLLBACK\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[1;32m 1118\u001b[0m \u001b[38;5;28;01mtry\u001b[39;00m:\n\u001b[0;32m-> 1119\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mengine\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdialect\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mdo_rollback\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mconnection\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 1120\u001b[0m \u001b[38;5;28;01mexcept\u001b[39;00m \u001b[38;5;167;01mBaseException\u001b[39;00m \u001b[38;5;28;01mas\u001b[39;00m e:\n\u001b[1;32m 1121\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_handle_dbapi_exception(e, \u001b[38;5;28;01mNone\u001b[39;00m, \u001b[38;5;28;01mNone\u001b[39;00m, \u001b[38;5;28;01mNone\u001b[39;00m, \u001b[38;5;28;01mNone\u001b[39;00m)\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/sqlalchemy_iris/base.py:1072\u001b[0m, in \u001b[0;36mIRISDialect.do_rollback\u001b[0;34m(self, connection)\u001b[0m\n\u001b[1;32m 1071\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mdo_rollback\u001b[39m(\u001b[38;5;28mself\u001b[39m, connection):\n\u001b[0;32m-> 1072\u001b[0m \u001b[43mconnection\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrollback\u001b[49m\u001b[43m(\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/_IRISConnection.py:494\u001b[0m, in \u001b[0;36m_IRISConnection.rollback\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 492\u001b[0m sequence_number \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_get_new_sequence_number()\n\u001b[1;32m 493\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_out_message\u001b[38;5;241m.\u001b[39m_send(sequence_number)\n\u001b[0;32m--> 494\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_in_message\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_read_message_sql\u001b[49m\u001b[43m(\u001b[49m\u001b[43msequence_number\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m-\u001b[39;49m\u001b[38;5;241;43m1\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m[\u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m]\u001b[49m\u001b[43m)\u001b[49m\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/_InStream.py:46\u001b[0m, in \u001b[0;36m_InStream._read_message_sql\u001b[0;34m(self, expected_message_id, expected_statement_id, type, allowedErrors)\u001b[0m\n\u001b[1;32m 44\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21m_read_message_sql\u001b[39m(\u001b[38;5;28mself\u001b[39m, expected_message_id, expected_statement_id\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m-\u001b[39m\u001b[38;5;241m1\u001b[39m, \u001b[38;5;28mtype\u001b[39m\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m0\u001b[39m, allowedErrors\u001b[38;5;241m=\u001b[39m\u001b[38;5;28;01mNone\u001b[39;00m):\n\u001b[1;32m 45\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m \u001b[38;5;28;01mTrue\u001b[39;00m:\n\u001b[0;32m---> 46\u001b[0m is_for_gateway \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m__read_message_internal\u001b[49m\u001b[43m(\u001b[49m\u001b[43mexpected_message_id\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43mexpected_statement_id\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;28;43mtype\u001b[39;49m\u001b[43m)\u001b[49m\n\u001b[1;32m 47\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m is_for_gateway:\n\u001b[1;32m 48\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_connection\u001b[38;5;241m.\u001b[39m_get_gateway()\u001b[38;5;241m.\u001b[39m_dispatch_reentrancy(\u001b[38;5;28mself\u001b[39m)\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/_InStream.py:59\u001b[0m, in \u001b[0;36m_InStream.__read_message_internal\u001b[0;34m(self, expected_message_id, expected_statement_id, call_type)\u001b[0m\n\u001b[1;32m 57\u001b[0m final_buffer \u001b[38;5;241m=\u001b[39m \u001b[38;5;28mbytearray\u001b[39m()\n\u001b[1;32m 58\u001b[0m \u001b[38;5;28;01mwhile\u001b[39;00m high_bit \u001b[38;5;241m!=\u001b[39m \u001b[38;5;241m0\u001b[39m:\n\u001b[0;32m---> 59\u001b[0m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m__read_buffer\u001b[49m\u001b[43m(\u001b[49m\u001b[43mheader\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mbuffer\u001b[49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[38;5;241;43m0\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43m_MessageHeader\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mHEADER_SIZE\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 60\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_log_stream \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 61\u001b[0m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_log_stream\u001b[38;5;241m.\u001b[39m_dump_header(\n\u001b[1;32m 62\u001b[0m header\u001b[38;5;241m.\u001b[39mbuffer, _LogFileStream\u001b[38;5;241m.\u001b[39mLOG_RECEIVED, \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_connection)\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/_InStream.py:138\u001b[0m, in \u001b[0;36m_InStream.__read_buffer\u001b[0;34m(self, buffer, offset, length)\u001b[0m\n\u001b[1;32m 136\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mself\u001b[39m\u001b[38;5;241m.\u001b[39m_device \u001b[38;5;129;01mis\u001b[39;00m \u001b[38;5;28;01mNone\u001b[39;00m:\n\u001b[1;32m 137\u001b[0m \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mRuntimeError\u001b[39;00m(\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mno longer connected to server\u001b[39m\u001b[38;5;124m\"\u001b[39m)\n\u001b[0;32m--> 138\u001b[0m data \u001b[38;5;241m=\u001b[39m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_device\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrecv\u001b[49m\u001b[43m(\u001b[49m\u001b[43mlength\u001b[49m\u001b[43m)\u001b[49m\n\u001b[1;32m 139\u001b[0m buffer[offset:offset\u001b[38;5;241m+\u001b[39m\u001b[38;5;28mlen\u001b[39m(data)] \u001b[38;5;241m=\u001b[39m data\n\u001b[1;32m 140\u001b[0m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;28mlen\u001b[39m(data) \u001b[38;5;241m==\u001b[39m length:\n",
"File \u001b[0;32m~/miniconda3/envs/hackupc/lib/python3.11/site-packages/intersystems_iris/_Device.py:40\u001b[0m, in \u001b[0;36m_Device.recv\u001b[0;34m(self, len)\u001b[0m\n\u001b[1;32m 39\u001b[0m \u001b[38;5;28;01mdef\u001b[39;00m \u001b[38;5;21mrecv\u001b[39m(\u001b[38;5;28mself\u001b[39m, \u001b[38;5;28mlen\u001b[39m):\n\u001b[0;32m---> 40\u001b[0m \u001b[38;5;28;01mreturn\u001b[39;00m \u001b[38;5;28;43mself\u001b[39;49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43m_socket\u001b[49m\u001b[38;5;241;43m.\u001b[39;49m\u001b[43mrecv\u001b[49m\u001b[43m(\u001b[49m\u001b[38;5;28;43mlen\u001b[39;49m\u001b[43m)\u001b[49m\n",
"\u001b[0;31mConnectionResetError\u001b[0m: [Errno 54] Connection reset by peer"
]
}
],
"source": [
"# Calculate distance between entities\n",
"with engine.connect() as conn:\n",
" with conn.begin():\n",
" sql = f\"\"\"\n",
" SELECT TOP 10 e1.uri AS uri1, e2.uri AS uri2, e1.label AS label1, e2.label AS label2,\n",
" VECTOR_COSINE(e1.embedding, e2.embedding) AS distance\n",
" FROM Test.EntityEmbeddings e1, Test.EntityEmbeddings e2\n",
" WHERE e1.uri = 'http://identifiers.org/medgen/C0002395'\n",
" ORDER BY distance DESC\n",
" \"\"\"\n",
" result = conn.execute(text(sql))\n",
" data = result.fetchall()\n",
" display(data)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Load clinical trials\n",
"\n",
"relation_embeddings = pd.read_csv('./relation_embeddings.csv', index_col=0)\n",
"relation_embeddings[\"embedding\"] = relation_embeddings[\"embedding\"].apply(\n",
" lambda x: x[1:-1])\n",
"\n",
"len_label = relation_embeddings['label'].str.len().max()\n",
"len_uri = relation_embeddings['uri'].str.len().max()\n",
"# TODO: set varchar length dynamically as above\n",
"with engine.connect() as conn:\n",
" with conn.begin():# Load \n",
" result = conn.execute(text('DROP TABLE IF EXISTS Test.RelationEmbeddings'))\n",
" sql = f\"\"\"\n",
" CREATE TABLE Test.RelationEmbeddings (\n",
" embedding VECTOR(DOUBLE, 50),\n",
" label VARCHAR({len_label}),\n",
" uri VARCHAR({len_uri})\n",
" )\n",
" \"\"\"\n",
" result = conn.execute(text(sql))\n",
"\n",
"with engine.connect() as conn:\n",
" with conn.begin():\n",
" for index, row in relation_embeddings.iterrows():\n",
" sql = text(\"\"\"\n",
" INSERT INTO Test.RelationEmbeddings \n",
" (embedding, label, uri) \n",
" VALUES (TO_VECTOR(:embedding), :label, :uri)\n",
" \"\"\")\n",
" conn.execute(sql, {\n",
" 'embedding': str(row['embedding']),\n",
" 'label': row['label'], \n",
" 'uri': row['uri']\n",
" })"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>desease_condition</th>\n",
" <th>embeddings</th>\n",
" <th>nct_id</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>marijuana abuse, substance-related disorders, ...</td>\n",
" <td>-0.8323991298675537, 1.47855544090271, 0.00130...</td>\n",
" <td>NCT03055377</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>tuberculosis, latent tuberculosis, infections,...</td>\n",
" <td>-0.43443307280540466, 0.9625586271286011, -0.1...</td>\n",
" <td>NCT03042754</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>heart failure, heart diseases, cardiovascular ...</td>\n",
" <td>-0.5791705250740051, 0.13008448481559753, 0.13...</td>\n",
" <td>NCT03035123</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>lymphoma, neoplasms by histologic type, neopla...</td>\n",
" <td>-0.1608569175004959, 0.8489153981208801, -0.55...</td>\n",
" <td>NCT02272751</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>anemia, hematologic diseases</td>\n",
" <td>0.21379394829273224, 0.17073844373226166, -0.1...</td>\n",
" <td>NCT00931606</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" desease_condition \\\n",
"0 marijuana abuse, substance-related disorders, ... \n",
"1 tuberculosis, latent tuberculosis, infections,... \n",
"2 heart failure, heart diseases, cardiovascular ... \n",
"3 lymphoma, neoplasms by histologic type, neopla... \n",
"4 anemia, hematologic diseases \n",
"\n",
" embeddings nct_id \n",
"0 -0.8323991298675537, 1.47855544090271, 0.00130... NCT03055377 \n",
"1 -0.43443307280540466, 0.9625586271286011, -0.1... NCT03042754 \n",
"2 -0.5791705250740051, 0.13008448481559753, 0.13... NCT03035123 \n",
"3 -0.1608569175004959, 0.8489153981208801, -0.55... NCT02272751 \n",
"4 0.21379394829273224, 0.17073844373226166, -0.1... NCT00931606 "
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Load knowledge graph\n",
"clinical_trials = pd.read_csv(\"clinical_trials_embeddings.csv\")\n",
"clinical_trials[\"embeddings\"] = clinical_trials[\"embeddings\"].apply(lambda x: x[1:-1])\n",
"display(clinical_trials.head())\n",
"\n",
"# TODO: set varchar length dynamically as above\n",
"with engine.connect() as conn:\n",
" with conn.begin():\n",
" result = conn.execute(text(\"DROP TABLE IF EXISTS Test.ClinicalTrials\"))\n",
" sql = f\"\"\"\n",
" CREATE TABLE Test.ClinicalTrials (\n",
" nct_id VARCHAR(11) PRIMARY KEY,\n",
" diseases TEXT,\n",
" embedding VECTOR(DOUBLE, 768)\n",
" )\n",
" \"\"\"\n",
" result = conn.execute(text(sql))\n",
"\n",
"with engine.connect() as conn:\n",
" with conn.begin():\n",
" for index, row in clinical_trials.iterrows():\n",
"\n",
" sql = text(\n",
" \"\"\"\n",
" INSERT INTO Test.ClinicalTrials \n",
" (nct_id, diseases, embedding)\n",
" VALUES (:nct_id, :diseases, TO_VECTOR(:embedding))\n",
" \"\"\"\n",
" )\n",
" conn.execute(\n",
" sql,\n",
" {\n",
" \"nct_id\": row[\"nct_id\"],\n",
" \"diseases\": row[\"desease_condition\"],\n",
" \"embedding\": str(row[\"embeddings\"]),\n",
" },\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" definition \\\n",
"0 A sudden onset of abdominal pain with associat... \n",
"1 A type of abdominal pain characterized by a fe... \n",
"2 Distention of the abdomen. \n",
"3 An abnormal enlargement or swelling in the abd... \n",
"4 New abnormal growth of tissue in the ABDOMEN. \n",
"\n",
" uri \\\n",
"0 http://identifiers.org/medgen/C0000727 \n",
"1 http://identifiers.org/medgen/C0000729 \n",
"2 http://identifiers.org/medgen/C0000731 \n",
"3 http://identifiers.org/medgen/C0000734 \n",
"4 http://identifiers.org/medgen/C0000735 \n",
"\n",
" embeddings \n",
"0 0.07821787893772125, 0.9349365234375, -0.11445... \n",
"1 -0.48267558217048645, 0.8518325090408325, -0.1... \n",
"2 -0.4706612527370453, -0.23200057446956635, -0.... \n",
"3 0.03700314462184906, 0.6256464123725891, -0.47... \n",
"4 -0.5286742448806763, 0.06493321806192398, -1.0... \n"
]
}
],
"source": [
"# %%\n",
"import pandas as pd\n",
"import rdflib\n",
"\n",
"# Load the disease descriptions from MGDEF.RRF\n",
"df_disease_descriptions = pd.read_csv(\"disease_descriptions_with_embeddings.csv\")\n",
"df_disease_descriptions[\"embeddings\"] = df_disease_descriptions[\"embeddings\"].apply(lambda x: x[1:-1])\n",
"\n",
"with engine.connect() as conn:\n",
" with conn.begin(): \n",
" result = conn.execute(text('DROP TABLE IF EXISTS Test.DiseaseDescriptions'))\n",
" sql = f\"\"\"\n",
" CREATE TABLE Test.DiseaseDescriptions (\n",
" definition TEXT,\n",
" uri TEXT,\n",
" embedding VECTOR(DOUBLE, 768)\n",
" )\n",
" \"\"\"\n",
" result = conn.execute(text(sql))\n",
"\n",
"with engine.connect() as conn:\n",
" with conn.begin():\n",
" for index, row in df_disease_descriptions.iterrows():\n",
" sql = text(\"\"\"\n",
" INSERT INTO Test.DiseaseDescriptions \n",
" (uri, definition, embedding)\n",
" VALUES (:uri, :definition, TO_VECTOR(:embedding))\n",
" \"\"\")\n",
" conn.execute(sql, {\n",
" 'uri': row['uri'],\n",
" 'definition': row['definition'],\n",
" 'embedding': str(row['embeddings'])\n",
" })"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "treehacks",
"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.9"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|