Skip to content

Commit

Permalink
Add AVS_IP and AVS_PORT variables
Browse files Browse the repository at this point in the history
  • Loading branch information
dwelch-spike committed Aug 15, 2024
1 parent 5267e2d commit 461beaf
Showing 1 changed file with 61 additions and 47 deletions.
108 changes: 61 additions & 47 deletions basic-search/basic_search.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,33 @@
"\n",
"AVS provides a gRPC API and a Python client that developers can use to create AI applications leveraging its search capability. This example shows basic usage of the Aerospike Vector Search Python client including, writing, reading, and searching for vectors with AVS. For more detailed client documentation visit the [Python client's read the docs page](https://aerospike-vector-search-python-client.readthedocs.io/en/latest/).\n",
"\n",
"## Setup\n",
"Before getting started, you need to setup a running Aerospike Vector Search (AVS) deployment.\n",
"See the [AVS installation guide](https://aerospike.com/docs/vector/install) for tips on setting up AVS.\n",
"\n",
"### Prerequisites\n",
"\n",
"- Python 3.9 or higher\n",
"- pip 9.0.1 or higher\n",
"- An accessible AVS deployment\n",
"\n",
"## Installing AVS\n",
"Before getting started, you need to setup a running Aerospike Vector Search (AVS) deployment.\n",
"See the [AVS installation guide](https://aerospike.com/docs/vector/install) for tips on setting up AVS.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "246d7e0c-57ea-433f-8e5e-a1d72f83e35b",
"metadata": {},
"outputs": [],
"source": [
"AVS_HOST = \"<avs-ip>\"\n",
"AVS_PORT = 5000"
]
},
{
"cell_type": "markdown",
"id": "34efa0ea-abfd-46a8-92d7-fbfd2dcf8139",
"metadata": {},
"source": [
"## Installing the Aerospike Vector Search Client\n",
"\n",
"The Aerospike Vector Search Python client is distributed on pypi so you can install directly with pip."
Expand Down Expand Up @@ -81,22 +98,19 @@
"outputs": [],
"source": [
"# Admin client configuration\n",
"# host is the address of the AVS seed node to connect to\n",
"host = \"localhost\"\n",
"# port is the port number that AVS is listening on\n",
"port = \"5000\"\n",
"# listener_name corresponds to the AVS advertised_listener config.\n",
"# LISTENER_NAME corresponds to the AVS advertised_listener config.\n",
"# https://aerospike.com/docs/vector/operate/configuration#advertised-listener\n",
"# this is often needed when connection to AVS clusters in the cloud\n",
"listener_name = None\n",
"# load_balancer is True if the AVS cluster is load balanced.\n",
"# using a \n",
"load_balancer = True\n",
"LISTENER_NAME = None\n",
"# LOAD_BALANCED is True if the AVS cluster is load balanced\n",
"# using a load balancer with AVS is best practice and even works \n",
"# with a single node AVS cluster that is not load balanced\n",
"LOAD_BALANCED = True\n",
"\n",
"admin_client = AdminClient(\n",
" seeds=types.HostPort(host=host, port=port),\n",
" listener_name=listener_name,\n",
" is_loadbalancer=load_balancer,\n",
" seeds=types.HostPort(host=AVS_HOST, port=AVS_PORT),\n",
" listener_name=LISTENER_NAME,\n",
" is_loadbalancer=LOAD_BALANCED,\n",
")"
]
},
Expand All @@ -122,23 +136,23 @@
"outputs": [],
"source": [
"# Index creation arguments\n",
"# namespace is the namespace that the indexed data will be stored in\n",
"namespace = \"avs-data\"\n",
"# index_name is the name of the HNSW index to create\n",
"index_name = \"basic_index\"\n",
"# vector_field is the Aerospike record bin that stores its vector data\n",
"# NAMESPACE is the namespace that the indexed data will be stored in\n",
"NAMESPACE = \"test\"\n",
"# INDEX_NAME is the name of the HNSW index to create\n",
"INDEX_NAME = \"basic_index\"\n",
"# VECTOR_FIELD is the Aerospike record bin that stores its vector data\n",
"# The created index will use the data in this bin to perform nearest neighbor searches etc\n",
"vector_field = \"vector\"\n",
"# dimensions is the dimensionality of the vectors\n",
"dimensions = 2\n",
"VECTOR_FIELD = \"vector\"\n",
"# DIMENSIONS is the dimensionality of the vectors\n",
"DIMENSIONS = 2\n",
"\n",
"try:\n",
" print(\"creating index\")\n",
" admin_client.index_create(\n",
" namespace=namespace,\n",
" name=index_name,\n",
" vector_field=vector_field,\n",
" dimensions=dimensions,\n",
" namespace=NAMESPACE,\n",
" name=INDEX_NAME,\n",
" vector_field=VECTOR_FIELD,\n",
" dimensions=DIMENSIONS,\n",
" )\n",
"except Exception as e:\n",
" print(\"failed creating index \" + str(e))\n",
Expand All @@ -163,9 +177,9 @@
"outputs": [],
"source": [
"client = Client(\n",
" seeds=types.HostPort(host=host, port=port),\n",
" listener_name=listener_name,\n",
" is_loadbalancer=load_balancer,\n",
" seeds=types.HostPort(host=AVS_HOST, port=AVS_PORT),\n",
" listener_name=LISTENER_NAME,\n",
" is_loadbalancer=LOAD_BALANCED,\n",
")"
]
},
Expand Down Expand Up @@ -195,14 +209,14 @@
"outputs": [],
"source": [
"# set_name is the Aerospike set to write the records to\n",
"set_name = \"basic-set\"\n",
"SET_NAME = \"basic-set\"\n",
"\n",
"print(\"inserting vectors\")\n",
"for i in range(10):\n",
" key = \"r\" + str(i)\n",
" client.upsert(\n",
" namespace=namespace,\n",
" set_name=set_name,\n",
" namespace=NAMESPACE,\n",
" set_name=SET_NAME,\n",
" key=key,\n",
" record_data={\n",
" \"url\": f\"http://host.com/data{i}\",\n",
Expand Down Expand Up @@ -231,7 +245,7 @@
"outputs": [],
"source": [
"print(\"waiting for indexing to complete\")\n",
"client.wait_for_index_completion(namespace=namespace, name=index_name)"
"client.wait_for_index_completion(namespace=NAMESPACE, name=INDEX_NAME)"
]
},
{
Expand All @@ -252,10 +266,10 @@
"outputs": [],
"source": [
"status = client.is_indexed(\n",
" namespace=namespace,\n",
" set_name=set_name,\n",
" namespace=NAMESPACE,\n",
" set_name=SET_NAME,\n",
" key=key,\n",
" index_name=index_name,\n",
" index_name=INDEX_NAME,\n",
")\n",
"\n",
"print(\"indexed: \", status)"
Expand All @@ -282,8 +296,8 @@
"for i in range(10):\n",
" print(\" query \" + str(i))\n",
" results = client.vector_search(\n",
" namespace=namespace,\n",
" index_name=index_name,\n",
" namespace=NAMESPACE,\n",
" index_name=INDEX_NAME,\n",
" query=[i * 1.0, i * 1.0],\n",
" limit=3,\n",
" )\n",
Expand Down Expand Up @@ -311,9 +325,9 @@
"key = \"r0\"\n",
"\n",
"result = client.get(\n",
" namespace=namespace,\n",
" namespace=NAMESPACE,\n",
" key=key,\n",
" set_name=set_name,\n",
" set_name=SET_NAME,\n",
")\n",
"\n",
"print(str(result.key.key) + \" -> \" + str(result.fields))"
Expand All @@ -339,15 +353,15 @@
"from aerospike_vector_search.aio import Client as asyncClient\n",
"\n",
"async_client = asyncClient(\n",
" seeds=types.HostPort(host=host, port=port),\n",
" listener_name=listener_name,\n",
" is_loadbalancer=load_balancer,\n",
" seeds=types.HostPort(host=AVS_HOST, port=AVS_PORT),\n",
" listener_name=LISTENER_NAME,\n",
" is_loadbalancer=LOAD_BALANCED,\n",
")\n",
"\n",
"# Use await on client methods to await completion of the coroutine\n",
"results = await async_client.vector_search(\n",
" namespace=namespace,\n",
" index_name=index_name,\n",
" namespace=NAMESPACE,\n",
" index_name=INDEX_NAME,\n",
" query=[8.0, 8.0],\n",
" limit=3,\n",
")\n",
Expand Down

0 comments on commit 461beaf

Please sign in to comment.