Skip to content

Repository files navigation

🏛️ PhiloGPT

PhiloGPT is a purpose-built AI philosophical guide designed to help you navigate life's challenges with the timeless wisdom of Marcus Aurelius, Seneca, and Epictetus.

Built with a focus on privacy, speed, and architectural elegance, PhiloGPT combines a stateless backend with a stateful client to provide a seamless, serverless-native experience.


🏗️ System Architecture

PhiloGPT uses a hybrid state management system to ensure performance and scalability.

flowchart LR
    User((User)) -->|Message| Frontend[Next.js Frontend]
    Frontend -->|POST /api/chat| Middleware[Middleware]
    Middleware -->|Validated| API[API Route]
    API -->|Sliding Window Check| Redis[(Upstash Redis)]
    Redis -->|Allowed / Blocked| API
    API -->|System Prompt + History| AI[AI Provider]
    AI -->|Stream Chunks| API
    API -->|SSE Stream| Frontend
    Frontend -->|Persist| Storage[(LocalStorage)]
Loading

Core Design Patterns

  • Strategy Pattern: Abstracted AI providers allowing instant switching between Gemini and OpenAI.
  • Stateless Backend: The server holds no session state, making it infinitely scalable on Vercel.
  • Stateful Client: All chat history and session persistence live in the user's browser for maximum privacy.
  • Atomic Rate Limiting: Uses Lua scripting inside Redis to provide an unbreakable sliding window barrier against abuse.

🛡️ Rate Limiting

Unlike basic rate limiters that reset on server restarts, PhiloGPT uses a Persistent Sliding Window algorithm:

  • Global Memory: All server instances share a single source of truth via Upstash Redis.
  • Lua Atomicity: The limit check and increment occur as a single atomic operation, preventing race conditions.
  • Sliding Window: Provides a smooth experience by tracking individual request timestamps rather than fixed-minute blocks.

🚀 Getting Started

1. Prerequisites

2. Setup Environment

Create a .env.local file in the root directory:

# AI Providers
GEMINI_API_KEY="your-gemini-key"
OPENAI_API_KEY="your-openai-key"

# Global Rate Limiting (Upstash)
UPSTASH_REDIS_REST_URL="https://your-db-url.upstash.io"
UPSTASH_REDIS_REST_TOKEN="your-rest-token-here"

# App Settings
AI_PROVIDER="gemini"

3. Installation

npm install
npm run dev

🛠️ Tech Stack


📜 Philosophy

PhiloGPT is guided by the Dichotomy of Control:

"Some things are within our power, while others are not. Within our power are opinion, motivation, desire, aversion, and, in a word, whatever is our own doing; not within our power are our body, our property, reputation, office, and, in a word, whatever is not our own doing." — Epictetus


Created with focus and reason.

About

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages