Official Python SDK for the Shilp Vector Database API
Project description
Shilp Python SDK
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_filters=[
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() -> ListCollectionsResponseadd_collection(request: AddCollectionRequest) -> GenericResponsedrop_collection(name: str) -> GenericResponserename_collection(old_name: str, new_name: str) -> GenericResponseload_collection(name: str) -> GenericResponseunload_collection(name: str) -> GenericResponseflush_collection(name: str) -> GenericResponsereindex_collection(name: str) -> GenericResponseexport_collection(name: str) -> BinaryIOimport_collection(file_path: str) -> GenericResponse
Data Operations Methods
insert_record(request: InsertRecordRequest) -> InsertRecordResponsedelete_record(collection_name: str, record_id: str) -> GenericResponseexpiry_cleanup(collection_name: str) -> GenericResponseingest_data(request: IngestRequest) -> IngestResponsesearch_data(request: SearchRequest) -> SearchResponse
Storage Methods
list_storage(path: str = "") -> ListStorageResponseread_document(path: str, rows: int = 0, skip: int = 0) -> ReadDocumentResponselist_embedding_models() -> ListEmbeddingModelsResponse
Debug Methods
get_collection_distance(collection_name: str, field: str, node_id: int, text: str) -> DebugDistanceResponseget_collection_node_info(collection_name: str, field: str, node_id: int) -> DebugNodeInfoResponseget_collection_node_neighbors_at_level(...) -> DebugNodeInfoResponseget_collection_levels(collection_name: str) -> DebugLevelsResponseget_collection_nodes_at_level(collection_name: str, level: int) -> DebugNodesAtLevelResponseget_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) -> GetOplogResponseupdate_replica_lsn(collection: str, replica_id: str, lsn: int) -> UpdateReplicaLSNResponseregister_replica(replica_id: str) -> GenericResponseunregister_replica(replica_id: str) -> GenericResponseget_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
- shilp-sdk-go - Official Go SDK for Shilp
Project details
Release history Release notifications | RSS feed
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 shilp_sdk-0.11.0.tar.gz.
File metadata
- Download URL: shilp_sdk-0.11.0.tar.gz
- Upload date:
- Size: 22.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e8683a69a2a6a1c79c499517cfe0f8f759b146a846ae7de5f25b8770ce2e1ad8
|
|
| MD5 |
e3fed27e2d7e0a3aaaaa548cce0214cd
|
|
| BLAKE2b-256 |
899149c1f03b2fa1ee4b9385f74c73c6278d4a65b481765104b43e54cd23d57d
|
File details
Details for the file shilp_sdk-0.11.0-py3-none-any.whl.
File metadata
- Download URL: shilp_sdk-0.11.0-py3-none-any.whl
- Upload date:
- Size: 16.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2d74245ebdf8b8a2bcb2cc645f0bd36ff0a347dbf478717283224fa5a8001605
|
|
| MD5 |
c5adf2dd9b94a84306882ac2d811e024
|
|
| BLAKE2b-256 |
da93d9c06f503a49d21bf20c5cead135444d584ce46e18c88bdc049eac67a53d
|