okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ba8745e13
main
1#!/usr/bin/env python3 2"""mkprompts.py: the 10 greedy prompts of the llama.cpp reference run (runs/base1/answers_{chat,think}) as token ids. 3 4Renders each request with the GGUF's chat template (jinja2, thinking_mode chat | thinking), tokenizes with llama-tokenize 5(--no-bos (special tokens parse by default): the template writes the BOS text itself), and keeps the reference's greedy output ids. 6Writes OUT/prompts.json: [{"id", "mode", "ids": [...], "expect_ids": [...], "expect_text", "finish"}]. 7""" 8import json ,os ,subprocess ,sys ,tempfile 9import jinja2 10from jinja2 .sandbox import ImmutableSandboxedEnvironment 11 12ref_dir ,template ,shard ,tokenize ,out_dir = sys .argv [1 :6 ] 13env = ImmutableSandboxedEnvironment (trim_blocks = True ,lstrip_blocks = True ) 14env .filters ["tojson" ]= lambda x ,** k :json .dumps (x ,ensure_ascii = False ) 15def raise_exception (m ): 16raise jinja2 .exceptions .TemplateError (m ) 17env .globals ["raise_exception" ]= raise_exception 18tpl = env .from_string (open (template ,encoding = "utf-8" ).read ()) 19libs = os .environ .get ("LD_LIBRARY_PATH" ,"" ) 20 21def tok (text ): 22with tempfile .NamedTemporaryFile ("w" ,suffix = ".txt" ,delete = False ,encoding = "utf-8" )as f : 23f .write (text ) 24p = f .name 25r = subprocess .run ([tokenize ,"-m" ,shard ,"-f" ,p ,"--ids" ,"--no-bos" ,"--no-escape" ], 26capture_output = True ,text = True ,env = dict (os .environ ,LD_LIBRARY_PATH = libs )) 27os .unlink (p ) 28line = [l for l in r .stdout .splitlines ()if l .startswith ("[" )][- 1 ] 29return json .loads (line ) 30 31out = [] 32for mode_dir ,mode in (("answers_chat" ,"chat" ), ("answers_think" ,"thinking" )): 33d = os .path .join (ref_dir ,mode_dir ) 34for fn in sorted (os .listdir (d )): 35if fn == "summary.json" or not fn .endswith (".json" ): 36continue 37r = json .load (open (os .path .join (d ,fn ),encoding = "utf-8" )) 38b = r ["request_body" ] 39text = tpl .render (messages = b ["messages" ],tools = b .get ("tools" ),thinking_mode = mode ,reasoning_effort = None , 40add_generation_prompt = True ) 41ids = tok (text ) 42ch = r ["response" ]["choices" ][0 ] 43out .append ({"id" :fn [:- 5 ],"mode" :mode ,"ids" :ids ,"max_tokens" :b .get ("max_tokens" ,256 ), 44"expect_ids" : [t ["id" ]for t in ch ["logprobs" ]["content" ]], 45"expect_text" :ch ["message" ].get ("content" ),"finish" :ch ["finish_reason" ]}) 46mode ,fn [:- 5 ],len (ids ),"prompt tokens;" ,len (out [- 1 ]["expect_ids" ]),"expected" ) 47os .makedirs (out_dir ,exist_ok = True ) 48json .dump (out ,open (os .path .join (out_dir ,"prompts.json" ),"w" ))