Files
Sprachmittler/scripts/capture-fixtures.sh
T

71 lines
3.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# Records real Ollama responses into test fixtures (T1.1). Synthetic input only (SP-12).
# Usage: scripts/capture-fixtures.sh [base_url]
set -euo pipefail
cd "$(dirname "$0")/.."
BASE="${1:-http://127.0.0.1:11434}"
OUT="Packages/SprachmittlerKit/Tests/SprachmittlerOllamaTests/Fixtures"
mkdir -p "$OUT"
command -v jq >/dev/null || { echo "jq required (brew install jq)" >&2; exit 1; }
j() { curl -sS -H 'Content-Type: application/json' "$@"; }
EN="The quick brown fox jumps over the lazy dog. Please open https://example.com/docs?lang=en and set the limit to {{limit}}."
DE="Größenänderungen und Straßen sind wichtig. Bitte öffne https://example.de/hilfe und setze das Limit auf {{limit}}."
MD="# Release notes
Open **Settings** and set \`max_tokens\` to {{limit}}. See [the docs](https://example.com/docs)."
echo "→ version / tags / ps"
j "$BASE/api/version" | tee "$OUT/version.json" | jq -c .
j "$BASE/api/tags" > "$OUT/tags.json"
j "$BASE/api/ps" > "$OUT/ps.json"
models=$(jq -r '.models[].name' "$OUT/tags.json" | grep -i 'hy-mt3' || true)
[[ -z "$models" ]] && { echo "No Hy-MT3 model installed. See docs/HYMT3_MODEL.md, e.g. ollama pull hf.co/tencent/Hy-MT3-7B-GGUF:Q4_K_M" >&2; exit 1; }
prompt() { # $1 target, $2 text
printf 'Translate the following text into %s. Note that you should **only output the translated result without any additional explanation**:\n\n%s' "$1" "$2"
}
body() { # $1 model, $2 target, $3 text, $4 num_predict
jq -nc --arg m "$1" --arg p "$(prompt "$2" "$3")" --argjson n "$4" \
'{model:$m, messages:[{role:"user",content:$p}], stream:true, keep_alive:"5m",
options:{temperature:0.7, top_p:0.6, top_k:20, repeat_penalty:1.05, num_predict:$n, num_ctx:8192}}'
}
meta_models="[]"
for m in $models; do
safe=$(echo "$m" | sed 's#[/:]#_#g')
echo "→ show $m"
j -X POST "$BASE/api/show" -d "$(jq -nc --arg m "$m" '{model:$m}')" \
| jq '{capabilities, details, parameters, template, model_info: (.model_info // {} | with_entries(select(.key|test("context_length|block_count|embedding_length"))))}' \
> "$OUT/show-$safe.json"
echo " capabilities: $(jq -c '.capabilities' "$OUT/show-$safe.json")"
digest=$(jq -r --arg m "$m" '.models[] | select(.name==$m) | .digest[0:12]' "$OUT/tags.json")
meta_models=$(jq -c --arg m "$m" --arg d "$digest" '. + [{model:$m,digest:$d}]' <<<"$meta_models")
echo "→ chat $m [en→de]"
j -N "$BASE/api/chat" -d "$(body "$m" German "$EN" 1024)" > "$OUT/chat-$safe-en-de.ndjson"
echo "→ chat $m [de→en]"
j -N "$BASE/api/chat" -d "$(body "$m" English "$DE" 1024)" > "$OUT/chat-$safe-de-en.ndjson"
echo "→ chat $m [markdown]"
j -N "$BASE/api/chat" -d "$(body "$m" German "$MD" 1024)" > "$OUT/chat-$safe-markdown.ndjson"
echo "→ chat $m [truncated]"
j -N "$BASE/api/chat" -d "$(body "$m" German "$EN" 16)" > "$OUT/chat-$safe-truncated.ndjson"
echo " done_reason: $(tail -1 "$OUT/chat-$safe-truncated.ndjson" | jq -r '.done_reason // .error')"
done
echo "→ error case"
code=$(curl -sS -o "$OUT/error-missing-model.json" -w '%{http_code}' -H 'Content-Type: application/json' "$BASE/api/chat" \
-d '{"model":"sprachmittler/does-not-exist","messages":[{"role":"user","content":"x"}],"stream":false}')
echo " missing model: HTTP $code"
jq -n --arg date "$(date -u +%Y-%m-%dT%H:%M:%SZ)" --arg hw "$(sysctl -n machdep.cpu.brand_string 2>/dev/null || uname -m)" \
--arg mem "$(sysctl -n hw.memsize 2>/dev/null || echo unknown)" --slurpfile v "$OUT/version.json" --argjson models "$meta_models" \
'{date:$date, ollama:$v[0].version, hardware:$hw, memoryBytes:$mem, input:"synthetic EN/DE/Markdown", models:$models}' > "$OUT/capture-meta.json"
echo "→ checking fixture sizes"
find "$OUT" -type f -size +200k -print | while IFS= read -r f; do echo "! larger than 200 KB: $f"; done
echo "Done. Review $OUT, then close the VERIFY items in docs/OLLAMA_TRANSLATION_API.md."