apiVersion: apps/v1
kind: Deployment
metadata:
  name: cerebral-api-deployment
  namespace: contoso-hypermarket
spec:
  replicas: 1
  selector:
    matchLabels:
      app: cerebral-api
  template:
    metadata:
      labels:
        app: cerebral-api
    spec:
      initContainers: 
        - name: init-documents
          image: mcr.microsoft.com/cbl-mariner/base/core:2.0
          command: 
          - sh
          - -c
          - |
            tdnf update -y
            tdnf install wget -y
            cd /app/docs
            wget -O "SmartShelf_Manual.pdf" "https://download.microsoft.com/download/2b3927e0-5342-4d84-8b55-ab958281259b/SmartShelf%20System%20User%20Manual%20for%20Contoso%20Hypermarket.pdf" && \
            wget -O "Scale_Manual.pdf" "https://download.microsoft.com/download/2b3927e0-5342-4d84-8b55-ab958281259b/Scale%20System%20User%20Manual%20for%20Contoso%20Hypermarket.pdf" && \
            wget -O "Refrigeration_Manual.pdf" "https://download.microsoft.com/download/2b3927e0-5342-4d84-8b55-ab958281259b/Refrigeration%20System%20User%20Manual%20for%20Contoso%20Hypermarket.pdf" && \
            wget -O "Checkout_Manual.pdf" "https://download.microsoft.com/download/2b3927e0-5342-4d84-8b55-ab958281259b/Automated%20Checkout%20System%20User%20Manual%20for%20Contoso%20Hypermarket.pdf" && \
            wget -O "HVAC_Manual.pdf" "https://download.microsoft.com/download/2b3927e0-5342-4d84-8b55-ab958281259b/HVAC%20System%20User%20Manual%20for%20Contoso%20Hypermarket.pdf"
          volumeMounts:
          - name: cerebral-docs
            mountPath: /app/docs
        - name: init-slm-model
          image: mcr.microsoft.com/azurelinux/base/python:3.12
          command: 
          - sh
          - -c
          - |
              pip install -U "huggingface_hub[cli]"
              huggingface-cli download microsoft/Phi-3-mini-4k-instruct-onnx \
              --include "cpu_and_mobile/cpu-int4-rtn-block-32-acc-level-4/*" \
              --local-dir /app/models
              if [ ! -d "/app/models/phi3" ]; then
                mkdir /app/models/phi3
              fi
          volumeMounts:
          - name: cerebral-slm-model
            mountPath: /app/models
      containers:
      - name: cerebral-api
        image: mcr.microsoft.com/jumpstart/agora/cerebral_api:1.0.0
        ports:
        - containerPort: 5003
        env:
        - name: AZURE_OPENAI_API_KEY
          value: "replaceme"
        - name: CHATGPT_MODEL
          value: "gpt-35-turbo"
        - name: AZURE_OPENAI_ENDPOINT
          value: ""
        - name: OPENAI_API_VERSION
          value: "2024-03-01-preview"
        - name: INFLUXDB_URL
          value: "http://influxdb.contoso-hypermarket.svc.cluster.local:8086"
        - name: INFLUXDB_BUCKET
          value: "hypermarket"
        - name: INFLUXDB_TOKEN
          value: "secret-token"
        - name: INFLUXDB_ORG
          value: "InfluxData"
        - name: REDIS_URL
          value: "redis://redis-service.contoso-hypermarket.svc.cluster.local:6379"
        - name: SQL_DATABASENAME
          value: "RetailDB"
        - name: SQL_SERVER
          value: "mssql-service"
        - name: SQL_USERNAME
          value: "SA"
        - name: SQL_PASSWORD
          value: "ArcPassword123!!"
        - name: WHISPER_MODEL_PATH
          value: ""
        - name: AZURE_AI_SPEECH_KEY
          value: ""
        - name: AZURE_AI_SPEECH_REGION
          value: ""
        - name: USE_LOCAL_LLM
          value: "false"
        - name: MODEL_PATH
          value: "/app/models"
        - name: CHROMA_HOST
          value: "chroma-db-service.contoso-hypermarket.svc.cluster.local"
        - name: CHROMA_PORT
          value: "8040"
        - name: CHROMA_COLLECTION
          value: "documents"
      volumes:
      - name: cerebral-docs
        persistentVolumeClaim:
          claimName: cerebral-docs
      - name: cerebral-slm-model
        persistentVolumeClaim:
          claimName: cerebral-slm-model
---
apiVersion: v1
kind: Service
metadata:
  name: cerebral-api-service
  namespace: contoso-hypermarket
spec:
  type: LoadBalancer
  selector:
    app: cerebral-api
  ports:
    - protocol: TCP
      port: 5003
      targetPort: 5003