From 7a6e8234c80197f0db18e4c0ce927bdcd16f126b Mon Sep 17 00:00:00 2001 From: jamescalam <james.briggs@hotmail.com> Date: Thu, 28 Nov 2024 22:24:21 +0100 Subject: [PATCH] fix: sync issues --- docs/indexes/pinecone-sync-routes.ipynb | 10 +-- poetry.lock | 82 +++++++++++++++++++++---- pyproject.toml | 2 +- semantic_router/encoders/bm25.py | 1 - semantic_router/index/__init__.py | 2 + semantic_router/index/pinecone.py | 1 - semantic_router/index/postgres.py | 17 +++-- semantic_router/routers/base.py | 4 -- semantic_router/routers/semantic.py | 3 - tests/unit/test_sync.py | 51 +++++++++++++-- 10 files changed, 137 insertions(+), 36 deletions(-) diff --git a/docs/indexes/pinecone-sync-routes.ipynb b/docs/indexes/pinecone-sync-routes.ipynb index d2ba412f..0c6dc4e0 100644 --- a/docs/indexes/pinecone-sync-routes.ipynb +++ b/docs/indexes/pinecone-sync-routes.ipynb @@ -125,14 +125,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "## RouteLayer" + "## SemanticRouter" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "The `RouteLayer` class supports both sync and async operations by default, so we initialize as usual:" + "The `SemanticRouter` class supports both sync and async operations by default, so we initialize as usual:" ] }, { @@ -160,10 +160,10 @@ } ], "source": [ - "from semantic_router.routers import RouteLayer\n", + "from semantic_router.routers import SemanticRouter\n", "import time\n", "\n", - "rl = RouteLayer(encoder=encoder, routes=routes, index=pc_index, auto_sync=\"local\")\n", + "rl = SemanticRouter(encoder=encoder, routes=routes, index=pc_index, auto_sync=\"local\")\n", "# due to pinecone indexing latency we wait 3 seconds\n", "time.sleep(3)" ] @@ -210,7 +210,7 @@ "source": [ "del rl\n", "\n", - "rl = RouteLayer(encoder=encoder, routes=[politics], index=pc_index)\n", + "rl = SemanticRouter(encoder=encoder, routes=[politics], index=pc_index)\n", "time.sleep(3)" ] }, diff --git a/poetry.lock b/poetry.lock index 17a234f6..19288adb 100644 --- a/poetry.lock +++ b/poetry.lock @@ -3377,21 +3377,79 @@ dev = ["black", "check-manifest", "coverage", "packaging", "pylint", "pyperf", " test = ["pytest", "pytest-xdist", "setuptools"] [[package]] -name = "psycopg2" +name = "psycopg2-binary" version = "2.9.10" description = "psycopg2 - Python-PostgreSQL Database Adapter" optional = true python-versions = ">=3.8" files = [ - {file = "psycopg2-2.9.10-cp310-cp310-win32.whl", hash = "sha256:5df2b672140f95adb453af93a7d669d7a7bf0a56bcd26f1502329166f4a61716"}, - {file = "psycopg2-2.9.10-cp310-cp310-win_amd64.whl", hash = "sha256:c6f7b8561225f9e711a9c47087388a97fdc948211c10a4bccbf0ba68ab7b3b5a"}, - {file = "psycopg2-2.9.10-cp311-cp311-win32.whl", hash = "sha256:47c4f9875125344f4c2b870e41b6aad585901318068acd01de93f3677a6522c2"}, - {file = "psycopg2-2.9.10-cp311-cp311-win_amd64.whl", hash = "sha256:0435034157049f6846e95103bd8f5a668788dd913a7c30162ca9503fdf542cb4"}, - {file = "psycopg2-2.9.10-cp312-cp312-win32.whl", hash = "sha256:65a63d7ab0e067e2cdb3cf266de39663203d38d6a8ed97f5ca0cb315c73fe067"}, - {file = "psycopg2-2.9.10-cp312-cp312-win_amd64.whl", hash = "sha256:4a579d6243da40a7b3182e0430493dbd55950c493d8c68f4eec0b302f6bbf20e"}, - {file = "psycopg2-2.9.10-cp39-cp39-win32.whl", hash = "sha256:9d5b3b94b79a844a986d029eee38998232451119ad653aea42bb9220a8c5066b"}, - {file = "psycopg2-2.9.10-cp39-cp39-win_amd64.whl", hash = "sha256:88138c8dedcbfa96408023ea2b0c369eda40fe5d75002c0964c78f46f11fa442"}, - {file = "psycopg2-2.9.10.tar.gz", hash = "sha256:12ec0b40b0273f95296233e8750441339298e6a572f7039da5b260e3c8b60e11"}, + {file = "psycopg2-binary-2.9.10.tar.gz", hash = "sha256:4b3df0e6990aa98acda57d983942eff13d824135fe2250e6522edaa782a06de2"}, + {file = "psycopg2_binary-2.9.10-cp310-cp310-macosx_12_0_x86_64.whl", hash = "sha256:0ea8e3d0ae83564f2fc554955d327fa081d065c8ca5cc6d2abb643e2c9c1200f"}, + {file = "psycopg2_binary-2.9.10-cp310-cp310-macosx_14_0_arm64.whl", hash = "sha256:3e9c76f0ac6f92ecfc79516a8034a544926430f7b080ec5a0537bca389ee0906"}, + {file = "psycopg2_binary-2.9.10-cp310-cp310-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:2ad26b467a405c798aaa1458ba09d7e2b6e5f96b1ce0ac15d82fd9f95dc38a92"}, + {file = "psycopg2_binary-2.9.10-cp310-cp310-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:270934a475a0e4b6925b5f804e3809dd5f90f8613621d062848dd82f9cd62007"}, + {file = "psycopg2_binary-2.9.10-cp310-cp310-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:48b338f08d93e7be4ab2b5f1dbe69dc5e9ef07170fe1f86514422076d9c010d0"}, + {file = "psycopg2_binary-2.9.10-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:7f4152f8f76d2023aac16285576a9ecd2b11a9895373a1f10fd9db54b3ff06b4"}, + {file = "psycopg2_binary-2.9.10-cp310-cp310-musllinux_1_2_aarch64.whl", hash = "sha256:32581b3020c72d7a421009ee1c6bf4a131ef5f0a968fab2e2de0c9d2bb4577f1"}, + {file = "psycopg2_binary-2.9.10-cp310-cp310-musllinux_1_2_i686.whl", hash = "sha256:2ce3e21dc3437b1d960521eca599d57408a695a0d3c26797ea0f72e834c7ffe5"}, + {file = "psycopg2_binary-2.9.10-cp310-cp310-musllinux_1_2_ppc64le.whl", hash = "sha256:e984839e75e0b60cfe75e351db53d6db750b00de45644c5d1f7ee5d1f34a1ce5"}, + {file = "psycopg2_binary-2.9.10-cp310-cp310-musllinux_1_2_x86_64.whl", hash = "sha256:3c4745a90b78e51d9ba06e2088a2fe0c693ae19cc8cb051ccda44e8df8a6eb53"}, + {file = "psycopg2_binary-2.9.10-cp310-cp310-win32.whl", hash = "sha256:e5720a5d25e3b99cd0dc5c8a440570469ff82659bb09431c1439b92caf184d3b"}, + {file = "psycopg2_binary-2.9.10-cp310-cp310-win_amd64.whl", hash = "sha256:3c18f74eb4386bf35e92ab2354a12c17e5eb4d9798e4c0ad3a00783eae7cd9f1"}, + {file = "psycopg2_binary-2.9.10-cp311-cp311-macosx_12_0_x86_64.whl", hash = "sha256:04392983d0bb89a8717772a193cfaac58871321e3ec69514e1c4e0d4957b5aff"}, + {file = "psycopg2_binary-2.9.10-cp311-cp311-macosx_14_0_arm64.whl", hash = "sha256:1a6784f0ce3fec4edc64e985865c17778514325074adf5ad8f80636cd029ef7c"}, + {file = "psycopg2_binary-2.9.10-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:b5f86c56eeb91dc3135b3fd8a95dc7ae14c538a2f3ad77a19645cf55bab1799c"}, + {file = "psycopg2_binary-2.9.10-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:2b3d2491d4d78b6b14f76881905c7a8a8abcf974aad4a8a0b065273a0ed7a2cb"}, + {file = "psycopg2_binary-2.9.10-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:2286791ececda3a723d1910441c793be44625d86d1a4e79942751197f4d30341"}, + {file = "psycopg2_binary-2.9.10-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:512d29bb12608891e349af6a0cccedce51677725a921c07dba6342beaf576f9a"}, + {file = "psycopg2_binary-2.9.10-cp311-cp311-musllinux_1_2_aarch64.whl", hash = "sha256:5a507320c58903967ef7384355a4da7ff3f28132d679aeb23572753cbf2ec10b"}, + {file = "psycopg2_binary-2.9.10-cp311-cp311-musllinux_1_2_i686.whl", hash = "sha256:6d4fa1079cab9018f4d0bd2db307beaa612b0d13ba73b5c6304b9fe2fb441ff7"}, + {file = "psycopg2_binary-2.9.10-cp311-cp311-musllinux_1_2_ppc64le.whl", hash = "sha256:851485a42dbb0bdc1edcdabdb8557c09c9655dfa2ca0460ff210522e073e319e"}, + {file = "psycopg2_binary-2.9.10-cp311-cp311-musllinux_1_2_x86_64.whl", hash = "sha256:35958ec9e46432d9076286dda67942ed6d968b9c3a6a2fd62b48939d1d78bf68"}, + {file = "psycopg2_binary-2.9.10-cp311-cp311-win32.whl", hash = "sha256:ecced182e935529727401b24d76634a357c71c9275b356efafd8a2a91ec07392"}, + {file = "psycopg2_binary-2.9.10-cp311-cp311-win_amd64.whl", hash = "sha256:ee0e8c683a7ff25d23b55b11161c2663d4b099770f6085ff0a20d4505778d6b4"}, + {file = "psycopg2_binary-2.9.10-cp312-cp312-macosx_12_0_x86_64.whl", hash = "sha256:880845dfe1f85d9d5f7c412efea7a08946a46894537e4e5d091732eb1d34d9a0"}, + {file = "psycopg2_binary-2.9.10-cp312-cp312-macosx_14_0_arm64.whl", hash = "sha256:9440fa522a79356aaa482aa4ba500b65f28e5d0e63b801abf6aa152a29bd842a"}, + {file = "psycopg2_binary-2.9.10-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:e3923c1d9870c49a2d44f795df0c889a22380d36ef92440ff618ec315757e539"}, + {file = "psycopg2_binary-2.9.10-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:7b2c956c028ea5de47ff3a8d6b3cc3330ab45cf0b7c3da35a2d6ff8420896526"}, + {file = "psycopg2_binary-2.9.10-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:f758ed67cab30b9a8d2833609513ce4d3bd027641673d4ebc9c067e4d208eec1"}, + {file = "psycopg2_binary-2.9.10-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8cd9b4f2cfab88ed4a9106192de509464b75a906462fb846b936eabe45c2063e"}, + {file = "psycopg2_binary-2.9.10-cp312-cp312-musllinux_1_2_aarch64.whl", hash = "sha256:6dc08420625b5a20b53551c50deae6e231e6371194fa0651dbe0fb206452ae1f"}, + {file = "psycopg2_binary-2.9.10-cp312-cp312-musllinux_1_2_i686.whl", hash = "sha256:d7cd730dfa7c36dbe8724426bf5612798734bff2d3c3857f36f2733f5bfc7c00"}, + {file = "psycopg2_binary-2.9.10-cp312-cp312-musllinux_1_2_ppc64le.whl", hash = "sha256:155e69561d54d02b3c3209545fb08938e27889ff5a10c19de8d23eb5a41be8a5"}, + {file = "psycopg2_binary-2.9.10-cp312-cp312-musllinux_1_2_x86_64.whl", hash = "sha256:c3cc28a6fd5a4a26224007712e79b81dbaee2ffb90ff406256158ec4d7b52b47"}, + {file = "psycopg2_binary-2.9.10-cp312-cp312-win32.whl", hash = "sha256:ec8a77f521a17506a24a5f626cb2aee7850f9b69a0afe704586f63a464f3cd64"}, + {file = "psycopg2_binary-2.9.10-cp312-cp312-win_amd64.whl", hash = "sha256:18c5ee682b9c6dd3696dad6e54cc7ff3a1a9020df6a5c0f861ef8bfd338c3ca0"}, + {file = "psycopg2_binary-2.9.10-cp313-cp313-macosx_12_0_x86_64.whl", hash = "sha256:26540d4a9a4e2b096f1ff9cce51253d0504dca5a85872c7f7be23be5a53eb18d"}, + {file = "psycopg2_binary-2.9.10-cp313-cp313-macosx_14_0_arm64.whl", hash = "sha256:e217ce4d37667df0bc1c397fdcd8de5e81018ef305aed9415c3b093faaeb10fb"}, + {file = "psycopg2_binary-2.9.10-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:245159e7ab20a71d989da00f280ca57da7641fa2cdcf71749c193cea540a74f7"}, + {file = "psycopg2_binary-2.9.10-cp313-cp313-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:3c4ded1a24b20021ebe677b7b08ad10bf09aac197d6943bfe6fec70ac4e4690d"}, + {file = "psycopg2_binary-2.9.10-cp313-cp313-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:3abb691ff9e57d4a93355f60d4f4c1dd2d68326c968e7db17ea96df3c023ef73"}, + {file = "psycopg2_binary-2.9.10-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:8608c078134f0b3cbd9f89b34bd60a943b23fd33cc5f065e8d5f840061bd0673"}, + {file = "psycopg2_binary-2.9.10-cp313-cp313-musllinux_1_2_aarch64.whl", hash = "sha256:230eeae2d71594103cd5b93fd29d1ace6420d0b86f4778739cb1a5a32f607d1f"}, + {file = "psycopg2_binary-2.9.10-cp313-cp313-musllinux_1_2_i686.whl", hash = "sha256:bb89f0a835bcfc1d42ccd5f41f04870c1b936d8507c6df12b7737febc40f0909"}, + {file = "psycopg2_binary-2.9.10-cp313-cp313-musllinux_1_2_ppc64le.whl", hash = "sha256:f0c2d907a1e102526dd2986df638343388b94c33860ff3bbe1384130828714b1"}, + {file = "psycopg2_binary-2.9.10-cp313-cp313-musllinux_1_2_x86_64.whl", hash = "sha256:f8157bed2f51db683f31306aa497311b560f2265998122abe1dce6428bd86567"}, + {file = "psycopg2_binary-2.9.10-cp38-cp38-macosx_12_0_x86_64.whl", hash = "sha256:eb09aa7f9cecb45027683bb55aebaaf45a0df8bf6de68801a6afdc7947bb09d4"}, + {file = "psycopg2_binary-2.9.10-cp38-cp38-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:b73d6d7f0ccdad7bc43e6d34273f70d587ef62f824d7261c4ae9b8b1b6af90e8"}, + {file = "psycopg2_binary-2.9.10-cp38-cp38-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:ce5ab4bf46a211a8e924d307c1b1fcda82368586a19d0a24f8ae166f5c784864"}, + {file = "psycopg2_binary-2.9.10-cp38-cp38-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:056470c3dc57904bbf63d6f534988bafc4e970ffd50f6271fc4ee7daad9498a5"}, + {file = "psycopg2_binary-2.9.10-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:73aa0e31fa4bb82578f3a6c74a73c273367727de397a7a0f07bd83cbea696baa"}, + {file = "psycopg2_binary-2.9.10-cp38-cp38-musllinux_1_2_aarch64.whl", hash = "sha256:8de718c0e1c4b982a54b41779667242bc630b2197948405b7bd8ce16bcecac92"}, + {file = "psycopg2_binary-2.9.10-cp38-cp38-musllinux_1_2_i686.whl", hash = "sha256:5c370b1e4975df846b0277b4deba86419ca77dbc25047f535b0bb03d1a544d44"}, + {file = "psycopg2_binary-2.9.10-cp38-cp38-musllinux_1_2_ppc64le.whl", hash = "sha256:ffe8ed017e4ed70f68b7b371d84b7d4a790368db9203dfc2d222febd3a9c8863"}, + {file = "psycopg2_binary-2.9.10-cp38-cp38-musllinux_1_2_x86_64.whl", hash = "sha256:8aecc5e80c63f7459a1a2ab2c64df952051df196294d9f739933a9f6687e86b3"}, + {file = "psycopg2_binary-2.9.10-cp39-cp39-macosx_12_0_x86_64.whl", hash = "sha256:7a813c8bdbaaaab1f078014b9b0b13f5de757e2b5d9be6403639b298a04d218b"}, + {file = "psycopg2_binary-2.9.10-cp39-cp39-manylinux_2_17_aarch64.manylinux2014_aarch64.whl", hash = "sha256:d00924255d7fc916ef66e4bf22f354a940c67179ad3fd7067d7a0a9c84d2fbfc"}, + {file = "psycopg2_binary-2.9.10-cp39-cp39-manylinux_2_17_i686.manylinux2014_i686.whl", hash = "sha256:7559bce4b505762d737172556a4e6ea8a9998ecac1e39b5233465093e8cee697"}, + {file = "psycopg2_binary-2.9.10-cp39-cp39-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl", hash = "sha256:e8b58f0a96e7a1e341fc894f62c1177a7c83febebb5ff9123b579418fdc8a481"}, + {file = "psycopg2_binary-2.9.10-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl", hash = "sha256:6b269105e59ac96aba877c1707c600ae55711d9dcd3fc4b5012e4af68e30c648"}, + {file = "psycopg2_binary-2.9.10-cp39-cp39-musllinux_1_2_aarch64.whl", hash = "sha256:79625966e176dc97ddabc142351e0409e28acf4660b88d1cf6adb876d20c490d"}, + {file = "psycopg2_binary-2.9.10-cp39-cp39-musllinux_1_2_i686.whl", hash = "sha256:8aabf1c1a04584c168984ac678a668094d831f152859d06e055288fa515e4d30"}, + {file = "psycopg2_binary-2.9.10-cp39-cp39-musllinux_1_2_ppc64le.whl", hash = "sha256:19721ac03892001ee8fdd11507e6a2e01f4e37014def96379411ca99d78aeb2c"}, + {file = "psycopg2_binary-2.9.10-cp39-cp39-musllinux_1_2_x86_64.whl", hash = "sha256:7f5d859928e635fa3ce3477704acee0f667b3a3d3e4bb109f2b18d4005f38287"}, + {file = "psycopg2_binary-2.9.10-cp39-cp39-win32.whl", hash = "sha256:3216ccf953b3f267691c90c6fe742e45d890d8272326b4a8b20850a03d05b7b8"}, + {file = "psycopg2_binary-2.9.10-cp39-cp39-win_amd64.whl", hash = "sha256:30e34c4e97964805f715206c7b789d54a78b70f3ff19fbe590104b71c45600e5"}, ] [[package]] @@ -5494,11 +5552,11 @@ hybrid = ["pinecone-text"] local = ["llama-cpp-python", "tokenizers", "torch", "transformers"] mistralai = ["mistralai"] pinecone = ["pinecone"] -postgres = ["psycopg2"] +postgres = [] qdrant = ["qdrant-client"] vision = ["pillow", "torch", "torchvision", "transformers"] [metadata] lock-version = "2.0" python-versions = ">=3.9,<3.13" -content-hash = "4e5cc6b4f04b77600390de5aef4ec0afd983df5cae13639dce7e2d3f1101aae4" +content-hash = "d649e614d9c7122c642ed351abf8886cc008960f0fd7664483cfb5dde7865808" diff --git a/pyproject.toml b/pyproject.toml index 9e922661..e1eff0c7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -35,7 +35,7 @@ boto3 = { version = "^1.34.98", optional = true } botocore = {version = "^1.34.110", optional = true} aiohttp = "^3.10.11" fastembed = {version = "^0.3.0", optional = true} -psycopg2 = {version = "^2.9.9", optional = true} +psycopg2-binary = {version = "^2.9.9", optional = true} sphinx = {version = "^7.0.0", optional = true} sphinxawesome-theme = {version = "^5.2.0", optional = true} tornado = {version = "^6.4.2", optional = true} diff --git a/semantic_router/encoders/bm25.py b/semantic_router/encoders/bm25.py index ea0a89a6..b5365cee 100644 --- a/semantic_router/encoders/bm25.py +++ b/semantic_router/encoders/bm25.py @@ -48,7 +48,6 @@ class BM25Encoder(TfidfEncoder): if len(docs) == 1: sparse_dicts = self.model.encode_queries(docs) elif len(docs) > 1: - print(docs) sparse_dicts = self.model.encode_documents(docs) else: raise ValueError("No documents to encode.") diff --git a/semantic_router/index/__init__.py b/semantic_router/index/__init__.py index 3a43abe9..14b60da3 100644 --- a/semantic_router/index/__init__.py +++ b/semantic_router/index/__init__.py @@ -2,6 +2,7 @@ from semantic_router.index.base import BaseIndex from semantic_router.index.hybrid_local import HybridLocalIndex from semantic_router.index.local import LocalIndex from semantic_router.index.pinecone import PineconeIndex +from semantic_router.index.postgres import PostgresIndex from semantic_router.index.qdrant import QdrantIndex __all__ = [ @@ -10,4 +11,5 @@ __all__ = [ "LocalIndex", "QdrantIndex", "PineconeIndex", + "PostgresIndex", ] diff --git a/semantic_router/index/pinecone.py b/semantic_router/index/pinecone.py index de324f28..25df1c47 100644 --- a/semantic_router/index/pinecone.py +++ b/semantic_router/index/pinecone.py @@ -364,7 +364,6 @@ class PineconeIndex(BaseIndex): top_k: int = 5, route_filter: Optional[List[str]] = None, sparse_vector: dict[int, float] | SparseEmbedding | None = None, - **kwargs: Any, ) -> Tuple[np.ndarray, List[str]]: """Search the index for the query vector and return the top_k results. diff --git a/semantic_router/index/postgres.py b/semantic_router/index/postgres.py index 67de0ada..4cdd94f3 100644 --- a/semantic_router/index/postgres.py +++ b/semantic_router/index/postgres.py @@ -1,16 +1,18 @@ import os import uuid from enum import Enum -from typing import Any, Dict, List, Optional, Tuple, Union +from typing import Any, Dict, List, Optional, Tuple, TYPE_CHECKING, Union import numpy as np -import psycopg2 -from pydantic import BaseModel +from pydantic import BaseModel, Field from semantic_router.index.base import BaseIndex from semantic_router.schema import ConfigParameter, Metric, SparseEmbedding from semantic_router.utils.logger import logger +if TYPE_CHECKING: + import psycopg2 + class MetricPgVecOperatorMap(Enum): """ @@ -104,8 +106,9 @@ class PostgresIndex(BaseIndex): dimensions: int = 1536 metric: Metric = Metric.COSINE namespace: Optional[str] = "" - conn: Optional[psycopg2.extensions.connection] = None + conn: Optional["psycopg2.extensions.connection"] = None type: str = "postgres" + pg2: Any = Field(default=None, exclude=True) def __init__( self, @@ -133,6 +136,12 @@ class PostgresIndex(BaseIndex): :type namespace: Optional[str] """ super().__init__() + # try and import psycopg2 + try: + import psycopg2 + except ImportError: + raise ImportError("Please install psycopg2 to use PostgresIndex. " + "You can install it with: `pip install 'semantic-router[postgres]'`") if connection_string: self.connection_string = connection_string else: diff --git a/semantic_router/routers/base.py b/semantic_router/routers/base.py index ad2971a0..f21131c8 100644 --- a/semantic_router/routers/base.py +++ b/semantic_router/routers/base.py @@ -497,14 +497,10 @@ class BaseRouter(BaseModel): vector_arr = self._encode(text=[text]) else: vector_arr = np.array(vector) - print(f"{text=}") - print(f"{vector_arr}") # get relevant utterances results = self._retrieve(xq=vector_arr) - print(f"{results=}") # decide most relevant routes categories_with_scores = self._semantic_classify_multiple_routes(results) - print(f"{categories_with_scores=}") return [ RouteChoice(name=category, similarity_score=score) for category, score in categories_with_scores diff --git a/semantic_router/routers/semantic.py b/semantic_router/routers/semantic.py index ff7f73f2..64ccbaf2 100644 --- a/semantic_router/routers/semantic.py +++ b/semantic_router/routers/semantic.py @@ -31,9 +31,6 @@ class SemanticRouter(BaseRouter): aggregation=aggregation, auto_sync=auto_sync, ) - # run initialize index now if auto sync is active - if self.auto_sync: - self._init_index_state() def _encode(self, text: list[str]) -> Any: """Given some text, encode it.""" diff --git a/tests/unit/test_sync.py b/tests/unit/test_sync.py index 35093a6b..3a640f02 100644 --- a/tests/unit/test_sync.py +++ b/tests/unit/test_sync.py @@ -5,14 +5,20 @@ import pytest import time from typing import Optional from semantic_router.encoders import DenseEncoder, CohereEncoder, OpenAIEncoder -from semantic_router.index.pinecone import PineconeIndex +from semantic_router.index import ( + PineconeIndex, + HybridLocalIndex, + LocalIndex, + QdrantIndex, + PostgresIndex, +) from semantic_router.schema import Utterance from semantic_router.routers import SemanticRouter from semantic_router.route import Route from platform import python_version -PINECONE_SLEEP = 12 +PINECONE_SLEEP = 6 def mock_encoder_call(utterances): @@ -97,6 +103,34 @@ routes: - how are things going? """ +# not all indexes support metadata, so we map the feature here +INCLUDE_METADATA_MAP = { + PineconeIndex: True, + HybridLocalIndex: False, + LocalIndex: False, + QdrantIndex: False, + PostgresIndex: False, +} +def include_metadata(index_cls): + return INCLUDE_METADATA_MAP.get(index_cls, False) + +MERGE_FORCE_LOCAL_RESULT_WITH_METADATA = [ + Utterance(route="Route 1", utterance="Hello"), + Utterance(route="Route 1", utterance="Hi"), + Utterance(route="Route 2", utterance="Au revoir"), + Utterance(route="Route 2", utterance="Bye"), + Utterance(route="Route 2", utterance="Goodbye"), + Utterance(route="Route 2", utterance="Hi"), +] + +MERGE_FORCE_LOCAL_RESULT_WITHOUT_METADATA = [ + Utterance(route="Route 1", utterance="Hello"), + Utterance(route="Route 1", utterance="Hi"), + Utterance(route="Route 2", utterance="Au revoir"), + Utterance(route="Route 2", utterance="Bye"), + Utterance(route="Route 2", utterance="Goodbye"), + Utterance(route="Route 2", utterance="Hi"), +] @pytest.fixture def base_encoder(): @@ -332,7 +366,10 @@ class TestSemanticRouter: # now confirm utterances are correct local_utterances = route_layer.index.get_utterances() # we sort to ensure order is the same - local_utterances.sort(key=lambda x: x.to_str(include_metadata=True)) + # TODO JB: there is a bug here where if we include_metadata=True it fails + local_utterances.sort( + key=lambda x: x.to_str(include_metadata=False) + ) assert local_utterances == [ Utterance(route="Route 1", utterance="Hello"), Utterance(route="Route 1", utterance="Hi"), @@ -370,7 +407,9 @@ class TestSemanticRouter: # now confirm utterances are correct local_utterances = route_layer.index.get_utterances() # we sort to ensure order is the same - local_utterances.sort(key=lambda x: x.to_str(include_metadata=True)) + local_utterances.sort( + key=lambda x: x.to_str(include_metadata=include_metadata(index_cls)) + ) assert local_utterances == [ Utterance( route="Route 1", utterance="Hello", metadata={"type": "default"} @@ -426,7 +465,9 @@ class TestSemanticRouter: # now confirm utterances are correct local_utterances = route_layer.index.get_utterances() # we sort to ensure order is the same - local_utterances.sort(key=lambda x: x.to_str(include_metadata=True)) + local_utterances.sort( + key=lambda x: x.to_str(include_metadata=include_metadata(index_cls)) + ) assert local_utterances == [ Utterance( route="Route 1", utterance="Hello", metadata={"type": "default"} -- GitLab