Skip to main content

Client library for the Casper Vector Database

Project description

Casper Python client

Python client library for the Casper Vector Database.

Installation

pip install casper-client

Quick start

import math
import os
import random
import time

from casper_client import CasperClient, VectorPoint


def gen_vec(dim: int) -> list[float]:
    vec = [random.uniform(-1.0, 1.0) for _ in range(dim)]
    norm = math.sqrt(sum(x * x for x in vec)) or 1.0
    return [x / norm for x in vec]


def main() -> None:
    host = os.getenv("CASPER_HOST", "http://127.0.0.1")
    http_port = int(os.getenv("CASPER_HTTP_PORT", "7222"))

    with CasperClient(host=host, http_port=http_port) as client:
        client.health()

        collection = "example_collection"
        dim = 128

        client.create_collection(collection, dim=dim, max_size=10_000)

        # Upsert vectors (batch)
        points = [VectorPoint(id=vid, vector=gen_vec(dim)) for vid in range(1, 6)]
        client.upsert_vectors(collection, points)

        # Mixed insert + delete
        batch = [VectorPoint(id=vid, vector=gen_vec(dim)) for vid in range(6, 11)]
        client.batch_update(collection, insert=batch, delete=[])

        # Create HNSW index — build is asynchronous on the server.
        client.create_hnsw_index(
            collection,
            metric="inner-product",
            quantization="i8",
            m=16,
            m0=32,
            ef_construction=200,
            normalization=True,
        )

        # Wait until the index is ready before searching.
        while not client.get_collection(collection).has_index:
            time.sleep(0.2)

        hits = client.search(collection, query=gen_vec(dim), limit=10)
        for i, h in enumerate(hits[:5], start=1):
            print(f"  {i}. id={h.id} score={h.score}")

        client.delete_vectors(collection, [10])
        client.delete_index(collection)
        client.delete_collection(collection)


if __name__ == "__main__":
    main()

Examples

Run the example provided in this repository:

python examples/example.py

The example demonstrates:

  • Collection management (create, delete, list, info)
  • Vector operations: upsert_vectors (batch PUT), delete_vectors (batch by id), batch_update (mixed insert+delete), get_vector
  • Mute / unmute (mute_collection, unmute_collection)
  • Index management (create_hnsw_index, delete_index) — the index build is asynchronous; the server returns 202 and the client should poll get_collection().has_index
  • Search (search, with optional ef_search override). Binary response format is decoded automatically.

License

Licensed under the Apache License Version 2.0.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

casper_client-0.1.2.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

casper_client-0.1.2-py3-none-any.whl (13.9 kB view details)

Uploaded Python 3

File details

Details for the file casper_client-0.1.2.tar.gz.

File metadata

  • Download URL: casper_client-0.1.2.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.12.7 Linux/6.17.0-35-generic

File hashes

Hashes for casper_client-0.1.2.tar.gz
Algorithm Hash digest
SHA256 89581047a48cd130e9c6416eb6f9963bf42b35094cafae6332c051f414dddef7
MD5 afe7e67a3132dca81687477b9d93f72f
BLAKE2b-256 a13d755c22ac6e73feaeb28eee54003e3f2d29f8ad836762e4769f7de5e38e8a

See more details on using hashes here.

File details

Details for the file casper_client-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: casper_client-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 13.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.2 CPython/3.12.7 Linux/6.17.0-35-generic

File hashes

Hashes for casper_client-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 bf385969be078d1bd543df6283c2a59ef7ec17484c4f35366e9f3f6857c2ecfa
MD5 4b0f883b5450110f00fe893e41cbd1d2
BLAKE2b-256 7fccaa1e4a9fe74893b0a6e7cda95ebf43b3fc421ada0b7eeca253a4b0f47745

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page