File size: 4,432 Bytes
46bee4a
dc8ac30
46bee4a
dc8ac30
 
 
46bee4a
 
 
 
 
 
da32fba
 
46bee4a
 
 
 
da32fba
46bee4a
 
 
 
 
da32fba
 
 
46bee4a
da32fba
46bee4a
da32fba
 
 
 
 
 
dc8ac30
 
46bee4a
 
 
da32fba
dc8ac30
46bee4a
dc8ac30
46bee4a
 
dc8ac30
da32fba
dc8ac30
46bee4a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#!/usr/bin/env bash
# RysoAI TinyQA-1 Moss: load the models in the background, then ask questions from the terminal.
#   ./moss.sh setup                  one-off: Python 3.11 venv with CPU torch and the pinned packages (~1 GB download)
#   ./moss.sh start                  load the models in the background (DuckDuckGo search, no keys needed)
#   ./moss.sh ask "Who painted the Night Watch?"     one answer (needs ./moss.sh start)
#   ./moss.sh ask                    type questions one after another; add --local to answer from the index
#   ./moss.sh index ~/Documents/notes               index a folder of .txt/.md files (or a .jsonl of {"title","text"}) for offline use
#   ./moss.sh ask --local "What is the boiling point of water at altitude?"   answer from the indexed files, no network
#   ./moss.sh stop | status
# Optional: put SERPER_API_KEY=... in .env next to this script for Google results and verifier-gated retries.
set -uo pipefail
ROOT=$(cd "$(dirname "$0")" && pwd); CODE="$ROOT/code"; PY="$ROOT/.venv/bin/python"; PORT=${MOSS_PORT:-8091}; URL="http://127.0.0.1:$PORT"
LOGS="$ROOT/data"; mkdir -p "$LOGS"
stop_all() { pkill -f "tinyqa.server --port $PORT" 2>/dev/null; pkill -f "llama-server.*--port 80(89|93|95)" 2>/dev/null; true; }
case "${1:-start}" in
  setup)
    command -v python3.11 >/dev/null || { echo "needs Python 3.11 (macOS: brew install python@3.11)"; exit 1; }
    python3.11 -m venv "$ROOT/.venv" && "$ROOT/.venv/bin/pip" install -q --upgrade pip && "$ROOT/.venv/bin/pip" install -q -r "$ROOT/requirements.txt" || exit 1
    command -v llama-server >/dev/null || echo "optional: install llama.cpp (macOS: brew install llama.cpp) so the 113M models write the answer line; without it a template writes it"
    echo "setup done; next: ./moss.sh start";;
  start)
    [ -x "$PY" ] || { echo "run ./moss.sh setup first"; exit 1; }
    stop_all; sleep 1
    [ -f "$ROOT/.env" ] && set -a && . "$ROOT/.env" && set +a
    export MOSS_HOME="$ROOT" MOSS_VERIFIER="$ROOT/models/verifier" MOSS_RANKER="$ROOT/models/ranker" MOSS_TAU_V=1.5
    export MOSS_SOURCE_PENALTY=1 MOSS_QUOTABLE=1 MOSS_SEARCH=ddgs
    export MOSS_BLOCK_DOMAINS="${MOSS_BLOCK_DOMAINS:-}"   # no sites blocked unless you list some
    # retries for uncertified answers go to Google; DuckDuckGo retries take 10-40 s, so without a Serper key they stay off
    if [ -n "${SERPER_API_KEY:-}" ]; then export MOSS_FALLBACK=1 MOSS_PLANNER_RETRY=1 MOSS_SIEVE_RETRY=1; else export MOSS_FALLBACK=0 MOSS_PLANNER_RETRY=0 MOSS_SIEVE_RETRY=0; fi
    if command -v llama-server >/dev/null; then
      export MOSS_VERBALIZE=own MOSS_VERBALISER=http://127.0.0.1:8095
      (nohup llama-server -m "$ROOT/gguf/tinyqa-1-moss-glue-Q8_0.gguf" -c 2048 --port 8089 -t 4 --special > "$LOGS/decoder.log" 2>&1 &)
      (nohup llama-server -m "$ROOT/gguf/tinyqa-1-moss-planner-Q8_0.gguf" -c 1024 --port 8093 -t 4 --special > "$LOGS/planner.log" 2>&1 &)
      (nohup llama-server -m "$ROOT/gguf/tinyqa-1-moss-verbaliser-Q8_0.gguf" -c 1024 --port 8095 -t 4 --special > "$LOGS/verbaliser.log" 2>&1 &)
    else export MOSS_VERBALIZE=template; echo "llama-server not found: the answer line comes from a template"; fi
    (cd "$CODE" && nohup "$PY" -m tinyqa.server --port "$PORT" > "$LOGS/moss.log" 2>&1 &)
    for i in $(seq 1 180); do curl -s -m 1 "$URL/health" | grep -q '"models": true' && { echo "ready: ./moss.sh ask \"your question\""; exit 0; }; sleep 1; done
    echo "did not start; see $LOGS/moss.log"; exit 1;;
  index)
    [ -x "$PY" ] || { echo "run ./moss.sh setup first"; exit 1; }
    src=${2:?usage: ./moss.sh index <folder-or-jsonl>}; src=$(cd "$(dirname "$src")" && pwd)/$(basename "$src")
    (cd "$CODE" && MOSS_HOME="$ROOT" "$PY" -c 'import sys; from tinyqa import pipeline; print(f"indexed {pipeline.index_corpus(sys.argv[1]):,} passages from {sys.argv[1]}")' "$src" 2>&1 | grep -v -i warning)
    echo "ready to use: ./moss.sh ask --local \"your question\"";;
  stop) stop_all; echo stopped;;
  status) curl -s -m 1 "$URL/health" | grep -q '"models": true' && echo running || echo "not running";;
  ask)
    curl -s -m 1 "$URL/health" | grep -q '"models": true' || { echo "not running: ./moss.sh start first"; exit 1; }
    [ -f "$ROOT/.env" ] && set -a && . "$ROOT/.env" && set +a
    shift; cd "$CODE" && MOSS_PROVIDER=$([ -n "${SERPER_API_KEY:-}" ] && echo serper || echo ddgs) "$PY" -m tinyqa.cli "$URL" "$@";;
  *) sed -n 2,9p "$0";;
esac