Skip to content

Commit

Permalink
When other text-like cells are included in the chat context, use a sp…
Browse files Browse the repository at this point in the history
…ecial indicator for chat inputs to distinguish them from regular text cells
  • Loading branch information
rhennigan committed Jan 26, 2024
1 parent e5ec25d commit f680b74
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 10 deletions.
13 changes: 13 additions & 0 deletions Source/Chatbook/ChatMessages.wl
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ makeChatMessages[ settings_, cells_ ] :=
settings[ "MaxCellStringLength" ],
Except[ $$size ] -> $defaultMaxCellStringLength
],
$chatInputIndicator = mixedContentQ @ cells,
$cellStringBudget
},
$cellStringBudget = $initialCellStringBudget;
Expand Down Expand Up @@ -274,6 +275,18 @@ makeChatMessages0[ settings0_, cells_List ] := Enclose[

makeChatMessages0 // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsection::Closed:: *)
(*mixedContentQ*)
mixedContentQ // beginDefinition;

mixedContentQ[ cells: { ___Cell } ] := And[
MemberQ[ cells, Cell[ __, $$chatInputStyle, ___ ] ],
MemberQ[ cells, Cell[ _, Except[ $$chatInputStyle|$$chatOutputStyle, _String ], ___ ] ]
];

mixedContentQ // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsection::Closed:: *)
(*addExcisedCellMessage*)
Expand Down
4 changes: 3 additions & 1 deletion Source/Chatbook/Common.wl
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ BeginPackage[ "Wolfram`Chatbook`Common`" ];
`$thisPaclet;
`$debugData;
`$settingsData;

`$chatIndicatorSymbol;
`$chatDelimiterStyles;
`$chatIgnoredStyles;
`$chatInputStyles;
Expand Down Expand Up @@ -63,6 +63,8 @@ $cloudNotebooks := TrueQ @ CloudSystem`$CloudNotebooks;
(* ::**************************************************************************************************************:: *)
(* ::Section::Closed:: *)
(*Config*)
$chatIndicatorSymbol = "\|01f4ac";

$chatDelimiterStyles = { "ChatBlockDivider", "ChatDelimiter", "ExcludedChatDelimiter" };
$chatIgnoredStyles = { "ChatExcluded" };
$chatInputStyles = { "ChatInput", "SideChat", "ChatQuery", "ChatSystemInput" };
Expand Down
27 changes: 20 additions & 7 deletions Source/Chatbook/Prompting.wl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ $basePromptOrder = {
"DocumentationLinkSyntax",
"InlineSymbolLinks",
"MessageConversionHeader",
"ChatInputIndicator",
"ConversionLargeOutputs",
"ConversionGraphics",
"MarkdownImageBox",
Expand Down Expand Up @@ -73,6 +74,7 @@ $basePromptDependencies = Append[ "GeneralInstructionsHeader" ] /@ <|
"DocumentationLinkSyntax" -> { },
"InlineSymbolLinks" -> { },
"MessageConversionHeader" -> { "NotebooksPreamble" },
"ChatInputIndicator" -> { "MessageConversionHeader" },
"ConversionLargeOutputs" -> { "MessageConversionHeader" },
"ConversionGraphics" -> { "MessageConversionHeader" },
"MarkdownImageBox" -> { "MessageConversionHeader" },
Expand Down Expand Up @@ -151,6 +153,11 @@ Only do this in text, not code.";
$basePromptComponents[ "MessageConversionHeader" ] = "\
* The messages you see have been converted from notebook content, and will often be different from what the user sees:";

$basePromptComponents[ "ChatInputIndicator" ] = "\
* Users send you chat messages via special evaluatable \"ChatInput\" cells and will be indicated with the string \
$$chatIndicatorSymbol$$. Cells appearing above the chat input are included to provide additional context, \
but chat inputs represent the actual message from the user to you.";

$basePromptComponents[ "ConversionLargeOutputs" ] = "\
* Large outputs may be shortened: ``DynamicModule[<<4>>]``";

Expand Down Expand Up @@ -234,13 +241,19 @@ $basePrompt := buildPrompt[ ];
(*buildPrompt*)
buildPrompt // beginDefinition;

buildPrompt[ ] := (
expandPromptComponents[ ];
StringRiffle[
Values @ KeyTake[ $basePromptComponents, Values @ KeyTake[ $collectedPromptComponents, $basePromptOrder ] ],
"\n"
]
);
buildPrompt[ ] := Enclose[
Module[ { keys, ordered, string },
expandPromptComponents[ ];
keys = ConfirmMatch[ Values @ KeyTake[ $collectedPromptComponents, $basePromptOrder ], { ___String }, "Keys" ];
ordered = ConfirmMatch[ Values @ KeyTake[ $basePromptComponents, keys ], { ___String }, "Ordered" ];
string = ConfirmBy[ StringRiffle[ ordered, "\n" ], StringQ, "String" ];
If[ StringQ @ $chatIndicatorSymbol,
StringReplace[ string, "$$chatIndicatorSymbol$$" -> $chatIndicatorSymbol ],
string
]
],
throwInternalFailure
];

buildPrompt // endDefinition;

Expand Down
13 changes: 13 additions & 0 deletions Source/Chatbook/SendChat.wl
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ sendChat[ evalCell_, nbo_, settings0_ ] /; $useLLMServices := catchTopAs[ Chatbo
];

$multimodalMessages = TrueQ @ settings[ "Multimodal" ];
$chatIndicatorSymbol = chatIndicatorSymbol @ settings;

If[ TrueQ @ settings[ "EnableChatGroupSettings" ],
AppendTo[ settings, "ChatGroupSettings" -> getChatGroupSettings @ evalCell ]
Expand Down Expand Up @@ -197,6 +198,7 @@ sendChat[ evalCell_, nbo_, settings0_ ] := catchTopAs[ ChatbookAction ] @ Enclos
];

$multimodalMessages = TrueQ @ settings[ "Multimodal" ];
$chatIndicatorSymbol = chatIndicatorSymbol @ settings;

If[ TrueQ @ settings[ "EnableChatGroupSettings" ],
AppendTo[ settings, "ChatGroupSettings" -> getChatGroupSettings @ evalCell ]
Expand Down Expand Up @@ -351,6 +353,16 @@ makeHTTPRequest[ settings_Association? AssociationQ, messages: { __Association }

makeHTTPRequest // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsection::Closed:: *)
(*chatIndicatorSymbol*)
chatIndicatorSymbol // beginDefinition;
chatIndicatorSymbol[ settings_Association ] := chatIndicatorSymbol @ settings[ "ChatInputIndicator" ];
chatIndicatorSymbol[ $$unspecified ] := $chatIndicatorSymbol;
chatIndicatorSymbol[ None|"" ] := None;
chatIndicatorSymbol[ string_String? StringQ ] := string;
chatIndicatorSymbol // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsection::Closed:: *)
(*prepareMessagesForHTTPRequest*)
Expand Down Expand Up @@ -1255,6 +1267,7 @@ resolveAutoSetting // endDefinition;
resolveAutoSetting0 // beginDefinition;
resolveAutoSetting0[ as_, "Assistance" ] := False;
resolveAutoSetting0[ as_, "DynamicAutoFormat" ] := dynamicAutoFormatQ @ as;
resolveAutoSetting0[ as_, "ChatInputIndicator" ] := "\|01f4ac";
resolveAutoSetting0[ as_, "EnableLLMServices" ] := $useLLMServices;
resolveAutoSetting0[ as_, "HandlerFunctionsKeys" ] := chatHandlerFunctionsKeys @ as;
resolveAutoSetting0[ as_, "IncludeHistory" ] := Automatic;
Expand Down
8 changes: 8 additions & 0 deletions Source/Chatbook/Serialization.wl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ CellToString[cell$] serializes a Cell expression as a string for use in chat.\

`$CellToStringDebug;
`$CurrentCell;
`$chatInputIndicator;
`$defaultMaxCellStringLength;
`$defaultMaxOutputCellStringLength;
`$longNameCharacters;
Expand Down Expand Up @@ -311,6 +312,13 @@ cellToString[ cells: { __CellObject } ] := cellsToString @ NotebookRead @ cells;
cellToString[ Cell[ a__, style: $$noCellLabelStyle, b___, CellLabel -> _, c___ ] ] :=
cellToString @ Cell[ a, style, b, c ];

(* Include chat input indicator for mixed content *)
cellToString[ cell: Cell[ __, $$chatInputStyle, ___ ] ] /; $chatInputIndicator && StringQ @ $chatIndicatorSymbol :=
Block[ { $chatInputIndicator = False },
needsBasePrompt[ "ChatInputIndicator" ];
$chatIndicatorSymbol <> " " <> cellToString @ cell
];

(* Convert delimiters to equivalent markdown *)
cellToString[ Cell[ __, $$delimiterStyle, ___ ] ] := $delimiterString;
cellToString[ Cell[ __, "ExcludedChatDelimiter", ___ ] ] := "";
Expand Down
5 changes: 3 additions & 2 deletions Source/Chatbook/Settings.wl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ $defaultChatSettings = <|
"ChatContextPreprompt" -> Automatic,
"ChatDrivenNotebook" -> False,
"ChatHistoryLength" -> 100,
"ChatInputIndicator" -> Automatic,
"DynamicAutoFormat" -> Automatic,
"EnableChatGroupSettings" -> False,
"EnableLLMServices" -> Automatic, (* TODO: remove this once LLMServices is widely available *)
Expand Down Expand Up @@ -654,8 +655,8 @@ $currentEvaluationObject := $FrontEndSession;
(* ::Subsection::Closed:: *)
(*verifyInheritance*)
verifyInheritance // beginDefinition;
verifyInheritance[ obj: $$feObj ] /; $cloudNotebooks := True;
verifyInheritance[ obj: $$feObj? inheritingQ ] := True;
verifyInheritance[ obj_ ] /; $SynchronousEvaluation || $cloudNotebooks := True;
verifyInheritance[ obj_? inheritingQ ] := True;
verifyInheritance[ obj: $$feObj ] := With[ { verified = verifyInheritance0 @ obj }, inheritingQ @ obj ];
verifyInheritance // endDefinition;

Expand Down

0 comments on commit f680b74

Please sign in to comment.