Skip to main content

Python SDK for AI Spine - The Stripe for AI Agent Orchestration

Project description

AI Spine Python SDK

Python Version License PyPI Version

Python SDK for AI Spine - The Stripe for AI Agent Orchestration.

AI Spine provides a powerful platform for orchestrating AI agents and workflows, making it easy to build, deploy, and manage complex AI-powered applications.

Features

  • 🚀 Simple Integration - Get started with just a few lines of code
  • 🔄 Flow Execution - Execute and monitor AI workflows
  • 🤖 Agent Management - Create and manage AI agents
  • Async Support - Built-in polling and timeout handling
  • 🛡️ Robust Error Handling - Comprehensive exception hierarchy
  • 🔧 Type Safety - Full type hints for better IDE support
  • 📊 System Monitoring - Health checks and metrics

Installation

pip install ai-spine-sdk

For development dependencies:

pip install ai-spine-sdk[dev]

Quick Start

from ai_spine import AISpine

# Initialize the client
client = AISpine()

# Execute a flow and wait for completion
result = client.execute_and_wait(
    flow_id="sentiment-analysis",
    input_data={"text": "This product is amazing!"}
)

print(f"Result: {result['output_data']}")

Configuration

Client Initialization

from ai_spine import AISpine

client = AISpine(
    api_key="your-api-key",  # Optional - currently not required
    base_url="https://custom-api.ai-spine.com",  # Optional
    timeout=30,  # Request timeout in seconds
    max_retries=3,  # Maximum retry attempts
    debug=True  # Enable debug logging
)

Environment Variables

You can also configure the client using environment variables:

export AI_SPINE_API_KEY="your-api-key"
export AI_SPINE_BASE_URL="https://custom-api.ai-spine.com"

API Reference

Flow Execution

Execute Flow

# Start a flow execution
execution = client.execute_flow(
    flow_id="credit-analysis",
    input_data={
        "customer_id": "CUST-001",
        "loan_amount": 50000
    },
    metadata={"source": "api"}  # Optional metadata
)

print(f"Execution ID: {execution['execution_id']}")

Get Execution Status

# Check execution status
status = client.get_execution(execution_id="exec-123")
print(f"Status: {status['status']}")

Wait for Completion

# Wait for execution to complete
result = client.wait_for_execution(
    execution_id="exec-123",
    timeout=300,  # Maximum wait time in seconds
    interval=2  # Polling interval in seconds
)

Execute and Wait (Convenience Method)

# Execute and wait in one call
result = client.execute_and_wait(
    flow_id="sentiment-analysis",
    input_data={"text": "Great product!"},
    timeout=120
)

Flow Management

# List all flows
flows = client.list_flows()
for flow in flows:
    print(f"{flow['flow_id']}: {flow['name']}")

# Get flow details
flow = client.get_flow("sentiment-analysis")
print(f"Flow: {flow['name']} - {flow['description']}")

Agent Management

# List agents
agents = client.list_agents()

# Create an agent
agent = client.create_agent({
    "name": "Data Processor",
    "type": "processor",
    "configuration": {
        "model": "gpt-4",
        "temperature": 0.7
    }
})

# Delete an agent
success = client.delete_agent("agent-123")

System Operations

# Health check
health = client.health_check()
print(f"System status: {health['status']}")

# Get metrics
metrics = client.get_metrics()
print(f"Total executions: {metrics['executions_total']}")

# Get system status
status = client.get_status()
print(f"Uptime: {status['uptime']} seconds")

Error Handling

The SDK provides a comprehensive exception hierarchy for robust error handling:

from ai_spine import (
    AISpine,
    ValidationError,
    ExecutionError,
    TimeoutError,
    RateLimitError,
    AuthenticationError
)

client = AISpine()

try:
    result = client.execute_and_wait(
        flow_id="analysis",
        input_data={"data": "test"},
        timeout=60
    )
except ValidationError as e:
    print(f"Invalid input: {e}")
except ExecutionError as e:
    print(f"Execution failed: {e}")
    print(f"Execution ID: {e.execution_id}")
except TimeoutError as e:
    print(f"Execution timed out after {e.timeout} seconds")
except RateLimitError as e:
    print(f"Rate limit exceeded. Retry after {e.retry_after} seconds")
except AuthenticationError as e:
    print(f"Authentication failed: {e}")

Advanced Usage

Batch Processing

Process multiple items efficiently:

import concurrent.futures
from ai_spine import AISpine

def process_item(client, item):
    return client.execute_and_wait(
        flow_id="sentiment-analysis",
        input_data=item
    )

client = AISpine()
items = [
    {"text": "Great product!"},
    {"text": "Not satisfied"},
    {"text": "Average experience"}
]

# Parallel processing
with concurrent.futures.ThreadPoolExecutor(max_workers=3) as executor:
    results = list(executor.map(lambda item: process_item(client, item), items))

Context Manager

Use the client as a context manager for automatic cleanup:

with AISpine() as client:
    result = client.execute_and_wait(
        flow_id="analysis",
        input_data={"data": "test"}
    )
    # Session automatically closed when exiting context

Custom Retry Logic

Implement custom retry strategies:

import time
from ai_spine import AISpine, AISpineError

def execute_with_retry(client, flow_id, input_data, max_retries=3):
    for attempt in range(max_retries):
        try:
            return client.execute_and_wait(flow_id, input_data)
        except AISpineError as e:
            if attempt < max_retries - 1:
                wait_time = 2 ** attempt  # Exponential backoff
                time.sleep(wait_time)
            else:
                raise

Data Models

The SDK includes data model classes for type-safe operations:

from ai_spine.models import Flow, Execution, Agent

# Parse API responses into model objects
flow_data = client.get_flow("sentiment-analysis")
flow = Flow.from_dict(flow_data)

print(f"Flow: {flow.name}")
print(f"Created: {flow.created_at}")

# Check execution status
execution_data = client.get_execution("exec-123")
execution = Execution.from_dict(execution_data)

if execution.is_successful:
    print(f"Output: {execution.output_data}")
elif execution.is_failed:
    print(f"Error: {execution.error_message}")

Examples

Check the examples/ directory for complete examples:

  • basic_usage.py - Simple usage patterns
  • error_handling.py - Comprehensive error handling
  • batch_processing.py - Batch and parallel processing

Development

Setup Development Environment

# Clone the repository
git clone https://github.com/Dataframe-Consulting/ai-spine-sdk-python.git
cd ai-spine-sdk-python

# Create virtual environment
python -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install development dependencies
pip install -r requirements-dev.txt
pip install -e .

Running Tests

# Run all tests
pytest

# Run with coverage
pytest --cov=ai_spine --cov-report=html

# Run specific test file
pytest tests/test_client.py -v

Code Quality

# Format code
black ai_spine tests

# Lint code
flake8 ai_spine tests

# Type checking
mypy ai_spine

Building and Publishing

# Build distribution
python -m build

# Upload to TestPyPI
twine upload --repository testpypi dist/*

# Upload to PyPI
twine upload dist/*

Contributing

We welcome contributions! Please see our Contributing Guidelines for details.

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Support

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • Built with ❤️ by the Dataframe AI team
  • Inspired by the simplicity of Stripe's SDK design
  • Thanks to all contributors and users

Note: This SDK is currently in beta. APIs may change in future versions. Please report any issues or feedback through GitHub Issues.

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

ai_spine_sdk-2.3.0.tar.gz (24.5 kB view details)

Uploaded Source

Built Distribution

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

ai_spine_sdk-2.3.0-py3-none-any.whl (17.1 kB view details)

Uploaded Python 3

File details

Details for the file ai_spine_sdk-2.3.0.tar.gz.

File metadata

  • Download URL: ai_spine_sdk-2.3.0.tar.gz
  • Upload date:
  • Size: 24.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for ai_spine_sdk-2.3.0.tar.gz
Algorithm Hash digest
SHA256 733c9682bf4d888ddd60914651825893ee0c055ad4777cdec990076c2c401f64
MD5 62954bcf99cadc5d0ed03219019f35dd
BLAKE2b-256 98ae23355d7c2db23742a16bdadff04859232b36660645bfd19b4a871b818cb5

See more details on using hashes here.

File details

Details for the file ai_spine_sdk-2.3.0-py3-none-any.whl.

File metadata

  • Download URL: ai_spine_sdk-2.3.0-py3-none-any.whl
  • Upload date:
  • Size: 17.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.13

File hashes

Hashes for ai_spine_sdk-2.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2b2e0ab9b6a113d96abf16f78443e36449a6e254105ad2d089dff9a99f557a0c
MD5 8f22d629adac29ec20e78a8bd007f17c
BLAKE2b-256 60c9fc44d1e4ec9ab92630c512e3fe18dc2032ab5e9ae432b04b4de66c2ed90c

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