Skip to main content

Self-hosted AI gateway — LLM proxy, MCP tool registry, RAG knowledge base, agent memory. One API, zero lock-in.

Project description

PhiGateway

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

CI Python 3.12+ License: MIT

Install

pip install phi-gateway

Quick Start

# 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:

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

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

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

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

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

phi_gateway-0.4.1.tar.gz (64.3 kB view details)

Uploaded Source

Built Distribution

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

phi_gateway-0.4.1-py3-none-any.whl (86.9 kB view details)

Uploaded Python 3

File details

Details for the file phi_gateway-0.4.1.tar.gz.

File metadata

  • Download URL: phi_gateway-0.4.1.tar.gz
  • Upload date:
  • Size: 64.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for phi_gateway-0.4.1.tar.gz
Algorithm Hash digest
SHA256 291a542f523cae019ee8cd3230654465d552d7ac0d03fbf5c386e7ed79066d7b
MD5 8a1d44ae20cd00162dac78d6dbd617a1
BLAKE2b-256 4a7397d998ef2861e4d6f740ba39939520db3f1953258e82f980a5a2f48edcd1

See more details on using hashes here.

File details

Details for the file phi_gateway-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: phi_gateway-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 86.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for phi_gateway-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e27af56abb3f195de19a1d1ebfee938cdb3d605592c86c1b8924d2031f2ab7bf
MD5 465546935e215301e15a691b7213c0be
BLAKE2b-256 38877acb2d76e677f2f7d5b0bb36ca498fd0dd2e460036fc4783d8bf3f26968e

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