Skip to main content

VCL Python SDK - Verified Compute Ledger for AI inference

Project description

VCL Python SDK

A drop-in replacement for OpenAI and Anthropic clients that generates cryptographically signed receipts for every AI inference request.

Installation

pip install vcl-sdk

Or install from source:

cd sdk/python
pip install -e .

Quick Start

from vcl import VCLClient

# Initialize client
client = VCLClient(
    api_key="vcl_your_api_key",
    base_url="https://your-vcl-server.com"
)

# OpenAI-compatible usage
response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}]
)

print(response["choices"][0]["message"]["content"])

# Get the receipt for this request
receipt = client.last_receipt
print(f"Receipt ID: {receipt.receipt_id}")
print(f"Input tokens: {receipt.execution.input_tokens}")
print(f"Output tokens: {receipt.execution.output_tokens}")
print(f"Compute units: {receipt.execution.compute_units}")

Anthropic-Compatible Usage

from vcl import VCLClient

client = VCLClient(
    api_key="vcl_your_api_key",
    base_url="https://your-vcl-server.com"
)

response = client.messages.create(
    model="claude-3-sonnet-20240229",
    messages=[{"role": "user", "content": "Hello!"}],
    max_tokens=1024
)

print(response["content"][0]["text"])

Streaming

# OpenAI streaming
for chunk in client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Tell me a story"}],
    stream=True
):
    if chunk.get("choices"):
        content = chunk["choices"][0].get("delta", {}).get("content", "")
        print(content, end="", flush=True)

# Anthropic streaming
for chunk in client.messages.create(
    model="claude-3-sonnet-20240229",
    messages=[{"role": "user", "content": "Tell me a story"}],
    max_tokens=1024,
    stream=True
):
    if chunk.get("type") == "content_block_delta":
        print(chunk["delta"]["text"], end="", flush=True)

Receipt Verification

from vcl import VCLClient, verify_receipt

client = VCLClient(api_key="vcl_...", base_url="https://...")

# Make a request
response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}]
)

# Verify the receipt
result = client.verify_receipt(client.last_receipt_id)
print(f"Valid: {result['valid']}")
print(f"Model: {result['model']}")
print(f"Timestamp: {result['timestamp']}")

# Or verify any receipt by ID (public endpoint)
result = verify_receipt("https://your-vcl-server.com", "receipt-id-here")

Receipt Details

receipt = client.last_receipt

# Basic info
print(f"Receipt ID: {receipt.receipt_id}")
print(f"Timestamp: {receipt.timestamp}")
print(f"Provider: {receipt.provider['name']}")

# Execution details
print(f"Model: {receipt.execution.model_name}")
print(f"Input tokens: {receipt.execution.input_tokens}")
print(f"Output tokens: {receipt.execution.output_tokens}")
print(f"Total tokens: {receipt.total_tokens}")
print(f"Compute units: {receipt.execution.compute_units}")

# Verification
print(f"Receipt hash: {receipt.verification.receipt_hash}")
print(f"Signature: {receipt.verification.signature}")

# On-chain anchor (if available)
if receipt.is_anchored:
    anchor = receipt.verification.anchor
    print(f"Chain: {anchor.chain}")
    print(f"TX Hash: {anchor.tx_hash}")
    print(f"Block: {anchor.block_num}")
    print(f"Merkle Root: {anchor.merkle_root}")

# TEE attestation (if available)
if receipt.is_tee_attested:
    tee = receipt.tee_attestation
    print(f"TEE Type: {tee.type}")
    print(f"MR Enclave: {tee.mr_enclave}")

API Key Management

# List your API keys
keys = client.list_api_keys()
for key in keys["keys"]:
    print(f"{key['name']}: {key['key_prefix']}...")

# Create a new key
new_key = client.create_api_key(name="Production")
print(f"New key: {new_key['key']}")  # Save this!

# Delete a key
client.delete_api_key(key_id="key-id-here")

Usage Statistics

stats = client.get_stats()
print(f"Total receipts: {stats['total_receipts']}")
print(f"Total compute units: {stats['total_compute_units']}")

Error Handling

from vcl import VCLClient, VCLError, AuthenticationError, RateLimitError

client = VCLClient(api_key="vcl_...", base_url="https://...")

try:
    response = client.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": "Hello!"}]
    )
except AuthenticationError:
    print("Invalid API key")
except RateLimitError:
    print("Rate limit exceeded, try again later")
except VCLError as e:
    print(f"Error: {e.message} (status: {e.status_code})")

Local Merkle Proof Verification

from vcl.receipts import verify_merkle_proof

receipt = client.last_receipt

if receipt.is_anchored:
    # Verify the Merkle proof locally
    is_valid = verify_merkle_proof(
        leaf_hash=receipt.verification.receipt_hash,
        merkle_root=receipt.verification.anchor.merkle_root,
        proof=receipt.verification.anchor.proof,
        leaf_index=receipt.verification.anchor.leaf_index,
    )
    print(f"Merkle proof valid: {is_valid}")

License

MIT

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

vcl_sdk-0.1.0.tar.gz (9.6 kB view details)

Uploaded Source

Built Distribution

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

vcl_sdk-0.1.0-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file vcl_sdk-0.1.0.tar.gz.

File metadata

  • Download URL: vcl_sdk-0.1.0.tar.gz
  • Upload date:
  • Size: 9.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for vcl_sdk-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b31565331bcf8f8aee154c09bd92035c2fd89e91671cb731de5c9d414519e90d
MD5 a363ab3d3bae4c3117ade04eb2856d3b
BLAKE2b-256 02ae92248930e1b74fb2d23089a794d1615edc8e6ef9a730e39a2adfc8d796d6

See more details on using hashes here.

File details

Details for the file vcl_sdk-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: vcl_sdk-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.0

File hashes

Hashes for vcl_sdk-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 489ef5ea539e5968d7600132693849af72d7733d1273a06e8165dd2ab000cd35
MD5 5f4d96d6d741e6b64b18526c6563d765
BLAKE2b-256 1e056ce89a93b911c7f91eef88856a6753b331d6e9d94fc2c14d5f321f45aab6

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