File size: 2,364 Bytes
a0496e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""DE-ToolCall-Bench – Auswertung / evaluation script.
Usage: python evaluate.py <hf-model-id> [results.json]
Benötigt/needs: transformers, torch, huggingface_hub
"""
import json, sys, time, collections, torch
from huggingface_hub import hf_hub_download
from transformers import AutoTokenizer, AutoModelForCausalLM
from score import parse_calls, score

REPO = "roskosmos19/DE-ToolCall-Bench"
SYSTEM = ("Du bist ein hilfreicher Assistent. Heute ist Montag, der 23.11.2026, es ist 09:00 Uhr. "
          "Nutze ein Werkzeug nur, wenn es nötig ist. Fehlen nötige Angaben, frage nach statt zu raten. Antworte auf Deutsch.")

def main(model_id, out="results.json"):
    items = [json.loads(l) for l in open(hf_hub_download(REPO, "data/test.jsonl", repo_type="dataset"))]
    tools = json.load(open(hf_hub_download(REPO, "tools.json", repo_type="dataset")))
    tok = AutoTokenizer.from_pretrained(model_id)
    model = AutoModelForCausalLM.from_pretrained(model_id, dtype=torch.float32).eval()
    res, t0 = [], time.time()
    for it in items:
        msgs = [{"role": "system", "content": SYSTEM}, {"role": "user", "content": it["query"]}]
        prompt = tok.apply_chat_template(msgs, tools=tools, add_generation_prompt=True, tokenize=False,
                                         enable_thinking=False)
        ids = tok(prompt, return_tensors="pt")
        with torch.no_grad():
            o = model.generate(**ids, max_new_tokens=160, do_sample=False)
        txt = tok.decode(o[0][ids["input_ids"].shape[1]:], skip_special_tokens=False)
        exact, sel = score(json.loads(it["expected_calls"]), parse_calls(txt))
        res.append({"id": it["id"], "category": it["category"], "exact": exact, "tool_selection": sel, "output": txt})
    cat = collections.defaultdict(list)
    for r in res: cat[r["category"]].append(r["exact"])
    summary = {"model": model_id, "n": len(res),
               "exact_match": sum(r["exact"] for r in res) / len(res),
               "tool_selection": sum(r["tool_selection"] for r in res) / len(res),
               "per_category": {k: sum(v) / len(v) for k, v in cat.items()},
               "seconds": round(time.time() - t0)}
    json.dump({"summary": summary, "results": res}, open(out, "w"), ensure_ascii=False, indent=1)
    print(json.dumps(summary, indent=1))

if __name__ == "__main__":
    main(*sys.argv[1:])