75 lines
2.6 KiB
Bash
75 lines
2.6 KiB
Bash
#!/usr/bin/env bash
|
|
# Smoke-test AI Insight generate (qwen2.5:3b). Prefer NUC/server, not laptop as proof.
|
|
# Prerequisites: API + RAG + llm containers up, model pulled, DB migrated.
|
|
#
|
|
# export API_BASE=http://127.0.0.1:18000/api/v1
|
|
# export API_KEY=...
|
|
# ./scripts/insight_smoke.sh
|
|
set -euo pipefail
|
|
|
|
API_BASE="${API_BASE:-http://127.0.0.1:18000/api/v1}"
|
|
CYCLE_A="${CYCLE_A:-1}"
|
|
KANDANG_A="${KANDANG_A:-1}"
|
|
CYCLE_B="${CYCLE_B:-2}"
|
|
KANDANG_B="${KANDANG_B:-2}"
|
|
TOPIC="${TOPIC:-eef}"
|
|
AUTH_HEADER=()
|
|
if [[ -n "${API_KEY:-}" ]]; then
|
|
AUTH_HEADER=(-H "X-API-Key: ${API_KEY}")
|
|
fi
|
|
|
|
ROOT="$(cd "$(dirname "$0")/.." && pwd)"
|
|
FIXTURE_A="${ROOT}/fixtures/insight/kandang-01.json"
|
|
FIXTURE_B="${ROOT}/fixtures/insight/kandang-02.json"
|
|
|
|
echo "== Health =="
|
|
curl -fsS "${API_BASE}/health/" | head -c 400; echo
|
|
|
|
echo "== RAG health (optional) =="
|
|
curl -fsS "${RAG_URL:-http://127.0.0.1:5002}/health" || echo "RAG not up — generate may fall back without SOP chunks"
|
|
|
|
echo "== Ollama tags (optional) =="
|
|
curl -fsS "${OLLAMA_URL:-http://127.0.0.1:11434}/api/tags" | head -c 400 || echo "Ollama not up — expect local_fallback"
|
|
echo
|
|
|
|
gen() {
|
|
local cycle="$1" kandang="$2" fixture="$3" force="$4"
|
|
local ctx
|
|
ctx="$(python - <<PY
|
|
import json
|
|
ctx=json.load(open(r'''${fixture}''', encoding='utf-8'))
|
|
ctx['kandangId']=int('${kandang}')
|
|
ctx['cycleId']=int('${cycle}')
|
|
print(json.dumps(ctx, ensure_ascii=False))
|
|
PY
|
|
)"
|
|
curl -fsS -X POST "${API_BASE}/ai-insights/generate/" \
|
|
"${AUTH_HEADER[@]}" \
|
|
-H "Content-Type: application/json" \
|
|
-d "{\"cycle_id\":${cycle},\"kandang_id\":${kandang},\"topic\":\"${TOPIC}\",\"report_type\":\"page\",\"report_period\":\"smoke\",\"force_refresh\":${force},\"context\":${ctx}}"
|
|
}
|
|
|
|
echo "== Generate Kandang A =="
|
|
OUT_A="$(gen "$CYCLE_A" "$KANDANG_A" "$FIXTURE_A" true)"
|
|
echo "$OUT_A" | head -c 500; echo
|
|
|
|
echo "== Generate Kandang B =="
|
|
OUT_B="$(gen "$CYCLE_B" "$KANDANG_B" "$FIXTURE_B" true)"
|
|
echo "$OUT_B" | head -c 500; echo
|
|
|
|
echo "== Cache hit Kandang A (force_refresh=false) =="
|
|
OUT_A2="$(gen "$CYCLE_A" "$KANDANG_A" "$FIXTURE_A" false)"
|
|
echo "$OUT_A2" | head -c 300; echo
|
|
|
|
python - <<PY
|
|
import json
|
|
a=json.loads('''${OUT_A}''')
|
|
b=json.loads('''${OUT_B}''')
|
|
a2=json.loads('''${OUT_A2}''')
|
|
assert a.get('source') in ('generated','local_fallback','cache')
|
|
assert b.get('source') in ('generated','local_fallback','cache')
|
|
assert a2.get('source') == 'cache' or a2.get('insight_text') == a.get('insight_text')
|
|
assert a.get('insight_text') != b.get('insight_text') or a.get('summary') != b.get('summary'), 'A/B text must differ (anti bleed)'
|
|
print('SMOKE OK: A→B→A cache / no cross-kandang bleed')
|
|
PY
|