Python SDK for Velatir - AI function monitoring and approval
Project description
velatir
Official Python SDK for Velatir, a service that allows you to monitor and approve/reject AI function calls through review tasks.
Installation
pip install velatir
Quick Start
import velatir
# Initialize the SDK with your API key
velatir.init(api_key="your-api-key")
# Decorate functions you want to monitor
@velatir.review_task()
async def send_email(to: str, subject: str, body: str):
"""Send an email to the customer"""
print(f"Sending email to {to}: {subject}")
# Your email sending logic here
# Call the function as usual (or from LLM tool)
await send_email("customer@example.com", "Welcome!", "Hello from Velatir!")
How It Works
The @review_task() decorator intercepts function calls and:
- Creates a review task with function details and arguments via the Velatir API
- Processes the API response:
- If
approved: The function runs immediately - If
pending: The SDK polls the API every 5 seconds until approved or denied - If
denied: An exception is raised and the function doesn't run
- If
Features
- Monitor function calls in real-time through review tasks
- Approve or reject function execution with optional feedback
- Automatically handle pending approval states
- Works with both synchronous and asynchronous functions
- Configurable polling intervals and timeout settings
- Review task chaining via parent task IDs
- LLM explanation support for better context
Advanced Usage
Custom Polling Configuration
@velatir.review_task(polling_interval=2.0, max_attempts=30)
async def delete_user(user_id: str):
"""Delete a user from the system"""
# Deletion logic here
Adding Metadata
@velatir.review_task(metadata={"priority": "high", "team": "billing"})
async def charge_credit_card(card_id: str, amount: float):
"""Charge a customer's credit card"""
# Charging logic here
Direct Client Usage
You can also use the client directly for more control:
# Get the global client
client = velatir.get_client()
# Create a review task synchronously
response = client.create_review_task_sync(
function_name="charge_card",
args={"card_id": "card_123", "amount": 99.99},
doc="Charge customer credit card",
llm_explanation="LLM is requesting to charge the customer",
metadata={"priority": "high"}
)
# Wait for approval synchronously
if response.is_pending:
approval = client.wait_for_approval_sync(
review_task_id=response.review_task_id,
polling_interval=2.0
)
Asynchronous Client Usage
async with velatir.Client(api_key="your-api-key") as client:
# Create a review task
response = await client.create_review_task(
function_name="send_email",
args={"to": "user@example.com", "subject": "Hello"},
doc="Send email to user",
llm_explanation="AI is sending a welcome email",
metadata={"priority": "low"}
)
# Wait for approval if pending
if response.is_pending:
approval = await client.wait_for_approval(
review_task_id=response.review_task_id,
polling_interval=5.0
)
Configuration
Configure logging and retries:
import velatir
from velatir import LogLevel
velatir.init(
api_key="your-api-key",
log_level=LogLevel.INFO, # 0=NONE, 1=ERROR, 2=INFO, 3=DEBUG
max_retries=3,
retry_backoff=0.5
)
# Configure Python's logging for Velatir
import logging
velatir.configure_logging(level=logging.INFO)
Error Handling
When a review task is denied:
try:
await risky_function()
except velatir.VelatirReviewTaskDeniedError as e:
print(f"Review task denied: {e}")
print(f"Review Task ID: {e.review_task_id}")
print(f"Requested Change: {e.requested_change}")
Available Error Types
# Base error class
velatir.VelatirError
# API-specific errors
velatir.VelatirAPIError # API returned an error
velatir.VelatirTimeoutError # Request timed out
velatir.VelatirReviewTaskDeniedError # Review task execution denied
API Reference
Client Methods
Async Methods
create_review_task(function_name, args, doc=None, llm_explanation=None, metadata=None, parent_review_task_id=None)- Create a new review taskget_review_task_status(review_task_id)- Get the status of a review taskwait_for_approval(review_task_id, polling_interval=5.0, max_attempts=None)- Wait for approval
Sync Methods
create_review_task_sync(...)- Synchronous version ofcreate_review_taskget_review_task_status_sync(review_task_id)- Synchronous version ofget_review_task_statuswait_for_approval_sync(...)- Synchronous version ofwait_for_approval
Response Properties
review_task_id: str- Unique identifier for the review taskstate: str- Current state ('approved', 'denied', or 'pending')requested_change: Optional[str]- Optional feedback when deniedis_approved: bool- Helper propertyis_denied: bool- Helper propertyis_pending: bool- Helper property
Configuration Options
velatir.init(
api_key="your-api-key", # Your Velatir API key
base_url="https://api.velatir.com/api/v1", # Custom API base URL
log_level=LogLevel.ERROR, # Logging verbosity level
max_retries=3, # Maximum number of retries
retry_backoff=0.5 # Base backoff time in seconds
)
Decorator Options
@velatir.review_task(
polling_interval=5.0, # Seconds between polling attempts
max_attempts=None, # Maximum number of polling attempts
metadata={} # Additional metadata to send
)
Working with Sync Functions
The decorator works seamlessly with both sync and async functions:
@velatir.review_task()
def sync_function(param: str):
"""A synchronous function"""
print(f"Processing: {param}")
return f"Result: {param}"
# This will work even though the function is sync
result = sync_function("test")
Python Version Support
- Python 3.7+
- Full async/await support
- Type hints included
Development
Running Tests
pip install pytest pytest-asyncio
pytest tests/
Building the Package
pip install build
python -m build
Documentation
For more detailed documentation, visit https://www.velatir.com/docs
License
MIT - see LICENSE file for details.
Support
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
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 velatir-1.0.3.tar.gz.
File metadata
- Download URL: velatir-1.0.3.tar.gz
- Upload date:
- Size: 15.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0b4fcb080aea407f2c2fc134d08259666d694748272b1f05e90f7f3c597615f9
|
|
| MD5 |
8f7d5f27c144c9b928f04322a86683b4
|
|
| BLAKE2b-256 |
79204f55a01868a61c5840fa3f92f8f40027b7e4520555e687093997581a88d9
|
Provenance
The following attestation bundles were made for velatir-1.0.3.tar.gz:
Publisher:
python-publish.yml on eliassorensen/velatir-demo
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
velatir-1.0.3.tar.gz -
Subject digest:
0b4fcb080aea407f2c2fc134d08259666d694748272b1f05e90f7f3c597615f9 - Sigstore transparency entry: 612426437
- Sigstore integration time:
-
Permalink:
eliassorensen/velatir-demo@6f0da5638e6c6919decdec65b9ade721534ab961 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/eliassorensen
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@6f0da5638e6c6919decdec65b9ade721534ab961 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file velatir-1.0.3-py3-none-any.whl.
File metadata
- Download URL: velatir-1.0.3-py3-none-any.whl
- Upload date:
- Size: 12.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
052c18e080dbf163e964fec7379548923d083d8789b120370e13b4fbcba7f5da
|
|
| MD5 |
64f31267f4eb1ee49f843ffc13952c90
|
|
| BLAKE2b-256 |
5cf5fe3e565e87eca6dcea32152e4aac1f858e4fcd9c68f1b5527510066b5469
|
Provenance
The following attestation bundles were made for velatir-1.0.3-py3-none-any.whl:
Publisher:
python-publish.yml on eliassorensen/velatir-demo
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
velatir-1.0.3-py3-none-any.whl -
Subject digest:
052c18e080dbf163e964fec7379548923d083d8789b120370e13b4fbcba7f5da - Sigstore transparency entry: 612426515
- Sigstore integration time:
-
Permalink:
eliassorensen/velatir-demo@6f0da5638e6c6919decdec65b9ade721534ab961 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/eliassorensen
-
Access:
private
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@6f0da5638e6c6919decdec65b9ade721534ab961 -
Trigger Event:
workflow_dispatch
-
Statement type: