Skip to main content

Python SDK for AI Security Gateway — PII redaction, prompt injection defense, and smart routing for any LLM.

Project description

aisg — AI Security Gateway Python SDK

PyPI License: Apache 2.0 Python 3.9+

Python SDK for AI Security Gateway — PII redaction, prompt injection defense, and smart cost routing for any LLM.

Works with both the managed cloud service and self-hosted (Docker) deployments.

Installation

pip install aisg

Quick Start

from aisg import AISG

client = AISG(api_key="oah_your_project_key")

response = client.chat.create(
    model="oah/llama-4-maverick",
    messages=[{"role": "user", "content": "Explain quantum computing simply."}],
)

print(response.content)
print(response.aisg_metadata.provider_selected)  # e.g. "together"
print(response.aisg_metadata.pii_detected)        # False
print(response.aisg_metadata.cost_usd)            # 0.000042

Configuration

Parameter Env Variable Default Description
api_key AISG_API_KEY Your API key (required)
base_url AISG_BASE_URL https://api.aisecuritygateway.ai/v1 API endpoint
timeout 120 Request timeout (seconds)

Cloud (default)

client = AISG(api_key="oah_abc123")

Self-Hosted

client = AISG(
    api_key="my-gateway-key",
    base_url="http://localhost:8000/v1",
)

Environment Variables

export AISG_API_KEY="oah_abc123"
export AISG_BASE_URL="https://api.aisecuritygateway.ai/v1"
from aisg import AISG
client = AISG()  # picks up from env

Chat Completions

Basic

response = client.chat.create(
    model="oah/llama-4-maverick",
    messages=[{"role": "user", "content": "Hello!"}],
    max_tokens=512,
    temperature=0.7,
)

print(response.content)
print(response.usage.total_tokens)

Streaming

for chunk in client.chat.create(
    model="oah/llama-4-maverick",
    messages=[{"role": "user", "content": "Write a poem."}],
    stream=True,
):
    delta = chunk.get("choices", [{}])[0].get("delta", {})
    print(delta.get("content", ""), end="", flush=True)

With Routing Headers

response = client.chat.create(
    model="oah/llama-4-maverick",
    messages=[{"role": "user", "content": "Hello!"}],
    extra_headers={
        "x-provider": "together",     # pin to a specific provider
        "x-feature": "chatbot-v2",    # analytics tag
        "x-env": "production",        # environment tag
    },
)

OpenAI SDK Compatibility

The AISG API is fully OpenAI-compatible. You can also use the OpenAI SDK directly:

from openai import OpenAI

client = OpenAI(
    base_url="https://api.aisecuritygateway.ai/v1",
    api_key="oah_abc123",
)

response = client.chat.completions.create(
    model="oah/llama-4-maverick",
    messages=[{"role": "user", "content": "Hello!"}],
)

The AISG SDK adds typed metadata, structured error handling, and model discovery on top.

Model Discovery

# List all models
models = client.models.list()
for m in models:
    print(f"{m.id}: {m.family}, vision={m.supports_vision}")
    print(f"  ${m.pricing.input_per_1m_tokens}/1M input tokens")

# Filter by family
llama_models = client.models.list(family="llama")

# Filter by capability
vision_models = client.models.list(capability="vision")

# Filter by provider
together_models = client.models.list(provider="together")

Security Metadata

Every response includes aisg_metadata with security and routing information:

response = client.chat.create(
    model="oah/llama-4-maverick",
    messages=[{"role": "user", "content": "Email john@example.com about the project."}],
)

meta = response.aisg_metadata
print(meta.pii_detected)          # True
print(meta.dlp_action)            # "redact"
print(meta.entity_types_detected) # ["EMAIL_ADDRESS"]
print(meta.dlp_latency_ms)        # 28.0
print(meta.provider_selected)     # "together"
print(meta.routing_mode)          # "smart_route"
print(meta.cost_usd)              # 0.000042

Error Handling

from aisg import AISG, DLPBlockError, BudgetExhaustedError, RateLimitError, ModelNotFoundError

client = AISG(api_key="oah_abc123")

try:
    response = client.chat.create(
        model="oah/llama-4-maverick",
        messages=[{"role": "user", "content": "My SSN is 123-45-6789"}],
    )
except DLPBlockError as e:
    print(f"Blocked: {e.violations}")
    print(f"Request ID: {e.request_id}")

except BudgetExhaustedError:
    print("Project budget exhausted — upgrade or add credits")

except RateLimitError:
    print("Rate limited — back off and retry")

except ModelNotFoundError as e:
    print(f"Model unavailable. Try: {e.suggested_model}")

Async Client

import asyncio
from aisg import AsyncAISG

async def main():
    async with AsyncAISG(api_key="oah_abc123") as client:
        response = await client.chat.create(
            model="oah/llama-4-maverick",
            messages=[{"role": "user", "content": "Hello!"}],
        )
        print(response.content)

        models = await client.models.list(family="llama")
        for m in models:
            print(m.id)

asyncio.run(main())

Response Types

All responses are typed dataclasses:

Type Description
ChatCompletion Full chat response with .content, .usage, .aisg_metadata
AISGMetadata Security metadata: PII detection, routing, costs
ModelInfo Model entry with capabilities, pricing, providers
DLPViolation Detected PII entity with type, position, confidence

Links

License

Apache 2.0 — see 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

aisg-0.1.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

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

aisg-0.1.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for aisg-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0e4256b804d025a7af24e2eebc25ef0a9f2f042bdbefe4f1a98cc8a7d20cf29f
MD5 d215d7e56c4c4e28f9066b842d654fb4
BLAKE2b-256 24c8914b6e6e306813735b22b54f7217f3682a9f4ffd8d55440ef8676d63254e

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for aisg-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 37c430844d8f6cc3e5cf8a6f5cf4397a53e145cbc0482c2906830ab8f0fa4d6b
MD5 c26c2e21012516dce5a5c8daa54e4b4f
BLAKE2b-256 59b3239728f8740637b2e0e856c24baf01676230b11ce2f4ab73f0e654187b57

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