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) -> Collectionget_collection(collection_name: str) -> Collectionlist_collections() -> requests.Responsecollections() -> Iterator[Collection]
Collection
Represents a collection in the vector database.
collection = client.get_collection("my_collection")
Methods:
create_index(distance_metric: str = "cosine", ...) -> Indexindex(distance_metric: str = "cosine") -> Indexget_info() -> Dict[str, Any]
Index
Manages indexes and vector operations.
index = collection.create_index()
Methods:
create_transaction() -> Transactiontransaction() -> 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]]) -> Selfcommit() -> Optional[Dict[str, Any]]abort() -> Optional[Dict[str, Any]]
Best Practices
-
Connection Management
- Reuse the client instance across your application
- The client automatically handles authentication and token management
-
Vector Operations
- Use transactions for batch operations
- The context manager (
withstatement) automatically handles commit/abort - Maximum batch size is 200 vectors per transaction
-
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
-
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9ae0416bb2233a26d6670abe861354024b8ce406a5b034a0a28972a43e65cd33
|
|
| MD5 |
25d0f72a52bc9eb8c7dc6bcb6cc77795
|
|
| BLAKE2b-256 |
519c68e1a3c74e718720bce0d619345f719e6a1c2c9fabc15b3a6662430e4ed5
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e83d8a912e79d42bf8aa7632e809e291f77817eaa73c49137177d227eda82a28
|
|
| MD5 |
0926ad6307be5005af7a89c31ada34fb
|
|
| BLAKE2b-256 |
060e45d56bdfaf18bc132ff7859c6eb56b547beddcaa931625f5815911f6de03
|