Skip to main content

Simple AI Gateway - 100+ LLM providers, one function call

Project description

TinyMagiq AI Gateway

Simple AI Gateway wrapping LiteLLM - 100+ providers, one function call.

Install

pip install tinymagiq-ai-gateway

Quick Start

from tinymagiq_ai_gateway import completion

response = completion(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}]
)
print(response["content"])

Switch Providers Instantly

# OpenAI
response = completion("gpt-4", [{"role": "user", "content": "Hi"}])

# Anthropic
response = completion("anthropic/claude-3-opus", [{"role": "user", "content": "Hi"}])

# Azure
response = completion("azure/gpt-4", [{"role": "user", "content": "Hi"}])

# Bedrock
response = completion("bedrock/anthropic.claude-v2", [{"role": "user", "content": "Hi"}])

Features

Caching (Save Money)

from tinymagiq_ai_gateway import enable_cache, completion

enable_cache(ttl=3600)  # Cache for 1 hour
response = completion("gpt-4", [{"role": "user", "content": "Hello"}])
# Second identical call returns instantly from cache

Fallback (Increase Reliability)

from tinymagiq_ai_gateway import with_fallback

response = with_fallback(
    models=["gpt-4", "claude-3-opus", "gpt-3.5-turbo"],
    messages=[{"role": "user", "content": "Hello"}]
)
# Tries gpt-4 first, falls back to claude-3 if it fails

Load Balancing (Distribute Traffic)

from tinymagiq_ai_gateway import with_load_balance

router = with_load_balance(
    deployments=[
        {"model_name": "gpt4-east", "litellm_params": {"model": "gpt-4"}},
        {"model_name": "gpt4-west", "litellm_params": {"model": "gpt-4"}},
    ],
    strategy="least-busy"
)
response = router.completion(
    model="gpt4-east",
    messages=[{"role": "user", "content": "Hello"}]
)

Guardrails (Block Harmful Content)

from tinymagiq_ai_gateway import add_block_pattern, check_content

add_block_pattern("ssn", r"\d{3}-\d{2}-\d{4}")
add_block_pattern("credit_card", r"\d{4}-\d{4}-\d{4}-\d{4}")

if check_content("My SSN is 123-45-6789"):
    print("Safe to process")
else:
    print("Blocked - contains sensitive data")

Smart Routing (Optimize Cost/Latency)

from tinymagiq_ai_gateway import smart_route

router = smart_route(
    models=[
        {"model_name": "expensive", "litellm_params": {"model": "gpt-4"}},
        {"model_name": "cheap", "litellm_params": {"model": "gpt-4o-mini"}},
    ],
    goal="cost"  # Always pick cheapest
)

Logging (Track Usage)

from tinymagiq_ai_gateway import enable_logging

enable_logging(providers=["console"])
# All API calls are now logged

Available Functions

Function Description
completion() Generate text from any LLM
acompletion() Async version of completion()
embedding() Convert text to vector
embedding_batch() Convert multiple texts to vectors
with_fallback() Try multiple models if one fails
awith_fallback() Async version of with_fallback()
enable_cache() Cache responses to save money
disable_cache() Turn off caching
clear_cache() Delete cached responses
cache_status() Check if caching is enabled
with_load_balance() Distribute requests across deployments
add_block_pattern() Block content matching regex
remove_block_pattern() Remove a block rule
check_content() Check if content is safe
find_matches() Find blocked patterns in text
clear_patterns() Remove all block rules
list_patterns() List configured block rules
smart_route() Route to cheapest/fastest model
enable_logging() Log API calls
disable_logging() Turn off logging
redact_messages() Hide message content from logs
unredact_messages() Show message content in logs

License

MIT License - see License for details.

Powered by LiteLLM (MIT License).

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

tinymagiq_ai_gateway-0.1.0.tar.gz (14.5 kB view details)

Uploaded Source

Built Distribution

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

tinymagiq_ai_gateway-0.1.0-py3-none-any.whl (19.5 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for tinymagiq_ai_gateway-0.1.0.tar.gz
Algorithm Hash digest
SHA256 e53fcbdfef65f24975b2352ea5db2285c9c6a95ad62a8592e8de05d2d78e861f
MD5 4d758969945ed0a767163923d3404abc
BLAKE2b-256 74a8a925d44166e64ae5fe4f491ee5ca30dd58cae3db176eff2d484329ceebbb

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for tinymagiq_ai_gateway-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2502e8b2b6d0ef59795fd297cca61dad69f96852612d32907f1c76e463334ea8
MD5 78daffce1320016a7772da1da62c3c19
BLAKE2b-256 adb012222498a04ce096e5aec48db75f0ef465115aa0c55b2039ce3be9779fb3

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