0ce4af8833059b234a6b8d40291aea31c1102c87
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
- Node.js 14+ and npm
- PostgreSQL 12+
- Ollama running locally (http://localhost:11434)
Setup
- Clone the repository
- Install dependencies:
npm install
- 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
- Create the database:
createdb ai_db
- Run the database migrations:
psql -d ai_db -f src/database/schema.sql
- 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
orAuthorization: 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
Languages
JavaScript
100%