#!/bin/bash # Wird von tests/run.sh eingebunden: Antwort-Cache und seine Grenze. echo "== 22. Cache nur für vorhersagbare Antworten ==" XPORT=$((PORT+51)) cat > "$TMP/cache.conf" < "$TMP/cache.log" 2>&1 & PIDS+=($!) wait_port "$XPORT" 30 || bad "the cache instance did not come up" ask() { # ask -> Cache-Zustand aus dem Kopf curl -s -m 10 -D - -o /dev/null -X POST -H 'Content-Type: application/json' -d "$1" \ "http://127.0.0.1:$XPORT/v1/chat/completions" \ | tr -d '\r' | sed -n 's/^[Xx]-[Jj]unction-[Cc]ache: //p' } DET='{"model":"m","messages":[{"role":"user","content":"dieselbe frage"}],"max_tokens":5,"temperature":0}' HOT='{"model":"m","messages":[{"role":"user","content":"dieselbe frage"}],"max_tokens":5,"temperature":0.7}' NONE='{"model":"m","messages":[{"role":"user","content":"dieselbe frage"}],"max_tokens":5}' [ "$(ask "$DET")" = "miss" ] && ok "first deterministic request misses the cache" \ || bad "first deterministic request did not report a miss" [ "$(ask "$DET")" = "hit" ] && ok "second identical request is served from the cache" \ || bad "second identical request was not cached" if [ "$(ask "$HOT")" = "off" ]; then ok "a request with temperature above zero is never cached" else bad "a non-deterministic request was routed through the cache" fi if [ "$(ask "$NONE")" = "off" ]; then ok "a request without a stated temperature is not cached" else bad "a request without a temperature was cached anyway" fi # Einbetter sind reproduzierbar: derselbe Text ergibt denselben Vektor. EMB='{"model":"e","input":"derselbe text"}' emb_state() { curl -s -m 10 -D - -o /dev/null -X POST -H 'Content-Type: application/json' -d "$EMB" \ "http://127.0.0.1:$XPORT/v1/embeddings" | tr -d '\r' \ | sed -n 's/^[Xx]-[Jj]unction-[Cc]ache: //p' } emb_state >/dev/null # erster Aufruf füllt OUT=$(emb_state) if [ "$OUT" = "hit" ]; then ok "an embedding request is cached without asking for a temperature" else bad "an embedding request was not cached (state: ${OUT:-none})" fi STATUS=$(curl -s -m 5 "http://127.0.0.1:$XPORT/status") case "$STATUS" in *'"hits":'*) ok "/status reports cache counters" ;; *) bad "/status has no cache counters" ;; esac