Sign inSign up

gitmotion/ollama-api-server

By gitmotion

•Updated over 1 year ago

Expose a local 🦙 ollama api behind defined api keys - easy setup with docker!

Image
0

3.6K

gitmotion/ollama-api-server repository overview

⁠🦙 Ollama API Server

Docker

A simple, easy-to-use api server that sits in front of your local ollama instance to add additional security when making requests to ollama.

Buy me a coffee ⁠

⁠📑 Table of Contents

⁠⭐ Features

  • 🔑 API Key Authentication
  • 🔄 CORS Support
  • 🚀 Connection Pooling
  • 🌊 Streaming Support
  • 🐳 Easy Docker Setup
  • 🔌 All Ollama API Endpoints Supported

⁠🎛️ Example Flow

  • Example of connecting a local ollama instance to an open-webui docker container on the same docker network
flowchart TD
    user([External User]) --> webui[Open WebUI]
    
    webui -->|Request with API Key| api[Ollama API Server]
    
    api --> auth{API Key Valid?}
    auth -->|No| reject[Reject Connection]
    auth -->|Yes| ollama[Ollama LLM Service]
    
    ollama -->|Response| api
    api -->|Response| webui
    webui -->|Response| user
    
    subgraph "Docker: ollama-network"
        webui
        api
        auth
        ollama
    end
    
    classDef green fill:#d1e7dd,stroke:#0f5132,stroke-width:1px,color:#0f5132;
    classDef blue fill:#cfe2ff,stroke:#084298,stroke-width:1px,color:#084298;
    classDef red fill:#f8d7da,stroke:#842029,stroke-width:1px,color:#842029;
    classDef yellow fill:#fff3cd,stroke:#664d03,stroke-width:1px,color:#664d03;
    classDef gray fill:#f8f9fa,stroke:#343a40,stroke-width:1px,color:#343a40;
    
    class user gray
    class webui blue
    class api blue
    class auth yellow
    class ollama green
    class reject red

⁠🚀 Quick Start

⁠📂 Docker Image

The official Docker image is available on Docker Hub and GitHub Container Registry:

# Docker Hub
docker pull gitmotion/ollama-api-server:latest

# GitHub Container Registry
docker pull ghcr.io/gitmotion/ollama-api-server:latest
⁠🚀 Docker Run
docker run -d \
  --name ollama-api-server \
  --restart unless-stopped \
  -p 7777:7777 \
  -e PORT=7777 \
  -e OLLAMA_BASE_URL=http://internal-ip-where-ollama-installed:11434 \
  -e CORS_ORIGIN=* \
  -e API_KEYS=default-key-1,default-key-2 \
  gitmotion/ollama-api-server:latest
⁠📄 Docker Compose Configuration

The server uses the following docker-compose.yml configuration:

services:
  api:
    image: gitmotion/ollama-api-server:latest
    container_name: ollama-api-server
    restart: unless-stopped
    ports:
      - "${PORT_EXTERNAL:-7777}:7777"
    environment:
      - PORT=7777
      - OLLAMA_BASE_URL=http://internal-ip-where-ollama-installed:11434 # must serve your ollama server with 0.0.0.0
      - CORS_ORIGIN=*
      - API_KEYS=${API_KEYS:-default-key-1,default-key-2}
⁠Example of ollama-api-server with your open-webui stack:
services:
  ollama-api-server:
    image: gitmotion/ollama-api-server:latest
    container_name: ollama-api-server
    restart: unless-stopped
    ports:
      - "${PORT_EXTERNAL:-7777}:7777"
    environment:
      - PORT=7777
      - OLLAMA_BASE_URL=http://internal-ip-where-ollama-installed:11434 # must serve your ollama server with 0.0.0.0
      - CORS_ORIGIN=*
      - API_KEYS=${API_KEYS:-secure-api-key-1,secure-api-key-2} # UPDATE THESE KEYS - comma separated
    networks:
      - ollama-network

  open-webui:
    image: openwebui/open-webui:latest
    container_name: open-webui
    restart: unless-stopped
    depends_on:
      - ollama-api-server
    ports:
      - "3000:3000"
    environment:
      - OLLAMA_BASE_URL=http://ollama-api-server:7777 # Configure the api key via UI
      - WEBUI_SECRET_KEY=${WEBUI_SECRET_KEY}
    volumes:
      - ./open-webui-data:/app/backend/data
    networks:
      - ollama-network
      - your-external-reverse-proxy

networks:
  ollama-network:
    driver: bridge
  your-external-reverse-proxy:
    external: true

This configuration:

  • Uses the official Docker image
  • Maps the container's port 7777 to your host's port (configurable via PORT_EXTERNAL)
  • Sets up the required environment variables
  • Provides default API keys if none are specified

You can customize the configuration by:

  1. Changing the external port (PORT_EXTERNAL in .env)
  2. Setting your API keys (API_KEYS in .env)
  3. Modifying the Ollama base URL if needed
  4. Adjusting CORS settings for your environment
⁠💻 Local Setup
  1. Clone the repository:

    git clone https://github.com/gitmotion/ollama-api-server.git
    cd ollama-api-server
    
  2. Install dependencies:

    npm install
    
  3. Create and configure .env file:

    cp .env.example .env
    # Edit .env with your settings
    
  4. Build and start the server:

    npm run build
    npm start
    

⁠⚙️ Configuration

The server can be configured using environment variables:

VariableDescriptionDefault
PORTServer port7777
OLLAMA_BASE_URLURL of your Ollama instancehttp://localhost:11434⁠
CORS_ORIGINCORS origin setting*
API_KEYSComma-separated list of valid API keysdefault-key-1,default-key-2

⁠🔌 API Endpoints

All Ollama API endpoints are supported with authentication:

  • POST /api/chat - Chat completion
  • POST /api/generate - Text generation
  • POST /api/embeddings - Generate embeddings
  • GET /api/tags - List available models
  • POST /api/show - Show model details
  • POST /api/pull - Pull a model
  • DELETE /api/delete - Delete a model
  • POST /api/copy - Copy a model
  • GET /api/version - Get Ollama version
  • GET /health - Health check endpoint

⁠🔐 Authentication

Include your API key in requests using one of these methods:

  1. X-API-Key header:

    curl -H "x-api-key: your-api-key" http://localhost:7777/api/tags
    
  2. Authorization header:

    curl -H "Authorization: Bearer your-api-key" http://localhost:7777/api/tags
    
  3. Request body:

    curl -X POST -H "Content-Type: application/json" \
         -d '{"apiKey": "your-api-key"}' \
         http://localhost:7777/api/tags
    

Made with ❤️ by gitmotion⁠

Tag summary

Content type

Image

Digest

sha256:586449537…

Size

58.8 MB

Last updated

over 1 year ago

docker pull gitmotion/ollama-api-server