Skip to content

Commit 4b65442

Browse files
authored
Merge pull request #1366 from WolframResearch/feature/custom-instructions
Add setting for persistent user instructions
2 parents 0cc383e + 54e003d commit 4b65442

File tree

11 files changed

+166
-9
lines changed

11 files changed

+166
-9
lines changed

FrontEnd/TextResources/ChatbookStrings.tr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"OKButton" -> "OK",
66
"CancelButton" -> "Cancel",
77
"DeleteButton" -> "Delete",
8+
"SaveChangesButton" -> "Save Changes",
89
"SendButton" -> "Send",
910
"URLButton" -> "URL",
1011
"ScopeGlobal" -> "Global",
@@ -107,6 +108,9 @@
107108
"PreferencesContentModelLabel" -> "Model:",
108109
"PreferencesContentModelLabelAlt" -> "Model",
109110
"PreferencesContentTemperatureLabel" -> "Temperature:",
111+
"PreferencesContentSubsectionInstructions" -> "Standing Instructions",
112+
"PreferencesContentInstructionsDescription" -> "Instructions specified here will be applied to all conversations with Notebook Assistant and other chats.",
113+
"PreferencesContentInstructionsHint" -> "e.g. 'Before making any tool calls, briefly explain what you are doing and why.'",
110114
"PreferencesContentSubsectionChat" -> "Chat Notebook Cells",
111115
"PreferencesContentFormatOutputLabel" -> "Format chat output",
112116
"PreferencesContentIncludeHistoryLabel" -> "Include chat history",

FrontEnd/TextResources/ChineseSimplified/ChatbookStrings.tr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"OKButton" -> "\:786E\:8BA4",
66
"CancelButton" -> "\:53D6\:6D88",
77
"DeleteButton" -> "\:5220\:9664",
8+
"SaveChangesButton" -> "Save Changes",
89
"SendButton" -> "\:53D1\:9001",
910
"URLButton" -> "URL",
1011
"ScopeGlobal" -> "\:5168\:5C40",
@@ -107,6 +108,9 @@
107108
"PreferencesContentModelLabel" -> "\:6A21\:578B\:ff1a",
108109
"PreferencesContentModelLabelAlt" -> "\:6A21\:578B",
109110
"PreferencesContentTemperatureLabel" -> "\:6E29\:5EA6\:ff1a",
111+
"PreferencesContentSubsectionInstructions" -> "Standing Instructions",
112+
"PreferencesContentInstructionsDescription" -> "Instructions specified here will be applied to all conversations with Notebook Assistant and other chats.",
113+
"PreferencesContentInstructionsHint" -> "e.g. 'Before making any tool calls, briefly explain what you are doing and why.'",
110114
"PreferencesContentSubsectionChat" -> "\:804A\:5929\:7B14\:8BB0\:672C\:4E2D\:7684\:5355\:5143",
111115
"PreferencesContentFormatOutputLabel" -> "\:683C\:5F0F\:5316\:804A\:5929\:8F93\:51FA",
112116
"PreferencesContentIncludeHistoryLabel" -> "\:5305\:62EC\:804A\:5929\:8BB0\:5F55",

FrontEnd/TextResources/ChineseTraditional/ChatbookStrings.tr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"OKButton" -> "\:78BA\:5B9A",
66
"CancelButton" -> "\:53D6\:6D88",
77
"DeleteButton" -> "\:522A\:9664",
8+
"SaveChangesButton" -> "Save Changes",
89
"SendButton" -> "\:767C\:9001",
910
"URLButton" -> "\:7DB2\:5740",
1011
"ScopeGlobal" -> "\:7E3D\:9AD4",
@@ -107,6 +108,9 @@
107108
"PreferencesContentModelLabel" -> "\:6A21\:578B\:FF1A",
108109
"PreferencesContentModelLabelAlt" -> "\:6A21\:578B",
109110
"PreferencesContentTemperatureLabel" -> "\:6EAB\:5EA6\:FF1A",
111+
"PreferencesContentSubsectionInstructions" -> "Standing Instructions",
112+
"PreferencesContentInstructionsDescription" -> "Instructions specified here will be applied to all conversations with Notebook Assistant and other chats.",
113+
"PreferencesContentInstructionsHint" -> "e.g. 'Before making any tool calls, briefly explain what you are doing and why.'",
110114
"PreferencesContentSubsectionChat" -> "\:7DB2\:8DEF\:804A\:5929\:7CFB\:7D71\:7B46\:8A18\:672C\:5132\:5B58\:683C",
111115
"PreferencesContentFormatOutputLabel" -> "\:683C\:5F0F\:5316\:7DB2\:8DEF\:804A\:5929\:7CFB\:7D71\:8F38\:51FA",
112116
"PreferencesContentIncludeHistoryLabel" -> "\:6536\:7D0D\:7DB2\:8DEF\:804A\:5929\:7CFB\:7D71\:6B77\:53F2\:7D00\:9304",

FrontEnd/TextResources/French/ChatbookStrings.tr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"OKButton" -> "Ok",
66
"CancelButton" -> "Annuler",
77
"DeleteButton" -> "Supprimer",
8+
"SaveChangesButton" -> "Save Changes",
89
"SendButton" -> "Envoyer",
910
"URLButton" -> "URL",
1011
"ScopeGlobal" -> "Global",
@@ -107,6 +108,9 @@
107108
"PreferencesContentModelLabel" -> "Mod\[EGrave]le\[NonBreakingSpace]:",
108109
"PreferencesContentModelLabelAlt" -> "Mod\[EGrave]le",
109110
"PreferencesContentTemperatureLabel" -> "Temp\[EAcute]rature\[NonBreakingSpace]:",
111+
"PreferencesContentSubsectionInstructions" -> "Standing Instructions",
112+
"PreferencesContentInstructionsDescription" -> "Instructions specified here will be applied to all conversations with Notebook Assistant and other chats.",
113+
"PreferencesContentInstructionsHint" -> "e.g. 'Before making any tool calls, briefly explain what you are doing and why.'",
110114
"PreferencesContentSubsectionChat" -> "Cellules du notebook de chat",
111115
"PreferencesContentFormatOutputLabel" -> "Mise en forme de la sortie du chat",
112116
"PreferencesContentIncludeHistoryLabel" -> "Inclure l\[CloseCurlyQuote]historique du chat",

FrontEnd/TextResources/Japanese/ChatbookStrings.tr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"OKButton" -> "OK",
66
"CancelButton" -> "\:30ad\:30e3\:30f3\:30bb\:30eb",
77
"DeleteButton" -> "\:524a\:9664",
8+
"SaveChangesButton" -> "Save Changes",
89
"SendButton" -> "\:9001\:4fe1",
910
"URLButton" -> "URL",
1011
"ScopeGlobal" -> "\:30b0\:30ed\:30fc\:30d0\:30eb",
@@ -107,6 +108,9 @@
107108
"PreferencesContentModelLabel" -> "\:30e2\:30c7\:30eb\:ff1a",
108109
"PreferencesContentModelLabelAlt" -> "\:30e2\:30c7\:30eb",
109110
"PreferencesContentTemperatureLabel" -> "\:6e29\:5ea6\:ff1a",
111+
"PreferencesContentSubsectionInstructions" -> "Standing Instructions",
112+
"PreferencesContentInstructionsDescription" -> "Instructions specified here will be applied to all conversations with Notebook Assistant and other chats.",
113+
"PreferencesContentInstructionsHint" -> "e.g. 'Before making any tool calls, briefly explain what you are doing and why.'",
110114
"PreferencesContentSubsectionChat" -> "\:30c1\:30e3\:30c3\:30c8\:30ce\:30fc\:30c8\:30d6\:30c3\:30af\:30bb\:30eb",
111115
"PreferencesContentFormatOutputLabel" -> "\:30c1\:30e3\:30c3\:30c8\:51fa\:529b\:306e\:66f8\:5f0f\:8a2d\:5b9a",
112116
"PreferencesContentIncludeHistoryLabel" -> "\:30c1\:30e3\:30c3\:30c8\:5c65\:6b74\:3092\:542b\:3081\:308b",

FrontEnd/TextResources/Korean/ChatbookStrings.tr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"OKButton" -> "OK",
66
"CancelButton" -> "\:CDE8\:C18C",
77
"DeleteButton" -> "\:C0AD\:C81C",
8+
"SaveChangesButton" -> "Save Changes",
89
"SendButton" -> "\:C804\:C1A1",
910
"URLButton" -> "URL",
1011
"ScopeGlobal" -> "\:AE00\:B85C\:BC8C",
@@ -107,6 +108,9 @@
107108
"PreferencesContentModelLabel" -> "\:BAA8\:B378:",
108109
"PreferencesContentModelLabelAlt" -> "\:BAA8\:B378",
109110
"PreferencesContentTemperatureLabel" -> "\:C628\:B3C4",
111+
"PreferencesContentSubsectionInstructions" -> "Standing Instructions",
112+
"PreferencesContentInstructionsDescription" -> "Instructions specified here will be applied to all conversations with Notebook Assistant and other chats.",
113+
"PreferencesContentInstructionsHint" -> "e.g. 'Before making any tool calls, briefly explain what you are doing and why.'",
110114
"PreferencesContentSubsectionChat" -> "\:CC44\:D305 \:B178\:D2B8\:BD81 \:C140",
111115
"PreferencesContentFormatOutputLabel" -> "\:CC44\:D305 \:CD9C\:B825\:C758 \:C11C\:C2DD \:C9C0\:C815",
112116
"PreferencesContentIncludeHistoryLabel" -> "\:CC44\:D305 \:AE30\:B85D\:C744 \:D3EC\:D568",

FrontEnd/TextResources/Spanish/ChatbookStrings.tr

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"OKButton" -> "Aceptar",
66
"CancelButton" -> "Cancelar",
77
"DeleteButton" -> "Eliminar",
8+
"SaveChangesButton" -> "Save Changes",
89
"SendButton" -> "Enviar",
910
"URLButton" -> "URL",
1011
"ScopeGlobal" -> "Global",
@@ -107,6 +108,9 @@
107108
"PreferencesContentModelLabel" -> "Modelo:",
108109
"PreferencesContentModelLabelAlt" -> "Modelo",
109110
"PreferencesContentTemperatureLabel" -> "Temperatura:",
111+
"PreferencesContentSubsectionInstructions" -> "Standing Instructions",
112+
"PreferencesContentInstructionsDescription" -> "Instructions specified here will be applied to all conversations with Notebook Assistant and other chats.",
113+
"PreferencesContentInstructionsHint" -> "e.g. 'Before making any tool calls, briefly explain what you are doing and why.'",
110114
"PreferencesContentSubsectionChat" -> "Celdas de Cuadernos de chat",
111115
"PreferencesContentFormatOutputLabel" -> "Formatear salida de chat",
112116
"PreferencesContentIncludeHistoryLabel" -> "Incluir historial de chat",

LLMConfiguration/Personas/RawModel/LLMConfiguration.wl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<|
22
"BasePrompt" -> None,
3+
"UserInstructions" -> None,
34
"DisplayName" -> Dynamic @ FEPrivate`FrontEndResource[ "ChatbookStrings", "PersonaNameRawModel" ],
45
"Icon" -> RawBoxes @ TemplateBox[ { }, "PersonaRawModel" ],
56
"Description" -> "No custom prompting, just the raw LLM",

Source/Chatbook/ChatMessages.wl

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -397,21 +397,53 @@ addPrompts // endDefinition;
397397
(* ::Subsubsection::Closed:: *)
398398
(*assembleCustomPrompt*)
399399
assembleCustomPrompt // beginDefinition;
400-
assembleCustomPrompt[ settings_Association ] := assembleCustomPrompt[ settings, Lookup[ settings, "Prompts" ] ];
401-
assembleCustomPrompt[ settings_, $$unspecified ] := None;
402-
assembleCustomPrompt[ settings_, prompt_String ] := prompt;
403-
assembleCustomPrompt[ settings_, prompts: { ___String } ] := StringRiffle[ prompts, "\n\n" ];
400+
assembleCustomPrompt[ settings_ ] := addUserInstructions[ assembleCustomPrompt0 @ settings, settings ];
401+
assembleCustomPrompt // endDefinition;
402+
403+
assembleCustomPrompt0 // beginDefinition;
404+
assembleCustomPrompt0[ settings_Association ] := assembleCustomPrompt0[ settings, Lookup[ settings, "Prompts" ] ];
405+
assembleCustomPrompt0[ settings_, $$unspecified ] := None;
406+
assembleCustomPrompt0[ settings_, prompt_String ] := prompt;
407+
assembleCustomPrompt0[ settings_, prompts: { ___String } ] := StringRiffle[ prompts, "\n\n" ];
404408

405-
assembleCustomPrompt[ settings_? AssociationQ, templated: { ___, _TemplateObject, ___ } ] := Enclose[
409+
assembleCustomPrompt0[ settings_? AssociationQ, templated: { ___, _TemplateObject, ___ } ] := Enclose[
406410
Module[ { params, prompts },
407411
params = ConfirmBy[ Association[ settings, $ChatHandlerData ], AssociationQ, "Params" ];
408412
prompts = Replace[ templated, t_TemplateObject :> applyPromptTemplate[ t, params ], { 1 } ];
409-
assembleCustomPrompt[ settings, prompts ] /; MatchQ[ prompts, { ___String } ]
413+
assembleCustomPrompt0[ settings, prompts ] /; MatchQ[ prompts, { ___String } ]
410414
],
411415
throwInternalFailure
412416
];
413417

414-
assembleCustomPrompt // endDefinition;
418+
assembleCustomPrompt0 // endDefinition;
419+
420+
(* ::**************************************************************************************************************:: *)
421+
(* ::Subsubsection::Closed:: *)
422+
(*addUserInstructions*)
423+
addUserInstructions // beginDefinition;
424+
425+
addUserInstructions[ prompts_, settings_Association ] :=
426+
addUserInstructions[ prompts, settings[ "UserInstructions" ] ];
427+
428+
addUserInstructions[ prompts_, None | $$unspecified ] :=
429+
prompts;
430+
431+
addUserInstructions[ prompts_String, custom_String ] :=
432+
prompts <> "\n\n" <> addUserInstructions[ None, custom ];
433+
434+
addUserInstructions[ None, custom_String ] :=
435+
TemplateApply[ $customInstructionsTemplate, custom ];
436+
437+
addUserInstructions // endDefinition;
438+
439+
$customInstructionsTemplate = StringTemplate[ "\
440+
# User Instructions
441+
442+
IMPORTANT: The following user instructions take precedence over ALL other instructions.
443+
444+
<user-instructions>
445+
`1`
446+
</user-instructions>" ];
415447

416448
(* ::**************************************************************************************************************:: *)
417449
(* ::Subsubsection::Closed:: *)

Source/Chatbook/PreferencesContent.wl

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ createNotebookSettingsPanel[ ] := Enclose[
236236
Module[
237237
{
238238
defaultSettingsContent,
239+
instructionsLabel, instructionsContent,
239240
interfaceLabel, interfaceContent,
240241
featuresLabel, featuresContent,
241242
content
@@ -248,6 +249,16 @@ createNotebookSettingsPanel[ ] := Enclose[
248249
"DefaultSettings"
249250
];
250251

252+
(* Label for the standing instructions section: *)
253+
instructionsLabel = subsectionText @ tr[ "PreferencesContentSubsectionInstructions" ];
254+
255+
(* Retrieve and confirm the content for personalization: *)
256+
instructionsContent = ConfirmMatch[
257+
scopedTrackedDynamic[ makeInstructionsContent[ ], "Preferences" ],
258+
Except[ _makeInstructionsContent ],
259+
"Instructions"
260+
];
261+
251262
(* Label for the interface section using a style from SystemDialog.nb: *)
252263
interfaceLabel = subsectionText @ tr[ "PreferencesContentSubsectionChat" ];
253264

@@ -276,6 +287,9 @@ createNotebookSettingsPanel[ ] := Enclose[
276287
{ defaultSettingsContent },
277288
{ Spacer[ 1 ] },
278289
{ Spacer[ 1 ] },
290+
{ instructionsLabel },
291+
{ instructionsContent },
292+
{ Spacer[ 1 ] },
279293
{ interfaceLabel },
280294
{ interfaceContent },
281295
{ Spacer[ 1 ] },
@@ -284,7 +298,7 @@ createNotebookSettingsPanel[ ] := Enclose[
284298
{ featuresContent }
285299
},
286300
Alignment -> { Left, Baseline },
287-
Dividers -> { False, { 3 -> True, 7 -> True } },
301+
Dividers -> { False, { 3 -> True, 6 -> True, 10 -> True } },
288302
ItemSize -> { Fit, Automatic },
289303
Spacings -> { 0, 0.7 }
290304
];
@@ -821,6 +835,87 @@ makeOpenAICompletionURLInput[ False ] := highlightControl[
821835

822836
makeOpenAICompletionURLInput // endDefinition;
823837

838+
(* ::**************************************************************************************************************:: *)
839+
(* ::Subsection::Closed:: *)
840+
(*makeInstructionsContent*)
841+
makeInstructionsContent // beginDefinition;
842+
843+
makeInstructionsContent[ ] :=
844+
DynamicModule[ { initialText, currentText, modified },
845+
846+
initialText = Replace[
847+
CurrentChatSettings[ $preferencesScope, "UserInstructions" ],
848+
"" | Except[ _String ] -> Null
849+
];
850+
851+
currentText = initialText;
852+
modified = False;
853+
854+
Column[
855+
{
856+
tr[ "PreferencesContentInstructionsDescription" ],
857+
EventHandler[
858+
Pane[
859+
prefsInputField[
860+
None,
861+
Dynamic[
862+
currentText,
863+
Function[
864+
currentText = If[ # === "", Null, # ];
865+
modified = currentText =!= initialText;
866+
]
867+
],
868+
String,
869+
ContinuousAction -> True,
870+
FieldHint -> tr[ "PreferencesContentInstructionsHint" ],
871+
ImageSize -> { Scaled[ 1 ], Automatic },
872+
BaseStyle -> {
873+
"controlText",
874+
AutoSpacing -> False,
875+
FontColor -> color @ "PreferencesContentFont_2",
876+
FontSize -> 13
877+
}
878+
],
879+
AppearanceElements -> { },
880+
FrameMargins -> { { 0, 0 }, { 0, 0 } },
881+
ImageSize -> { Scaled[ 1 ], UpTo[ 200 ] },
882+
ImageMargins -> 0,
883+
Scrollbars -> { Automatic, Automatic }
884+
],
885+
{ "ReturnKeyDown" :> FrontEndExecute @ { NotebookWrite[ InputNotebook[ ], "\n", After ] } }
886+
],
887+
PaneSelector[
888+
{
889+
True -> ChoiceButtons[
890+
{
891+
tr[ "SaveChangesButton" ],
892+
tr[ "CancelButton" ]
893+
},
894+
{
895+
CurrentChatSettings[ $preferencesScope, "UserInstructions" ] =
896+
If[ StringQ @ currentText && StringTrim @ currentText =!= "",
897+
currentText,
898+
Inherited
899+
];
900+
901+
initialText = currentText;
902+
modified = False;
903+
,
904+
currentText = initialText;
905+
modified = False;
906+
}
907+
],
908+
False -> Spacer[ 1 ]
909+
},
910+
Dynamic @ modified,
911+
ImageSize -> Automatic
912+
]
913+
}
914+
]
915+
];
916+
917+
makeInstructionsContent // endDefinition;
918+
824919
(* ::**************************************************************************************************************:: *)
825920
(* ::Subsection::Closed:: *)
826921
(*makeInterfaceContent*)
@@ -1928,7 +2023,7 @@ resetButton[ Dynamic[ tab_ ] ] :=
19282023
label,
19292024

19302025
Needs[ "Wolfram`Chatbook`" -> None ];
1931-
resetChatPreferences @ Replace[ tab, CurrentChatSettings[ $preferencesScope, "CurrentPreferencesTab" ], $$unspecified -> "Services" ]
2026+
resetChatPreferences @ Replace[ tab, $$unspecified -> "Services" ]
19322027
,
19332028
BaseStyle -> {
19342029
FontFamily -> Dynamic @ FrontEnd`CurrentValue[ "ControlsFontFamily" ],

0 commit comments

Comments
 (0)