Python SDK Overview
The PaiTIENT Secure Model Service Python SDK provides a comprehensive interface for deploying, managing, and using AI models in a HIPAA/SOC2 compliant environment.
Installation
Install the SDK using pip:
pip install paitient-secure-modelFor detailed installation instructions, see the Installation Guide.
Authentication
Before using the SDK, you need to set up authentication:
from paitient_secure_model import Client
# Using environment variables (recommended)
client = Client()
# Or with explicit credentials
client = Client(
api_key="your-api-key",
client_id="your-client-id"
)Basic Usage
Deploying a Model
# Deploy a model
deployment = client.create_deployment(
model_name="ZimaBlueAI/HuatuoGPT-o1-8B",
deployment_name="clinical-assistant",
compute_type="gpu",
instance_type="g4dn.xlarge"
)
print(f"Deployment ID: {deployment.id}")
print(f"Status: {deployment.status}")
# Wait for deployment to be ready
deployment.wait_until_ready()Generating Text
# Generate text from a deployed model
response = client.generate_text(
deployment_id=deployment.id,
prompt="What are the potential side effects of metformin?",
max_tokens=500,
temperature=0.7
)
print(response.text)Managing Deployments
# List all deployments
deployments = client.list_deployments()
for dep in deployments:
print(f"{dep.id}: {dep.name} - {dep.status}")
# Get a specific deployment
deployment = client.get_deployment("dep_12345abcde")
# Update a deployment
client.update_deployment(
deployment_id="dep_12345abcde",
min_replicas=2,
max_replicas=5
)
# Delete a deployment
client.delete_deployment("dep_12345abcde")Advanced Features
Streaming Responses
For long-form text generation, streaming the response can provide a better user experience:
# Stream the response as it's generated
for chunk in client.generate_text_stream(
deployment_id="dep_12345abcde",
prompt="Write a detailed summary of diabetes management techniques.",
max_tokens=1000
):
print(chunk.text, end="", flush=True)Batch Processing
For processing multiple prompts efficiently:
# Define a list of prompts
prompts = [
"What are the symptoms of hypertension?",
"What are common treatments for type 2 diabetes?",
"Explain the mechanism of action for statins."
]
# Process in batch
results = client.generate_text_batch(
deployment_id="dep_12345abcde",
prompts=prompts,
max_tokens=300
)
for i, result in enumerate(results):
print(f"Prompt {i+1}: {prompts[i]}")
print(f"Response: {result.text}")
print()Fine-tuning Models
The SDK provides support for fine-tuning models:
# Prepare training data
train_file = client.prepare_fine_tuning_file(
file_path="training_data.jsonl",
validation_split=0.1
)
# Start fine-tuning job
fine_tune_job = client.create_fine_tuning_job(
model="ZimaBlueAI/HuatuoGPT-o1-8B",
training_file=train_file.id,
epochs=3
)
# Wait for fine-tuning to complete
fine_tune_job.wait_until_complete()
# Deploy fine-tuned model
deployment = client.create_deployment(
model_name=fine_tune_job.fine_tuned_model,
deployment_name="custom-clinical-assistant"
)Secure Multi-tenancy
For applications serving multiple clients, the SDK supports secure multi-tenancy:
# Initialize with multi-tenant support
client = Client(multi_tenant=True)
# Generate text for a specific tenant
response = client.generate_text(
deployment_id="dep_12345abcde",
prompt="What are the potential side effects of metformin?",
tenant_id="tenant_12345" # Ensures strong isolation
)Error Handling
The SDK raises specific exceptions for different error types:
from paitient_secure_model import Client
from paitient_secure_model.exceptions import (
PaiTIENTApiError,
AuthenticationError,
ResourceNotFoundError,
RateLimitError
)
client = Client()
try:
deployment = client.get_deployment("non-existent-id")
except ResourceNotFoundError as e:
print(f"Resource not found: {e.message}")
except AuthenticationError as e:
print(f"Authentication failed: {e.message}")
except RateLimitError as e:
print(f"Rate limit exceeded, retry after {e.retry_after} seconds")
except PaiTIENTApiError as e:
print(f"API error: {e.code} - {e.message}")Logging
Control the SDK's logging level:
import logging
from paitient_secure_model import Client
# Enable debug logging
logging.basicConfig(level=logging.DEBUG)
client = Client()
# Or configure through the client
client = Client(log_level=logging.INFO)Async Support
The SDK provides async versions of all methods for use with asyncio:
import asyncio
from paitient_secure_model.async_client import AsyncClient
async def main():
client = AsyncClient()
# Deploy a model
deployment = await client.create_deployment(
model_name="ZimaBlueAI/HuatuoGPT-o1-8B",
deployment_name="clinical-assistant"
)
# Wait for deployment to be ready
await deployment.wait_until_ready()
# Generate text
response = await client.generate_text(
deployment_id=deployment.id,
prompt="What are the potential side effects of metformin?"
)
print(response.text)
# Run the async function
asyncio.run(main())SDK Reference
For complete details on all classes and methods, see the Python SDK API Reference.
Next Steps
- Learn about Deployment
- Explore Text Generation
- Understand Fine-tuning
- Review Security Best Practices