networks:
  r2r-network:
    external: true
          #driver: bridge
    attachable: true
    labels:
      - "com.docker.compose.recreate=always"

volumes:
  postgres_data:
    name: ${VOLUME_POSTGRES_DATA:-postgres_data}

services:
  postgres:
    image: pgvector/pgvector:pg17
    profiles: [postgres]
    environment:
      - POSTGRES_USER=${R2R_POSTGRES_USER:-${POSTGRES_USER:-postgres}} # Eventually get rid of POSTGRES_USER, but for now keep it for backwards compatibility
      - POSTGRES_PASSWORD=${R2R_POSTGRES_PASSWORD:-${POSTGRES_PASSWORD:-cocorobo-123}} # Eventually get rid of POSTGRES_PASSWORD, but for now keep it for backwards compatibility
      - POSTGRES_HOST=${R2R_POSTGRES_HOST:-${POSTGRES_HOST:-postgres}} # Eventually get rid of POSTGRES_HOST, but for now keep it for backwards compatibility
      - POSTGRES_PORT=${R2R_POSTGRES_PORT:-${POSTGRES_PORT:-5432}} # Eventually get rid of POSTGRES_PORT, but for now keep it for backwards compatibility
      - POSTGRES_MAX_CONNECTIONS=${R2R_POSTGRES_MAX_CONNECTIONS:-${POSTGRES_MAX_CONNECTIONS:-102400}} # Eventually get rid of POSTGRES_MAX_CONNECTIONS, but for now keep it for backwards compatibility
      - PGPORT=${R2R_POSTGRES_PORT:-5432}
    volumes:
      - ./postgres_data:/var/lib/postgresql/data
    networks:
      - r2r-network
    ports:
      - "${R2R_POSTGRES_PORT:-5432}:${R2R_POSTGRES_PORT:-5432}"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U ${R2R_POSTGRES_USER:-postgres}"]
      interval: 10s
      timeout: 5s
      retries: 5
    restart: on-failure
    command: >
      postgres
      -c max_connections=${R2R_POSTGRES_MAX_CONNECTIONS:-1024}

  r2r:
    image: ${R2R_IMAGE:-ragtoriches/prod:latest}
    build:
      context: .
      args:
        PORT: ${R2R_PORT:-7272}
        R2R_PORT: ${R2R_PORT:-7272}
        HOST: ${R2R_HOST:-0.0.0.0}
        R2R_HOST: ${R2R_HOST:-0.0.0.0}
    ports:
      - "${R2R_PORT:-7272}:${R2R_PORT:-7272}"
    environment:
      - PYTHONUNBUFFERED=1
      - R2R_PORT=${R2R_PORT:-7272}
      - R2R_HOST=${R2R_HOST:-0.0.0.0}

      # R2R
      - R2R_CONFIG_NAME=${R2R_CONFIG_NAME:-}
      - R2R_CONFIG_PATH=${R2R_CONFIG_PATH:-}
      - R2R_PROJECT_NAME=${R2R_PROJECT_NAME:-r2r_default}

      # Postgres
      - R2R_POSTGRES_USER=${R2R_POSTGRES_USER:-postgres}
      - R2R_POSTGRES_PASSWORD=${R2R_POSTGRES_PASSWORD:-cocorobo-123}
      - R2R_POSTGRES_HOST=${R2R_POSTGRES_HOST:-postgres}
      - R2R_POSTGRES_PORT=${R2R_POSTGRES_PORT:-5432}
      - R2R_POSTGRES_DBNAME=${R2R_POSTGRES_DBNAME:-cocorobo-file}
      - R2R_POSTGRES_MAX_CONNECTIONS=${R2R_POSTGRES_MAX_CONNECTIONS:-102400}
      - R2R_POSTGRES_PROJECT_NAME=${R2R_POSTGRES_PROJECT_NAME:-cocorobo-knowledge}
      - R2R_POSTGRES_STATEMENT_CACHE_SIZE=${R2R_POSTGRES_STATEMENT_CACHE_SIZE:-100}

      # OpenAI
      - OPENAI_API_KEY=${OPENAI_API_KEY:-sk-vTqeBKDC2j6osbGt89A2202dAd1c4fE8B1D294388b569e54}
      - OPENAI_API_BASE=${OPENAI_API_BASE:-https://onehub.cocorobo.cn/v1}

      # Anthropic
      - ANTHROPIC_API_KEY=${ANTHROPIC_API_KEY:-}

      # Azure
      - AZURE_API_KEY=${AZURE_API_KEY:-}
      - AZURE_API_BASE=${AZURE_API_BASE:-}
      - AZURE_API_VERSION=${AZURE_API_VERSION:-}

      # Google Vertex AI
      - GOOGLE_APPLICATION_CREDENTIALS=${GOOGLE_APPLICATION_CREDENTIALS:-}
      - VERTEX_PROJECT=${VERTEX_PROJECT:-}
      - VERTEX_LOCATION=${VERTEX_LOCATION:-}

      # AWS Bedrock
      - AWS_ACCESS_KEY_ID=${AWS_ACCESS_KEY_ID:-AKIATLPEDU37QV5CHLMH}
      - AWS_SECRET_ACCESS_KEY=${AWS_SECRET_ACCESS_KEY:-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}
      - AWS_REGION_NAME=${AWS_REGION_NAME:-cn-northwest-1}

      # Groq
      - GROQ_API_KEY=${GROQ_API_KEY:-}

      # Cohere
      - COHERE_API_KEY=${COHERE_API_KEY:-}

      # Anyscale
      - ANYSCALE_API_KEY=${ANYSCALE_API_KEY:-ZYLf8VLMoW8QRJ86bXEec9rX}

      # Ollama
      - OLLAMA_API_BASE=${OLLAMA_API_BASE:-http://host.docker.internal:11434}

    networks:
      - r2r-network
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:${R2R_PORT:-7272}/v3/health"]
      interval: 6s
      timeout: 5s
      retries: 5
    restart: on-failure
    volumes:
      - ${R2R_CONFIG_PATH:-/}:${R2R_CONFIG_PATH:-/app/config}
    extra_hosts:
      - host.docker.internal:host-gateway

  r2r-dashboard:
    image: emrgntcmplxty/r2r-dashboard:latest
    environment:
      - NEXT_PUBLIC_R2R_DEPLOYMENT_URL=${R2R_DEPLOYMENT_URL:-https://r2rserver.cocorobo.cn}
    networks:
      - r2r-network
    ports:
      - "${R2R_DASHBOARD_PORT:-7273}:3000"
