Metadata-Version: 2.4
Name: polyswitch
Version: 0.1.0
Summary: Enterprise Drop-in SDK for Polyswitch AI Gateway — 1-line migration from OpenAI
Author-email: Polyswitch <dev@polyswitch.dev>
License-Expression: MIT
Project-URL: Homepage, https://polyswitch.io
Project-URL: Repository, https://github.com/polyswitch/polyswitch
Project-URL: Documentation, https://polyswitch.io/docs
Project-URL: Bug Tracker, https://github.com/polyswitch/polyswitch/issues
Keywords: openai,ai,gateway,llm,polyswitch,agent,routing,drop-in
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: openai>=1.0.0
Provides-Extra: test
Requires-Dist: pytest>=7.0.0; extra == "test"
Requires-Dist: pytest-asyncio>=0.21.0; extra == "test"
Provides-Extra: dev
Requires-Dist: build>=1.0.0; extra == "dev"
Requires-Dist: twine>=4.0.0; extra == "dev"
Dynamic: license-file

# polyswitch (Python SDK)

> **1-line drop-in replacement for the OpenAI Python SDK** — route all your AI traffic through Polyswitch Enterprise AI Gateway with zero code changes.

[![PyPI version](https://img.shields.io/pypi/v/polyswitch)](https://pypi.org/project/polyswitch/)
[![Python Versions](https://img.shields.io/pypi/pyversions/polyswitch)](https://pypi.org/project/polyswitch/)
[![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE)

---

## Installation

```bash
pip install polyswitch
```

---

## Quick Start

Replace your existing `openai` import — everything else stays exactly the same:

```python
# Before
from openai import OpenAI
client = OpenAI(api_key="sk-...")

# After — 1-line change
from polyswitch import Polyswitch
client = Polyswitch(api_key="pr_your_key_here")

# Or use the alias for truly zero-change migration
from polyswitch import OpenAI
client = OpenAI()  # reads POLYSWITCH_API_KEY from env automatically
```

---

## Environment Variables

| Variable | Description | Default |
|----------|-------------|---------|
| `POLYSWITCH_API_KEY` | Your Polyswitch API key | `pr_guest` (demo mode) |
| `POLYSWITCH_GATEWAY_URL` | Gateway endpoint | `http://localhost:3000/api/v1` |
| `OPENAI_API_KEY` | Fallback — also accepted | — |
| `OPENAI_BASE_URL` | Fallback — also accepted | — |

---

## Routing Strategies

Control how Polyswitch routes your requests across AI providers:

```python
from polyswitch import Polyswitch, Strategies

client = Polyswitch(
    strategy=Strategies.auto_smart()               # Auto-pick best model (default)
    # strategy=Strategies.cost_optimized(1.0)      # Stay under $1/M tokens
    # strategy=Strategies.hedged_latency(500, "gpt-4o-mini")  # Hedge on latency
    # strategy=Strategies.kv_affinity("user-session-123")     # Sticky routing
)

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

---

## PII Redaction

Automatically strip PII from all requests before they leave your network:

```python
client = Polyswitch(pii_redact=True)
```

---

## Async Support

Full async support via `AsyncPolyswitch`:

```python
import asyncio
from polyswitch import AsyncPolyswitch, AsyncOpenAI

async def main():
    client = AsyncPolyswitch()
    response = await client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": "Hello!"}]
    )
    print(response.choices[0].message.content)

asyncio.run(main())
```

---

## Extra APIs

The SDK exposes additional Polyswitch-native APIs beyond standard OpenAI:

### Agents

```python
result = client.agents.run(
    persona={"name": "Aria", "role": "analyst", "systemPrompt": "You are a data analyst."},
    task_goal="Summarize Q4 revenue trends",
    tools=[{"name": "fetch_data", "description": "Fetch financial data"}],
    limits={"maxToolCalls": 5}
)
```

### OCR

```python
with open("document.pdf", "rb") as f:
    result = client.ocr.extract(f, model="mistral/mistral-ocr")
```

### Rerank

```python
result = client.rerank.create(
    query="What is the capital of France?",
    documents=["Paris is in France.", "London is in England."],
    model="cohere/rerank-english-v3.0",
    top_n=1
)
```

### Search

```python
result = client.search.query(
    query="latest AI research papers",
    max_results=5
)
```

### Quota

```python
quota = client.quota.get()
```

---

## Type Hints

Full type hints included. Compatible with mypy and pyright:

```python
from polyswitch import Polyswitch, Strategies
from typing import Optional

def create_client(strategy: Optional[str] = None) -> Polyswitch:
    return Polyswitch(strategy=strategy or Strategies.auto_smart())
```

---

## License

MIT © Polyswitch
