fix(advice): find the marker however the model formats it
The instruction asks for "ADVICE:", but the model writes "**ADVICE:**",
"## ADVICE:" or puts it in a code block. The extraction has to cope,
because leftover markdown in the advice is visible to the brewer.
The marker is now matched case-insensitively and with the decoration the
model puts around it, and the leftover formatting is stripped.
plain marker -> 'Warm it to 20C and wait.'
bold marker -> 'Warm it to 20C and wait.'
in a code block -> 'Warm it to 20C and wait.'
marker mid-text -> 'Warm it to 20C and wait.'
markdown heading -> 'Warm it to 20C and wait.'
lowercase marker -> 'Warm it to 20C and wait.'
no marker -> 'Warm it to 20C and wait.'
bold + code block -> 'Warm it to 20C and wait.'
182 tests (up from 181).
This commit is contained in:
@@ -25,6 +25,7 @@ from __future__ import annotations
|
||||
import json
|
||||
import logging
|
||||
import os
|
||||
import re
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
|
||||
@@ -271,12 +272,38 @@ def _strip_reasoning(text: str) -> str:
|
||||
working did leak in, the brewer sees a slightly rambling reply
|
||||
rather than no advice at all.
|
||||
"""
|
||||
index = text.find(_ADVICE_MARKER)
|
||||
if index == -1:
|
||||
index = _find_marker(text)
|
||||
if index is None:
|
||||
return text.strip()
|
||||
|
||||
advice = text[index + len(_ADVICE_MARKER):].strip()
|
||||
return advice or ""
|
||||
advice = text[index:].strip()
|
||||
|
||||
# The model may wrap the marker in markdown or a code block. Strip
|
||||
# the leftover formatting so the advice reads as prose.
|
||||
advice = re.sub(r"^\*\*|\*\*$", "", advice).strip()
|
||||
if advice.startswith("```"):
|
||||
advice = advice[3:]
|
||||
if advice.endswith("```"):
|
||||
advice = advice[:-3]
|
||||
if advice.startswith("##"):
|
||||
advice = advice.lstrip("#").strip()
|
||||
|
||||
return advice.strip() or ""
|
||||
|
||||
|
||||
def _find_marker(text: str) -> int | None:
|
||||
"""Where the advice starts, or ``None`` if there is no marker.
|
||||
|
||||
The marker is matched case-insensitively and with the markdown
|
||||
decoration the model might put around it - ``**ADVICE:**``,
|
||||
``## ADVICE:`` - because the instruction asks for "ADVICE:" but the
|
||||
model formats it its own way often enough to matter.
|
||||
"""
|
||||
match = re.search(r"^[\s#>\*]*advice\s*:[\s\*>]*", text,
|
||||
re.IGNORECASE | re.MULTILINE)
|
||||
if match:
|
||||
return match.end()
|
||||
return None
|
||||
|
||||
|
||||
def generate_advice(run: dict, reading: dict, history: list[dict], *,
|
||||
|
||||
Reference in New Issue
Block a user