ai: ground answers in a live library map, budget the context

Response-quality work, all aimed at giving the model enough context to
answer accurately:

- Every chat's system prompt now carries a live library map (today's
  date, counts, and the exact names of channels, domains, zims, and
  inventories, capped and cached 60s).  Exact spellings are in context
  before the first token, and the list_collections round-trip — minutes
  on slow hardware — is usually unnecessary.
- The request is budgeted against the model's real context: oldest
  turns are trimmed first, the current question is never dropped, and
  an over-budget turn gets a clear "start a new conversation" error
  instead of a silently truncated prompt.  cache_prompt reuses the KV
  cache for the stable prefix, which is most of the latency on a Pi.
- context_size plumbing fixed: selecting a model adopts its catalog
  default (the 4B was running at 8k instead of 16k), and llama-server
  runs --parallel 1 so the single user gets the whole context.
- temperature is configurable in ai.yaml (default 0.3 — small models
  call tools far more reliably near 0.2-0.3 than at server defaults).
- One worked tool-use example per mode is prepended to every
  conversation; small models imitate far better than they obey.
- scripts/ai_eval.py scores the assistant against a live WROLPi on
  mechanical checks (finished, called the right tool first, included a
  WROLPi link, no external URLs) so prompt changes stop being validated
  by anecdote.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Roland 2026-09-02 16:48:19 -06:00
parent 82e11cbaba
commit ced78e181f
No known key found for this signature in database
GPG key ID: 1BA620CE6C9A4680
11 changed files with 502 additions and 7 deletions

View file

@ -82,6 +82,62 @@ MODES = {
),
}
def _example(*messages) -> list:
"""A compact worked exchange (user -> tool call -> tool result -> answer) prepended to every
conversation. Small models imitate far better than they obey; one example per mode is what
stops them answering from memory or quitting after the first tool call."""
return list(messages)
def _tool_call(call_id: str, name: str, arguments: str) -> dict:
return dict(role='assistant', content=None, tool_calls=[
dict(id=call_id, type='function', function=dict(name=name, arguments=arguments))])
def _tool_result(call_id: str, content: str) -> dict:
return dict(role='tool', tool_call_id=call_id, content=content)
MODE_EXAMPLES = {
'help': _example(
dict(role='user', content='How do I tag a video?'),
_tool_call('ex_help_1', 'search_help', '{"search_str": "tag video"}'),
_tool_result('ex_help_1',
'{"results": [{"slug": "system/tags", "title": "Tags", "link": "/system/tags/",'
' "snippet": "…Tags group related content across the library…"}], "total": 1}'),
_tool_call('ex_help_2', 'get_help_doc', '{"slug": "system/tags"}'),
_tool_result('ex_help_2',
'{"content": "# Tags\\nOpen any video and press the tag button to add a tag.",'
' "next_offset": null, "total_chars": 62}'),
dict(role='assistant', content='Open any video and press the tag button to add a tag.'
' See the Tags help page: /system/tags/'),
),
'research': _example(
dict(role='user', content='Summarize the bakehouse channel'),
_tool_call('ex_res_1', 'list_collections', '{"kind": "channel"}'),
_tool_result('ex_res_1',
'{"results": [{"id": 7, "name": "Bakehouse", "kind": "channel",'
' "link": "/videos/channel/7/video"}], "total": 1}'),
_tool_call('ex_res_2', 'search_videos', '{"channel_id": 7}'),
_tool_result('ex_res_2',
'{"results": [{"id": 12, "kind": "video", "title": "Sourdough Basics",'
' "link": "/videos/12", "channel": "Bakehouse", "duration": 900}], "total": 1}'),
dict(role='assistant', content='The Bakehouse channel (/videos/channel/7/video) has one video:'
' Sourdough Basics (/videos/12), a 15 minute introduction to'
' sourdough baking.'),
),
'system': _example(
dict(role='user', content='Is everything running?'),
_tool_call('ex_sys_1', 'get_system_status', '{}'),
_tool_result('ex_sys_1',
'{"version": "0.28", "wrol_mode": false, "services": [{"name": "wrolpi-api",'
' "status": "running"}, {"name": "wrolpi-kiwix", "status": "failed"}], "errors": []}'),
dict(role='assistant', content='Almost: wrolpi-kiwix has failed; everything else is running.'
' I can read its logs to find out why.'),
),
}
# Suggestion chips shown by the Chat tab; small models do much better when the opening message
# matches what the mode's prompt was tuned for.
MODE_SUGGESTIONS = {