Skip to content

Commit

Permalink
Merge pull request #536 from WolframResearch/529-cloud-support-global…
Browse files Browse the repository at this point in the history
…-chat-preferences

Initial version of a chat preferences dialog
  • Loading branch information
rhennigan authored Jan 10, 2024
2 parents b7bef28 + 074cd7d commit eaabc3a
Show file tree
Hide file tree
Showing 6 changed files with 384 additions and 228 deletions.
4 changes: 2 additions & 2 deletions FrontEnd/StyleSheets/Chatbook.nb
Original file line number Diff line number Diff line change
Expand Up @@ -1161,7 +1161,7 @@ Notebook[
],
Cell[
StyleData["ChatStyleSheetInformation"],
TaggingRules -> <|"StyleSheetVersion" -> "1.4.0.3913547000"|>
TaggingRules -> <|"StyleSheetVersion" -> "1.4.1.3913792210"|>
],
Cell[
StyleData["Text"],
Expand Down Expand Up @@ -17593,4 +17593,4 @@ Notebook[
]
},
StyleDefinitions -> "PrivateStylesheetFormatting.nb"
]
]
1 change: 1 addition & 0 deletions Source/Chatbook/ChatHistory.wl
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ chatExcludedQ[ KeyValuePattern[ "Style" -> $$chatIgnoredStyle ] ] := True;
chatExcludedQ[ KeyValuePattern[ "ChatNotebookSettings" -> settings_ ] ] := chatExcludedQ @ settings;
chatExcludedQ[ KeyValuePattern[ "ExcludeFromChat" -> exclude_ ] ] := TrueQ @ exclude;
chatExcludedQ[ KeyValuePattern[ { } ] ] := False;
chatExcludedQ[ Inherited ] := False;

chatExcludedQ // endDefinition;

Expand Down
179 changes: 57 additions & 122 deletions Source/Chatbook/CloudToolbar.wl
Original file line number Diff line number Diff line change
Expand Up @@ -34,142 +34,77 @@ $notebookTypeLabelOptions = Sequence[
(*makeChatCloudDockedCellContents*)
makeChatCloudDockedCellContents // beginDefinition;

makeChatCloudDockedCellContents[ ] :=
Block[ { $preferencesScope := EvaluationNotebook[ ] },
DynamicWrapper[
Grid[
{
{
Item[ $cloudChatBanner, Alignment -> Left ],
Item[ "", ItemSize -> Fit ],
makePersonaSelector[ ],
cloudModelSelector[ ]
}
},
Alignment -> { Left, Baseline },
Dividers -> { { False, False, False, True }, False },
Spacings -> { 2, 0 },
BaseStyle -> { "Text", FontSize -> 14, FontColor -> GrayLevel[ 0.4 ] },
FrameStyle -> Directive[ Thickness[ 2 ], GrayLevel[ 0.9 ] ]
],
Needs[ "GeneralUtilities`" -> None ];
CurrentValue[ EvaluationNotebook[ ], TaggingRules ] =
GeneralUtilities`ToAssociations @ Replace[
CurrentValue[ EvaluationNotebook[ ], TaggingRules ],
Except[ KeyValuePattern @ { } ] :> <| |>
]
]
];
makeChatCloudDockedCellContents[ ] := Grid[
{
{
Item[ $cloudChatBanner, Alignment -> Left ],
Item[ "", ItemSize -> Fit ],
makePersonaSelector[ ],
cloudModelSelector[ ],
cloudPreferencesButton[ ]
}
},
Alignment -> { Left, Baseline },
Dividers -> { { False, False, False, True, True }, False },
Spacings -> { 2, 0 },
BaseStyle -> { "Text", FontSize -> 14, FontColor -> GrayLevel[ 0.4 ] },
FrameStyle -> Directive[ Thickness[ 2 ], GrayLevel[ 0.9 ] ]
];

makeChatCloudDockedCellContents // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsection::Closed:: *)
(*cloudModelSelector*)
cloudModelSelector // beginDefinition;
(*cloudPreferencesButton*)
cloudPreferencesButton // beginDefinition;

cloudPreferencesButton[ ] := Enclose[
Module[ { iconTemplate, colorTemplate, mouseover, buttonIcon, button },

cloudModelSelector[ ] :=
DynamicModule[ { serviceSelector, modelSelector },

serviceSelector = PopupMenu[
Dynamic[
Replace[
CurrentValue[ EvaluationNotebook[ ], { TaggingRules, "ChatNotebookSettings", "Model" } ],
{
_String|Inherited :> "OpenAI",
KeyValuePattern[ "Service" -> service_String ] :> service,
_ :> Set[
CurrentValue[
EvaluationNotebook[ ],
{ TaggingRules, "ChatNotebookSettings", "Model" }
],
$DefaultModel
][ "Service" ]
}
],
Function[
CurrentValue[
EvaluationNotebook[ ],
{ TaggingRules, "ChatNotebookSettings", "Model", "Service" }
] = #1;

CurrentValue[
EvaluationNotebook[ ],
{ TaggingRules, "ChatNotebookSettings", "Model", "Name" }
] = Automatic;

cloudModelNameSelector[ Dynamic @ modelSelector, #1 ]
]
],
KeyValueMap[
#1 -> Row @ { inlineTemplateBoxes[ #2[ "Icon" ] ], Spacer[ 1 ], #2[ "Service" ] } &,
$availableServices
]
iconTemplate = ConfirmMatch[
chatbookIcon[ "ToolManagerCog", False ],
RawBoxes @ TemplateBox[ { }, __ ],
"IconTemplate"
];

cloudModelNameSelector[
Dynamic @ modelSelector,
Replace[
CurrentValue[ EvaluationNotebook[ ], { TaggingRules, "ChatNotebookSettings", "Model" } ],
{
_String|Inherited :> "OpenAI",
KeyValuePattern[ "Service" -> service_String ] :> service,
_ :> Set[
CurrentValue[ EvaluationNotebook[ ], { TaggingRules, "ChatNotebookSettings", "Model" } ],
$DefaultModel
][ "Service" ]
}
]
colorTemplate = ConfirmMatch[
Insert[ iconTemplate, #, { 1, 1, 1 } ],
RawBoxes @ TemplateBox[ { _? ColorQ }, __ ],
"Colorize"
] &;

mouseover = Mouseover[
colorTemplate @ GrayLevel[ 0.65 ],
colorTemplate @ Hue[ 0.59, 0.9, 0.93 ]
];

Row @ {
"LLM Service: ", serviceSelector,
Spacer[ 5 ],
"Model: ", Dynamic @ modelSelector
}
];
buttonIcon = DeleteCases[
mouseover /. HoldPattern[ ImageSize -> _ ] :> ImageSize -> { 22, 22 },
BaselinePosition -> _,
Infinity
];

cloudModelSelector // endDefinition;
button = Button[
Tooltip[ buttonIcon, "Global chat preferences" ],
$cloudEvaluationNotebook = EvaluationNotebook[ ];
CreateDialog @ Style[ Dynamic @ notebookSettingsPanel[ ], "Text" ],
Appearance -> "Suppressed",
Method -> "Queued"
];

cloudPreferencesButton[ ] = Pane[ button, FrameMargins -> { { 0, 10 }, { 0, 0 } } ]
],
throwInternalFailure
];

cloudPreferencesButton // endDefinition;

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

cloudModelNameSelector[ Dynamic[ modelSelector_ ], service_String ] :=
modelSelector = DynamicModule[ { display, models },
display = ProgressIndicator[ Appearance -> "Percolate" ];
Dynamic[ display ],
Initialization :> (
models = getServiceModelList @ service;
If[ SameQ[
CurrentValue[ EvaluationNotebook[ ], { TaggingRules, "ChatNotebookSettings", "Model", "Name" } ],
Automatic
],
CurrentValue[ EvaluationNotebook[ ], { TaggingRules, "ChatNotebookSettings", "Model", "Name" } ] =
First[ models, <| "Name" -> Automatic |> ][ "Name" ]
];

display = PopupMenu[
Dynamic[
Replace[
CurrentChatSettings[ EvaluationNotebook[ ], "Model" ],
{ KeyValuePattern[ "Name" -> model_String ] :> model, _ :> Automatic }
],
Function[
CurrentValue[
EvaluationNotebook[ ],
{ TaggingRules, "ChatNotebookSettings", "Model", "Name" }
] = #1
]
],
(#Name -> #DisplayName &) /@ models
]
),
SynchronousInitialization -> False
];

cloudModelNameSelector // endDefinition;
(*cloudModelSelector*)
cloudModelSelector // beginDefinition;
cloudModelSelector[ ] := makeModelSelector[ ];
cloudModelSelector // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Section::Closed:: *)
Expand Down
Loading

0 comments on commit eaabc3a

Please sign in to comment.