# https://github.com/fatwang2/sai
apiVersion: v1
kind: Service
metadata:
  name: sai
  namespace: ai
spec:
  ports:
    - port: 8800
      targetPort: 8800
  selector:
    app: sai
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: sai
spec:
  revisionHistoryLimit: 2
  selector:
    matchLabels:
      app: sai
  replicas: 1
  template:
    metadata:
      labels:
        app: sai
    spec:
      volumes:
        - name: db
          persistentVolumeClaim:
            claimName: sai-pvc
      containers:
        - name: sai
          image: wahyd4/tmp:latest
          imagePullPolicy: Always
          ports:
            - containerPort: 8800
          env:
            - name: KV_NAME
              value: "/db/search.db"
            - name: OPENAI_API_KEY
              value: "Nothing1sFree8!"
            - name: OPENAI_BASE_URL
              value: https://no-free-chatgpt.junv.cc/v1
            - name: LLM_MODEL
              value: llama3-8b-8192
            - name: RELATED_QUESTIONS
              value: "1"
            - name: BACKEND
              value: SEARXNG
            - name: SEARXNG_BASE_URL
              value: http://searxng.home-apps:8080/
            - name: GROQ_API_KEY
              valueFrom:
                secretKeyRef:
                  name: groq-api-key
                  key: key
            - name: SYSTEM_PROMPT
              value: |-
                You are a trustworthy and experienced large language AI assistant. You are given a user question, and please write clean, concise and accurate answer to the question.
                You will be given a set of related contexts to the question, each starting with a reference number like [[citation:x]], where x is a number.
                Please use the context and cite the context at the end of each sentence if applicable.
                Please cite the contexts with the reference numbers, in the format [citation:x]. If a sentence comes from multiple contexts, please list all applicable citations, like [citation:3][citation:5].
                Other than code and specific names and citations, your answer must be written in the same language as the question or Grandma will be super angry.
                {context}
                Remember, don't blindly repeat the contexts verbatim and here is the user question:
            - name: RELATED_QUESTIONS_SYSTEM_PROMPT
              value: |-
                You are a trustworthy and experienced large language AI assistant that helps the user to ask related questions, based on user's original question and the related contexts.
                Please identify worthwhile topics that can be follow-ups, and write questions no longer than 20 words each.
                Always be as much specific as you can about the follow up, make sure events, names, locations, are included in follow up questions so they can be asked completely seperate.
                For example, if the original question asks about "the Manhattan project", in the follow up question, Never say "the project" or "that project", always use the full name "the Manhattan project".
                Here are the contexts of the question:
                {context}
                Remember, based on the original question and related contexts, suggest three such further questions. Do NOT repeat the original question.
                Each related question should be no longer than 20 words. Your related questions must be in the same language as the original question otherwise Grandma will be super angry. and initial response from the context history.
                Here is the original question:
          securityContext:
            runAsUser: 1000
            runAsGroup: 1000
            fsGroup: 1000
          resources:
            requests:
              cpu: 200m
              memory: 200Mi
            limits:
              cpu: 1000m
              memory: 1024Mi
          volumeMounts:
            - name: db
              mountPath: /db


---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: sai-ingress
  annotations:
    kubernetes.io/ingress.class: "nginx"
    kubernetes.io/tls-acme: "true"
    cert-manager.io/cluster-issuer: "letsencrypt-prod"
    nginx.ingress.kubernetes.io/auth-url: "https://id.junv.cc/oauth2/auth"
    nginx.ingress.kubernetes.io/auth-signin: "https://id.junv.cc/oauth2/start?rd=https%3A%2F%2F$host$request_uri"
spec:
  tls:
    - hosts:
        - sai.junv.cc
      secretName: sai-tls
  rules:
    - host: sai.junv.cc
      http:
        paths:
          - backend:
              service:
                name: sai
                port:
                  number: 8800
            path: /
            pathType: Prefix

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  namespace: ai
  name: sai-pvc
spec:
  accessModes:
    - ReadWriteOnce
  storageClassName: nfs-client
  resources:
    requests:
      storage: 2Gi
