Skip to main content

Official Python SDK for the Shilp Vector Database API

Project description

Shilp Python SDK

PyPI version

Official Python SDK for the Shilp Vector Database API.

Installation

pip install shilp-sdk

Or install from source:

git clone https://github.com/anvitra-ai/shilp-sdk-py.git
cd shilp-sdk-py
pip install -e .

Usage

from shilp import Client, AddCollectionRequest, InsertRecordRequest, SearchRequest

# Initialize the client
client = Client("http://localhost:3000")

# Check health
health = client.health_check()
print(f"Health: {health.success}, Version: {health.version}")

# List collections
collections = client.list_collections()
print(f"Collections: {[c.name for c in collections.data]}")

# Drop collection if exists
try:
    client.drop_collection("my-collection")
except:
    pass

# Create a new collection
client.add_collection(
    AddCollectionRequest(
        name="my-collection",
        storage_type=StorageBackendType.FILE,
        reference_storage_type=StorageBackendType.FILE
    )
)

# Insert a record
client.insert_record(InsertRecordRequest(
    collection="my-collection",
    id="record-1",
    record={
        "title": "Hello World",
        "content": "This my test description"
    },
    fields=["title", "content"],
))

# Flush collection (important after inserting records)
client.flush_collection("my-collection")

# Search
results = client.search_data(SearchRequest(
    collection="my-collection",
    query="Hello",
    fields=["title"],
    limit=10,
))
print(f"Search results: {results.data}")

# Advanced search with max distance filter
results = client.search_data(SearchRequest(
    collection="my-collection",
    query="Hello",
    fields=["title"],
    limit=10,
    max_distance=0.5,
))
print(f"Advanced search results: {results.data}")

# Clean up
client.drop_collection("my-collection")

Features

  • Collection Management: List, add, drop, rename, load, unload, flush, reindex
  • Data Ingestion & Search: Insert records, ingest data, search with keyword fields support
  • Record Management: Insert, delete, expiry cleanup
  • Debug Collection Operations: Distance, node info, levels, neighbors
  • Oplog Operations: Replica registration, heartbeat, get entries, status
  • Storage Listing: List and read storage files
  • Health Check: Monitor API health and version

Debug Operations

The SDK provides debug endpoints for inspecting collection internals:

# Re-index a collection
client.reindex_collection("my-collection")

# Get collection levels
levels = client.get_collection_levels("my-collection")

# Get nodes at a specific level
nodes = client.get_collection_nodes_at_level("my-collection", 0)

# Get node information
node_info = client.get_collection_node_info("my-collection", "title", 123)

# Get node neighbors at a level
neighbors = client.get_collection_node_neighbors_at_level(
    "my-collection", "title", 123, 0, limit=10, offset=0
)

# Get distance calculation
distance = client.get_collection_distance("my-collection", "title", 123, "some text")

# Get node by reference ID
ref_node = client.get_collection_node_by_reference_node_id("my-collection", 456)

Oplog Operations

The SDK provides oplog (operation log) endpoints for replica synchronization:

# Register a replica for oplog tracking
register_resp = client.register_replica("replica-1")
print(f"Registered replica: {register_resp.success}")

# Get oplog status for a collection
status = client.get_oplog_status("my-collection")
print(f"Oplog status - Last LSN: {status.last_lsn}, "
      f"Retention LSN: {status.retention_lsn}, "
      f"Replicas: {status.replica_count}")

# Get oplog entries after a specific LSN
entries = client.get_oplog_entries("my-collection", after_lsn=1000, limit=100)
print(f"Retrieved {entries.count} oplog entries, last LSN: {entries.last_lsn}")

# Get oplog entries for all collections
all_entries = client.get_oplog_entries("", after_lsn=1000, limit=100)

# Update replica LSN (heartbeat)
update_resp = client.update_replica_lsn("my-collection", "replica-1", 1050)
print(f"Updated replica LSN: {update_resp.success}")

# Unregister replica
client.unregister_replica("replica-1")

Filtering and Sorting

The SDK supports advanced filtering and sorting:

from shilp import FilterExpression, CompoundFilter, SortExpression, CompoundSort, FilterOp, SortOrder, AttrType

# Create filters
filters = CompoundFilter(and_=[
    FilterExpression(attribute="age", op=FilterOp.GREATER_THAN, value=25),
    FilterExpression(attribute="status", op=FilterOp.EQUALS, value="active"),
])

# Create sort criteria
sort = CompoundSort(sorts=[
    SortExpression(attribute="created_at", order=SortOrder.DESCENDING),
])

# Search with filters and sorting
results = client.search_data(SearchRequest(
    collection="my-collection",
    query="search term",
    fields=["content"],
    limit=20,
    filters=filters,
    sort=sort,
))

Export and Import Collections

# Export a collection
with client.export_collection("my-collection") as f:
    with open("my-collection-export.bin", "wb") as out:
        out.write(f.read())

# Import a collection
client.import_collection("my-collection-export.bin")

Embedding Models

# List available embedding models
models = client.list_embedding_models()
for provider in models.data:
    print(f"Provider: {provider.name} (default: {provider.is_default})")
    for model in provider.models:
        print(f"  - {model.name} (default: {model.is_default})")

API Reference

Client

Client(base_url: str, timeout: int = 30, session: Optional[requests.Session] = None)

Initialize the Shilp API client.

Parameters:

  • base_url: Base URL of the Shilp server (e.g., "http://localhost:3000")
  • timeout: Request timeout in seconds (default: 30)
  • session: Optional custom requests.Session instance

Collection Management Methods

  • list_collections() -> ListCollectionsResponse
  • add_collection(request: AddCollectionRequest) -> GenericResponse
  • drop_collection(name: str) -> GenericResponse
  • rename_collection(old_name: str, new_name: str) -> GenericResponse
  • load_collection(name: str) -> GenericResponse
  • unload_collection(name: str) -> GenericResponse
  • flush_collection(name: str) -> GenericResponse
  • reindex_collection(name: str) -> GenericResponse
  • export_collection(name: str) -> BinaryIO
  • import_collection(file_path: str) -> GenericResponse

Data Operations Methods

  • insert_record(request: InsertRecordRequest) -> InsertRecordResponse
  • delete_record(collection_name: str, record_id: str) -> GenericResponse
  • expiry_cleanup(collection_name: str) -> GenericResponse
  • ingest_data(request: IngestRequest) -> IngestResponse
  • search_data(request: SearchRequest) -> SearchResponse

Storage Methods

  • list_storage(path: str = "") -> ListStorageResponse
  • read_document(path: str, rows: int = 0, skip: int = 0) -> ReadDocumentResponse
  • list_embedding_models() -> ListEmbeddingModelsResponse

Debug Methods

  • get_collection_distance(collection_name: str, field: str, node_id: int, text: str) -> DebugDistanceResponse
  • get_collection_node_info(collection_name: str, field: str, node_id: int) -> DebugNodeInfoResponse
  • get_collection_node_neighbors_at_level(...) -> DebugNodeInfoResponse
  • get_collection_levels(collection_name: str) -> DebugLevelsResponse
  • get_collection_nodes_at_level(collection_name: str, level: int) -> DebugNodesAtLevelResponse
  • get_collection_node_by_reference_node_id(collection_name: str, node_id: int) -> DebugReferenceNodeResponse

Oplog Methods

  • get_oplog_entries(collection: str, after_lsn: int, limit: int = 0) -> GetOplogResponse
  • update_replica_lsn(collection: str, replica_id: str, lsn: int) -> UpdateReplicaLSNResponse
  • register_replica(replica_id: str) -> GenericResponse
  • unregister_replica(replica_id: str) -> GenericResponse
  • get_oplog_status(collection: str) -> OplogStatusResponse

Health Check

  • health_check() -> HealthResponse

Development

# Install development dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Format code
black shilp/

# Type checking
mypy shilp/

License

MIT License - see LICENSE file for details.

Related Projects

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

shilp_sdk-0.15.0.tar.gz (33.0 kB view details)

Uploaded Source

Built Distribution

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

shilp_sdk-0.15.0-py3-none-any.whl (22.8 kB view details)

Uploaded Python 3

File details

Details for the file shilp_sdk-0.15.0.tar.gz.

File metadata

  • Download URL: shilp_sdk-0.15.0.tar.gz
  • Upload date:
  • Size: 33.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for shilp_sdk-0.15.0.tar.gz
Algorithm Hash digest
SHA256 cc74fab7f4a196450ca9e3023588de61a79e0d6e9d1bd31067a3c57b01471b7b
MD5 c26452d03ea8d780ced10549afa7f7aa
BLAKE2b-256 97ec3a3fb25d99af259f957813e6c1d5d960520259ea84b3db3a408995fb6505

See more details on using hashes here.

File details

Details for the file shilp_sdk-0.15.0-py3-none-any.whl.

File metadata

  • Download URL: shilp_sdk-0.15.0-py3-none-any.whl
  • Upload date:
  • Size: 22.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for shilp_sdk-0.15.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2c54003d0a03b8b47794fce7fe5059d8676e331058636a9bf0baa8f2be3da359
MD5 fb9b495934167d1167e635cf523a7820
BLAKE2b-256 8ba6dd651a7ef3bce6813c63938b04fcfc3a1199b55d8b69701716a02c7c0d5e

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