@@ -18,6 +18,7 @@ $ContextAliases[ "tokens`" ] = "Wolfram`LLMFunctions`Utilities`Tokenization`";
1818Chatbook ::InvalidMessageList = "The value `1` is not a valid list of chat messages." ;
1919Chatbook ::InvalidChatSettings = "The value `1` is not a valid chat settings specification." ;
2020Chatbook ::InvalidExcludedBasePrompts = "The value `1` is not a valid list of excluded base prompts." ;
21+ Chatbook ::InvalidPromptComponent = "Expected a string or list of strings instead of `1`." ;
2122
2223(* ::**************************************************************************************************************:: *)
2324(* ::Section::Closed:: *)
@@ -407,10 +408,11 @@ assembleCustomPrompt0[ settings_, prompt_String ] := prompt;
407408assembleCustomPrompt0 [ settings_ , prompts : { ___ String } ] := StringRiffle [ prompts , "\n\n " ];
408409
409410assembleCustomPrompt0 [ settings_ ? AssociationQ , templated : { ___ , _ TemplateObject , ___ } ] := Enclose [
410- Module [ { params , prompts },
411- params = ConfirmBy [ Association [ settings , $ChatHandlerData ], AssociationQ , "Params" ];
412- prompts = Replace [ templated , t_ TemplateObject :> applyPromptTemplate [ t , params ], { 1 } ];
413- assembleCustomPrompt0 [ settings , prompts ] /; MatchQ [ prompts , { ___ String } ]
411+ Module [ { params , prompts , validated },
412+ params = ConfirmBy [ Association [ settings , $ChatHandlerData ], AssociationQ , "Params" ];
413+ prompts = Replace [ templated , t_ TemplateObject :> applyPromptTemplate [ t , params ], { 1 } ];
414+ validated = ConfirmMatch [ checkPromptComponent /@ prompts , { ___ String }, "Validated" ];
415+ assembleCustomPrompt0 [ settings , validated ]
414416 ],
415417 throwInternalFailure
416418];
@@ -452,12 +454,21 @@ applyPromptTemplate // beginDefinition;
452454
453455applyPromptTemplate [ template_ TemplateObject , params_ Association ] :=
454456 If [ FreeQ [ template , TemplateSlot [ _ String , ___ ] ],
455- TemplateApply @ template ,
456- TemplateApply [ template , params ]
457+ TemplateApply @ replaceCellContext @ template ,
458+ TemplateApply [ replaceCellContext @ template , params ]
457459 ];
458460
459461applyPromptTemplate // endDefinition ;
460462
463+ (* ::**************************************************************************************************************:: *)
464+ (* ::Subsubsection::Closed:: *)
465+ (*checkPromptComponent*)
466+ checkPromptComponent // beginDefinition ;
467+ checkPromptComponent [ prompt_ String ] := prompt ;
468+ checkPromptComponent [ prompts : { ___ String } ] := StringJoin @ prompts ;
469+ checkPromptComponent [ other_ ] := throwFailure [ "InvalidPromptComponent" , other ];
470+ checkPromptComponent // endDefinition ;
471+
461472(* ::**************************************************************************************************************:: *)
462473(* ::Subsection::Closed:: *)
463474(*makeChatMessages*)
0 commit comments