File size: 1,669 Bytes
2309cb1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Chat with Têtu Code 2 in the terminal (no tools). For the agent in OpenCode: python -m tetu_code.serve

    python chat.py            (GPU)      python chat.py --cpu
"""
import argparse
from pathlib import Path
import re

import torch

from tetu_code import fmt
from tetu_code.generate import Decoder, load

ROOT = Path(__file__).resolve().parent


def main():
    p = argparse.ArgumentParser()
    p.add_argument("--cpu", action="store_true")
    p.add_argument("--temp", type=float, default=0.2)
    p.add_argument("--max_new", type=int, default=1024)
    a = p.parse_args()
    dev = "cpu" if a.cpu or not torch.cuda.is_available() else "cuda"
    model, tok = load(str(ROOT), device=dev, dtype=torch.float32 if dev == "cpu" else torch.bfloat16)
    dec = Decoder(model, tok, max_len=16384, graph=dev == "cuda")
    stop = {tok.token_to_id(t) for t in ("<|end|>", "<|eos|>", "<|user|>", "<|/call|>")}
    text = f"<|system|>{fmt.system_text()}"
    print("Têtu Code 2 — /reset to clear, Ctrl+C to quit")
    while True:
        q = input("\n> ").strip()
        if q == "/reset":
            text = f"<|system|>{fmt.system_text()}"
            continue
        text += f"<|user|>{q}<|assistant|>"
        ids = tok.encode(text, add_special_tokens=False).ids
        out = []
        for t in dec.generate(ids, max_new=a.max_new, temp=a.temp, stop=stop):
            if t in stop:
                break
            out.append(t)
            print(tok.decode([t], skip_special_tokens=False), end="", flush=True)
        reply = tok.decode(out, skip_special_tokens=False)
        text += reply + "<|end|>"
        print()


if __name__ == "__main__":
    main()