perf-sdk
Official Python SDK for Perf - the AI Runtime Orchestrator.
Perf automatically picks the best AI model for your prompt based on:
- Task type and complexity
- Cost constraints
- Output reliability
- Fallback logic
Installation
pip install perf-sdk
Quick Start
from perf import PerfClient
client = PerfClient(api_key="pk_live_your_api_key")
# Simple chat completion
response = client.chat(
messages=[
{"role": "user", "content": "What is the capital of France?"}
]
)
print(response.choices[0].message.content)
# Output: "The capital of France is Paris."
# Access Perf metadata
print(response.perf)
# PerfMetadata(model_used='gpt-4o-mini', cost_usd=0.0001, latency_ms=234, ...)
Streaming
# Stream responses
for chunk in client.chat_stream(
messages=[{"role": "user", "content": "Write a haiku about coding"}]
):
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
# Or collect into a string
content = client.chat_stream_to_string(
messages=[{"role": "user", "content": "Explain quantum computing"}]
)
Async Support
import asyncio
from perf import AsyncPerfClient
async def main():
async with AsyncPerfClient(api_key="pk_live_your_api_key") as client:
# Async chat completion
response = await client.chat(
messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)
# Async streaming
async for chunk in client.chat_stream(
messages=[{"role": "user", "content": "Tell me a story"}]
):
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
asyncio.run(main())
Error Handling
from perf import (
PerfClient,
PerfError,
RateLimitError,
AuthenticationError,
)
import time
client = PerfClient(api_key="pk_live_your_api_key")
try:
response = client.chat(
messages=[{"role": "user", "content": "Hello!"}]
)
except RateLimitError as e:
# Wait and retry
retry_after = e.retry_after or 60
print(f"Rate limited. Retry after {retry_after} seconds")
time.sleep(retry_after)
except AuthenticationError:
print("Invalid API key")
except PerfError as e:
print(f"API Error: {e.code} - {e.message}")
except Exception as e:
print(f"Unexpected error: {e}")
Configuration Options
client = PerfClient(
api_key="pk_live_your_api_key", # Required
base_url="https://api.withperf.pro", # Optional, default shown
timeout=120.0, # Request timeout in seconds (default: 120)
max_retries=3, # Retry attempts (default: 3)
retry_delay=1.0, # Base retry delay in seconds (default: 1)
)
Request Options
response = client.chat(
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Hello!"},
],
model="gpt-4o", # Optional: override model selection
max_tokens=1000, # Optional: limit response length
temperature=0.7, # Optional: sampling temperature
max_cost_per_call=0.01, # Optional: cost budget in USD
metadata={ # Optional: custom metadata
"user_id": "123",
"session_id": "abc",
},
)
Context Manager
Both sync and async clients support context managers for proper resource cleanup:
# Sync
with PerfClient(api_key="pk_live_xxx") as client:
response = client.chat(messages=[...])
# Async
async with AsyncPerfClient(api_key="pk_live_xxx") as client:
response = await client.chat(messages=[...])
Features
- Full type hints with Pydantic models
- Sync and async clients for different use cases
- Streaming support with iterators/async iterators
- Automatic retries with exponential backoff
- Typed exceptions for different error types
- Timeout handling with configurable limits
Requirements
- Python 3.9 or higher
- httpx
- pydantic
Links
License
MIT
Metadata
Release files for perf-sdk 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| perf_sdk-0.2.0.tar.gz | 7.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| perf_sdk-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.7 kB
Release files / perf_sdk-0.2.0.tar.gz
| Download URL | perf_sdk-0.2.0.tar.gz |
|---|---|
| Size | 7.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
81cf8448cfb1e4e7dc4c5d2d5ca07c508825a2ed72d7ec1f5b0d7ddd62670ab6
|
|
BLAKE2b-256 checksum How to use checksums |
8ed29e5036bf84cfc2e37d43ca8b83c7aa944075b315070c77fe979d21b65ab6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|
Release files / perf_sdk-0.2.0-py3-none-any.whl
| Download URL | perf_sdk-0.2.0-py3-none-any.whl |
|---|---|
| Size | 8.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8ea12aec0a659ee0b916a227d5bcbed21f7d1086dd4d8c1f1d60c649211d6596
|
|
BLAKE2b-256 checksum How to use checksums |
2f882643d01cb7aeb0d55ff9a8146631ef9a3ed1f677c20291633282a59a36b9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.9.6
|
Security for perf-sdk 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.