# Python Client for Lantern
## Install
```sh
pip install lantern-client
```
## Basic usage
```python
from lantern import SyncClient
# Initialize the SyncClient with the database URL and table configuration
DB_URL="postgresql://postgres@localhost:5432/lantern"
client = SyncClient(url=DB_URL, table_name="small_world", dimensions=3, distance_type="l2sq", m=12, ef=64, ef_construction=64)
# Attempt to drop the existing table if it exists
try:
client.drop()
except:
print(f"An error occurred while dropping the table: {e}")
# Create a new table
client.create_table()
# Bulk insert data into the table
client.bulk_insert([
("1", [0,0,0], { "name": "a" }),
("2", [0,1,0], { "name": "b" }),
("3", [0,0,1], { "name": "c" })
])
# Create an index to optimize search queries
client.create_index()
# Retrieve a specific record by ID, selecting only certain fields
vec_by_id = client.get_by_id(id="1", select_fields=["id", "metadata"])
assert(vec_by_id.embedding is None)
# Retrieve a record by ID without field restrictions
vec_by_id = client.get_by_id("1")
assert(vec_by_id.id == "1")
# Retrieve multiple records by their IDs
vectors_by_ids = client.get_by_ids(["1", "3"])
assert(len(vectors_by_ids) == 2)
assert(vectors_by_ids[0].id == "1")
assert(vectors_by_ids[1].id == "3")
# Insert or update a single record
client.upsert(("4", [1,0,0], { "name": "d" }))
# Update the metadata of a record by its ID
client.update_by_id(id="4", metadata={ "name": "4" })
# Get the total number of records in the table
row_count = client.count()
assert(row_count == 4)
# Retrieve a record by ID, selecting specific fields
vec_by_id = client.get_by_id(id="4", select_fields=["id", "embedding", "metadata"])
assert(vec_by_id.metadata['name'] == "4")
# Search for vectors similar to the one with ID "4"
vectors = client.search(query_id="4")
assert(len(vectors) == row_count)
assert(vectors[0].id == "4")
assert(vectors[0].distance == 0)
# Search for vectors using the embedding of the vector with ID "4"
# Limit the results to 2 and apply a filter on the metadata
vectors = client.search(query_embedding=vec_by_id.embedding, limit=2, filter={"name": "a"}, select_fields=["id"])
assert(len(vectors) == 1)
assert(vectors[0].id == "1")
assert(vectors[0].metadata is None)
assert(vectors[0].embedding is None)
```
Raw data
{
"_id": null,
"home_page": "https://github.com/lanterndata/lantern-python",
"name": "lantern-client",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": null,
"author": "Varik Matevosyan",
"author_email": "varik@lantern.dev",
"download_url": "https://files.pythonhosted.org/packages/18/bb/64f90ff4afd07721f24bfc94fbc6892810bc63d00903d4bf53aa550ad31e/lantern_client-0.0.6.tar.gz",
"platform": null,
"description": "# Python Client for Lantern\n\n## Install\n\n```sh\npip install lantern-client\n```\n\n## Basic usage\n\n```python\nfrom lantern import SyncClient\n\n# Initialize the SyncClient with the database URL and table configuration\nDB_URL=\"postgresql://postgres@localhost:5432/lantern\"\nclient = SyncClient(url=DB_URL, table_name=\"small_world\", dimensions=3, distance_type=\"l2sq\", m=12, ef=64, ef_construction=64)\n\n# Attempt to drop the existing table if it exists\ntry:\n client.drop()\nexcept:\n print(f\"An error occurred while dropping the table: {e}\")\n\n# Create a new table\nclient.create_table()\n\n# Bulk insert data into the table\nclient.bulk_insert([\n (\"1\", [0,0,0], { \"name\": \"a\" }),\n (\"2\", [0,1,0], { \"name\": \"b\" }),\n (\"3\", [0,0,1], { \"name\": \"c\" })\n])\n\n# Create an index to optimize search queries\nclient.create_index()\n\n# Retrieve a specific record by ID, selecting only certain fields\nvec_by_id = client.get_by_id(id=\"1\", select_fields=[\"id\", \"metadata\"])\nassert(vec_by_id.embedding is None)\n\n# Retrieve a record by ID without field restrictions\nvec_by_id = client.get_by_id(\"1\")\nassert(vec_by_id.id == \"1\")\n\n# Retrieve multiple records by their IDs\nvectors_by_ids = client.get_by_ids([\"1\", \"3\"])\nassert(len(vectors_by_ids) == 2)\nassert(vectors_by_ids[0].id == \"1\")\nassert(vectors_by_ids[1].id == \"3\")\n\n# Insert or update a single record\nclient.upsert((\"4\", [1,0,0], { \"name\": \"d\" }))\n\n# Update the metadata of a record by its ID\nclient.update_by_id(id=\"4\", metadata={ \"name\": \"4\" })\n\n# Get the total number of records in the table\nrow_count = client.count()\nassert(row_count == 4)\n\n# Retrieve a record by ID, selecting specific fields\nvec_by_id = client.get_by_id(id=\"4\", select_fields=[\"id\", \"embedding\", \"metadata\"])\nassert(vec_by_id.metadata['name'] == \"4\")\n\n# Search for vectors similar to the one with ID \"4\"\nvectors = client.search(query_id=\"4\")\nassert(len(vectors) == row_count)\nassert(vectors[0].id == \"4\")\nassert(vectors[0].distance == 0)\n\n# Search for vectors using the embedding of the vector with ID \"4\"\n# Limit the results to 2 and apply a filter on the metadata\nvectors = client.search(query_embedding=vec_by_id.embedding, limit=2, filter={\"name\": \"a\"}, select_fields=[\"id\"])\nassert(len(vectors) == 1)\nassert(vectors[0].id == \"1\")\nassert(vectors[0].metadata is None)\nassert(vectors[0].embedding is None)\n\n```\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Python client for Lantern",
"version": "0.0.6",
"project_urls": {
"Bug Tracker": "https://github.com/lanterndata/lantern-python/issues",
"Homepage": "https://github.com/lanterndata/lantern-python"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "877e16d8435e7946271f1dd04a143fc1fac1b45234464a0557684cda498ed5b3",
"md5": "1775196caa8b643158bc044b00a33b1a",
"sha256": "317ecc3c9c4faf878124a7caf5b6708ed691345a330d349754ef0ae38536277a"
},
"downloads": -1,
"filename": "lantern_client-0.0.6-py3-none-any.whl",
"has_sig": false,
"md5_digest": "1775196caa8b643158bc044b00a33b1a",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 9397,
"upload_time": "2024-11-06T11:58:31",
"upload_time_iso_8601": "2024-11-06T11:58:31.558323Z",
"url": "https://files.pythonhosted.org/packages/87/7e/16d8435e7946271f1dd04a143fc1fac1b45234464a0557684cda498ed5b3/lantern_client-0.0.6-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "18bb64f90ff4afd07721f24bfc94fbc6892810bc63d00903d4bf53aa550ad31e",
"md5": "a9bc4508d1e6e78e726c3582f11f78c9",
"sha256": "8f58483f9974184c6861a967b5f29b4bc004c1b55b693fcc71706c63fcdedca9"
},
"downloads": -1,
"filename": "lantern_client-0.0.6.tar.gz",
"has_sig": false,
"md5_digest": "a9bc4508d1e6e78e726c3582f11f78c9",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 8478,
"upload_time": "2024-11-06T11:58:32",
"upload_time_iso_8601": "2024-11-06T11:58:32.621849Z",
"url": "https://files.pythonhosted.org/packages/18/bb/64f90ff4afd07721f24bfc94fbc6892810bc63d00903d4bf53aa550ad31e/lantern_client-0.0.6.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-06 11:58:32",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "lanterndata",
"github_project": "lantern-python",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"requirements": [],
"lcname": "lantern-client"
}