AI API Server

A Node.js/Express.js backend server that proxies requests to Ollama and provides logging and monitoring capabilities.

Features

  • Proxies requests to Ollama (running locally at http://localhost:11434)
  • Supports both chat and generate endpoints
  • Streaming responses for real-time AI interactions
  • PostgreSQL database for logging prompts and errors
  • API key authentication
  • Modular, maintainable codebase

Prerequisites

Setup

  1. Clone the repository
  2. Install dependencies:
    npm install
    
  3. Create a .env file with the following variables:
    PORT=5000
    NODE_ENV=development
    API_KEY=your_api_key_here
    DB_HOST=localhost
    DB_PORT=5432
    DB_NAME=ai_db
    DB_USER=your_db_user
    DB_PASSWORD=your_db_password
    
  4. Create the database:
    createdb ai_db
    
  5. Run the database migrations:
    psql -d ai_db -f src/database/schema.sql
    
  6. Start the server:
    npm start
    

API Endpoints

Generate/Chat

  • POST /api/generate
    • Supports both chat-style (messages array) and prompt-style requests
    • Requires API key in headers (api-key or Authorization: Bearer <key>)
    • Example request:
      {
        "model": "codellama:7b",
        "messages": [
          {"role": "user", "content": "Hello, how are you?"}
        ],
        "stream": true
      }
      

Logs

  • GET /api/errors/prompts

    • Returns latest 100 prompts
    • Query parameter: limit (default: 100)
    • Requires API key
  • GET /api/errors/logs

    • Returns latest 100 error logs
    • Query parameter: limit (default: 100)
    • Requires API key

Project Structure

src/
├── config/         # Configuration files
├── controllers/    # Request handlers
├── database/       # Database schema and migrations
├── middleware/     # Express middleware
├── models/         # Database models
├── network/        # External service communication
├── routes/         # API routes
└── server.js       # Application entry point

Error Handling

  • All errors are logged to the database
  • API errors return appropriate HTTP status codes
  • Streaming errors are handled gracefully

Security

  • API key authentication required for all endpoints
  • Environment variables for sensitive data
  • Input validation and sanitization
  • Secure headers with Helmet (TODO)

Development

  • Use npm run dev for development with auto-reload
  • Use npm test to run tests (TODO)
  • Follow the existing code style and structure

License

MIT

Description
No description provided
Readme MIT 245 KiB
Languages
JavaScript 100%