File size: 1,107 Bytes
8a9f1c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""CLI mínimo de chat com streaming.

Uso: uv run python -m espelho.chat_cli
"""

from espelho import config
from espelho.model import load_model, stream_generate


def main() -> None:
    print(f"Carregando {config.ACTIVE_MODEL} ...")
    tokenizer, model = load_model()
    print("Pronto. Digite sua mensagem (Ctrl-D ou 'sair' para encerrar).\n")

    messages: list[dict] = []
    while True:
        try:
            user = input("você> ").strip()
        except (EOFError, KeyboardInterrupt):
            print()
            break
        if not user or user.lower() == "sair":
            break
        messages.append({"role": "user", "content": user})
        print("modelo> ", end="", flush=True)
        reply = ""
        for chunk in stream_generate(
            tokenizer, model, messages,
            max_new_tokens=config.MAX_NEW_TOKENS,
            temperature=config.TEMPERATURE,
        ):
            reply += chunk
            print(chunk, end="", flush=True)
        print("\n")
        messages.append({"role": "assistant", "content": reply})


if __name__ == "__main__":
    main()