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.0.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.0-py3-none-any.whl (86.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: phi_gateway-0.4.0.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.0.tar.gz
Algorithm Hash digest
SHA256 355774278c8f357e0582c5343f77dbebfc6a6daf4818e357c15afb7b4333d6ef
MD5 b3e84e442811bc1ac5bf6ae332bf4c54
BLAKE2b-256 f6e85938618e12a553b9ecb750619994ca9827e1cb970cc3ce9a7f2344b2ce0c

See more details on using hashes here.

File details

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

File metadata

  • Download URL: phi_gateway-0.4.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 29cfb5d537f5e8e15fb6b5c8acd03547c509feef88cf1f673c2a92c071c1ca9e
MD5 870b50cd4c9a606bd5317fc3b195ea6d
BLAKE2b-256 b78ef957d68871112d24b8e0c45d548fd48ac3133a7a24171a13b70ab4a71ab4

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