Skip to content

ProngsDev/ai-gateway

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸš€ Rust AI Gateway

A lightning-fast, production-ready API gateway that unifies OpenAI and Google Gemini models into a single endpoint. Built with Rust for maximum performance and reliability.

✨ Features

  • πŸ”„ Multi-Provider Support - Seamlessly integrate OpenAI and Gemini
  • ⚑ Automatic Failover - Falls back to secondary provider if primary fails
  • πŸ’Ύ Smart Caching - In-memory cache for improved performance and reduced costs
  • 🎯 Manual Provider Selection - Override automatic routing when needed
  • πŸ“Š Request Logging - Structured logging with tracing for observability
  • πŸ”’ Type-Safe - Leverages Rust's type system for reliability
  • βš™οΈ Async/Await - Non-blocking I/O for high concurrency

πŸ—οΈ Architecture

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Client    β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    Axum API Gateway             β”‚
β”‚  /generate endpoint             β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Router (model selection)      β”‚
β”‚   - Automatic failover          β”‚
β”‚   - Manual override             β”‚
β””β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
       β”‚
       β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
       β–Ό              β–Ό             β–Ό
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ OpenAI   β”‚   β”‚  Gemini  β”‚   β”‚  Cache  β”‚
β”‚ Client   β”‚   β”‚  Client  β”‚   β”‚  Layer  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

πŸš€ Quick Start

Prerequisites

Installation

  1. Clone the repository

    git clone https://github.com/ProngsDev/ai-gateway.git
    cd ai-gateway
  2. Set up environment variables

    cp .env.example .env

    Edit .env with your API keys:

    PORT=8080
    OPENAI_API_KEY=sk-your-openai-key-here
    GEMINI_API_KEY=your-gemini-key-here
  3. Build and run

    cargo run --release

The server will start on http://localhost:8080

πŸ“– API Reference

Health Check

GET /health

Response:

AI Gateway is healthy

Generate Text

POST /generate
Content-Type: application/json

Request Body:

{
  "prompt": "Explain Rust ownership in one sentence",
  "provider": "OpenAI"  // Optional: "OpenAI" or "Gemini"
}

Response:

{
  "provider": "OpenAI",
  "output": "Rust's ownership system ensures memory safety by enforcing that each value has a single owner, and when the owner goes out of scope, the value is automatically deallocated.",
  "cached": false
}

Fields:

  • provider (optional): Specify "OpenAI" or "Gemini". If omitted, automatic failover is used.
  • prompt (required): The text prompt to send to the AI model.

Response Fields:

  • provider: Which provider generated the response
  • output: The generated text
  • cached: Whether the response was served from cache

πŸ’‘ Usage Examples

Automatic Failover (Recommended)

curl -X POST http://localhost:8080/generate \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "What is Rust?"
  }'

The gateway will:

  1. Try OpenAI first
  2. If OpenAI fails, automatically fall back to Gemini
  3. Cache the successful response

Manual Provider Selection

Use OpenAI specifically:

curl -X POST http://localhost:8080/generate \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Explain async/await",
    "provider": "OpenAI"
  }'

Use Gemini specifically:

curl -X POST http://localhost:8080/generate \
  -H "Content-Type: application/json" \
  -d '{
    "prompt": "Explain async/await",
    "provider": "Gemini"
  }'

Cache Behavior

Send the same prompt twice:

# First request - hits OpenAI
curl -X POST http://localhost:8080/generate \
  -H "Content-Type: application/json" \
  -d '{"prompt": "Hello"}' | jq

# Response: {"provider": "OpenAI", "output": "...", "cached": false}

# Second request - served from cache
curl -X POST http://localhost:8080/generate \
  -H "Content-Type: application/json" \
  -d '{"prompt": "Hello"}' | jq

# Response: {"provider": "OpenAI", "output": "...", "cached": true}

πŸ› οΈ Development

Run Tests

cargo test

Run with Debug Logging

RUST_LOG=debug cargo run

Build for Production

cargo build --release

The optimized binary will be in target/release/ai-gateway

🐳 Docker Deployment

Build Docker Image

docker build -t ai-gateway .

Run Container

docker run -p 8080:8080 \
  -e OPENAI_API_KEY=your-key \
  -e GEMINI_API_KEY=your-key \
  ai-gateway

Docker Compose

docker-compose up -d

See Docker Setup section for details.

πŸ“ Project Structure

ai-gateway/
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ main.rs              # Server setup & initialization
β”‚   β”œβ”€β”€ routes.rs            # API endpoint handlers
β”‚   β”œβ”€β”€ router.rs            # Provider routing & failover logic
β”‚   β”œβ”€β”€ cache.rs             # In-memory cache implementation
β”‚   β”œβ”€β”€ error.rs             # Custom error types
β”‚   └── providers/
β”‚       β”œβ”€β”€ mod.rs           # AIProvider trait definition
β”‚       β”œβ”€β”€ openai.rs        # OpenAI client implementation
β”‚       └── gemini.rs        # Gemini client implementation
β”œβ”€β”€ Cargo.toml               # Dependencies & metadata
β”œβ”€β”€ .env                     # Environment variables (gitignored)
β”œβ”€β”€ Dockerfile               # Docker build configuration
β”œβ”€β”€ docker-compose.yml       # Docker Compose setup
└── README.md                # This file

πŸ”§ Configuration

Environment Variables

Variable Description Required Default
PORT Server port No 8080
OPENAI_API_KEY OpenAI API key Yes -
GEMINI_API_KEY Google Gemini API key Yes -

Provider Priority

The default failover order is:

  1. OpenAI (primary)
  2. Gemini (fallback)

To change this, modify the provider order in src/main.rs:

// Current order (OpenAI first)
ai_router.add_provider(openai_client);
ai_router.add_provider(gemini_client);

// To make Gemini primary, swap the order:
ai_router.add_provider(gemini_client);
ai_router.add_provider(openai_client);

🎯 Use Cases

  • SaaS Applications - Add AI features with built-in resilience
  • Cost Optimization - Route to cheaper providers, cache expensive calls
  • High Availability - Automatic failover ensures uptime
  • Multi-Model Apps - Leverage strengths of different models
  • Development/Testing - Single API for multiple providers

πŸ”’ Security Considerations

  • Store API keys in environment variables, never in code
  • Use HTTPS in production
  • Consider adding rate limiting for public deployments
  • Implement authentication/authorization as needed

πŸ“Š Performance

  • Async I/O - Non-blocking requests for high concurrency
  • In-Memory Cache - Sub-millisecond cache hits
  • Compiled - Native performance with Rust
  • Lightweight - Minimal resource footprint

🀝 Contributing

Contributions are welcome! Areas for improvement:

  • Add more providers (Claude, Llama, Cohere)
  • Implement Redis caching for distributed systems
  • Add rate limiting
  • Prometheus metrics
  • Streaming responses (SSE)
  • Configuration via YAML/TOML

πŸ“ License

MIT License - See LICENSE file for details

πŸ™ Acknowledgments

Built with:


Made with ❀️ and Rust

About

Lightening-fast AI API gateway

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors