File size: 1,083 Bytes
17e10c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import json
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

BASE = "openbmb/MiniCPM5-1B"
tok = AutoTokenizer.from_pretrained(BASE)
model = AutoModelForCausalLM.from_pretrained(BASE, dtype=torch.float32, use_kernels=False)
model.eval()
im_end = tok.convert_tokens_to_ids("<|im_end|>")

rows = [json.loads(l) for l in open("/data/test.jsonl", encoding="utf-8")]
pos = [r for r in rows if r["messages"][-1].get("tool_calls")][:3]
for r in pos:
    prompt = tok.apply_chat_template(
        r["messages"][:-1], tools=r["tools"], tokenize=False, add_generation_prompt=True
    )
    enc = tok(prompt, return_tensors="pt", add_special_tokens=False)
    with torch.no_grad():
        out = model.generate(**enc, max_new_tokens=192, do_sample=False, eos_token_id=im_end, pad_token_id=tok.pad_token_id)
    text = tok.decode(out[0][enc["input_ids"].shape[1]:], skip_special_tokens=False).split("<|im_end|>")[0]
    print("\n=== BASE RAW ===")
    print(repr(text[:800]))
    print("EXPECTED:", json.dumps(r["messages"][-1]["tool_calls"], ensure_ascii=False)[:300])