File size: 2,910 Bytes
b8406ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
scripts/chat.py — Tahap 6: ngobrol dengan Fozan.

Default sampling sudah disetel untuk model char-level 735 ribu parameter:
temperature 0.3, top-k 20, top-p 0.85, repetition penalty 1.25, maks 160
karakter, dan berhenti di newline. Itu jauh lebih stabil daripada default
lama (temperature 0.7, top-k 40, 220 token).

Pemakaian:
  python scripts/chat.py                  # mode interaktif
  python scripts/chat.py "Halo, Fozan!"   # sekali jalan
  python scripts/chat.py --greedy         # selalu token paling mungkin
  python scripts/chat.py --temp 0.5 --topk 40   # lebih kreatif
  python scripts/chat.py --int8           # quantisasi INT8 (lebih ringan)
"""
from __future__ import annotations

import argparse
import sys
import warnings
from pathlib import Path

ROOT = Path(__file__).resolve().parents[1]
if str(ROOT) not in sys.path:
    sys.path.insert(0, str(ROOT))

from fozan.console import enable_utf8  # noqa: E402

enable_utf8()

from fozan.chat import ChatEngine  # noqa: E402
from fozan.config import MODEL_CKPT  # noqa: E402

warnings.filterwarnings("ignore", message=".*torch.ao.quantization.*")


def main():
    ap = argparse.ArgumentParser(description="Chat dengan Fozan Mini")
    ap.add_argument("question", nargs="*", help="pertanyaan sekali-jalan (opsional)")
    ap.add_argument("--ckpt", type=Path, default=MODEL_CKPT)
    ap.add_argument("--temp", type=float, default=0.3)
    ap.add_argument("--topk", type=int, default=20)
    ap.add_argument("--topp", type=float, default=0.85)
    ap.add_argument("--reply", type=int, default=160, help="maks token jawaban")
    ap.add_argument("--rep", type=float, default=1.25,
                    help="repetition penalty (1.0 = mati)")
    ap.add_argument("--greedy", action="store_true",
                    help="selalu ambil token paling mungkin (deterministik)")
    ap.add_argument("--int8", action="store_true",
                    help="quantisasi Linear ke INT8 (lebih cepat di CPU)")
    args = ap.parse_args()

    engine = ChatEngine(args.ckpt, use_int8=args.int8)
    kwargs = dict(temperature=args.temp, top_k=args.topk, top_p=args.topp,
                  max_new_tokens=args.reply, repetition_penalty=args.rep)
    if args.greedy:
        kwargs.update(temperature=0.01, top_k=1, top_p=1.0)

    print(f"Fozan Mini dimuat | {engine.describe()}")
    one_shot = " ".join(args.question).strip()
    if one_shot:
        print(f"Kamu  : {one_shot}")
        print(f"Fozan : {engine.answer(one_shot, **kwargs)}")
        return

    print("Ketik 'keluar' untuk berhenti.\n")
    while True:
        try:
            user = input("Kamu  : ").strip()
        except (EOFError, KeyboardInterrupt):
            break
        if not user:
            continue
        if user.lower() in {"keluar", "exit", "quit"}:
            break
        print(f"Fozan : {engine.answer(user, **kwargs)}\n")


if __name__ == "__main__":
    main()