Metadata-Version: 2.4
Name: phi-gateway
Version: 0.4.0
Summary: Self-hosted AI gateway — LLM proxy, MCP tool registry, RAG knowledge base, agent memory. One API, zero lock-in.
Author-email: Muhammad Reihan Pandanarang <mrpandanarang@gmail.com>
License: MIT
Project-URL: Documentation, https://github.com/raindragon14/phi-gateway
Project-URL: Repository, https://github.com/raindragon14/phi-gateway
Project-URL: Issues, https://github.com/raindragon14/phi-gateway/issues
Keywords: llm,ai,gateway,proxy,mcp,rag,agent,openai,anthropic,fastapi
Classifier: Development Status :: 3 - Alpha
Classifier: Environment :: Web Environment
Classifier: Framework :: FastAPI
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: System Administrators
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Internet :: WWW/HTTP :: HTTP Servers
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.12
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: fastapi>=0.115.0
Requires-Dist: starlette<1.0
Requires-Dist: uvicorn>=0.34.0
Requires-Dist: pydantic>=2.10.0
Requires-Dist: pydantic-settings>=2.7.0
Requires-Dist: sqlalchemy[asyncio]>=2.0.36
Requires-Dist: aiosqlite>=0.20.0
Requires-Dist: alembic>=1.14.0
Requires-Dist: openai>=1.60.0
Requires-Dist: anthropic>=0.40.0
Requires-Dist: groq>=0.15.0
Requires-Dist: bcrypt>=4.2.0
Requires-Dist: httpx>=0.28.0
Requires-Dist: jinja2>=3.0.0
Requires-Dist: jsonschema>=4.20.0
Provides-Extra: dev
Requires-Dist: pytest>=8.3.0; extra == "dev"
Requires-Dist: pytest-asyncio>=0.24.0; extra == "dev"
Requires-Dist: pytest-cov>=6.0.0; extra == "dev"
Requires-Dist: pytest-xdist>=3.5.0; extra == "dev"
Requires-Dist: ruff>=0.8.0; extra == "dev"
Requires-Dist: mypy>=1.11.0; extra == "dev"
Requires-Dist: build>=1.0.0; extra == "dev"
Requires-Dist: twine>=5.0.0; extra == "dev"
Dynamic: license-file

# PhiGateway

Self-hosted AI gateway. LLM proxy, tool registry, RAG knowledge base, and agent memory behind one OpenAI-compatible endpoint.

[![CI](https://img.shields.io/github/actions/workflow/status/raindragon14/phi-gateway/ci.yml?branch=main&label=CI&logo=github)](https://github.com/raindragon14/phi-gateway/actions)
[![Python 3.12+](https://img.shields.io/badge/python-3.12+-blue.svg)](https://www.python.org/downloads/)
[![License: MIT](https://img.shields.io/badge/license-MIT-green.svg)](LICENSE)

## Install

```bash
pip install phi-gateway
```

## Quick Start

```bash
# Start the gateway
uvicorn phi_gateway.main:app

# Create an API key
curl -sX POST http://localhost:8000/v1/keys \
  -H "Content-Type: application/json" \
  -d '{"name":"my-agent","tier":"free"}'

# Chat through the gateway
curl -s http://localhost:8000/v1/chat/completions \
  -H "Authorization: Bearer phi-sk-..." \
  -H "Content-Type: application/json" \
  -d '{"model":"groq/llama-3.3-70b","messages":[{"role":"user","content":"Hello"}]}'
```

With Docker:

```bash
git clone https://github.com/raindragon14/phi-gateway
cd phi-gateway
cp .env.example .env    # add your provider keys
docker compose up -d
```

## What It Does

**LLM Proxy** — Route to OpenAI, Anthropic, Groq, or OpenRouter. Switch providers or use fallback chains without changing agent code. Streaming, cost tracking, and logging included.

**Tool Registry** — Register tools with JSON Schema. Agents discover and call them via REST or MCP (JSON-RPC 2.0). MCP-native.

**Knowledge Base** — Chunk, embed, and search documents. Cosine similarity with keyword fallback. Everything in SQLite. No external vector database.

**Agent Memory** — Store conversations, paginate history, auto-trim context. Returns `X-Context-Truncated` header when messages are trimmed.

## Usage

```python
from openai import OpenAI

client = OpenAI(base_url="http://localhost:8000/v1", api_key="phi-sk-...")
response = client.chat.completions.create(
    model="groq/llama-3.3-70b",
    messages=[{"role": "user", "content": "Hello"}],
)
```

Full API reference at `/docs` when the server is running.

## Architecture

```
Caddy (reverse proxy, auto TLS)
  └── FastAPI (uvicorn)
        ├── /v1/chat/completions  →  LLM proxy  →  provider APIs
        ├── /v1/tools             →  tool registry
        ├── /v1/kb                →  RAG (SQLite + cosine similarity)
        ├── /v1/memory            →  agent memory
        ├── /mcp                  →  JSON-RPC 2.0 (MCP)
        └── /dashboard            →  HTMX admin UI
              └── SQLite (single file)
```

Idle RAM: ~250 MB. Python 3.12+. MIT license.

## Development

```bash
pip install -e ".[dev]"
pytest -v
ruff check src/ tests/
```

Code style: Google docstrings, ruff format, pytest. See `pyproject.toml`.

## License

[MIT](LICENSE)
