Files
junction/tests/cache.sh
T
Hermes 0503540760 Cache nur für vorhersagbare Antworten
Der Cache war seit dem ersten Tag da, aber standardmäßig aus — und das aus einem Grund,
der sich mit einem Schalter allein nicht beheben ließ: er hat jedes identische Paar aus
Pfad und Rumpf gleich behandelt. Zwei Anfragen mit Temperatur 0.7 sind aber nicht
identisch, sie sind nur gleich formuliert. Der zweite Fragesteller hätte die Antwort des
ersten bekommen.

Neu:

  cache_deterministic_only on     (Standard)

Damit landet nur im Cache, was reproduzierbar ist — Temperatur genau 0, oder ein Aufruf
an /v1/embeddings, wo derselbe Text immer denselben Vektor ergibt. Alles andere trägt
X-Junction-Cache: off und geht durch.

Gemessen: temp 0 → miss, hit, hit, hit; temp 0.7 → off, off; ohne Angabe → off.
Einbetter → beim zweiten Aufruf hit.

100 Prüfungen, keine Fehler. Der alte Cache-Test fragte ohne Temperatur und musste
mitziehen; die Einbetter-Prüfung fragte nur einmal und prüfte damit den leeren Cache.
2026-11-05 09:29:53 +00:00

66 lines
2.3 KiB
Bash

#!/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" <<CFG
listen 127.0.0.1:$XPORT
cache_ttl 300
cache_entries 50
cache_bytes 2000000
endpoint a { base http://127.0.0.1:$P_GATED/v1 }
req for "*" pass a
CFG
./junction --config "$TMP/cache.conf" > "$TMP/cache.log" 2>&1 &
PIDS+=($!)
sleep 0.8
ask() { # ask <body> -> 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