-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuniversal_brain_chat.py
More file actions
2351 lines (2119 loc) · 141 KB
/
Copy pathuniversal_brain_chat.py
File metadata and controls
2351 lines (2119 loc) · 141 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""Chat-style UI (single-line input + history) for the local "Universal Brain" stack.
**Default:** generative LM + TinyModel encoder + FAQ RAG + SQLite memory. **`--lm-only`**
turns off encoder/RAG/memory.
**Natural language:** the model **routes** each line to an intent (summarize, retrieve, remember,
plain chat, …). Slash commands (`/help`, `/status`, …) still work as shortcuts.
Requirements:
pip install -r optional-requirements-horizon2.txt
Examples:
python scripts/universal_brain_chat.py
python scripts/universal_brain_chat.py --no-smart-route
python scripts/universal_brain_chat.py --lm-only --smoke
Say what you want in plain language, or type `/help`.
"""
from __future__ import annotations
import argparse
import json
import os
import sqlite3
import sys
import uuid
import warnings
from pathlib import Path
from typing import Any
# Windows: avoid OpenMP/MKL oversubscription and duplicate CRT issues that can
# segfault during large `from_pretrained` CPU loads (common with torch+transformers).
if sys.platform == "win32":
os.environ.setdefault("OMP_NUM_THREADS", "1")
os.environ.setdefault("MKL_NUM_THREADS", "1")
os.environ.setdefault("KMP_DUPLICATE_LIB_OK", "TRUE")
os.environ.setdefault("TOKENIZERS_PARALLELISM", "false")
import torch
if sys.platform == "win32":
torch.set_num_threads(1)
try:
torch.set_num_interop_threads(1)
except RuntimeError:
pass
_scripts = Path(__file__).resolve().parent
_REPO = _scripts.parent
DEFAULT_MEMORY_DB = str(_REPO / ".tmp" / "ub_chat_memory.sqlite")
if str(_scripts) not in sys.path:
sys.path.insert(0, str(_scripts))
def _load_dotenv_if_present(root: Path) -> None:
"""Load ``root / .env`` into ``os.environ`` without overriding existing keys (stdlib only)."""
p = root / ".env"
if not p.is_file():
return
try:
text = p.read_text(encoding="utf-8")
except OSError:
return
for line in text.splitlines():
s = line.strip()
if not s or s.startswith("#"):
continue
if s.startswith("export "):
s = s[7:].strip()
if "=" not in s:
continue
k, _, v = s.partition("=")
k, v = k.strip(), v.strip()
if not k or k in os.environ:
continue
if len(v) >= 2 and v[0] == v[-1] and v[0] in "\"'":
v = v[1:-1]
os.environ[k] = v
from horizon2_core import ( # noqa: E402
DEFAULT_CHAT_SYSTEM,
DEFAULT_INSTRUCTION_MODEL,
SMOKE_MODEL_ID,
LoadedLM,
build_user_prompt,
format_for_model,
generate_chat_reply,
generate_completion,
load_causal_lm,
pick_device,
resolve_instruction_model,
)
from horizon3_store import ( # noqa: E402
clear_session,
connect,
export_scope_json,
forget_scope,
init_schema,
list_for_scope,
put,
)
from google_cse_client import ( # noqa: E402
format_cse_hits_markdown,
google_cse_search,
heuristic_suggests_web_search,
read_google_cse_settings,
)
from nl_controls import analyze_embedded_prompt_signals, parse_control_action # noqa: E402
from rag_faq_smoke import _pick_model, hybrid_retrieve, load_chunks # noqa: E402
from tinymodel_runtime import TinyModelRuntime # noqa: E402
HELP_TEXT = """**How to use**
- **Normal language:** ask in plain English (or mixed); the app **infers** what you want (summarize, search FAQ, save a note, etc.). Longer prompts may also **imply** reply shape for that turn only (for example trade-off questions → Pros/Cons layout or flowing prose comparison, “in a table” → markdown table preference, **no tables / tabular format in prose** → table style prefer or avoid, “answer in Spanish” → reply language, **code only** → code-first output, **explain the code / not code only in prose** → code with explanation, **pseudocode vs runnable code in prose** → algorithm layout, **cite your sources / no source links in prose** → citation style, **rank options in priority order in prose** → ranked_options, **decision matrix / criteria-as-rows in prose** → decision_matrix, **build vs buy / make vs buy in prose** → build_vs_buy, **one-pager / single-page executive brief in prose** → one_pager, **status report / weekly program update in prose** → status_report, **action plan with owners and due dates in prose** → action_plan, **RACI matrix / role assignment in prose** → raci, **stakeholder map / influence-interest in prose** → stakeholder_map, **exactly N options/alternatives in prose** → options_n=N, **Mermaid/flowchart vs no diagrams in prose** → diagram layout, **risks/downside vs benefits-first section order in prose** → risks_first / benefits_first, **risks and mitigations / risk register in prose** → risks_mitigations, **rewrite/polish my draft in prose** → revise_draft, **write/draft an email in prose** → email_format, **formal business letter in prose** → letter_format, **press release / media announcement in prose** → press_release, **release notes / changelog in prose** → release_notes, **operational runbook / on-call playbook in prose** → runbook_format, **job aid / quick reference cheat sheet in prose** → job_aid, **meeting agenda / timeboxed run-of-show in prose** → meeting_agenda, **before/after or track-changes on my draft in prose** → revise_diff, **don’t mention X / avoid discussing Y in prose** → topic_guard, **must cover / include a section on Z in prose** → topic_must, **STAR / PREP / IRAC format in prose** → frame_star / frame_prep / frame_irac, **SWOT analysis in prose** → swot, **PESTLE macro-environment analysis in prose** → pestle, **cost-benefit / CBA in prose** → cost_benefit, **open questions / TBD section in prose** → open_questions, **best/base/worst case scenario analysis in prose** → scenario_cases, **blameless postmortem format in prose** → postmortem, **sprint retrospective / retro format in prose** → sprint_retro, **user story / As a I want So that in prose** → user_story, **definition of done / DoD criteria in prose** → definition_of_done, **five whys / 5 whys root cause in prose** → five_whys, **fishbone / Ishikawa cause-and-effect in prose** → fishbone, **checklist / tick-box format in prose** → checklist layout, **in under N words** → length cap, **be brief / more detail in prose** → verbosity brief or detailed, **hints only / don’t give the full solution** → guided discovery, **give me the full solution in prose** → full_solution (not hints), **red team / sanity check my plan** → challenge-style pushback, **be supportive / assume good intent on my plan** → supportive coaching, **don’t remember this / off the record** → ephemeral hint, **screen reader friendly / WCAG** → accessibility layout hint, **ELI5 / lay audience in a long question** → beginner audience, **assume I'm technical / expert depth in prose** → technical audience, **board-ready / Slack-casual wording** → formal or casual register, **valid JSON / return JSON in prose** → JSON output mode, **plain text only / no JSON in prose** → plain output format, **don’t guess / stick to facts in prose** → strict speculation, **brainstorm freely / wild ideas in prose** → creative speculation, **TLDR first / BLUF in prose** → summary-first open, **lead with your recommendation in prose** → recommendation_first, **go/no-go gate verdict in prose** → go_no_go, **summary at the end / closing recap in prose** → summary_last, **answer directly / skip the summary in prose** → direct opening, **FAQ direct quotes vs paraphrase-only in prose** → quote style for excerpts, **emoji ok vs no emoji in prose** → emoji style, **FAQ-only vs FAQ-plus-general-knowledge in prose** → FAQ grounding, **show work vs final-answer-only in prose** → math detailing, **state assumptions / limitations / caveats** in prose → transparent confidence tone, **be decisive / don’t hedge in prose** → assertive confidence tone, **curl/bash/kubectl in prose** → runnable commands, **conceptual only / no commands in prose** → conceptual actionability, **bullet points vs plain paragraphs in prose** → reply format, **step-by-step vs continuous procedure prose in long prompts** → step style, **concrete / worked / toy example in prose** → richer examples, **example-free / skip examples in prose** → sparser examples, **define terms first / intuition or big-picture first in prose** → explanation order, **no questions at the end / suggest next steps in prose** → closing style, **ask questions before answering / answer without clarifiers in prose** → clarify-first mode, **markdown section headings vs flat prose in long prompts** → section layout, **analogy vs literal-only in long prompts** → analogy style, **bold key terms vs minimal bold in long prompts** → term emphasis, **spell out acronyms vs terse acronyms in long prompts** → acronym style, **err on the side of safety vs ship-fast pragmatism in long prompts** → risk posture, **fenced code blocks vs inline-only snippets in long prompts** → code block style) — see *Brain trace* **`prompt_signals:`** when detected.
- **Session controls (say it in chat, no slash command):**
- *What is my current scope?*, *Show my session settings* -> prints scope + toggles (FAQ context, routing, trace)
- *Start a new private session*, *Begin a fresh scope* -> generates a **new memory scope key** so notes are isolated from the shared default demo scope
- *Switch to scope my-team-123* / *Use session demo-key* -> set the Horizon 3 **`scope_key`** from chat (ASCII id)
- *Be brief* / *More detail please* / *Use bullet points* / *No bullets, plain paragraphs* -> soft **reply-style** hints (injected into the assistant system context; short control lines only)
- *Strict FAQ* / *FAQ only* / *Stick to the FAQ* vs *Relaxed FAQ* / *FAQ plus general knowledge* vs *Balanced FAQ* / *Normal FAQ* -> **FAQ grounding** hints for how tightly to treat injected FAQ excerpts vs general knowledge
- *Explain simply* / *ELI5* / *I'm a beginner* vs *Expert mode* / *Assume I'm technical* vs *Normal explanation level* -> **audience depth** hints (simple vs technical vs default)
- *TLDR first* / *Lead with a summary* vs *No TLDR* / *Answer directly* vs *Default answer structure* -> **answer opening** style (short upfront summary vs dive straight in)
- *Step by step* / *Numbered steps* vs *No numbered steps* / *Continuous prose* vs *Default step style* -> **procedure layout** (numbered steps vs flowing paragraphs)
- *Flag your assumptions* / *Be explicit about uncertainty* vs *Be decisive* / *Don't hedge* vs *Reset uncertainty* -> **confidence tone** hints
- *Suggest next steps* / *Offer follow-up questions* vs *No follow-up questions* / *No questions at the end* vs *Default follow-ups* -> **closing** style at end of answers
- *Definitions first* / *Define terms first* vs *Intuition first* / *Big picture first* vs *Default explanation order* -> **concept order** in explanations
- *Include examples* / *Use concrete examples* vs *Skip examples* / *No examples unless I ask* vs *Default examples* -> **example density**
- *Use pros and cons* / *Pros and cons sections* vs *Compare in flowing prose* / *No pros and cons sections* vs *Default comparison style* -> **comparison layout** for trade-offs
- *Formal tone* / *Professional register* vs *Casual tone* / *Speak casually* vs *Default tone* -> **writing register**
- *Use code fences* / *Fenced code blocks* vs *Inline code only* / *No fenced code blocks* vs *Default code formatting* -> **markdown code layout**
- *Use analogies* / *Analogies when helpful* vs *No analogies* / *Literal explanations only* vs *Default analogy style* -> **analogy / metaphor** usage
- *Spell out acronyms* / *Expand acronyms on first use* vs *Assume I know acronyms* / *Don't expand acronyms* vs *Default acronym style* -> **acronym verbosity**
- *Ask clarifying questions first* / *Clarify first* vs *No clarifying questions* / *Just answer without questions* vs *Default clarify mode* -> whether the assistant should ask for missing info before answering
- *No speculation* / *Stick to high confidence only* vs *Brainstorm freely* / *Wild ideas ok* vs *Default speculation* -> how strictly to avoid guessing vs allow ideation
- *Show your work* / *Show the derivation* vs *Final answer only* / *No derivation* vs *Default math detail* -> how much intermediate reasoning to show for math-like answers
- *Answer in JSON* / *JSON output* vs *Plain text only* / *No JSON* vs *Default output format* -> structured output preference
- *Be risk averse* / *Err on the side of safety* vs *Be pragmatic* / *Optimize for speed* vs *Default risk posture* -> conservative vs practical recommendations
- *Give me runnable commands* / *Make it actionable* vs *No commands* / *Conceptual only* vs *Default actionability* -> how command-heavy responses should be
- *Quote the FAQ excerpts* / *Use direct quotes* vs *Paraphrase only* / *Don't quote excerpts* vs *Default quote style* -> quoting vs paraphrasing when relying on injected excerpts
- *Use tables* / *Tabular format* vs *No tables* / *Avoid tables* vs *Default table style* -> whether markdown tables are preferred
- *Use emoji* / *Emoji ok* vs *No emoji* / *Avoid emoji* vs *Default emoji style* -> light **emoji** usage in answers
- *Use section headings* / *Organize with headings* vs *No section headings* / *Flat answer* vs *Default section headings* -> **markdown headings** vs flat prose
- *Bold key terms* / *Highlight important terms* vs *Minimal bold* / *Don't overuse bold* vs *Default emphasis* -> **inline bold** for key phrases vs sparse formatting
- *Challenge my assumptions* / *Play devils advocate* vs *Be supportive* / *Assume good intent* vs *Default counterpoints* -> how much to **push back** vs stay encouraging
- *Reset reply style* -> back to defaults for length + prose + balanced FAQ grounding + audience + opening + steps + confidence tone + follow-ups + concept order + examples + comparisons + register + code layout + analogy + acronym style + clarify + speculation + math detail + output format + risk posture + actionability + quote style + table style + emoji + section headings + term emphasis + counterpoints
- *Export my memories*, *Download my notes as JSON* -> returns a Horizon 3 export blob for **this Space session scope**
- *Delete all my memories for this chat* / *Erase everything you stored about me here* -> **forget-scope** wipe for this scope (**long-term + session** rows)
- *Clear my session notes* -> wipes **session** notes only
- *Turn off the FAQ context*, *Disable RAG snippets*, *Turn FAQ back on* -> toggles whether FAQ excerpts are injected into the chat system context
- *Turn off smart routing*, *Go back to normal chat only* -> disables the JSON intent router (slash commands still work)
- *Show the brain trace*, *Hide debug trace* -> toggles the optional *Brain trace* footer on replies
- **Shortcuts:** `/help`, `/status`, `/classify`, `/retrieve`, **`/web <query>`** (Google Programmable Search when `GOOGLE_CSE_API_KEY` + `GOOGLE_CSE_CX` are set), `/summarize`, `/reformulate`, `/grounded q ||| ctx`, `/remember`, `/session`, `/memories`, `/clear-session`, **`/similarity a ||| b`**, **`/embed` / `/embedding`**, **`/nearest q ||| c1 ||| c2`**.
**Intents the router understands** (examples, not exact wording):
- Ordinary chat / questions
- **Summarize** this text — provide the passage in the same message
- **Rewrite** professionally / rephrase
- **Answer using only** these facts — include both facts and question
- **Search** the FAQ / **find** in the knowledge base
- **Live web** (news, prices, “latest …”, fact-checking) — router uses **web_search**; with Google CSE configured, the server may also **auto-run** web search when your wording implies it (see brain trace **`+auto`**). Disable with **`--no-auto-web`** or env **`NO_AUTO_WEB=1`** on your own deployment.
- **Classify** (topic model) this paragraph
- **Similarity:** are these two snippets close in meaning? (encoder cosine)
- **Embedding** stats for a passage (dimension, norm, preview)
- **Nearest** among several options: which candidate is closest to a query? (`query ||| opt1 ||| opt2 …`)
- **Remember** / note / store: **long-term** vs **this session only**
- **Show** saved notes; **clear** session notes
- **Status** of loaded models
**Classifier** uses AG News–style labels on default Hub weights (World, Business, Sports, Sci/Tech).
If routing misfires, try rephrasing or use a slash command; **`--no-smart-route`** disables inference (chat only, plus `/…`)."""
# Shown under the chat + controls in the Gradio UI (Hugging Face Space and local).
GRADIO_INSTRUCTIONS_MARKDOWN = """### About this Space
**Universal Brain** is a **text-in / text-out** assistant: (1) **generative instruct LM** (default **SmolLM2-360M-Instruct**, override **`HORIZON2_MODEL`**), (2) **TinyModel1** encoder (**4 topic labels** + **embeddings**), (3) **FAQ hybrid RAG**, (4) **scoped SQLite memory**, (5) **JSON intent routing** (summarize, retrieve, web, memory, classify, …), (6) optional **Google web search** (`GOOGLE_CSE_API_KEY` + `GOOGLE_CSE_CX`), (7) **short session control phrases** + **embedded prompt signals** in long chat (see **`prompt_signals:`** in the brain trace). First CPU start can take several minutes while weights download.
#### What it can do (summary)
| Area | Capacity |
| --- | --- |
| **Chat & tools** | Summarize, rewrite, grounded Q&A (`|||` facts), FAQ search, **live web** (if configured), classify, similarity, embeddings, nearest-option, **/status**, memory CRUD — via natural language or **`/…`** shortcuts. |
| **Encoder** | Soft **topic hint** + trace line **`classify:…`**; **`/classify`** for full label probabilities. |
| **RAG** | Injects top FAQ **chunks**; tune strictness with phrases like *Strict FAQ* (see `/help`). |
| **Memory** | Long-term + session notes; **scope** isolation phrases for demos; export / forget from chat. |
| **Session controls** | Short phrases (no slash): scope, trace, FAQ/routing toggles, reply style — *Be brief*, *Strict FAQ*, *Start a new private session*, *Reset reply style*; full list via **`/help`**. |
| **Embedded signals (long chat)** | **40+** one-turn cues from wording: layout (pros/cons, tables, steps, bullets), code modes, decisions (`ranked_options`, `options_n=N`, checklist), diagrams, STAR/PREP/IRAC, risks/benefits order, revise draft, topic guardrails, language, tone, FAQ/web citation style, … — footer **`prompt_signals:`** when trace is on; step-by-step table **below**. |
| **Limits** | Small models can **hallucinate** or miss nuance; FAQ/web only **constrain** answers when relevant snippets exist. **Not multimodal** here. Shared default **memory scope** is not private auth. |
---
### Using the layout
1. **Conversation** — scroll the transcript; replies may end with a *Brain trace* line (classify / RAG / memory hints) if that toggle is on.
2. **Message box** — type a line or paragraph; press **Send** or submit with Enter.
3. **Clear** — wipes the visible chat and the input (does not delete long-term memory unless you use the forget commands below).
---
### Testing embedded prompt signals (this Space)
These behaviors apply when your line is handled as **normal chat** (not a short dedicated control like *Be brief*). The app scans your wording and adds **one-turn** system hints.
**How to test (two sends):**
1. Send **Show the brain trace** as its **own short line** (do **not** combine it with your question in one message).
2. Send your **long** test prompt as a **separate** message.
3. Scroll to the bottom of the assistant reply and look for **`prompt_signals:`** in the *Brain trace* footer (e.g. **`prompt_signals:sprint_retro`** or **`prompt_signals:stakeholder_map`**).
**If you only see** `classify:…` / `RAG:…` **without** `prompt_signals:…`, the live Space may be running an older build — redeploy via GitHub Actions **Deploy versioned space artifact to Hugging Face** after merging, then hard-refresh the Space.
| Goal | What to type (examples) | What to look for |
| --- | --- | --- |
| Comparison: pros/cons | In a **long** message, ask for **tradeoffs**, **pros and cons**, **compare X vs Y**, or **advantages and disadvantages** between concrete options (avoid mixing with **no pros and cons** / **flowing prose comparison** in the same line). | **`comparison_frame=pros_cons`** in **`prompt_signals:`**; reply should use **Pros** / **Cons** sections |
| Comparison: narrative prose | In a **long** comparison question, ask for **flowing prose**, **narrative comparison**, **prose comparison only**, or **no pros and cons sections** (avoid mixing with **pros and cons** / **tradeoffs** layout cues in the same line). | **`comparison_frame=narrative`** in **`prompt_signals:`**; reply should weave the comparison in continuous prose |
| Ranked options / priority order | In a **long** decision question naming **options, vendors, tools, or alternatives**, ask to **rank them**, **in order of priority**, **top 3 picks**, **best to worst**, or **which option first** (avoid mixing with **no ranking** / **order doesn’t matter** in the same line). | **`ranked_options`** in **`prompt_signals:`**; reply should order choices with clear 1-2-3 priority, not treat all as equal |
| Decision matrix (criteria × options) | In a **long** vendor/tool comparison, ask for a **decision matrix**, **comparison matrix**, **feature matrix**, **criteria as rows and options as columns**, or to **score each option against criteria** (avoid mixing with **no decision matrix** / **not a matrix** in the same line). | **`decision_matrix`** in **`prompt_signals:`**; reply should include a markdown table with criteria rows and option columns |
| Build vs buy (in-house vs vendor) | In a **long** technology decision, ask for a **build vs buy**, **make vs buy**, **build in-house vs vendor/SaaS**, or **custom build vs commercial** analysis (avoid mixing with **no build vs buy** / **skip build vs buy** in the same line). | **`build_vs_buy`** in **`prompt_signals:`**; reply should use **Build (in-house)** and **Buy (vendor/SaaS)** sections plus a short recommendation |
| One-pager (executive brief) | In a **long** leadership or steering-committee update, ask for a **one-pager**, **one-pager format**, **single-page brief**, or **one-page executive memo** with scannable sections (avoid mixing with **no one-pager** / **skip one-pager**; also avoid **BLUF** / **executive summary first** or **summary at the end** in the same line). | **`one_pager`** in **`prompt_signals:`**; reply should use **Title / Purpose**, **Context**, **Problem / Opportunity**, **Recommendation**, **Key points**, **Next steps**, **Risks / dependencies** |
| Status report (periodic update) | In a **long** program or project update, ask for a **status report**, **status report format**, **weekly status update**, or **RAG status** with highlights and blockers (avoid mixing with **no status report** / **skip status report format**; use **One-pager** for a single-page executive brief). | **`status_report`** in **`prompt_signals:`**; reply should use **Reporting period**, **Overall status**, **Highlights**, **Blockers**, **Next period focus**, **Risks / asks** |
| Action plan (owners & dates) | In a **long** rollout or program plan, ask for an **action plan**, **action plan format**, **who does what by when**, or **owners and due dates** for workstreams (avoid mixing with **no action plan** / **skip action plan**; use **checklist** row instead if you want `- [ ]` tick boxes). | **`action_plan`** in **`prompt_signals:`**; reply should use a table with **Action**, **Owner**, **Due / target date** (not checkbox checklists) |
| RACI matrix (role assignment) | In a **long** project or rollout plan, ask for a **RACI matrix**, **RACI chart**, or **Responsible / Accountable / Consulted / Informed** role table for tasks or workstreams (avoid mixing with **no RACI** / **skip RACI** in the same line). | **`raci`** in **`prompt_signals:`**; reply should include a table with **R**, **A**, **C**, **I** columns |
| Stakeholder map (influence & interest) | In a **long** change or rollout plan, ask for a **stakeholder map**, **stakeholder analysis**, **influence-interest matrix**, or **power-interest grid** for key groups (avoid mixing with **no stakeholder map** / **skip stakeholder analysis**; use **RACI** if you want task R/A/C/I roles). | **`stakeholder_map`** in **`prompt_signals:`**; reply should include a table with **Stakeholder**, **Interest**, **Influence**, **Key concerns**, **Engagement approach** |
| Fixed option count | In a **long** decision or brainstorming message, ask for **exactly three options**, **give me 5 alternatives**, **list four distinct approaches**, or **top 2 picks** only (avoid mixing two different counts like **3 options** and **5 options** in one line). | **`options_n=3`** (or another N) in **`prompt_signals:`**; reply should present exactly that many labeled options |
| Diagram / flowchart | In a **long** architecture or flow question, ask for a **Mermaid diagram**, **flowchart**, **sequence diagram**, or **ASCII diagram**. **Or** say **no diagrams**, **text only**, **without flowcharts**, **don't use Mermaid** (avoid mixing both in one line). | **`diagram`** or **`no_diagram`** in **`prompt_signals:`**; reply should include or omit a visual diagram block |
| Risks first vs benefits first | In a **long** plan, pitch, or rollout question, ask to **start with risks**, **downsides first**, **what could go wrong first**, or **cons before pros**. **Or** ask for **benefits first**, **upsides first**, **lead with the positives**, **pros before cons** (avoid mixing both in one line; distinct from **risk posture** safe vs pragmatic). | **`risks_first`** or **`benefits_first`** in **`prompt_signals:`**; reply should lead with downsides or upsides accordingly |
| Risks and mitigations (paired) | In a **long** rollout, security, or change plan, ask for **risks and mitigations**, a **risk register**, **mitigation for each risk**, or **paired risk-mitigation** bullets (avoid mixing with **risks only** / **skip mitigations** in the same line). | **`risks_mitigations`** in **`prompt_signals:`**; reply should pair each key risk with a concrete mitigation step |
| Revise / polish draft | In a **long** message that includes **your draft** (email, memo, Slack post, etc.), ask to **rewrite**, **polish**, **proofread**, or **make it more professional/concise**—paste the draft in the same line or label it **Draft:** / **here’s my draft** (avoid mixing with **don’t rewrite** / **keep my wording unchanged**). | **`revise_draft`** in **`prompt_signals:`**; reply should return an improved version of your text, not a generic essay |
| Email format (compose) | In a **long** message, ask to **write an email**, **draft an email to** someone, **compose a follow-up email**, or use **email format** with **Subject** and **Greeting** (avoid mixing with **no email format** / **not as an email**; use **Revise / polish draft** if you are polishing pasted copy). | **`email_format`** in **`prompt_signals:`**; reply should use **Subject:**, **Greeting**, body paragraphs, and **Sign-off** |
| Letter format (formal business) | In a **long** correspondence prompt, ask to **write a letter**, **draft a formal letter**, **business letter format**, or **letter to** an office/agency with **Date**, **To**, and **Salutation** (avoid mixing with **no letter format** / **not as a letter**; use **Email format** for Subject-line email layout). | **`letter_format`** in **`prompt_signals:`**; reply should use **Date:**, **To:**, **Salutation**, body, **Closing**, and **Signature** |
| Press release (media / PR) | In a **long** launch or announcement prompt, ask for a **press release**, **press release format**, **media release**, or **news release** with headline and dateline for journalists (avoid mixing with **no press release** / **skip press release format**; use **Email format** or **Letter format** for correspondence). | **`press_release`** in **`prompt_signals:`**; reply should use **FOR IMMEDIATE RELEASE**, **Headline**, **Dateline**, **Lead**, **Body**, **About [Company]**, **Media contact** |
| Release notes (changelog) | In a **long** product or engineering prompt, ask for **release notes**, **release notes format**, **changelog**, or **what's new** for a version with features, fixes, and upgrade notes (avoid mixing with **no release notes** / **skip release notes format**; use **Press release** for journalist-facing announcements). | **`release_notes`** in **`prompt_signals:`**; reply should use **Version**, **Release date**, **Summary**, **What's new**, **Improvements**, **Bug fixes**, **Breaking changes**, **Upgrade notes** |
| Runbook (ops / on-call) | In a **long** SRE or on-call prompt, ask for a **runbook**, **runbook format**, **operational runbook**, or **on-call playbook** with procedure and rollback steps (avoid mixing with **no runbook format** / **skip runbook format**; use **Postmortem** for after-action incident write-ups). | **`runbook_format`** in **`prompt_signals:`**; reply should use **Purpose**, **Prerequisites**, **Procedure**, **Verification**, **Rollback**, **Escalation** |
| Job aid (quick reference) | In a **long** training or frontline workflow prompt, ask for a **job aid**, **job aid format**, **quick reference card**, **cheat sheet**, or **performance support** guide for a task (avoid mixing with **no job aid** / **skip job aid format**; use **Runbook** for on-call ops with rollback/escalation). | **`job_aid`** in **`prompt_signals:`**; reply should use **Task / purpose**, **When to use**, **Quick steps**, **Tips & reminders**, **Common mistakes**, **Need help?** |
| Meeting agenda (timeboxed) | In a **long** sync or workshop prompt, ask for a **meeting agenda**, **agenda format**, **timeboxed agenda**, or **run-of-show** for an upcoming session (avoid mixing with **no meeting agenda** / **skip agenda format**; use **Action plan** if you want owner/due-date tables). | **`meeting_agenda`** in **`prompt_signals:`**; reply should use **Meeting title**, **Objective**, **Agenda** (timeboxed items), **Pre-reads**, **Decisions needed** |
| Revise with before/after diff | In a **long** revise message with your draft pasted, also ask for **before and after**, **show what changed**, **track changes**, **side by side**, or **diff format** (avoid mixing with **no diff** / **inline revision only**). | **`revise_diff`** in **`prompt_signals:`** (often with **`revise_draft`**); reply should label **Before** / **After** or mark edits clearly |
| Topic guardrails (omit subjects) | In a **long** question, say **don’t mention**, **avoid discussing**, **steer clear of**, or **no discussion of** a topic (e.g. pricing, competitors). Avoid mixing with **make sure to mention** / **must cover** mandatory topics in the same line. | **`topic_guard`** in **`prompt_signals:`**; reply should respect the omitted subjects |
| Required topics (must cover) | In a **long** question, say **make sure to mention**, **must cover**, **include a section on**, **don’t skip discussing**, or **address the topic of** specific subjects (e.g. security, SLA, migration). Avoid mixing with **don’t mention** / **avoid discussing** omit cues in the same line. | **`topic_must`** in **`prompt_signals:`**; reply should include each required topic with clear headings or bullets |
| Answer scaffold (STAR / PREP / IRAC) | In a **long** interview, case, or writing prompt, ask for **STAR format**, **PREP format**, or **IRAC format** (avoid naming two frameworks in one line). | **`frame_star`**, **`frame_prep`**, or **`frame_irac`** in **`prompt_signals:`**; reply should use that heading scaffold |
| SWOT analysis | In a **long** strategy or product question, ask for a **SWOT analysis**, **SWOT format**, or **strengths, weaknesses, opportunities, and threats** breakdown for one initiative (avoid mixing with **no SWOT** / **skip SWOT** in the same line). | **`swot`** in **`prompt_signals:`**; reply should use **Strengths**, **Weaknesses**, **Opportunities**, **Threats** headings |
| PESTLE analysis | In a **long** market-entry or policy question, ask for a **PESTLE analysis**, **PESTLE format**, or **political, economic, social, technological, legal, and environmental** factors (avoid mixing with **no PESTLE** / **skip PESTLE** in the same line). | **`pestle`** in **`prompt_signals:`**; reply should use **Political**, **Economic**, **Social**, **Technological**, **Legal**, **Environmental** headings |
| Cost-benefit analysis | In a **long** business-case or project question, ask for a **cost-benefit analysis**, **costs and benefits breakdown**, or to **weigh costs against benefits** (avoid mixing with **no cost-benefit** / **skip CBA** in the same line). | **`cost_benefit`** in **`prompt_signals:`**; reply should use **Costs**, **Benefits**, and a brief **Net assessment** |
| Open questions (TBD / unknowns) | In a **long** plan or memo, ask for an **open questions section**, **list what's still unknown**, **outstanding questions**, **TBD items**, or **information gaps** to flag (avoid mixing with **no open questions** / **skip the open questions section** in the same line). | **`open_questions`** in **`prompt_signals:`**; reply should end with an **Open questions** bullet list of unresolved unknowns—not stock “anything else?” closers |
| Best / base / worst case scenarios | In a **long** forecast or strategy question, ask for **best case, base case, and worst case**, a **scenario analysis**, or **optimistic / realistic / pessimistic** outcomes (avoid mixing with **no scenarios** / **skip scenario analysis** in the same line). | **`scenario_cases`** in **`prompt_signals:`**; reply should use **Best case**, **Base case**, **Worst case** headings with bullets under each |
| Length cap | End your question with **in under 80 words** or **at most 3 sentences**. | **`len_cap=80w`** or **`len_cap=3s`** in **`prompt_signals:`** (trace tag); the model should stay near that cap |
| Reply length (brief vs detailed) | In a **long** message, ask to **be brief**, **keep it short**, **concise replies**, **just the essentials**, etc. **Or** say **more detail**, **go deeper**, **explain thoroughly**, **comprehensive explanation** (avoid mixing both in one line; distinct from an exact **in under N words** cap). | **`verbosity=brief`** or **`verbosity=detailed`** in **`prompt_signals:`**; reply should stay short or go deeper accordingly |
| Code-only | Ask for a tiny snippet and add **code only, no explanation** (or **just the code**). | **`code_only`** in **`prompt_signals:`**; reply should be mostly a fenced code block |
| Code + explanation | In a **long** coding question, ask to **explain what the code does**, **walk me through the snippet**, **code with comments**, **show the code and explain each part**, or **not code only** (avoid mixing with **code only** / **just the code** in the same line). | **`code_explained`** in **`prompt_signals:`**; reply should include a fenced snippet **and** a concise walkthrough |
| Pseudocode vs runnable | In a **long** algorithm question, ask for **pseudocode**, **language-agnostic algorithm**, or **not runnable code**. **Or** ask for **runnable**, **executable**, **working code**, or **copy-paste code** (avoid mixing both in one line; also distinct from **code only** / **code explained**). | **`pseudocode`** or **`runnable_code`** in **`prompt_signals:`**; reply should stay abstract or be concrete executable code accordingly |
| Reply language | Ask for the answer **in spanish** (or another language) in the same line as your question. | **`language`** in **`prompt_signals:`** |
| Tables prefer vs avoid | In a **long** message, ask for a summary **in a markdown table**, **tabular format**, **rows and columns**, etc. **Or** say **no tables**, **avoid tables**, **without a table**, **no markdown tables** (avoid mixing both in one line). | **`table_style=prefer`** or **`table_style=avoid`** in **`prompt_signals:`**; reply should use or skip markdown tables accordingly |
| Numbered steps vs continuous prose | In a **long** how-to message, ask **step by step**, **walk me through**, **numbered steps**, or a **how to install/configure** style question. **Or** say **no numbered steps**, **continuous prose only**, **prose without steps**, **explain as connected paragraphs** (avoid mixing both in one line). | **`step_style=numbered`** or **`step_style=continuous`** in **`prompt_signals:`**; reply should use numbered steps or flowing prose accordingly |
| Bullets vs prose | In a **long** message, ask for **bullet points**, **use bullets**, **bulleted list**, **format as bullets**, etc. **Or** say **no bullets**, **plain paragraphs**, **prose only**, **avoid bullet lists** (avoid mixing both in one line). | **`reply_format=bullets`** or **`reply_format=prose`** in **`prompt_signals:`**; reply should list points or stay in paragraphs accordingly |
| Checklist (tick boxes) | In a **long** plan or rollout question, ask for a **checklist format**, **action-item checklist**, **tick-box list**, or **markdown checkboxes** (`- [ ]`). **Or** say **no checklist**, **not a checklist**, **don’t use checkboxes** (avoid mixing both in one line). | **`checklist`** or **`no_checklist`** in **`prompt_signals:`**; reply should use or avoid `- [ ]` task lines |
| Guided discovery (hints / Socratic) | Ask a **how / why** question and say you want **hints only** or **don’t give me the full solution yet** (keep the message substantive; avoid mixing with **give me the full solution** in the same line). | **`guided`** in **`prompt_signals:`**; first reply should skew toward questions and nudges |
| Full solution (not hints) | On a **how / why / solve** problem, ask to **give me the full solution**, **complete solution now**, **spell out the full solution**, or **I’m stuck—show the entire solution** (avoid mixing with **hints only** in the same line). | **`full_solution`** in **`prompt_signals:`**; reply should be a complete worked answer, not hint-only |
| Red-team / critique | In one paragraph, describe a **plan or design** and ask for a **red team**, **sanity check**, **what am I missing**, or **devil’s advocate** review (not a one-line control). | **`counterpoint_tone=challenge`** inside **`prompt_signals:`**; reply should stress-test assumptions |
| Supportive coaching | In one paragraph, describe a **plan, pitch, or idea** and ask to **be supportive**, **assume good intent**, **encourage my proposal**, **gentle feedback**, or **avoid harsh criticism** (not a one-line control; avoid mixing with red-team wording in the same line). | **`counterpoint_tone=supportive`** in **`prompt_signals:`**; reply should coach with constructive next steps, not harsh critique |
| Ephemeral / no memory | Say **off the record**, **don’t remember this**, **no memory for this**, or **don’t log this** in the same message as your question (demo: shared Space scopes are not true secrecy). | **`ephemeral`** in **`prompt_signals:`**; assistant should avoid pushing `/remember` for that content |
| Accessibility / screen readers | Ask for a **screen reader friendly** or **WCAG-aware** answer, or say the write-up is **for blind readers** / **for NVDA users** in a full sentence (not a one-word ping). | **`a11y`** in **`prompt_signals:`**; reply should favor linear structure, headings, and non-table-only facts |
| Beginner / ELI5 in context | In a **longer** question (not a one-line control), ask for **ELI5**, **explain like I'm five**, **total beginner**, **lay audience**, **no technical background**, etc., plus a normal **what/why/how** ask. | **`audience=simple`** in **`prompt_signals:`**; reply should use plain language and minimal jargon |
| Technical / expert audience | In a **longer** question (not a one-line control), say you're a **technical audience**, **assume I'm technical**, want a **deep technical** or **internals-focused** explanation, **skip the basics**, **staff-engineer level**, etc., plus a normal **what/why/how** ask (avoid mixing with ELI5/beginner wording in the same line). | **`audience=technical`** in **`prompt_signals:`**; reply may use domain jargon and skip hand-holding |
| Formal vs casual register | Ask for a **board-ready** / **client-facing** / **formal memo** / **for regulators** write-up, **or** say you want a **Slack message**, **keep it casual**, **water cooler** tone (one dominant style per message). | **`register_tone=formal`** or **`register_tone=casual`** in **`prompt_signals:`** |
| JSON / structured output | In a **long** message, ask for **valid JSON**, **return JSON**, **as a JSON object**, **machine-readable JSON**, etc. (avoid mixing with **plain text only** / **no json** in the same line). | **`output_format=json`** in **`prompt_signals:`**; reply should be parseable JSON when practical |
| Plain text (no JSON) | In a **long** message, ask for **plain text only**, **no JSON**, **no structured output**, or **don’t return JSON** in the reply (avoid mixing with **return JSON** / **valid JSON** in the same line). | **`output_format=plain`** in **`prompt_signals:`**; reply should stay in normal prose, not a JSON blob |
| Strict facts / low speculation | In a **long** message, ask to **not guess**, **avoid hallucinations**, **only high confidence**, **stick to facts**, **if unsure say so**, etc. (avoid mixing with **brainstorm freely** in the same line). | **`speculation=strict`** in **`prompt_signals:`**; reply should label uncertainty clearly |
| Creative brainstorming | In a **long** message, ask to **brainstorm freely**, **speculate freely**, welcome **wild ideas**, do **blue-sky thinking**, or **explore hypotheticals** (avoid mixing with **don’t guess** / **stick to facts** in the same line). | **`speculation=creative`** in **`prompt_signals:`**; reply may propose speculative ideas with clear assumption labels |
| Summary / BLUF first | In a **long** message, ask to **TLDR first**, **lead with a one-line summary**, **bottom line up front**, **BLUF**, **executive summary first**, etc. (avoid mixing with **answer directly** / **skip the summary** in the same line). | **`answer_lead=tldr_first`** in **`prompt_signals:`**; reply should open with a short summary line |
| Recommendation first | In a **long** decision question, ask to **lead with your recommendation**, **recommendation first**, **state your recommendation upfront**, or **recommendation before the analysis** (avoid mixing with **recommendation at the end** / **no upfront recommendation** in the same line). | **`recommendation_first`** in **`prompt_signals:`**; reply should open with a clear **Recommendation** line, then rationale |
| Go / no-go gate verdict | In a **long** rollout or approval question, ask for a **go/no-go decision**, **gate review**, **proceed or halt** verdict, or an **explicit go or no-go recommendation** (avoid mixing with **no go/no-go** / **skip go-no-go section** in the same line). | **`go_no_go`** in **`prompt_signals:`**; reply should open with **Go**, **No-go**, or **Conditional go**, then criteria/conditions |
| Direct answer (no TL;DR) | In a **long** message, ask to **answer directly**, **skip the summary**, **no TL;DR**, **jump straight to the answer**, or **omit the opening summary** (avoid mixing with **BLUF** / **summary first** in the same line). | **`answer_lead=direct`** in **`prompt_signals:`**; reply should start in-flow without a standalone TL;DR prelude |
| Summary at end (closing recap) | In a **long** message, ask to **wrap up with a summary**, **TLDR at the bottom**, **executive summary at the end**, or **end with a brief recap** (avoid mixing with **summary first** / **BLUF** / **TLDR first** in the same line). | **`summary_last`** in **`prompt_signals:`**; reply should close with a short Summary / TL;DR line after the main body |
| Runnable commands | In a **long** message, ask for **curl one-liner**, **bash snippet**, **kubectl**, **copy-paste into terminal**, **docker run example**, etc. (avoid mixing with **conceptual only** / **no commands** in the same line). | **`actionability=commands`** in **`prompt_signals:`**; reply should include concrete commands where sensible |
| Conceptual only (no commands) | In a **long** message, ask for **conceptual only**, **high level only**, **no shell commands**, **focus on concepts and rationale**, or an **architecture overview without command dumps** (avoid mixing with **kubectl** / **copy-paste into terminal** in the same line). | **`actionability=conceptual`** in **`prompt_signals:`**; reply should avoid runnable command dumps |
| Assumptions / limitations | In a **long** message, ask to **state your assumptions**, **assumptions and limitations**, **caveats upfront**, **scope and assumptions**, **what we are assuming**, or to **flag key uncertainties** (say **skip assumptions** to opt out; avoid mixing with **be decisive** in the same line). | **`confidence_tone=transparent`** in **`prompt_signals:`**; reply should surface assumptions, limits, and uncertainty clearly |
| Decisive / confident tone | In a **long** message, ask to **be decisive**, **don’t hedge**, **give firm answers**, **sound confident**, or **avoid disclaimers** (avoid mixing with **state your assumptions** / **caveats upfront** in the same line). | **`confidence_tone=assertive`** in **`prompt_signals:`**; reply should be direct with minimal hedging |
| Concrete examples vs example-free | In a **long** message, ask for a **worked example**, **walk me through a toy example**, **illustrate with a concrete example**, **ground your answer in an example**, etc. **Or** ask to **skip examples**, **theory only**, **keep it abstract**, **example-free** (avoid mixing both in one line). | **`example_density=rich`** or **`example_density=sparse`** in **`prompt_signals:`**; reply should include or omit short illustrative examples accordingly |
| Explanation order | In a **long** message, ask to **define terms first**, **definitions before details**, **formal definitions upfront**, **terminology first**, etc. **Or** ask for **intuition before math**, **big picture first**, **motivation before the formal proof**, **start with the high-level sketch** (avoid asking for both orders in one line). | **`exposition_order=definitions_first`** or **`exposition_order=intuition_first`** in **`prompt_signals:`**; reply should lead with definitions or with intuition accordingly |
| Glossary (key terms & definitions) | In a **long** technical write-up, ask for a short **glossary** or **define key terms** so readers can follow jargon (avoid “definitions first” wording if you specifically want a separate glossary section). | **`glossary`** in **`prompt_signals:`**; reply should include a **Glossary** section with 3-8 terms and brief definitions |
| UK vs US spelling | In a **long** message, ask for **British English** / **UK spelling** (e.g. colour, organise) **or** **American English** / **US spelling** (e.g. color, organize). Avoid mixing both locales in one line. | **`spelling_uk`** or **`spelling_us`** in **`prompt_signals:`**; reply should use that spelling convention throughout |
| Chronological timeline | In a **long** history or incident question, ask for **chronological order**, **timeline format**, **what happened when**, or **earliest → latest** (oldest first). **Or** ask for **reverse chronological**, **newest first**, or **most recent event first** (avoid mixing both explicit orders in one line). | **`timeline_chron`** or **`timeline_reverse`** in **`prompt_signals:`**; reply should list dated/phased milestones in that time order |
| Blameless postmortem | In a **long** incident or outage write-up, ask for **postmortem format**, a **blameless postmortem**, or a **postmortem outline** with summary, impact, timeline, root cause, lessons learned, and action items (avoid mixing with **no postmortem format** / **skip postmortem** in the same line). | **`postmortem`** in **`prompt_signals:`**; reply should use standard postmortem section headings |
| Sprint retrospective (retro) | In a **long** agile team reflection, ask for a **sprint retro**, **sprint retrospective format**, **retro format**, or **facilitate a retrospective** for an iteration (avoid mixing with **no sprint retro** / **skip retrospective format**; use **Postmortem** for incident/outage write-ups). | **`sprint_retro`** in **`prompt_signals:`**; reply should use **What went well**, **What didn't go well**, **Ideas / experiments**, **Action items** |
| User story (As a / I want / So that) | In a **long** backlog or product prompt, ask for **user stories**, **user story format**, **As a … I want … so that**, or **acceptance criteria for each story** (avoid mixing with **no user stories** / **skip user story format**; use **STAR format** for interview answers). | **`user_story`** in **`prompt_signals:`**; reply should use **Title**, **As a / I want / So that**, and **Acceptance criteria** per story |
| Definition of Done (DoD) | In a **long** agile delivery prompt, ask for a **definition of done**, **DoD format**, **done criteria**, or **what counts as done** before merge/release (avoid mixing with **no definition of done** / **skip DoD format**; use **Checklist** row for generic `- [ ]` task lists). | **`definition_of_done`** in **`prompt_signals:`**; reply should use **Definition of Done**, **Scope**, and verifiable **Done criteria** bullets |
| 5 Whys root-cause analysis | In a **long** incident or defect question, ask for a **five whys**, **5 whys analysis**, **root cause using 5 whys**, or **ask why five times** (avoid mixing with **no five whys** / **skip the why chain** in the same line). | **`five_whys`** in **`prompt_signals:`**; reply should use **Problem statement**, **Why 1–5**, and **Root cause** |
| Fishbone / Ishikawa diagram | In a **long** quality or incident question, ask for a **fishbone diagram**, **Ishikawa analysis**, **cause-and-effect diagram**, or **fishbone format** with categorized causes (avoid mixing with **no fishbone** / **skip fishbone** in the same line). | **`fishbone`** in **`prompt_signals:`**; reply should use **Problem / Effect** plus category headings (People, Process, Technology, etc.) with sub-causes |
| Second vs third person voice | In a **long** how-to or doc draft, ask to **address the reader as you**, **use second person**, or **speak directly to me**. **Or** ask for **third person**, **impersonal tone**, or **avoid second person** / **don't use you throughout** (avoid mixing both in one line). | **`voice_second`** or **`voice_third`** in **`prompt_signals:`**; reply should use you/your or neutral third-person phrasing accordingly |
| FAQ Q&A pairs (Q: / A:) | In a **long** FAQ or support write-up, ask for **Q&A format**, **question and answer format**, **FAQ-style Q&A**, or **each question followed by an answer** with **Q:** / **A:** labels (avoid mixing with **not Q&A format** / **prose not Q&A** in the same line). | **`faq_qa`** in **`prompt_signals:`**; reply should use labeled question-and-answer pairs, not one essay block |
| Closing / follow-ups | In a **long** message, ask for **no questions at the end**, **don’t ask if I need anything else**, **finish crisply**, **skip the stock closer**, etc. **Or** ask to **suggest next steps**, **end with actionable next steps**, **what should we do next**, **offer ways to go deeper** (avoid mixing both in one line). | **`followup_close=minimal`** or **`followup_close=suggest`** in **`prompt_signals:`**; reply should omit or include a light optional follow-up line accordingly |
| Clarify-first vs answer-first | In a **long** message, ask to **ask clarifying questions before you answer**, **if anything is unclear ask me first**, **confirm my constraints before**, etc. **Or** say **no clarifying questions**, **answer without asking questions first**, **don’t interrogate me first**, **give your best answer without asking** (avoid mixing both in one line). | **`clarify_first=on`** or **`clarify_first=off`** in **`prompt_signals:`**; first reply should ask brief questions first or answer directly |
| Section headings vs flat | In a **long** message, ask to **use markdown headings**, **organize with headings**, **structure the answer with clear headings**, **h2 or h3 headings for each topic**, etc. **Or** ask for a **flat answer**, **no section headings**, **avoid markdown headings**, **continuous prose only** (avoid mixing both in one line). | **`section_headings=prefer`** or **`section_headings=avoid`** in **`prompt_signals:`**; reply should use or avoid `##` / `###` title lines accordingly |
| Analogies vs literal | In a **long** message, ask to **use a helpful analogy**, **explain with a simple analogy**, **liken this to something familiar**, **map it to an everyday example**, etc. **Or** say **no analogies**, **skip metaphors**, **literal explanations only**, **stick to literal technical description** (avoid mixing both in one line). | **`analogy_use=prefer`** or **`analogy_use=avoid`** in **`prompt_signals:`**; reply may include one tight analogy or stay metaphor-free accordingly |
| Bold key terms vs minimal bold | In a **long** message, ask to **bold the key terms**, **highlight important phrases**, **make key terms stand out** for scanning, etc. **Or** say **minimal bold**, **don’t overuse bold**, **avoid excessive bold**, **sparse bold** (avoid mixing both in one line). | **`term_emphasis=highlight`** or **`term_emphasis=minimal`** in **`prompt_signals:`**; reply should use selective **bold** on keywords or keep bold sparse |
| Acronym expansion vs terse | In a **long** message, ask to **spell out acronyms**, **expand acronyms on first use**, **define acronyms when you introduce them** (e.g. for compliance readers). **Or** say **assume I know acronyms**, **don’t expand acronyms**, **keep acronyms as-is**, **acronym-literate audience** (avoid mixing both in one line). | **`acronym_style=spell_out`** or **`acronym_style=terse`** in **`prompt_signals:`**; reply should expand once as `Long Form (ACRONYM)` or reuse acronyms without expansion |
| Risk posture (safe vs pragmatic) | In a **long** message, ask to **err on the side of safety**, **minimize downside**, **prefer low-risk options**, **safety-first rollout**, etc. **Or** say **optimize for speed**, **be pragmatic**, **avoid over-engineering**, **good enough is fine**, **ship fast** (avoid mixing both in one line). | **`risk_posture=conservative`** or **`risk_posture=pragmatic`** in **`prompt_signals:`**; recommendations should favor safety or practical speed accordingly |
| FAQ quote vs paraphrase | In a **long** message about **FAQ / policy / excerpt** text, ask to **quote the FAQ excerpts**, **include direct quotes from the policy**, **verbatim passages from the excerpt**, etc. **Or** say **paraphrase the FAQ**, **paraphrase only**, **don’t quote the excerpts**, **summarize the policy in your own words** (avoid mixing both in one line). | **`quote_style=quote`** or **`quote_style=paraphrase`** in **`prompt_signals:`**; reply should quote or paraphrase injected excerpts accordingly |
| Emoji in replies | In a **long** message, ask to **use a few tasteful emoji**, **include emoji when helpful**, **emoji are ok**, **sprinkle emoji**, etc. **Or** say **no emoji in your reply**, **avoid emoji**, **emoji-free tone**, **don’t use emoji** (avoid mixing both in one line). | **`emoji_style=include`** or **`emoji_style=avoid`** in **`prompt_signals:`**; reply may use sparse emoji or stay emoji-free accordingly |
| FAQ grounding (strict vs relaxed) | In a **long** message about **FAQ / policy / excerpt** retrieval, ask to **stick to the FAQ**, **only use the FAQ excerpts**, **if it’s not in the FAQ say so**, **strict FAQ grounding**, etc. **Or** say **FAQ plus general knowledge**, **mix the FAQ with general knowledge**, **supplement the excerpts with brief general context** (avoid mixing both in one line). | **`faq_grounding=strict`** or **`faq_grounding=relaxed`** in **`prompt_signals:`**; reply should stay FAQ-only or allow separated general context accordingly |
| Source links / citations | In a **long** message about **FAQ, policy, web, or research** context, ask to **cite your sources**, **include source links**, **attribute each claim**, or **show the sources you used**. **Or** say **no source links**, **don’t cite sources**, **without links or footnotes**, **answer without citing** (avoid mixing both in one line). | **`cite_sources`** or **`cite_minimal`** in **`prompt_signals:`**; reply should include or skip inline `[FAQ excerpt N]` / `[Web n]` style attribution |
| Math steps vs final only | In a **long** math-style question, ask to **show your work**, **walk through the derivation**, **prove it step by step**, **show intermediate steps**, etc. **Or** say **final answer only**, **no derivation**, **skip the steps**, **just the result** for the equation (avoid mixing both in one line). | **`math_detail=show_work`** or **`math_detail=final_only`** in **`prompt_signals:`**; reply should include or omit intermediate math steps accordingly |
| Code fences vs inline | In a **long** message that includes **code / commands / scripts**, ask for **fenced code blocks**, **markdown code fences**, **triple-backtick fences**, etc. **Or** say **inline code only**, **no triple backticks**, **no fenced code blocks**, **keep snippets inline** (avoid mixing both in one line). | **`code_block_style=fenced`** or **`code_block_style=inline`** in **`prompt_signals:`**; reply should use ``` fences or inline backticks accordingly |
If there is no footer, brain trace is off for that session, or this deployment has **no** encoder / FAQ / memory / web layers and no prompt signals fired yet—**prompt signals alone** still turn the footer on once this feature triggers.
---
### What to try (step-by-step)
| Goal | What to type |
| --- | --- |
| See what is loaded | `/status` |
| Full in-chat manual | `/help` |
| Normal Q&A | Ask any question in plain language. |
| **Classifier** (full probability table) | `/classify Stocks rallied after earnings.` or ask naturally to classify a paragraph. |
| **FAQ search** (scored chunks) | `/retrieve shipping policy` or “search the FAQ for …”. |
| **Web search** (Google CSE) | `/web latest Python 3.13 release notes` or ask for **live web** / **Google** news (needs `GOOGLE_CSE_API_KEY` + `GOOGLE_CSE_CX`). |
| **Summarize** | `/summarize` + long text, or “summarize this: …”. |
| **Rephrase** | `/reformulate` + text, or “rewrite this professionally: …”. |
| **Answer from facts only** | `/grounded Will you refund? ||| Our policy is 14-day returns.` (question and context separated by `|||`). |
| **Similarity** (encoder cosine) | `/similarity The market rose. ||| Stocks gained today.` |
| **Embedding** preview | `/embed A short passage` or `/embedding …`. |
| **Pick nearest option** | `/nearest query ||| option one ||| option two` (add more `|||` segments for more candidates). |
| **Memory — long-term** | `/remember My project code is alpha-42` or say you want to remember something. |
| **Memory — this session** | `/session Temporary note for this chat` |
| **List saved notes** | `/memories` or ask to show stored notes. |
| **Clear session notes only** | `/clear-session` |
| **Export notes (JSON)** | Say *Export my memories* / *Download my notes as JSON*. |
| **Wipe all notes for this scope** | Say *Delete all my memories for this chat* (long-term + session for current scope). |
| **Isolate your notes (new scope)** | *Start a new private session* / *Begin a fresh scope* — then use `/remember` and `/memories` to confirm only new notes appear. |
| **Switch scope** | *Switch to scope my-key* (ASCII id) to attach memory to a named scope. |
| **Brain trace on/off** | *Show the brain trace* / *Hide debug trace* — then ask a normal question and check the footer line. |
| **FAQ snippets on/off** | *Turn off the FAQ context* / *Turn FAQ back on*. |
| **Routing on/off** | *Turn off smart routing* returns to plain chat + slash shortcuts; turn back on per `/help` phrasing. |
| **Reply style** | Phrases like *Be brief*, *Use bullet points*, *Strict FAQ*, *ELI5*, *Formal tone*, *Reset reply style* (see `/help` for the full list). |
---
### Google web search — Hugging Face Space setup and how to test
This Space can call **Google Programmable Search (Custom Search JSON API)** when you configure credentials on the Hub (and redeploy if you added new files).
**1) Space settings (Repository → Settings)**
| Name | Type | Value |
| --- | --- | --- |
| `GOOGLE_CSE_API_KEY` | **Secret** | Google Cloud API key restricted to **Custom Search API** (Application restrictions: **None** is typical for server-side Spaces). |
| `GOOGLE_CSE_CX` | **Variable** or **Secret** | Search engine ID from [Programmable Search Engine control panel](https://programmablesearchengine.google.com/controlpanel/all) → your engine → **Overview** → **Search engine ID** (the `cx` value). |
Optional **Variables**: `GOOGLE_CSE_NUM` (1–10, default 5), `GOOGLE_CSE_SAFE` (e.g. `off` or `active` — see Google’s `cse.list` docs).
**2) Restart**
After saving secrets/variables, **Restart this Space** (or trigger a new deployment) so the container picks up env vars.
**3) Verify configuration**
Type **`/status`** and press **Send**. The line **Google web search (CSE)** should show **on** when both `GOOGLE_CSE_API_KEY` and `GOOGLE_CSE_CX` are set. If it says **off**, the Space process does not see those variables yet.
**4) Test the API directly (no router)**
- **`/web`** — returns **raw search hits** (titles, URLs, snippets) only. Example: `/web Python 3.13 release date`
- Same as **`/search_web …`**
If you see an error about HTTP 403 or “API key not valid”, fix the key or enable **Custom Search API** for that GCP project.
**5) Test with the AI (smart routing)**
- Ensure **smart routing** is on (say *Turn on smart routing* if you turned it off).
- Ask in plain language for **live web** / **Google** / **today’s** information, e.g. *Search the web for the latest SpaceX launch summary* or *What does the web say about …?*
- The router uses intent **`web_search`**: the app fetches snippets, injects them into the model context, then the assistant replies **using those sources** (cite **[Web n]** when using a snippet).
- **Automatic web:** if Google CSE is configured, the app may also run a web search when your message **implies** fresh public facts (e.g. *latest*, *today*, *who won*, *stock price*, a recent year + question) even if you do not say “search the web”. On a self-hosted Space you can disable that with **`--no-auto-web`** or env **`NO_AUTO_WEB=1`**. Brain trace may show **`+auto`** on the web line when the upgrade came from this layer rather than the router alone.
- If the model stays in FAQ-only mode, use **`/web …`** first to confirm the API works, then try clearer web phrasing.
**6) Brain trace**
With **Show the brain trace** on, look for **`web:CSE:N`** (N = number of hits) at the bottom of the assistant message after a web-backed reply.
**7) Limits**
Google enforces **quotas** and may **restrict new signups** for the legacy Custom Search JSON API — check current Google documentation. This demo does not store your API key in the repo; it only reads **Space env** at runtime.
---
### Natural-language routing (no `/` required)
The app can infer intents such as **chat**, **summarize**, **reformulate**, **grounded Q&A**, **FAQ retrieve**, **web_search** (public web via Google CSE when configured), **classify**, **similarity**, **embedding**, **nearest candidate**, **remember / list / clear memory**, and **status**. If the wrong tool runs, repeat with a clearer verb or use the matching **slash command** from the table above.
---
### Session controls (plain English, no `/`)
These adjust **scope**, **memory**, **FAQ injection**, **routing**, **brain trace**, and **reply style** (hints fed into the system prompt). Examples (not exact wording required):
- **Scope / visibility:** *What is my current scope?* · *Show my session settings* · *Start a new private session* · *Switch to scope my-key*
- **Reply shape:** *Be brief* · *More detail please* · *Use bullet points* · *Reset reply style*
- **FAQ grounding:** *Strict FAQ* · *Relaxed FAQ* · *Balanced FAQ*
- **Audience & structure:** *ELI5* · *Expert mode* · *TLDR first* · *Answer directly* · *Step by step* · *No numbered steps* · *Definitions first* · *Intuition first*
- **Tone & format:** *Formal tone* · *Casual tone* · *Use code fences* · *Inline code only* · *Use tables* · *No tables* · *Use emoji* · *No emoji* · *Use section headings* · *Flat answer* · *Bold key terms* · *Minimal bold*
- **Reasoning habits:** *Flag your assumptions* · *Be decisive* · *Suggest next steps* · *No follow-up questions* · *Clarify first* · *No clarifying questions* · *No speculation* · *Brainstorm freely* · *Show your work* · *Final answer only*
- **Output & safety:** *Answer in JSON* · *Plain text only* · *Be risk averse* · *Be pragmatic* · *Give me runnable commands* · *No commands* · *Quote the FAQ excerpts* · *Paraphrase only*
- **Style extras:** *Use analogies* · *No analogies* · *Spell out acronyms* · *Don't expand acronyms* · *Include examples* · *Skip examples* · *Use pros and cons* · *Compare in flowing prose* · *Challenge my assumptions* · *Be supportive*
- **Memory maintenance:** *Clear my session notes* · *Export my memories* · *Delete all my memories for this chat*
- **Debug / behavior:** *Turn off FAQ context* · *Turn FAQ back on* · *Turn off smart routing* · *Show the brain trace* · *Hide debug trace*
---
### Encoder + trace
The encoder adds a soft **topic hint** to the system context and can show **`classify:…`** in the brain trace. Labels reflect **TinyModel1** training (≈ AG News). Use `/classify` when you want the full markdown probability table in the reply.
---
### Hugging Face API
On the Space page, open **Use via API** to call the **`chat`** endpoint (same pipeline as the Send button) from HTTP or the Gradio client.
---
### Tips
- **Shared demo**: the default scope may be shared with other visitors; use *Start a new private session* for isolated memory.
- **Optional Space env**: `HORIZON2_MODEL` can override the generative model id; `HF_TOKEN` (secret) helps with Hub downloads; **`GOOGLE_CSE_API_KEY`** + **`GOOGLE_CSE_CX`** enable web search (see section **Google web search** above).
- **More phrases**: the repo `README` and `/help` list additional natural phrasings for session controls."""
ROUTER_SYSTEM = """You are an intent router for a desktop AI assistant. The user speaks naturally (any language). Output EXACTLY one JSON object, one line, no markdown fences, no explanation.
Schema:
{"intent":"<name>","text":"","question":"","context":""}
intent must be one of:
- chat — general talk, advice, open questions, follow-ups; put the FULL user message in "text"
- summarize — user wants a shorter summary; put source in "text"
- reformulate — rewrite/clarify/professional tone; source in "text"
- grounded — answer only from given facts; put QUESTION in "question", FACTS in "context" (if user mixes both in one blob, split sensibly)
- retrieve — search **FAQ / internal knowledge** corpus only; put search query in "text"
- web_search — user wants **live web** facts (news, current events, URLs); put the **search query** in "text" (not for FAQ-only lookup)
- classify — show topic-classifier probabilities; put passage in "text"
- similarity — cosine similarity between two texts; put "text_a ||| text_b" in "text"
- embedding — embedding vector summary for one passage; put passage in "text"
- nearest — encoder top-k over candidates; put "query ||| candidate1 ||| candidate2 ||| …" in "text" (at least one candidate)
- remember — save a durable note; put note body in "text"
- session_note — save a session-only note; put note in "text"
- list_memories — user wants to see saved notes
- clear_session — user wants session-only notes deleted
- status — loaded components / debug info
- help — explain available capabilities
Rules:
- Default to "chat" when unsure; copy the entire user message into "text".
- Do not invent facts for "grounded": if no clear facts/context, use "chat" instead.
- Use **retrieve** for bundled FAQ / help-base search; use **web_search** when the user clearly needs the **public web** (today, external site, breaking news, "google this", etc.).
- **web_search vs chat (critical):** choose **web_search** when a good answer depends on **recent events**, **live or site-specific data** (prices, sports scores, releases after your knowledge cutoff, "what happened today", laws/regulations that change), **verifying a claim against the public web**, or **finding an official URL**. Choose **chat** for timeless explanations, coding how-to without needing today's docs, brainstorming, role-play, or personal opinion where web snippets would not change the answer.
- Extract minimal "text" for tool intents (do not repeat system chatter)."""
VALID_INTENTS = frozenset(
{
"chat",
"summarize",
"reformulate",
"grounded",
"retrieve",
"web_search",
"classify",
"similarity",
"embedding",
"nearest",
"remember",
"session_note",
"list_memories",
"clear_session",
"status",
"help",
}
)
_INTENT_ALIASES = {
"memory": "list_memories",
"memories": "list_memories",
"notes": "list_memories",
"search": "retrieve",
"faq": "retrieve",
"lookup": "retrieve",
"internet": "web_search",
"google": "web_search",
"browse_web": "web_search",
"similar": "similarity",
"cosine": "similarity",
"embed": "embedding",
"embeddings": "embedding",
"knn": "nearest",
"triage": "nearest",
"encoder_retrieve": "nearest",
}
def _parse_two_segments(blob: str) -> tuple[str, str]:
if "|||" not in blob:
raise ValueError("Need two segments separated by `|||` (e.g. `text A ||| text B`).")
a, _, b = blob.partition("|||")
a, b = a.strip(), b.strip()
if not a or not b:
raise ValueError("Both sides of `|||` must be non-empty.")
return a, b
def _parse_nearest_blob(blob: str) -> tuple[str, list[str]]:
parts = [p.strip() for p in blob.split("|||") if p.strip()]
if len(parts) < 2:
raise ValueError(
"Need `query ||| candidate1 ||| candidate2` (at least one candidate after `|||`)."
)
return parts[0], parts[1:]
def _embedding_summary_markdown(encoder: TinyModelRuntime, passage: str) -> str:
vec = encoder.embed([passage], normalize=False)[0]
dim = int(vec.shape[0])
norm = float(torch.linalg.vector_norm(vec))
k = min(8, dim)
head = ", ".join(f"{float(vec[i]):.4f}" for i in range(k))
return "\n".join(
[
"### Encoder embedding (raw [CLS], not L2-normalized)\n",
f"- **dim:** {dim}",
f"- **L2 norm:** {norm:.4f}",
f"- **first {k} values:** {head}",
]
)
def _nearest_markdown(
encoder: TinyModelRuntime,
query: str,
candidates: list[str],
*,
top_k: int,
) -> str:
hits = encoder.retrieve(query, candidates, top_k=top_k)
if not hits:
return "(No candidates.)"
lines = ["### Encoder nearest neighbors (cosine on pooled embeddings)\n"]
for rank, h in enumerate(hits, 1):
lines.append(
f"**#{rank}** score={h.score:.4f} · index={h.index}\n{_clip(h.text, 700)}\n"
)
return "\n".join(lines)
def _classifier_result_markdown(probs: dict[str, float]) -> str:
ranked = sorted(probs.items(), key=lambda x: -x[1])
top_lab, top_p = ranked[0]
lines = [
"### Classifier (TinyModel)\n",
f"**Winner:** `{top_lab}` · **p = {top_p:.4f}**\n",
"\n| rank | label | p |\n|:---:|:---|---:|",
]
for i, (lab, p) in enumerate(ranked[:12], 1):
mark = " **←**" if i == 1 else ""
lines.append(f"| {i} | {lab}{mark} | {p:.4f} |")
return "\n".join(lines)
def _ensure_gradio_can_reach_localhost() -> None:
"""Gradio probes localhost via httpx; HTTP(S)_PROXY can break that on Windows/VPN."""
extras = ("localhost", "127.0.0.1", "::1")
for var in ("NO_PROXY", "no_proxy"):
raw = os.environ.get(var, "")
parts = [p.strip() for p in raw.replace(";", ",").split(",") if p.strip()]
for h in extras:
if h not in parts:
parts.append(h)
os.environ[var] = ",".join(parts)
def _patch_gradio_localhost_probe() -> None:
"""Gradio's built-in `url_ok` uses httpx with env proxies; on Windows/VPN, HEAD to
127.0.0.1 often fails even though the app is up. Use direct (no-proxy) requests.
"""
import time as time_mod
import warnings as warn_mod
import gradio.networking as gn
import httpx
def url_ok(url: str) -> bool:
ok_codes = (200, 204, 401, 302, 303, 307)
for _ in range(5):
try:
with warn_mod.catch_warnings():
warn_mod.filterwarnings("ignore")
with httpx.Client(
timeout=5,
verify=False,
trust_env=False,
follow_redirects=True,
) as client:
r = client.head(url)
if r.status_code in ok_codes:
return True
r = client.get(url)
if r.status_code in ok_codes:
return True
except (ConnectionError, OSError, httpx.HTTPError, httpx.TimeoutException):
pass
time_mod.sleep(0.4)
return False
gn.url_ok = url_ok # type: ignore[assignment]
def _clip(s: str, n: int) -> str:
s = (s or "").strip()
if len(s) <= n:
return s
return s[: n - 3] + "..."
def _extract_json_object(s: str) -> dict | None:
s = (s or "").strip()
try:
d = json.loads(s)
return d if isinstance(d, dict) else None
except json.JSONDecodeError:
pass
start = s.find("{")
end = s.rfind("}")
if start >= 0 and end > start:
try:
d = json.loads(s[start : end + 1])
return d if isinstance(d, dict) else None
except json.JSONDecodeError:
return None
return None
def _normalize_intent(raw: str) -> str:
x = (raw or "chat").strip().lower().replace("-", "_")
x = _INTENT_ALIASES.get(x, x)
return x if x in VALID_INTENTS else "chat"
def infer_route(
lm: LoadedLM,
user_message: str,
*,
seed: int,
max_new_tokens: int,
) -> dict[str, str]:
u = (
f"USER_MESSAGE (verbatim):\n{user_message}\n\n"
"Output the JSON object now."
)
if getattr(lm.tokenizer, "chat_template", None):
prompt = lm.tokenizer.apply_chat_template(
[{"role": "system", "content": ROUTER_SYSTEM}, {"role": "user", "content": u}],
tokenize=False,
add_generation_prompt=True,
)
else:
prompt = f"{ROUTER_SYSTEM}\n\n{u}\nJSON:"
raw, _, _, _ = generate_completion(
lm,
prompt,
max_new_tokens=max_new_tokens,
seed=seed,
do_sample=False,
)
data = _extract_json_object(raw) or {}
intent = _normalize_intent(str(data.get("intent", "chat")))
return {
"intent": intent,
"text": str(data.get("text", "")).strip(),
"question": str(data.get("question", "")).strip(),
"context": str(data.get("context", "")).strip(),
}
def _format_status(
*,
meta_mid: str,
meta_encoder: str,
meta_rag_path: str | None,
rag_chunks: list[str] | None,
meta_mem_db: str | None,
scope_key: str,
) -> str:
rag_n = len(rag_chunks) if rag_chunks else 0
g_key, g_cx, _, _ = read_google_cse_settings()
cse_line = (
"**on** (`GOOGLE_CSE_API_KEY` + `GOOGLE_CSE_CX`)"
if g_key and g_cx
else "**off** (set `GOOGLE_CSE_API_KEY` and `GOOGLE_CSE_CX` for `/web` + routed web search)"
)
lines = [
"### Status\n",
f"- **Generative:** `{meta_mid}`",
f"- **Encoder:** {meta_encoder}",
f"- **RAG corpus:** {_clip(meta_rag_path or '—', 80)} · **chunks:** {rag_n}",
f"- **Memory DB:** `{meta_mem_db or 'off'}` · **scope:** `{scope_key}`",
f"- **Google web search (CSE):** {cse_line}",
]
return "\n".join(lines)
def run_routed_tool(
route: dict[str, str],
*,
msg: str,
lm: LoadedLM,
mem_conn: sqlite3.Connection | None,
scope_key: str,
encoder: TinyModelRuntime | None,
rag_chunks: list[str] | None,
rag_top_k: int,
task_max_new_tokens: int,
seed: int,
meta_mid: str,
meta_encoder: str,
meta_mem_db: str | None,
meta_rag_path: str | None,
) -> str:
intent = route["intent"]
text = route["text"]
question = route["question"]
context = route["context"]
if intent == "help":
return HELP_TEXT
if intent == "status":
return _format_status(
meta_mid=meta_mid,
meta_encoder=meta_encoder,
meta_rag_path=meta_rag_path,
rag_chunks=rag_chunks,
meta_mem_db=meta_mem_db,
scope_key=scope_key,
)
if intent == "classify":
if not encoder:
return "Classifier is not loaded (try without `--lm-only` / `--no-encoder`)."
passage = text or msg
if not passage:
return "Tell me what text to classify."
return _classifier_result_markdown(encoder.classify([passage])[0])
if intent == "retrieve":
if not encoder or not rag_chunks:
return "FAQ search needs encoder + corpus (defaults on unless disabled)."
q = text or msg
if not q:
return "What should I search for?"
hr = hybrid_retrieve(encoder, q, rag_chunks, top_k=rag_top_k)
if not hr:
return "(No matching chunks.)"
out = ["### Retrieved chunks\n"]
for i, (sc, _idx, txt) in enumerate(hr, 1):
out.append(f"**#{i}** score={sc:.4f}\n{_clip(txt, 700)}\n")
return "\n".join(out)
if intent == "similarity":
if not encoder:
return "Similarity needs the encoder (drop `--lm-only` / `--no-encoder`)."
blob = (text or msg).strip()
if not blob:
return "Provide two texts: `first ||| second`."
try:
ta, tb = _parse_two_segments(blob)
except ValueError as e:
return str(e)
score = encoder.similarity(ta, tb)
return (
"### Similarity (encoder cosine)\n"
f"**Score:** {score:.4f}\n\n"
f"**A:** {_clip(ta, 480)}\n\n"
f"**B:** {_clip(tb, 480)}"
)
if intent == "embedding":
if not encoder:
return "Embedding stats need the encoder (drop `--lm-only` / `--no-encoder`)."
passage = (text or msg).strip()
if not passage:
return "What text should I embed?"
return _embedding_summary_markdown(encoder, passage)
if intent == "nearest":
if not encoder:
return "Nearest-neighbor search needs the encoder (drop `--lm-only` / `--no-encoder`)."
blob = (text or msg).strip()
if not blob:
return "Usage: `query ||| option1 ||| option2 ...`"
try:
query, cands = _parse_nearest_blob(blob)
except ValueError as e:
return str(e)
k = max(1, min(rag_top_k, len(cands)))
return _nearest_markdown(encoder, query, cands, top_k=k)
if intent in ("summarize", "reformulate", "grounded"):
if intent == "grounded":
qn = question or text
ctx = context
if not qn or not ctx:
bod = text or msg
# one-blob fallback: first sentence as question rest as context heuristic weak
if "?" in bod:
qn = bod.split("?", 1)[0] + "?"
ctx = bod.split("?", 1)[1].strip() or bod
else:
return (
"For a grounded answer I need **facts** and a **question**. "
"Say both in one message (e.g. facts first, then your question)."
)
try:
up = build_user_prompt("grounded", qn.strip(), context=ctx.strip())
except ValueError as e:
return str(e)
else:
src = text or msg
if not src:
return "What text should I process?"
task = "summarize" if intent == "summarize" else "reformulate"
up = build_user_prompt(task, src)
prompt = format_for_model(lm.tokenizer, up)
out, _, _, sec = generate_completion(
lm,
prompt,
max_new_tokens=task_max_new_tokens,
seed=seed,
do_sample=True,
)
return f"**{intent}** ({sec:.2f}s)\n\n{out or '(empty)'}"
if intent in ("remember", "session_note", "list_memories", "clear_session"):
if mem_conn is None:
return "Memory is off (enable default DB or drop `--no-memory`)."
if intent == "remember":
note = text or msg
if not note:
return "What should I remember?"
put(mem_conn, scope_key=scope_key, kind="long_term", content=note)
return "Saved to **long-term** memory."
if intent == "session_note":
note = text or msg
if not note:
return "What should I store for this session?"
put(mem_conn, scope_key=scope_key, kind="session", content=note)
return "Saved to **session** memory."
if intent == "list_memories":
items = list_for_scope(mem_conn, scope_key)
if not items:
return "(No saved notes for this scope.)"
lines = [f"- **{it.kind}** · {_clip(it.content, 320)}" for it in items[:24]]
extra = f"\n\n… {len(items) - 24} more" if len(items) > 24 else ""
return "Saved notes:\n" + "\n".join(lines) + extra
if intent == "clear_session":
n = clear_session(mem_conn, scope_key)
return f"Cleared **{n}** session note(s). Long-term notes unchanged."
return ""
def handle_nl_control(
msg: str,
session: dict[str, Any],
*,
mem_conn: sqlite3.Connection | None,
scope_key: str,
rag_chunks_base: list[str] | None,
locked_no_smart_route: bool,
) -> str | None:
act = parse_control_action(msg)
if act is None:
return None
if act.name == "show_session":
bits = [
f"- scope: `{scope_key}`",
f"- smart routing: **{'on' if session.get('smart_route') and not locked_no_smart_route else 'off'}**",
f"- FAQ context: **{'on' if session.get('rag') and rag_chunks_base is not None else 'off'}**",
f"- brain trace footer: **{'on' if session.get('trace') else 'off'}**",
f"- memory store: **{'on' if mem_conn is not None else 'off'}**",
f"- reply length: **{session.get('verbosity', 'normal')}**",
f"- lists: **{'bullets when helpful' if session.get('reply_format') == 'bullets' else 'prose'}**",
f"- FAQ grounding: **{session.get('faq_grounding', 'normal')}**",
f"- audience: **{session.get('audience', 'normal')}**",
f"- answer opening: **{session.get('answer_lead', 'normal')}**",
f"- procedure steps: **{session.get('step_style', 'normal')}**",
f"- confidence tone: **{session.get('confidence_tone', 'normal')}**",
f"- follow-up ending: **{session.get('followup_close', 'normal')}**",
f"- concept order: **{session.get('exposition_order', 'normal')}**",
f"- examples: **{session.get('example_density', 'normal')}**",
f"- comparisons: **{session.get('comparison_frame', 'normal')}**",
f"- register: **{session.get('register_tone', 'normal')}**",
f"- code blocks: **{session.get('code_block_style', 'normal')}**",
f"- analogies: **{session.get('analogy_use', 'normal')}**",
f"- acronyms: **{session.get('acronym_style', 'normal')}**",
f"- clarify-first: **{session.get('clarify_first', 'normal')}**",
f"- speculation: **{session.get('speculation', 'normal')}**",
f"- math detail: **{session.get('math_detail', 'normal')}**",
f"- output format: **{session.get('output_format', 'normal')}**",
f"- risk posture: **{session.get('risk_posture', 'normal')}**",
f"- actionability: **{session.get('actionability', 'normal')}**",
f"- quote style: **{session.get('quote_style', 'normal')}**",
f"- tables: **{session.get('table_style', 'normal')}**",
f"- emoji: **{session.get('emoji_style', 'normal')}**",
f"- section headings: **{session.get('section_headings', 'normal')}**",
f"- term emphasis: **{session.get('term_emphasis', 'normal')}**",
f"- counterpoints: **{session.get('counterpoint_tone', 'normal')}**",
]
return "### Session settings\n" + "\n".join(bits)
if act.name == "new_private_session":
# Keep it readable and low-collision; not a secret, just a scope id.
new_scope = f"ub-{uuid.uuid4().hex[:8]}"
session["scope_key"] = new_scope
return (
f"**Started a new private session scope.**\n\n"
f"Current scope is now `{new_scope}`.\n"
"Memory operations (remember/export/forget) will apply to this new scope."
)
if act.name == "set_scope":
if not act.value:
return "Tell me the scope key, e.g. `Switch to scope demo-123`."
session["scope_key"] = act.value
return f"Switched session scope to `{act.value}`."
if act.name == "export_memory":
if mem_conn is None:
return "Memory is off for this Space (no SQLite store); nothing to export."
blob = export_scope_json(mem_conn, scope_key)
js = json.dumps(blob, indent=2, ensure_ascii=False)
max_chars = 48_000
if len(js) > max_chars:
js = js[:max_chars] + "\n…(truncated for chat; schema is horizon3_export/1.0)…"
return f"### Memory export (`{scope_key}`)\nPaste/save externally if needed.\n\n```json\n{js}\n```"
if act.name == "forget_scope":
if mem_conn is None:
return "Memory is off; nothing to delete."
n = forget_scope(mem_conn, scope_key)
return (
f"**Erased stored memory for this Space session.**\n\n"
f"Deleted **{n}** row(s) (**session + long-term**) for `{scope_key}`."
)
if act.name == "list_memories":
if mem_conn is None:
return "Memory is off."
items = list_for_scope(mem_conn, scope_key)
if not items:
return "(No saved notes for this scope.)"
lines = [f"- **{it.kind}** · {_clip(it.content, 320)}" for it in items[:24]]
extra = f"\n\n… {len(items) - 24} more" if len(items) > 24 else ""
return "**Saved notes:**\n" + "\n".join(lines) + extra
if act.name == "clear_session":
if mem_conn is None:
return "Memory is off."
n = clear_session(mem_conn, scope_key)
return f"Cleared **{n}** session note(s). Long-term notes unchanged."
if act.name == "set_trace":
session["trace"] = act.value == "on"
return f"**Brain trace** is now **{'on' if session['trace'] else 'off'}** (footer on assistant replies)."
if act.name == "set_smart_route":
if locked_no_smart_route:
return "Smart routing is **locked off** for this server (`--no-smart-route`)."
session["smart_route"] = act.value == "on"
return (
f"**Smart routing** is now **{'on' if session['smart_route'] else 'off'}** "
"(off = plain chat + FAQ context injection + slash shortcuts only)."
)
if act.name == "set_rag":
if rag_chunks_base is None:
return "FAQ/RAG corpus is **not loaded** on this deployment; nothing to toggle."
session["rag"] = act.value == "on"
return (
f"**FAQ/RAG excerpts in prompts** are now **{'on' if session['rag'] else 'off'}**."
)
if act.name == "reset_reply_style":
session["verbosity"] = "normal"
session["reply_format"] = "prose"
session["faq_grounding"] = "normal"
session["audience"] = "normal"
session["answer_lead"] = "normal"
session["step_style"] = "normal"
session["confidence_tone"] = "normal"
session["followup_close"] = "normal"
session["exposition_order"] = "normal"
session["example_density"] = "normal"
session["comparison_frame"] = "normal"
session["register_tone"] = "normal"
session["code_block_style"] = "normal"
session["analogy_use"] = "normal"