File size: 1,491 Bytes
53d5fc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Chat with Slayer1B 1.10 Instruct 30.11 (one GPU or CPU):  pip install torch safetensors tokenizers ;  python example.py "What is the capital of Poland?" """
import sys
from pathlib import Path

import torch
from tokenizers import Tokenizer

HERE = Path(__file__).resolve().parent
sys.path.insert(0, str(HERE))
import load_slayer  # noqa: E402
from chatfmt import Fmt, parse_calls  # noqa: E402
from model_io import generate  # noqa: E402

device = "cuda" if torch.cuda.is_available() else "cpu"
model = load_slayer.load(str(HERE))
model = model.to(device=device, dtype=torch.bfloat16 if device == "cuda" else torch.float32)
fmt = Fmt(Tokenizer.from_file(str(HERE / "tokenizer.json")))
tools = [{"name": "calculator", "description": "Evaluate an arithmetic expression exactly.",
          "parameters": {"type": "object", "properties": {"expression": {"type": "string", "description": "Arithmetic expression"}}, "required": ["expression"]}}]
messages = [{"role": "system", "content": "You are Slayer — a private, bilingual (PL/EN) assistant. Use tools only when necessary and never invent their results."},
            {"role": "user", "content": sys.argv[1] if len(sys.argv) > 1 else "What is 18% of 2450?"}]
out, stop = generate(model, fmt.prompt_ids(messages, tools), stop_ids={fmt.ime, fmt.bos}, max_new_tokens=256, device=device)
text = fmt.tok.decode(out, skip_special_tokens=False)
calls, errors = parse_calls(text)
print(text)
print("tool calls to validate and execute:", calls)