okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
263c99b96
main
1"""Conversation-cache demo: two long agent-style conversations interleaved through the live server. 2 3The engine's `--conversation-cache-mib` defaults to 0 (off): every follow-up re-reads the whole 4conversation. With 32 GiB / 8 slots (our production config), a follow-up restores the parked K/V 5instead. This script runs five requests — doc A, doc B, follow-up on A, follow-up on B, second 6follow-up on A — and reports, per request, the reused tokens (`usage.prompt_tokens_details.cached_tokens`), 7the freshly read tokens and the wall time, plus the engine's own `restored/parked` log lines. 8 9python bench/results/2026-10-02-community-rtx-5090-iq3s/conversation_cache_demo.py 10""" 11from __future__import annotations 12 13import json 14import re 15import sys 16import time 17import urllib .request 18from pathlib import Path 19 20ROOT = Path (__file__ ).resolve ().parents [3 ] 21sys .path .insert (0 ,str (ROOT / "tools" )) 22from needle_bench import haystack ,CHARS_PER_TOKEN # noqa: E402 23 24URL = "http://127.0.0.1:8080/v1/chat/completions" 25MODEL = "qwen3.8-flash-next-iq3_s" 26LOG = ROOT / "strata-iq3_s.log" 27DOC_TOKENS = 60_000 28 29 30def ask (messages )-> dict : 31body = {"model" :MODEL ,"max_tokens" :128 ,"temperature" :0 , 32"chat_template_kwargs" : {"enable_thinking" :False },"messages" :messages } 33req = urllib .request .Request (URL ,data = json .dumps (body ).encode (),headers = {"Content-Type" :"application/json" }) 34t0 = time .time () 35with urllib .request .urlopen (req ,timeout = 1200 )as r : 36out = json .load (r ) 37u = out ["usage" ] 38return {"prompt_tokens" :u ["prompt_tokens" ],"cached_tokens" : (u .get ("prompt_tokens_details" )or {}).get ("cached_tokens" ,0 ), 39"completion_tokens" :u ["completion_tokens" ],"seconds" :round (time .time ()- t0 ,2 ), 40"answer" : (out ["choices" ][0 ]["message" ].get ("content" )or "" )[:120 ]} 41 42 43def main ()-> int : 44base = int (time .time ())% 20_000_000 # a fresh text window per run, printed for reproducibility 45f"haystack window base: { base } " ) 46n = int (DOC_TOKENS * CHARS_PER_TOKEN ) 47doc_a = haystack (n + base )[base :] 48doc_b = haystack (n + base + 5_000_003 )[base + 5_000_003 :] 49q = "\n\nIn three sentences: what kinds of files make up the text above?" 50fu1 = "\n\nAnd which of those file types would I edit to change compiler flags?" 51fu2 = "\n\nGive one concrete example of a line you would change there." 52 53conv_a ,conv_b ,rows = [], [], [] 54steps = ["A1 fresh doc A" ,"B1 fresh doc B" ,"A2 follow-up on A" ,"B2 follow-up on B" , 55"A3 follow-up on A again" ] 56for name in steps : 57conv = conv_a if name .startswith ("A" )else conv_b 58if name .startswith ("A1" ): 59conv .append (("user" ,doc_a + q )) 60elif name .startswith ("B1" ): 61conv .append (("user" ,doc_b + q )) 62else : 63conv .append (("user" ,fu1 if name [1 ]== "2" else fu2 )) 64msgs = list (conv ) 65mark = len (LOG .read_text (encoding = "utf-8" ,errors = "replace" )) 66r = ask ([{"role" :m [0 ],"content" :m [1 ]}for m in msgs ]) 67conv .append (("assistant" ,r ["answer" ])) 68new = [l for l in re .findall (r"strata serve: (?:conversation cache:|prompt) [^\n]*" ,LOG .read_text (encoding = "utf-8" ,errors = "replace" )[mark :])] 69r ["step" ],r ["engine_lines" ]= name ,new 70rows .append (r ) 71f" { name :24 } prompt { r [ 'prompt_tokens' ]:>7, } reused { r [ 'cached_tokens' ]:>7, } " 72f"fresh { r [ 'prompt_tokens' ] - r [ 'cached_tokens' ]:>6, } { r [ 'seconds' ]:>6.2f } s" ,flush = True ) 73Path (__file__ ).with_name ("demo.json" ).write_text (json .dumps (rows ,indent = 1 ),encoding = "utf-8" ) 74"wrote demo.json" ) 75return 0 76 77 78if __name__ == "__main__" : 79sys .exit (main ())