# Azure DevOps Pipeline: Nightly RAG Groundedness Evaluation (PB-RAG-06-03, step 6)
# eval/production_sampled.jsonl is written by the trace export job before this run
# (query, context, response per line; no personal data). See PB-RAG-06-03, step 6.
trigger: none
schedules:
  - cron: "0 2 * * *"
    displayName: "Nightly 02:00 UTC Groundedness Audit"
    branches:
      include:
        - main
    always: true

pool:
  vmImage: "ubuntu-latest"

variables:
  - group: rag-evaluation-secrets
  - name: PYTHON_VERSION
    value: "3.11"
  - name: MIN_PASS_RATE
    value: "0.95"

steps:
  - task: UsePythonVersion@0
    inputs:
      versionSpec: $(PYTHON_VERSION)
    displayName: "Set Python version"

  - script: |
      python -m pip install --upgrade pip
      pip install azure-ai-evaluation azure-identity
    displayName: "Install evaluation dependencies"

  - script: |
      python snippets/groundedness_gate.py \
        --input eval/production_sampled.jsonl \
        --output $(Build.ArtifactStagingDirectory)/nightly_results.jsonl \
        --threshold $(MIN_PASS_RATE) \
        --min-score 4.0
    displayName: "Execute Nightly Groundedness Batch"
    env:
      AZURE_OPENAI_ENDPOINT: $(AOAI_ENDPOINT)
      JUDGE_DEPLOYMENT: $(JUDGE_DEPLOYMENT)

  - task: PublishPipelineArtifact@1
    condition: always()
    inputs:
      targetPath: $(Build.ArtifactStagingDirectory)/nightly_results.jsonl
      artifact: GroundednessNightlyResults
    displayName: "Publish evaluation results"
