Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add query index params #10

Merged
merged 1 commit into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 108 additions & 28 deletions nbs/00_vector.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
},
{
"cell_type": "code",
"execution_count": 97,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -21,7 +21,7 @@
},
{
"cell_type": "code",
"execution_count": 98,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -31,7 +31,7 @@
},
{
"cell_type": "code",
"execution_count": 99,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -42,7 +42,7 @@
},
{
"cell_type": "code",
"execution_count": 100,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -52,7 +52,7 @@
},
{
"cell_type": "code",
"execution_count": 101,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -73,7 +73,7 @@
},
{
"cell_type": "code",
"execution_count": 102,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -146,7 +146,7 @@
},
{
"cell_type": "code",
"execution_count": 103,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -265,6 +265,42 @@
" .format(index_name=index_name_quoted, table_name=table_name_quoted, column_name=column_name_quoted, with_clause=with_clause)\n"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Query Params"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#| export\n",
"\n",
"class QueryParams:\n",
" def __init__(self, params: dict[str, Any]) -> None:\n",
" self.params = params\n",
" \n",
" def get_statements(self) -> List[str]:\n",
" return [\"SET LOCAL \" + key + \" = \" + str(value) for key, value in self.params.items()]\n",
"\n",
"class TimescaleVectorIndexParams(QueryParams):\n",
" def __init__(self, search_list_size: int) -> None:\n",
" super().__init__({\"tsv.query_search_list_size\": search_list_size})\n",
"\n",
"class IvfflatIndexParams(QueryParams):\n",
" def __init__(self, probes: int) -> None:\n",
" super().__init__({\"ivfflat.probes\": probes})\n",
"\n",
"class HNSWIndexParams(QueryParams):\n",
" def __init__(self, ef_search: int) -> None:\n",
" super().__init__({\"hnsw.ef_search\": ef_search})"
]
},
{
"attachments": {},
"cell_type": "markdown",
Expand All @@ -275,7 +311,7 @@
},
{
"cell_type": "code",
"execution_count": 104,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -290,7 +326,7 @@
},
{
"cell_type": "code",
"execution_count": 105,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -388,7 +424,7 @@
},
{
"cell_type": "code",
"execution_count": 106,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -534,7 +570,7 @@
},
{
"cell_type": "code",
"execution_count": 107,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -848,7 +884,7 @@
},
{
"cell_type": "code",
"execution_count": 108,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -876,7 +912,7 @@
"Generates a query to create the tables, indexes, and extensions needed to store the vector data."
]
},
"execution_count": 108,
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -895,7 +931,7 @@
},
{
"cell_type": "code",
"execution_count": 109,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -1128,6 +1164,7 @@
" filter: Optional[Union[Dict[str, str], List[Dict[str, str]]]] = None,\n",
" predicates: Optional[Predicates] = None,\n",
" uuid_time_filter: Optional[UUIDTimeRange] = None,\n",
" query_params: Optional[QueryParams] = None\n",
" ): \n",
" \"\"\"\n",
" Retrieves similar records using a similarity query.\n",
Expand All @@ -1149,13 +1186,22 @@
" \"\"\"\n",
" (query, params) = self.builder.search_query(\n",
" query_embedding, limit, filter, predicates, uuid_time_filter)\n",
" async with await self.connect() as pool:\n",
" return await pool.fetch(query, *params)"
" if query_params is not None:\n",
" async with await self.connect() as pool:\n",
" async with pool.transaction():\n",
" #Looks like there is no way to pipeline this: https://github.com/MagicStack/asyncpg/issues/588\n",
" statements = query_params.get_statements()\n",
" for statement in statements:\n",
" await pool.execute(statement)\n",
" return await pool.fetch(query, *params)\n",
" else:\n",
" async with await self.connect() as pool:\n",
" return await pool.fetch(query, *params)"
]
},
{
"cell_type": "code",
"execution_count": 110,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -1183,7 +1229,7 @@
"Creates necessary tables."
]
},
"execution_count": 110,
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1194,7 +1240,7 @@
},
{
"cell_type": "code",
"execution_count": 111,
"execution_count": null,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -1222,7 +1268,7 @@
"Creates necessary tables."
]
},
"execution_count": 111,
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1233,9 +1279,21 @@
},
{
"cell_type": "code",
"execution_count": 112,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/cevian/.pyenv/versions/3.11.4/envs/nbdev_env/lib/python3.11/site-packages/fastcore/docscrape.py:225: UserWarning: potentially wrong underline length... \n",
"Returns \n",
"-------- in \n",
"Retrieves similar records using a similarity query.\n",
"...\n",
" else: warn(msg)\n"
]
},
{
"data": {
"text/markdown": [
Expand All @@ -1248,7 +1306,8 @@
"> Async.search (query_embedding:Optional[List[float]]=None, limit:int=10,\n",
"> filter:Union[Dict[str,str],List[Dict[str,str]],NoneType]=No\n",
"> ne, predicates:Optional[__main__.Predicates]=None,\n",
"> uuid_time_filter:Optional[__main__.UUIDTimeRange]=None)\n",
"> uuid_time_filter:Optional[__main__.UUIDTimeRange]=None,\n",
"> query_params:Optional[__main__.QueryParams]=None)\n",
"\n",
"Retrieves similar records using a similarity query.\n",
"\n",
Expand All @@ -1259,6 +1318,7 @@
"| filter | Union | None | A filter for metadata. Should be specified as a key-value object or a list of key-value objects (where any objects in the list are matched). |\n",
"| predicates | Optional | None | A Predicates object to filter the results. Predicates support more complex queries than the filter parameter. Predicates can be combined using logical operators (&, \\|, and ~). |\n",
"| uuid_time_filter | Optional | None | |\n",
"| query_params | Optional | None | |\n",
"| **Returns** | **List: List of similar records.** | | |"
],
"text/plain": [
Expand All @@ -1271,7 +1331,8 @@
"> Async.search (query_embedding:Optional[List[float]]=None, limit:int=10,\n",
"> filter:Union[Dict[str,str],List[Dict[str,str]],NoneType]=No\n",
"> ne, predicates:Optional[__main__.Predicates]=None,\n",
"> uuid_time_filter:Optional[__main__.UUIDTimeRange]=None)\n",
"> uuid_time_filter:Optional[__main__.UUIDTimeRange]=None,\n",
"> query_params:Optional[__main__.QueryParams]=None)\n",
"\n",
"Retrieves similar records using a similarity query.\n",
"\n",
Expand All @@ -1282,10 +1343,11 @@
"| filter | Union | None | A filter for metadata. Should be specified as a key-value object or a list of key-value objects (where any objects in the list are matched). |\n",
"| predicates | Optional | None | A Predicates object to filter the results. Predicates support more complex queries than the filter parameter. Predicates can be combined using logical operators (&, \\|, and ~). |\n",
"| uuid_time_filter | Optional | None | |\n",
"| query_params | Optional | None | |\n",
"| **Returns** | **List: List of similar records.** | | |"
]
},
"execution_count": 112,
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -1296,7 +1358,7 @@
},
{
"cell_type": "code",
"execution_count": 117,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -1317,7 +1379,7 @@
},
{
"cell_type": "code",
"execution_count": 118,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -1564,6 +1626,10 @@
"assert len(rec) == 0\n",
"rec = await vec.search([1.0, 2.0], limit=4, uuid_time_filter=UUIDTimeRange(end_date=specific_datetime+timedelta(seconds=1), time_delta=timedelta(days=7)))\n",
"assert len(rec) == 1\n",
"rec = await vec.search([1.0, 2.0], limit=4, query_params=TimescaleVectorIndexParams(10))\n",
"assert len(rec) == 2\n",
"rec = await vec.search([1.0, 2.0], limit=4, query_params=TimescaleVectorIndexParams(100))\n",
"assert len(rec) == 2\n",
"await vec.drop_table()\n",
"await vec.close()"
]
Expand Down Expand Up @@ -1883,6 +1949,7 @@
" filter: Optional[Union[Dict[str, str], List[Dict[str, str]]]] = None,\n",
" predicates: Optional[Predicates] = None,\n",
" uuid_time_filter: Optional[UUIDTimeRange] = None,\n",
" query_params: Optional[QueryParams] = None,\n",
" ):\n",
" \"\"\"\n",
" Retrieves similar records using a similarity query.\n",
Expand Down Expand Up @@ -1910,6 +1977,11 @@
" (query, params) = self.builder.search_query(\n",
" query_embedding_np, limit, filter, predicates, uuid_time_filter)\n",
" query, params = self._translate_to_pyformat(query, params)\n",
"\n",
" if query_params is not None:\n",
" prefix = \"; \".join(query_params.get_statements())\n",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the async client, we run each SET LOCAL separately, and in the sync we are concatenating. Does the asyncpg just not support this behavior?

" query = f\"{prefix}; {query}\"\n",
" \n",
" with self.connect() as conn:\n",
" with conn.cursor() as cur:\n",
" cur.execute(query, params)\n",
Expand Down Expand Up @@ -2021,7 +2093,8 @@
"> Sync.search (query_embedding:Optional[List[float]]=None, limit:int=10,\n",
"> filter:Union[Dict[str,str],List[Dict[str,str]],NoneType]=Non\n",
"> e, predicates:Optional[__main__.Predicates]=None,\n",
"> uuid_time_filter:Optional[__main__.UUIDTimeRange]=None)\n",
"> uuid_time_filter:Optional[__main__.UUIDTimeRange]=None,\n",
"> query_params:Optional[__main__.QueryParams]=None)\n",
"\n",
"Retrieves similar records using a similarity query.\n",
"\n",
Expand All @@ -2032,6 +2105,7 @@
"| filter | Union | None | A filter for metadata. Should be specified as a key-value object or a list of key-value objects (where any objects in the list are matched). |\n",
"| predicates | Optional | None | A Predicates object to filter the results. Predicates support more complex queries than the filter parameter. Predicates can be combined using logical operators (&, \\|, and ~). |\n",
"| uuid_time_filter | Optional | None | |\n",
"| query_params | Optional | None | |\n",
"| **Returns** | **List: List of similar records.** | | |"
],
"text/plain": [
Expand All @@ -2044,7 +2118,8 @@
"> Sync.search (query_embedding:Optional[List[float]]=None, limit:int=10,\n",
"> filter:Union[Dict[str,str],List[Dict[str,str]],NoneType]=Non\n",
"> e, predicates:Optional[__main__.Predicates]=None,\n",
"> uuid_time_filter:Optional[__main__.UUIDTimeRange]=None)\n",
"> uuid_time_filter:Optional[__main__.UUIDTimeRange]=None,\n",
"> query_params:Optional[__main__.QueryParams]=None)\n",
"\n",
"Retrieves similar records using a similarity query.\n",
"\n",
Expand All @@ -2055,6 +2130,7 @@
"| filter | Union | None | A filter for metadata. Should be specified as a key-value object or a list of key-value objects (where any objects in the list are matched). |\n",
"| predicates | Optional | None | A Predicates object to filter the results. Predicates support more complex queries than the filter parameter. Predicates can be combined using logical operators (&, \\|, and ~). |\n",
"| uuid_time_filter | Optional | None | |\n",
"| query_params | Optional | None | |\n",
"| **Returns** | **List: List of similar records.** | | |"
]
},
Expand Down Expand Up @@ -2314,6 +2390,10 @@
"assert len(rec) == 0\n",
"rec = vec.search([1.0, 2.0], limit=4, uuid_time_filter=UUIDTimeRange(end_date=specific_datetime+timedelta(seconds=1), time_delta=timedelta(days=7)))\n",
"assert len(rec) == 1\n",
"rec = vec.search([1.0, 2.0], limit=4, query_params=TimescaleVectorIndexParams(10))\n",
"assert len(rec) == 2\n",
"rec = vec.search([1.0, 2.0], limit=4, query_params=TimescaleVectorIndexParams(100))\n",
"assert len(rec) == 2\n",
"vec.drop_table()\n",
"vec.close()"
]
Expand Down
Loading