diff --git a/kitchen/bin/rh-chat.sh b/kitchen/bin/rh-chat.sh new file mode 100755 index 0000000..c32c0de --- /dev/null +++ b/kitchen/bin/rh-chat.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +# rabbithole chat helper — talkative defaults (temperature 1.05) +set -euo pipefail +MODEL="${1:?model}" +SYSTEM_FILE="${2:?system file or -}" +USER_MSG="${3:?user message}" +OUT="${4:?out path}" +TEMP="${TEMP:-1.05}" +TOP_P="${TOP_P:-0.95}" +NUM_PREDICT="${NUM_PREDICT:-900}" +BASE="${OLLAMA_HOST:-http://127.0.0.1:11436}" + +if [[ "$SYSTEM_FILE" == "-" ]]; then + SYS=$(cat) +else + SYS=$(cat "$SYSTEM_FILE") +fi + +python3 - "$MODEL" "$SYS" "$USER_MSG" "$OUT" "$TEMP" "$TOP_P" "$NUM_PREDICT" "$BASE" <<'PY' +import json, sys, urllib.request +model, system, user, out, temp, top_p, num_predict, base = sys.argv[1:9] +body = { + "model": model, + "stream": False, + "options": { + "temperature": float(temp), + "top_p": float(top_p), + "num_predict": int(num_predict), + }, + "messages": [ + {"role": "system", "content": system}, + {"role": "user", "content": user}, + ], +} +req = urllib.request.Request( + base.rstrip("/") + "/api/chat", + data=json.dumps(body).encode(), + headers={"Content-Type": "application/json"}, + method="POST", +) +with urllib.request.urlopen(req, timeout=300) as r: + text = (json.load(r).get("message") or {}).get("content") or "" +open(out, "w").write(text.strip() + "\n") +print("ok", out, "chars", len(text), "temp", temp) +PY