Skip to main content

A Python SDK for interacting with the Cosdata Vector Database

Project description

Cosdata Python SDK

A Python SDK for interacting with the Cosdata Vector Database.

Installation

pip install cosdata-client

Quick Start

from cosdata.client import Client  # Import the Client class

# Initialize the client (all parameters are optional)
client = Client(
    host="http://127.0.0.1:8443",  # Default host
    username="admin",               # Default username
    password="admin",               # Default password
    verify=False                    # SSL verification
)

# Create a collection
collection = client.create_collection(
    name="my_collection",
    dimension=768,                  # Vector dimension
    description="My vector collection"
)

# Create an index (all parameters are optional)
index = collection.create_index(
    distance_metric="cosine",       # Default: cosine
    num_layers=10,                   # Default: 10
    max_cache_size=1000,            # Default: 1000
    ef_construction=128,            # Default: 128
    ef_search=64,                  # Default: 64
    neighbors_count=32,             # Default: 32
    level_0_neighbors_count=64      # Default: 64
)

# Generate some vectors (example with random data)
import numpy as np

def generate_random_vector(id: int, dimension: int) -> dict:
    values = np.random.uniform(-1, 1, dimension).tolist()
    return {
        "id": id,
        "values": values,
        "metadata": {  # Optional metadata
            "created_at": "2024-03-20",
            "category": "example"
        }
    }

# Generate and insert vectors
vectors = [generate_random_vector(i, 768) for i in range(100)]

# Add vectors using a transaction
with index.transaction() as txn:
    txn.upsert(vectors)

# Search for similar vectors
results = index.query(
    vector=vectors[0]["values"],  # Use first vector as query
    nn_count=5                    # Number of nearest neighbors
)

# Fetch a specific vector
vector = index.fetch_vector(vector_id="1")

# Get collection information
collection_info = collection.get_info()
print(f"Collection info: {collection_info}")

# List all collections
print("Available collections:")
for coll in client.collections():
    print(f" - {coll.name} (dimension: {coll.dimension})")

API Reference

Client

The main client for interacting with the Vector Database API.

client = Client(
    host="http://127.0.0.1:8443",  # Optional
    username="admin",               # Optional
    password="admin",               # Optional
    verify=False                    # Optional
)

Methods:

  • create_collection(name: str, dimension: int = 1024, description: Optional[str] = None) -> Collection
  • get_collection(collection_name: str) -> Collection
  • list_collections() -> requests.Response
  • collections() -> Iterator[Collection]

Collection

Represents a collection in the vector database.

collection = client.get_collection("my_collection")

Methods:

  • create_index(distance_metric: str = "cosine", ...) -> Index
  • index(distance_metric: str = "cosine") -> Index
  • get_info() -> Dict[str, Any]

Index

Manages indexes and vector operations.

index = collection.create_index()

Methods:

  • create_transaction() -> Transaction
  • transaction() -> Iterator[Transaction] (context manager)
  • query(vector: List[float], nn_count: int = 5) -> Dict[str, Any]
  • fetch_vector(vector_id: Union[str, int]) -> Dict[str, Any]

Transaction

Manages batch operations on vectors.

# Using context manager (recommended)
with index.transaction() as txn:
    txn.upsert(vectors)

# Manual transaction management
txn = index.create_transaction()
txn.upsert(vectors)
txn.commit()  # or txn.abort()

Methods:

  • upsert(vectors: List[Dict[str, Any]]) -> Self
  • commit() -> Optional[Dict[str, Any]]
  • abort() -> Optional[Dict[str, Any]]

Best Practices

  1. Connection Management

    • Reuse the client instance across your application
    • The client automatically handles authentication and token management
  2. Vector Operations

    • Use transactions for batch operations
    • The context manager (with statement) automatically handles commit/abort
    • Maximum batch size is 200 vectors per transaction
  3. Error Handling

    • All operations raise exceptions on failure
    • Use try/except blocks for error handling
    • Transactions automatically abort on exceptions when using the context manager
  4. Performance

    • Adjust index parameters based on your use case
    • Use appropriate vector dimensions
    • Consider batch sizes for large operations

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

cosdata_client-0.1.4.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

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

cosdata_client-0.1.4-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file cosdata_client-0.1.4.tar.gz.

File metadata

  • Download URL: cosdata_client-0.1.4.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for cosdata_client-0.1.4.tar.gz
Algorithm Hash digest
SHA256 9ae0416bb2233a26d6670abe861354024b8ce406a5b034a0a28972a43e65cd33
MD5 25d0f72a52bc9eb8c7dc6bcb6cc77795
BLAKE2b-256 519c68e1a3c74e718720bce0d619345f719e6a1c2c9fabc15b3a6662430e4ed5

See more details on using hashes here.

File details

Details for the file cosdata_client-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: cosdata_client-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.2

File hashes

Hashes for cosdata_client-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 e83d8a912e79d42bf8aa7632e809e291f77817eaa73c49137177d227eda82a28
MD5 0926ad6307be5005af7a89c31ada34fb
BLAKE2b-256 060e45d56bdfaf18bc132ff7859c6eb56b547beddcaa931625f5815911f6de03

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