From f2c4f8674d0658fe390001cb87d48392be311add Mon Sep 17 00:00:00 2001 From: Rick Hennigan Date: Mon, 16 Dec 2024 13:00:50 -0500 Subject: [PATCH 1/4] Inherit some settings when creating a chat notebook from workspace chat --- PacletInfo.wl | 2 +- Source/Chatbook/ChatModes/UI.wl | 59 +++++++++++++++++++++++++++------ 2 files changed, 49 insertions(+), 12 deletions(-) diff --git a/PacletInfo.wl b/PacletInfo.wl index 2fa06525..0bc24492 100644 --- a/PacletInfo.wl +++ b/PacletInfo.wl @@ -1,7 +1,7 @@ PacletObject[ <| "Name" -> "Wolfram/Chatbook", "PublisherID" -> "Wolfram", - "Version" -> "2.0.4", + "Version" -> "2.0.5", "WolframVersion" -> "14.1+", "Description" -> "Wolfram Notebooks + LLMs", "License" -> "MIT", diff --git a/Source/Chatbook/ChatModes/UI.wl b/Source/Chatbook/ChatModes/UI.wl index 86752675..e05fd730 100644 --- a/Source/Chatbook/ChatModes/UI.wl +++ b/Source/Chatbook/ChatModes/UI.wl @@ -1380,7 +1380,7 @@ SaveAsChatNotebook // endExportedDefinition; saveAsChatNB // beginDefinition; saveAsChatNB[ targetObj_NotebookObject ] := Enclose[ - Catch @ Module[ { cellObjects, title, filepath, cells, nbExpr }, + Catch @ Module[ { cellObjects, title, filepath, cells, settings, nbExpr }, cellObjects = Cells @ targetObj; If[ ! MatchQ[ cellObjects, { __CellObject } ], Throw @ Null ]; title = Replace[ @@ -1400,7 +1400,8 @@ saveAsChatNB[ targetObj_NotebookObject ] := Enclose[ StringQ @ filepath && StringEndsQ[ filepath, ".nb" ], cells = NotebookRead @ cellObjects; If[ ! MatchQ[ cells, { __Cell } ], Throw @ Null ]; - nbExpr = ConfirmMatch[ cellsToChatNB @ cells, _Notebook, "Converted" ]; + settings = CurrentChatSettings @ targetObj; + nbExpr = ConfirmMatch[ cellsToChatNB[ cells, settings ], _Notebook, "Converted" ]; ConfirmBy[ Export[ filepath, nbExpr, "NB" ], FileExistsQ, "Exported" ], True, Null @@ -1415,24 +1416,24 @@ saveAsChatNB // endDefinition; (* ::Subsection::Closed:: *) (*popOutChatNB*) popOutChatNB // beginDefinition; -popOutChatNB[ nbo_NotebookObject ] := popOutChatNB @ NotebookGet @ nbo; -popOutChatNB[ Notebook[ cells_, ___ ] ] := popOutChatNB @ cells; -popOutChatNB[ Dynamic[ messageList_ ] ] := popOutChatNB @ messageList; -popOutChatNB[ cells: { ___Cell } ] := NotebookPut @ cellsToChatNB @ cells; -popOutChatNB[ chat_Association ] := popOutChatNB0 @ chat; +popOutChatNB[ nbo_NotebookObject ] := popOutChatNB[ NotebookGet @ nbo, CurrentChatSettings @ nbo ]; +popOutChatNB[ Notebook[ cells_, ___ ], settings_ ] := popOutChatNB[ cells, settings ]; +popOutChatNB[ cells: { ___Cell }, settings_ ] := NotebookPut @ cellsToChatNB[ cells, settings ]; +popOutChatNB[ chat_Association, settings_Association ] := popOutChatNB0[ chat, settings ]; popOutChatNB // endDefinition; popOutChatNB0 // beginDefinition; -popOutChatNB0[ id_ ] := Enclose[ - Module[ { loaded, uuid, title, messages, cells }, +popOutChatNB0[ id_, settings_Association ] := Enclose[ + Module[ { loaded, uuid, title, messages, cells, options }, loaded = ConfirmBy[ LoadChat @ id, AssociationQ, "Loaded" ]; uuid = ConfirmBy[ loaded[ "ConversationUUID" ], StringQ, "UUID" ]; title = ConfirmBy[ loaded[ "ConversationTitle" ], StringQ, "Title" ]; messages = ConfirmBy[ loaded[ "Messages" ], ListQ, "Messages" ]; cells = ConfirmMatch[ ChatMessageToCell @ messages, { __Cell }, "Cells" ]; - ConfirmMatch[ CreateChatNotebook @ cells, _NotebookObject, "Notebook" ] + options = Sequence @@ Normal[ settings, Association ]; + ConfirmMatch[ CreateChatNotebook[ cells, options ], _NotebookObject, "Notebook" ] ], throwInternalFailure ]; @@ -1447,9 +1448,45 @@ cellsToChatNB // beginDefinition; cellsToChatNB[ cells: { ___Cell } ] := Notebook[ cells /. $fromWorkspaceChatConversionRules, StyleDefinitions -> "Chatbook.nb" ]; +cellsToChatNB[ cells: { ___Cell }, settings_Association ] := + Module[ { dingbat, notebook }, + + dingbat = Cell[ BoxData @ makeOutputDingbat @ settings, Background -> None ]; + + notebook = ReplaceAll[ + cellsToChatNB @ cells, + { + Cell[ a__, "ChatInput", b___ ] :> Cell[ a, "ChatInput", b, CellDingbat -> $evaluatedChatInputDingbat ], + Cell[ a__, "ChatOutput", b___ ] :> Cell[ a, "ChatOutput", b, CellDingbat -> dingbat ] + } + ]; + + Append[ + notebook, + TaggingRules -> <| "ChatNotebookSettings" -> KeyTake[ settings, $popOutSettings ] |> + ] + ]; + cellsToChatNB // endDefinition; +$popOutSettings = { "LLMEvaluator", "Model" }; + + +$evaluatedChatInputDingbat = Cell[ + BoxData @ DynamicBox @ ToBoxes[ + If[ TrueQ @ CloudSystem`$CloudNotebooks, + RawBoxes @ TemplateBox[ { }, "ChatIconUser" ], + RawBoxes @ TemplateBox[ { }, "ChatInputCellDingbat" ] + ], + StandardForm + ], + Background -> None, + CellFrame -> 0, + CellMargins -> 0 +]; + + $fromWorkspaceChatConversionRules := $fromWorkspaceChatConversionRules = Dispatch @ { Cell[ BoxData @ TemplateBox[ { Cell[ TextData[ text_ ], ___ ] }, "UserMessageBox", ___ ], "ChatInput", ___ ] :> Cell[ Flatten @ TextData @ text, "ChatInput" ] @@ -1977,7 +2014,7 @@ makeHistoryMenuItem[ Dynamic[ rows_ ], nbo_NotebookObject, chat_Association ] := { { Button[ $popOutButtonLabel, - popOutChatNB @ chat, + popOutChatNB[ chat, CurrentChatSettings @ nbo ], Appearance -> "Suppressed" ], Button[ From b81b1469eb77ee50dcfcb1eeb347c75f9c11a6e8 Mon Sep 17 00:00:00 2001 From: Rick Hennigan Date: Mon, 16 Dec 2024 13:16:52 -0500 Subject: [PATCH 2/4] Update cell dingbats when popping out from history menu --- Source/Chatbook/ChatModes/UI.wl | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/Source/Chatbook/ChatModes/UI.wl b/Source/Chatbook/ChatModes/UI.wl index e05fd730..81c97e41 100644 --- a/Source/Chatbook/ChatModes/UI.wl +++ b/Source/Chatbook/ChatModes/UI.wl @@ -1426,12 +1426,13 @@ popOutChatNB // endDefinition; popOutChatNB0 // beginDefinition; popOutChatNB0[ id_, settings_Association ] := Enclose[ - Module[ { loaded, uuid, title, messages, cells, options }, + Module[ { loaded, uuid, title, messages, dingbat, cells, options }, loaded = ConfirmBy[ LoadChat @ id, AssociationQ, "Loaded" ]; uuid = ConfirmBy[ loaded[ "ConversationUUID" ], StringQ, "UUID" ]; title = ConfirmBy[ loaded[ "ConversationTitle" ], StringQ, "Title" ]; messages = ConfirmBy[ loaded[ "Messages" ], ListQ, "Messages" ]; - cells = ConfirmMatch[ ChatMessageToCell @ messages, { __Cell }, "Cells" ]; + dingbat = Cell[ BoxData @ makeOutputDingbat @ settings, Background -> None ]; + cells = ConfirmMatch[ updateCellDingbats[ ChatMessageToCell @ messages, dingbat ], { __Cell }, "Cells" ]; options = Sequence @@ Normal[ settings, Association ]; ConfirmMatch[ CreateChatNotebook[ cells, options ], _NotebookObject, "Notebook" ] ], @@ -1452,14 +1453,7 @@ cellsToChatNB[ cells: { ___Cell }, settings_Association ] := Module[ { dingbat, notebook }, dingbat = Cell[ BoxData @ makeOutputDingbat @ settings, Background -> None ]; - - notebook = ReplaceAll[ - cellsToChatNB @ cells, - { - Cell[ a__, "ChatInput", b___ ] :> Cell[ a, "ChatInput", b, CellDingbat -> $evaluatedChatInputDingbat ], - Cell[ a__, "ChatOutput", b___ ] :> Cell[ a, "ChatOutput", b, CellDingbat -> dingbat ] - } - ]; + notebook = updateCellDingbats[ cellsToChatNB @ cells, dingbat ]; Append[ notebook, @@ -1498,6 +1492,21 @@ $fromWorkspaceChatConversionRules := $fromWorkspaceChatConversionRules = Dispatc Cell[ Flatten @ TextData @ text, "ChatOutput" ] }; +(* ::**************************************************************************************************************:: *) +(* ::Subsubsection::Closed:: *) +(*updateCellDingbats*) +updateCellDingbats // beginDefinition; + +updateCellDingbats[ cells_, outputDingbat_ ] := ReplaceAll[ + cells, + { + Cell[ a__, "ChatInput" , b___ ] :> Cell[ a, "ChatInput" , b, CellDingbat -> $evaluatedChatInputDingbat ], + Cell[ a__, "ChatOutput", b___ ] :> Cell[ a, "ChatOutput", b, CellDingbat -> outputDingbat ] + } +]; + +updateCellDingbats // endDefinition; + (* ::**************************************************************************************************************:: *) (* ::Section::Closed:: *) (*Overlay Menus*) From b63053f06feaf99391415bffac76774bf098eaec Mon Sep 17 00:00:00 2001 From: Rick Hennigan Date: Mon, 16 Dec 2024 13:18:57 -0500 Subject: [PATCH 3/4] TODO --- Source/Chatbook/ChatModes/UI.wl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Source/Chatbook/ChatModes/UI.wl b/Source/Chatbook/ChatModes/UI.wl index 81c97e41..5341cf19 100644 --- a/Source/Chatbook/ChatModes/UI.wl +++ b/Source/Chatbook/ChatModes/UI.wl @@ -1466,7 +1466,7 @@ cellsToChatNB // endDefinition; $popOutSettings = { "LLMEvaluator", "Model" }; - +(* TODO: we should really have something better for this *) $evaluatedChatInputDingbat = Cell[ BoxData @ DynamicBox @ ToBoxes[ If[ TrueQ @ CloudSystem`$CloudNotebooks, From df931875b3bfef2b0e3dac8d2a4514e646031b79 Mon Sep 17 00:00:00 2001 From: Rick Hennigan Date: Mon, 16 Dec 2024 13:28:55 -0500 Subject: [PATCH 4/4] Inherit more settings --- Source/Chatbook/ChatModes/UI.wl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Source/Chatbook/ChatModes/UI.wl b/Source/Chatbook/ChatModes/UI.wl index 5341cf19..9689e365 100644 --- a/Source/Chatbook/ChatModes/UI.wl +++ b/Source/Chatbook/ChatModes/UI.wl @@ -1464,7 +1464,12 @@ cellsToChatNB[ cells: { ___Cell }, settings_Association ] := cellsToChatNB // endDefinition; -$popOutSettings = { "LLMEvaluator", "Model" }; +$popOutSettings = { + "LLMEvaluator", + "MaxContextTokens", + "MaxToolResponses", + "Model" +}; (* TODO: we should really have something better for this *) $evaluatedChatInputDingbat = Cell[