Log prompt/generation speed of every LLM call in the Prefect log
llama.content() returns the message text and prints llama-server's timings (prompt tokens + t/s, generated tokens + t/s); used by shots and report/memory. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
d9ec9c5b01
commit
bc16b01232
@@ -40,6 +40,13 @@ def current(vision=True):
|
|||||||
|
|
||||||
def choose(vision_key, text_key): CHOICE.write_text(f"{vision_key} {text_key}")
|
def choose(vision_key, text_key): CHOICE.write_text(f"{vision_key} {text_key}")
|
||||||
|
|
||||||
|
def content(resp, what):
|
||||||
|
"""Message text of a llama-server chat response; prints its speed (lands in the Prefect log via log_prints)."""
|
||||||
|
t = resp.get("timings") or {}
|
||||||
|
if t: print(f"llm {what}: prompt {t.get('prompt_n', 0)} tok @ {t.get('prompt_per_second', 0):.0f} t/s, "
|
||||||
|
f"gen {t.get('predicted_n', 0)} tok @ {t.get('predicted_per_second', 0):.1f} t/s", flush=True)
|
||||||
|
return resp["choices"][0]["message"]["content"]
|
||||||
|
|
||||||
def up(port=PORT):
|
def up(port=PORT):
|
||||||
try: _open(f"http://127.0.0.1:{port}/health", timeout=2); return True
|
try: _open(f"http://127.0.0.1:{port}/health", timeout=2); return True
|
||||||
except Exception: return False
|
except Exception: return False
|
||||||
|
|||||||
+1
-1
@@ -40,7 +40,7 @@ def chat(system, user, max_tokens, retries=2):
|
|||||||
if e.code != 400: raise
|
if e.code != 400: raise
|
||||||
body.pop("response_format", None)
|
body.pop("response_format", None)
|
||||||
r = _open(urllib.request.Request(API, json.dumps(body).encode(), {"Content-Type": "application/json"}), timeout=1800)
|
r = _open(urllib.request.Request(API, json.dumps(body).encode(), {"Content-Type": "application/json"}), timeout=1800)
|
||||||
t = json.load(r)["choices"][0]["message"]["content"]
|
t = llama.content(json.load(r), f"report ({max_tokens} max)")
|
||||||
t = re.sub(r"<think>.*?</think>", "", t, flags=re.S)
|
t = re.sub(r"<think>.*?</think>", "", t, flags=re.S)
|
||||||
d = json.loads(t[t.find("{"):t.rfind("}") + 1])
|
d = json.loads(t[t.find("{"):t.rfind("}") + 1])
|
||||||
if isinstance(d, dict): return d
|
if isinstance(d, dict): return d
|
||||||
|
|||||||
+1
-1
@@ -45,7 +45,7 @@ def _ask(path, json_mode=True):
|
|||||||
{"type": "image_url", "image_url": {"url": "data:image/jpeg;base64," + img}}]}]}
|
{"type": "image_url", "image_url": {"url": "data:image/jpeg;base64," + img}}]}]}
|
||||||
if json_mode: body["response_format"] = {"type": "json_schema", "json_schema": {"name": "shot", "schema": SCHEMA}} if SCHEMA else {"type": "json_object"}
|
if json_mode: body["response_format"] = {"type": "json_schema", "json_schema": {"name": "shot", "schema": SCHEMA}} if SCHEMA else {"type": "json_object"}
|
||||||
req = urllib.request.Request(API, json.dumps(body).encode(), {"Content-Type": "application/json"})
|
req = urllib.request.Request(API, json.dumps(body).encode(), {"Content-Type": "application/json"})
|
||||||
return json.load(_open(req, timeout=900))["choices"][0]["message"]["content"]
|
return llama.content(json.load(_open(req, timeout=900)), f"shot {path.name}")
|
||||||
|
|
||||||
|
|
||||||
def read_shot(path):
|
def read_shot(path):
|
||||||
|
|||||||
Reference in New Issue
Block a user