|
| 1 | + |
| 2 | +""" |
| 3 | +!!! warning |
| 4 | + The Sift Client is experimental and is subject to change. |
| 5 | +
|
| 6 | +
|
| 7 | +# Sift Client Library |
| 8 | +
|
| 9 | +This library provides a high-level Python client for interacting with Sift APIs. It offers both synchronous and |
| 10 | +asynchronous interfaces, strong type checking, and a Pythonic API design. |
| 11 | +
|
| 12 | +## Installation |
| 13 | +
|
| 14 | +```bash |
| 15 | +pip install sift-stack-py |
| 16 | +``` |
| 17 | +
|
| 18 | +## Getting Started |
| 19 | +
|
| 20 | +### Initializing the Client |
| 21 | +
|
| 22 | +You can initialize the Sift client with your API key and service URLs: |
| 23 | +
|
| 24 | +```python |
| 25 | +from sift_client import SiftClient |
| 26 | +from datetime import datetime |
| 27 | +
|
| 28 | +# Initialize with individual parameters |
| 29 | +client = SiftClient( |
| 30 | + api_key="your-api-key", |
| 31 | + grpc_url="your-sift-grpc-url", |
| 32 | + rest_url="your-sift-rest-url" |
| 33 | +) |
| 34 | +
|
| 35 | +# Or use a connection configuration |
| 36 | +from sift_client.transport import SiftConnectionConfig |
| 37 | +
|
| 38 | +config = SiftConnectionConfig( |
| 39 | + api_key="your-api-key", |
| 40 | + grpc_url="your-sift-grpc-url", |
| 41 | + rest_url="your-sift-rest-url" |
| 42 | +) |
| 43 | +client = SiftClient(connection_config=config) |
| 44 | +``` |
| 45 | +
|
| 46 | +The `SiftConnectionConfig` provides access to additional configuration options such as `use_ssl` and `cert_via_openssl`. |
| 47 | +
|
| 48 | +### Using Synchronous and Asynchronous APIs |
| 49 | +
|
| 50 | +The Sift client provides both synchronous and asynchronous versions of all APIs. You can choose the one that best fits |
| 51 | +your application's needs. |
| 52 | +
|
| 53 | +#### Synchronous API |
| 54 | +
|
| 55 | +The synchronous API is perfect for scripts, notebooks, and applications that don't need asynchronous operation: |
| 56 | +
|
| 57 | +```python |
| 58 | +# Get an asset by ID |
| 59 | +asset = client.assets.get(asset_id="asset123") |
| 60 | +
|
| 61 | +# List assets with filtering |
| 62 | +assets = client.assets.list_( |
| 63 | + name_contains="example", |
| 64 | + created_after=datetime(2023, 1, 1), |
| 65 | + include_archived=False |
| 66 | +) |
| 67 | +
|
| 68 | +# Find a single asset matching criteria |
| 69 | +asset = client.assets.find(name="my-asset") |
| 70 | +``` |
| 71 | +
|
| 72 | +#### Asynchronous API |
| 73 | +
|
| 74 | +The asynchronous API is ideal for high-performance applications and services that need to make concurrent API calls: |
| 75 | +
|
| 76 | +```python |
| 77 | +import asyncio |
| 78 | +
|
| 79 | +
|
| 80 | +async def get_asset_async(): |
| 81 | + # Get an asset by ID asynchronously |
| 82 | + asset = await client.assets_async.get(asset_id="asset123") |
| 83 | +
|
| 84 | + # Running Sync within async also works |
| 85 | + some_other_asset = client.assets.get(asset_id="asset456") |
| 86 | +
|
| 87 | + return asset |
| 88 | +
|
| 89 | +
|
| 90 | +# Run in an async context |
| 91 | +asset = asyncio.run(get_asset_async()) |
| 92 | +
|
| 93 | +``` |
| 94 | +
|
| 95 | +### Working with Sift Types |
| 96 | +
|
| 97 | +Sift types (like `Asset`, `Run`, etc.) are immutable Pydantic models that provide a convenient interface for working |
| 98 | +with Sift resources. |
| 99 | +
|
| 100 | +#### Accessing Properties |
| 101 | +
|
| 102 | +```python |
| 103 | +# Get an asset |
| 104 | +asset = client.assets.get(asset_id="asset123") |
| 105 | +
|
| 106 | +# Access properties |
| 107 | +print(f"Asset name: {asset.name}") |
| 108 | +print(f"Created on: {asset.created_date}") |
| 109 | +print(f"Tags: {', '.join(asset.tags)}") |
| 110 | +print(f"Is archived: {asset.is_archived}") |
| 111 | +``` |
| 112 | +
|
| 113 | +#### Using Methods on Sift Types |
| 114 | +
|
| 115 | +Sift types have convenient methods for common operations. These methods use the synchronous API internally. |
| 116 | +**Using these methods will update the instance in-place.** |
| 117 | +
|
| 118 | +```python |
| 119 | +# Get an asset |
| 120 | +asset = client.assets.get(asset_id="asset123") |
| 121 | +
|
| 122 | +# Archive the asset |
| 123 | +asset.archive(archive_runs=True) |
| 124 | +
|
| 125 | +# Update the asset |
| 126 | +asset.update({ |
| 127 | + "tags": ["updated", "example"] |
| 128 | +}) |
| 129 | +``` |
| 130 | +
|
| 131 | +> **Note:** Type methods only work with the synchronous API. If you need to use the asynchronous API, you should use the |
| 132 | +> resource APIs directly. |
| 133 | +
|
| 134 | +#### Creating Update Models |
| 135 | +
|
| 136 | +For more complex updates, you can create update models (instead of a key-value dictionary): |
| 137 | +
|
| 138 | +```python |
| 139 | +from sift_client.types.asset import AssetUpdate |
| 140 | +
|
| 141 | +# Create an update model |
| 142 | +update = AssetUpdate(tags=["new", "tags"]) |
| 143 | +
|
| 144 | +# Apply the update |
| 145 | +asset = client.assets.update(asset="asset123", update=update) |
| 146 | +
|
| 147 | +# Or using the asset method |
| 148 | +asset = client.assets.get(asset_id="asset123").update(update) |
| 149 | +``` |
| 150 | +
|
| 151 | +## Advanced Usage |
| 152 | +
|
| 153 | +### Working with Tags |
| 154 | +
|
| 155 | +Tags are a powerful way to organize and filter your assets: |
| 156 | +
|
| 157 | +```python |
| 158 | +# Add tags when updating an asset |
| 159 | +asset.update({ |
| 160 | + "tags": ["production", "model-v1", "trained"] |
| 161 | +}) |
| 162 | +
|
| 163 | +# Filter assets by tags |
| 164 | +production_assets = client.assets.list_( |
| 165 | + tags=["production"] |
| 166 | +) |
| 167 | +``` |
| 168 | +
|
| 169 | +### Filtering Assets |
| 170 | +
|
| 171 | +The client provides various ways to filter different Sift types: |
| 172 | +
|
| 173 | +```python |
| 174 | +# Filter by name (exact match) |
| 175 | +assets = client.assets.list_(name="my-model") |
| 176 | +
|
| 177 | +# Filter by name (contains) |
| 178 | +assets = client.assets.list_(name_contains="model") |
| 179 | +
|
| 180 | +# Filter by name (regex) |
| 181 | +assets = client.assets.list_(name_regex="model-v[0-9]+") |
| 182 | +
|
| 183 | +# Filter by creation date |
| 184 | +assets = client.assets.list_( |
| 185 | + created_after=datetime(2023, 1, 1), |
| 186 | + created_before=datetime(2023, 12, 31) |
| 187 | +) |
| 188 | +
|
| 189 | +# Filter by modification date |
| 190 | +assets = client.assets.list_( |
| 191 | + modified_after=datetime(2023, 6, 1) |
| 192 | +) |
| 193 | +
|
| 194 | +# Include archived assets |
| 195 | +all_assets = client.assets.list_(include_archived=True) |
| 196 | +
|
| 197 | +# Limit the number of results |
| 198 | +recent_assets = client.assets.list_( |
| 199 | + limit=10, |
| 200 | + order_by="modified_date desc" |
| 201 | +) |
| 202 | +``` |
| 203 | +
|
| 204 | +
|
| 205 | +""" |
| 206 | + |
| 207 | + |
1 | 208 | from sift_client.client import SiftClient |
2 | 209 | from sift_client.transport import SiftConnectionConfig |
0 commit comments