Skip to main content

Unified SDK for AI services with OpenAI compatibility

Project description

SDKRouter

Unified Python SDK for AI services with OpenAI compatibility. Access 300+ LLM models through a single interface, plus vision analysis, CDN, URL shortening, and HTML cleaning tools.

Installation

pip install sdkrouter

Quick Start

from sdkrouter import SDKRouter

client = SDKRouter(api_key="your-api-key")

# OpenAI-compatible chat completions
response = client.chat.completions.create(
    model="openai/gpt-4o",
    messages=[{"role": "user", "content": "Hello!"}]
)
print(response.choices[0].message.content)

Features

Chat Completions (OpenAI-Compatible)

# Non-streaming
response = client.chat.completions.create(
    model="anthropic/claude-3.5-sonnet",
    messages=[{"role": "user", "content": "Explain quantum computing"}],
    max_tokens=500,
)

# Streaming
for chunk in client.chat.completions.create(
    model="openai/gpt-4o",
    messages=[{"role": "user", "content": "Count to 5"}],
    stream=True,
):
    print(chunk.choices[0].delta.content, end="")

Vision Analysis

result = client.vision.analyze(
    image_url="https://example.com/image.jpg",
    prompt="Describe this image",
)
print(result.description)
print(f"Cost: ${result.cost_usd:.6f}")

CDN File Storage

# Upload file
file = client.cdn.upload(
    b"file content",
    filename="document.txt",
    is_public=True,
)
print(file.url)

# List files with pagination
files = client.cdn.list(page=1, page_size=20)
for f in files.results:
    print(f"{f.filename}: {f.url}")

URL Shortener

# Create short link
link = client.shortlinks.create(target_url="https://example.com/long-url")
print(link.short_url)

# Get statistics
stats = client.shortlinks.stats()
print(f"Total links: {stats.total_links}")

HTML Cleaner

result = client.cleaner.clean(
    html_content,
    output_format="markdown",
    remove_scripts=True,
    remove_styles=True,
)
print(result.cleaned_html)
print(f"Compression: {result.compression_ratio:.1f}x")

LLM Models API

# List available models
models = client.models.list(page=1, page_size=50)
for m in models.results:
    print(f"{m.model_id}: {m.name}")

# Get model details
model = client.models.get("openai/gpt-4o")
print(f"Context: {model.context_length} tokens")
print(f"Price: ${model.pricing.prompt}/M input tokens")

# Calculate cost
cost = client.models.calculate_cost(
    "openai/gpt-4o",
    input_tokens=1000,
    output_tokens=500,
)
print(f"Total: ${cost.total_cost_usd:.6f}")

Async Support

All features support async operations:

from sdkrouter import AsyncSDKRouter
import asyncio

async def main():
    client = AsyncSDKRouter(api_key="your-api-key")

    # Async chat completion
    response = await client.chat.completions.create(
        model="openai/gpt-4o",
        messages=[{"role": "user", "content": "Hello!"}]
    )

    # Parallel requests
    results = await asyncio.gather(
        client.vision.analyze(image_url="..."),
        client.models.list(),
        client.cdn.stats(),
    )

asyncio.run(main())

Type Safety

All responses are fully typed with Pydantic models:

from sdkrouter.tools import (
    VisionAnalyzeResponse,
    CDNFileDetail,
    PaginatedLLMModelListList,
    CleanResponse,
)

# IDE autocomplete and type checking
result: VisionAnalyzeResponse = client.vision.analyze(...)
print(result.description)  # str
print(result.cost_usd)     # float
print(result.usage.total_tokens)  # int

Configuration

# Environment variables
# SDKROUTER_API_KEY - API key
# SDKROUTER_BASE_URL - Custom base URL (default: https://ai.sdkrouter.com)

# Or pass directly
client = SDKRouter(
    api_key="your-key",
    base_url="https://your-server.com",
    timeout=60.0,
    max_retries=3,
)

# Use OpenRouter directly
client = SDKRouter(
    openrouter_api_key="your-openrouter-key",
    use_self_hosted=False,
)

Supported Models

Access 300+ models from providers:

  • OpenAI: GPT-4o, GPT-4 Turbo, GPT-3.5 Turbo
  • Anthropic: Claude 3.5 Sonnet, Claude 3 Opus, Claude 3 Haiku
  • Google: Gemini Pro, Gemini Flash
  • Meta: Llama 3.1, Llama 3.2
  • Mistral: Mistral Large, Mixtral
  • And many more via OpenRouter

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

sdkrouter-0.1.0.tar.gz (63.5 kB view details)

Uploaded Source

Built Distribution

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

sdkrouter-0.1.0-py3-none-any.whl (106.6 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for sdkrouter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a198abf9980573698e9252e050a11384df2617b541aee1ae85f0bf63df8d05f9
MD5 5a37e38b2f279a0bc9c1a28b756aba13
BLAKE2b-256 0409d74afb59562657763020778261e313a644a496ad59dfb2909f173c5f5470

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for sdkrouter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 23c89be046ed2eb497e5f65466b9f9682c3bcafae38b16a6fafe2e6f9741fcb9
MD5 9c0b16454e6598ea0f24c8871bc1acae
BLAKE2b-256 cefe1a05fdbf0040027e20bbf1fe9c23bb7a364347c133bbe521ec0b970546db

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