An enterprise-grade AI API service with built-in API key management, rate limiting, usage tracking, and a beautiful dashboard interface.
-
AI Models
- Support for multiple AI models
- Hugging Face model integration
- External API model integration
- Configurable model settings
- Pay-per-token pricing
- Support for both local and remote models
-
API Key Management
- Secure key generation and storage
- Customer-specific rate limits
- Multi-tenant support
- Easy key activation/deactivation
- Model-specific access control
-
Dashboard
- Real-time usage statistics
- Cost tracking and billing
- API key management interface
- Usage graphs and analytics
- Per-model usage tracking
-
Rate Limiting
- Per-key rate limits
- Configurable thresholds
- Protection against abuse
- Customizable rate limit settings
-
Usage Tracking
- Request counting
- Token consumption
- Cost calculation
- Response time monitoring
- OpenTelemetry integration
- Per-model cost tracking
- Python 3.8+
- pip (Python package manager)
- SQLite3
-
Clone the repository:
git clone https://github.com/RealShocky/NexusForge.git cd NexusForge
-
Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables:
cp .env.example .env
Then edit the
.env
file with your actual configuration values. -
Initialize the database:
python migrate_database.py
-
Run the application:
uvicorn main:app --reload --host 0.0.0.0 --port 8000
-
Make sure Docker and Docker Compose are installed on your system.
-
Configure environment variables:
cp .env.example .env
Then edit the
.env
file with your actual configuration values. -
Build and start the containers:
docker-compose up -d
-
Access the application at http://localhost:8000
The application is configured through environment variables defined in the .env
file:
DATABASE_URL
: Database connection string (default: SQLite)HOST
andPORT
: Host and port for the applicationAPI_KEY
: Master API key for administrative access
OPENAI_API_KEY
: Your OpenAI API key for AI model integrationOPENAI_MODEL
: Default OpenAI model to use
STRIPE_SECRET_KEY
: Your Stripe secret key for payment processingSTRIPE_PUBLISHABLE_KEY
: Your Stripe publishable key for client-side integrationSTRIPE_WEBHOOK_SECRET
: Secret for verifying Stripe webhook events
LOAD_DEFAULT_MODELS
: Whether to load default models on startupDEVELOPMENT_MODE
: Enable development mode with additional logging
Once the application is running, you can access the Swagger documentation at:
The admin dashboard is available at http://localhost:8000/admin for authorized users.
The service currently supports two types of models:
-
Local Models (Hugging Face)
- Default: GPT-2
- Locally hosted and processed
- Lower latency
- No external API costs
-
External API Models
- Lawgent-thinking (Legal domain expert)
- Accessed via external API
- Pay-per-use pricing
- Specialized capabilities
- Start the server:
python main.py
- Create a customer:
curl -X POST "http://localhost:8000/customers" \
-H "Content-Type: application/json" \
-d '{
"name": "Your Company",
"email": "contact@company.com",
"company": "Your Company Inc"
}'
- Create an API key:
curl -X POST "http://localhost:8000/api/keys" \
-H "Content-Type: application/json" \
-d '{
"customer_email": "contact@company.com",
"name": "Production Key",
"allowed_models": [1, 2] # 1 for GPT-2, 2 for lawgent-thinking
}'
- Generate text using local model (GPT-2):
curl -X POST "http://localhost:8000/generate" \
-H "Content-Type: application/json" \
-H "X-API-Key: YOUR_API_KEY" \
-d '{
"text": "Your prompt here",
"model_id": 1,
"max_length": 100
}'
- Generate text using external API (lawgent-thinking):
curl -X POST "http://localhost:8000/generate" \
-H "Content-Type: application/json" \
-H "X-API-Key: YOUR_API_KEY" \
-d '{
"text": "What legal considerations should I keep in mind for my business?",
"model_id": 2,
"max_length": 100
}'
- View the dashboard:
http://localhost:8000/dashboard/{customer_id}
Endpoint | Method | Description |
---|---|---|
/customers |
POST | Create a new customer |
/api/keys |
POST | Create a new API key |
/api/keys/{key_id}/toggle |
POST | Toggle API key status |
/generate |
POST | Generate text using AI |
/dashboard/{customer_id} |
GET | View customer dashboard |
{
"id": 1,
"name": "Your Company",
"email": "contact@company.com",
"company": "Your Company Inc",
"created_at": "2024-01-01T00:00:00"
}
{
"id": 1,
"key": "generated_api_key",
"name": "Production Key",
"rate_limit": 60,
"allowed_models": [1, 2]
}
{
"response": "AI-generated text",
"tokens_used": 100,
"cost": 0.001
}
- Default rate limit: 60 requests per minute per API key
- Customizable per API key
- Returns HTTP 429 when limit exceeded
- Separate tracking for each model
The system uses OpenTelemetry for monitoring and includes:
- Request tracking
- Performance metrics
- Error logging
- Usage statistics
- Cost tracking
- Per-model usage analytics
- API key authentication required for all endpoints
- Secure key storage in SQLite database
- Rate limiting protection
- Input validation and sanitization
- Customer isolation
- Model access control
-
Install Visual Studio Build Tools
- Download Visual Studio Build Tools 2022
- Run the installer
- Select "Desktop development with C++"
- Complete the installation
-
Python Setup
- Install Python 3.12 from python.org
- Ensure Python is added to PATH during installation
- Open Command Prompt as Administrator and run:
python -m pip install --upgrade pip pip install --upgrade setuptools wheel
-
Virtual Environment
# Create virtual environment python -m venv venv # Activate virtual environment .\venv\Scripts\activate
-
Install Dependencies
# Update pip and core tools python -m pip install --upgrade pip pip install --upgrade setuptools wheel # Install project dependencies pip install -r requirements.txt
If you encounter ChromaDB installation issues:
# Alternative installation method for ChromaDB pip install chromadb --no-cache-dir
-
Environment Variables Create a
.env
file in the project root:STRIPE_SECRET_KEY=your_stripe_key STRIPE_PUBLIC_KEY=your_public_key
-
ChromaDB Installation Errors
- Ensure Visual Studio Build Tools are installed correctly
- Try running Command Prompt as Administrator
- If issues persist, use the
--no-cache-dir
flag
-
Missing DLL Errors
- Install Visual C++ Redistributable from Microsoft
- Restart your system after installation
-
Python Path Issues
- Verify Python is in PATH:
python --version
- If not found, add Python installation directory to system PATH
- Verify Python is in PATH:
-
Activate Virtual Environment
.\venv\Scripts\activate
-
Run the Server
python -m uvicorn main:app --host 0.0.0.0 --port 8000 --reload
-
Access the Dashboard Open your browser and navigate to:
http://localhost:8000/dashboard
.
├── main.py # FastAPI application and routes
├── database.py # Database models and operations
├── rate_limiter.py # Rate limiting implementation
├── billing.py # Billing and cost tracking
├── model_manager.py # AI model management and integration
├── requirements.txt # Python dependencies
├── .env # Environment variables
└── README.md # Documentation
- Local Models (Hugging Face):
model_config = {
"model_path": "model_name_or_path",
"preload": True # Load model at startup
}
- External API Models:
model_config = {
"api_url": "https://api.example.com/v1/completions",
"api_key": "your_api_key",
"model_name": "model_name",
"preload": False
}
- Fork the repository
- Create a feature branch
- Implement your feature
- Add tests
- Submit a pull request
MIT License