apiVersion: apps/v1
kind: Deployment
metadata:
  name: cerebral-api-deployment
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 "Automated_Checkout_Manual.pdf" "https://download.microsoft.com/download/3ae1d7aa-a642-48cf-b848-67b4eaa81292/Automated Checkout System User Manual for Contoso Hypermarket.pdf" && \
            wget -O "SmartShelf_Manual.pdf" "https://download.microsoft.com/download/3ae1d7aa-a642-48cf-b848-67b4eaa81292/SmartShelf System User Manual for Contoso Hypermarket.pdf" && \
            wget -O "Refrigeration_Manual.pdf" "https://download.microsoft.com/download/3ae1d7aa-a642-48cf-b848-67b4eaa81292/Refrigeration System User Manual for Contoso Hypermarket.pdf" && \
            wget -O "HVAC_Manual.pdf" "https://download.microsoft.com/download/3ae1d7aa-a642-48cf-b848-67b4eaa81292/HVAC System User Manual for Contoso Hypermarket.pdf" && \
            wget -O "Scale_Manual.pdf" "https://download.microsoft.com/download/3ae1d7aa-a642-48cf-b848-67b4eaa81292/Scale System User Manual for Contoso Hypermarket.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: {{ .Values.images.cerebral.repository }}:{{ .Values.images.cerebral.tag }}
        imagePullPolicy: {{ .Values.images.cerebral.pullPolicy }}
        ports:
        - containerPort: 5003
        env:
        - name: AZURE_OPENAI_API_KEY
          valueFrom:
            secretKeyRef:
              name: azure-openai-secret
              key: azure-openai-key
        - name: AZURE_OPENAI_ENDPOINT
          valueFrom:
            secretKeyRef:
              name: azure-openai-secret
              key: azure-openai-endpoint
        - name: CHATGPT_MODEL
          valueFrom:
            secretKeyRef:
              name: azure-openai-secret
              key: azure-openai-deployment-name
        - name: OPENAI_API_VERSION
          valueFrom:
            secretKeyRef:
              name: azure-openai-secret
              key: azure-openai-api-version
        - 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
          valueFrom:
            secretKeyRef:
              name: azure-sqlpassword-secret
              key: azure-sqlpassword-secret
        - name: WHISPER_MODEL_PATH
          value: ""
        - name: AZURE_AI_SPEECH_KEY
          valueFrom:
            secretKeyRef:
              name: azure-openai-secret
              key: azure-openai-key
        - name: AZURE_AI_SPEECH_REGION
          valueFrom:
            secretKeyRef:
              name: azure-openai-secret
              key: region
        - 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"
        {{- if (lookup "v1" "Namespace" "" "gpu-operator") }}
        resources:
          limits:
            nvidia.com/gpu: "1"
        {{- end }}
      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
spec:
  type: LoadBalancer
  selector:
    app: cerebral-api
  ports:
    - protocol: TCP
      port: 5003
      targetPort: 5003
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: cerebral-docs
  namespace: contoso-hypermarket
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: cerebral-slm-model
  namespace: contoso-hypermarket
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi