Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Services" tab to cloud preferences #541

Merged
merged 4 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 62 additions & 16 deletions Source/Chatbook/CloudToolbar.wl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Needs[ "Wolfram`Chatbook`Dialogs`" ];
Needs[ "Wolfram`Chatbook`Dynamics`" ];
Needs[ "Wolfram`Chatbook`PreferencesContent`" ];
Needs[ "Wolfram`Chatbook`Services`" ];
Needs[ "Wolfram`Chatbook`UI`" ];

(* ::**************************************************************************************************************:: *)
(* ::Section::Closed:: *)
Expand All @@ -39,13 +40,10 @@ 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 ] ]
Expand All @@ -59,7 +57,7 @@ makeChatCloudDockedCellContents // endDefinition;
cloudPreferencesButton // beginDefinition;

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

iconTemplate = ConfirmMatch[
chatbookIcon[ "ToolManagerCog", False ],
Expand All @@ -73,24 +71,20 @@ cloudPreferencesButton[ ] := Enclose[
"Colorize"
] &;

mouseover = Mouseover[
colorTemplate @ GrayLevel[ 0.65 ],
colorTemplate @ Hue[ 0.59, 0.9, 0.93 ]
];
buttonLabel = Grid[
{ { Style[ "Chat Settings", FontColor -> # ], colorTemplate @ # } },
Alignment -> { Left, Baseline }
] &;

mouseover = Mouseover[ buttonLabel @ GrayLevel[ 0.25 ], buttonLabel @ Hue[ 0.59, 0.9, 0.93 ] ];

buttonIcon = DeleteCases[
mouseover /. HoldPattern[ ImageSize -> _ ] :> ImageSize -> { 22, 22 },
mouseover (*/. HoldPattern[ ImageSize -> _ ] :> ImageSize -> { 22, 22 }*),
BaselinePosition -> _,
Infinity
];

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

cloudPreferencesButton[ ] = Pane[ button, FrameMargins -> { { 0, 10 }, { 0, 0 } } ]
],
Expand All @@ -99,6 +93,58 @@ cloudPreferencesButton[ ] := Enclose[

cloudPreferencesButton // endDefinition;

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

togglePreferences[ nbo_NotebookObject ] :=
togglePreferences[ nbo, Flatten @ List @ CurrentValue[ nbo, DockedCells ] ];

togglePreferences[ nbo_NotebookObject, { cell_Cell } ] :=
SetOptions[ nbo, DockedCells -> { cell, $cloudPreferencesCell } ];

togglePreferences[ nbo_NotebookObject, { Inherited|$Failed } ] := SetOptions[
nbo,
DockedCells -> {
Cell[ BoxData @ DynamicBox @ ToBoxes @ MakeChatCloudDockedCellContents[ ], Background -> None ],
$cloudPreferencesCell
}
];

togglePreferences[ nbo_NotebookObject, { cell_Cell, _Cell } ] :=
SetOptions[ nbo, DockedCells -> { cell } ];

togglePreferences[ nbo_NotebookObject, _ ] :=
SetOptions[ nbo, DockedCells -> Inherited ];

togglePreferences // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsubsection::Closed:: *)
(*$cloudPreferencesCell*)
$cloudPreferencesCell := $cloudPreferencesCell = Cell[
BoxData @ ToBoxes @ Pane[
Dynamic[
Replace[
createCloudPreferencesContent[ ],
_createCloudPreferencesContent -> ProgressIndicator[ Appearance -> "Percolate" ]
],
BaseStyle -> { "Text" }
],
ImageSize -> { Scaled[ 1 ], Automatic },
Alignment -> { Center, Top }
],
Background -> GrayLevel[ 0.95 ]
];

(* ::**************************************************************************************************************:: *)
(* ::Subsubsection::Closed:: *)
(*createCloudPreferencesContent*)
createCloudPreferencesContent // beginDefinition;
createCloudPreferencesContent[ ] := createCloudPreferencesContent[ ] = createPreferencesContent[ ];
createCloudPreferencesContent // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsection::Closed:: *)
(*cloudModelSelector*)
Expand Down
25 changes: 24 additions & 1 deletion Source/Chatbook/Common.wl
Original file line number Diff line number Diff line change
Expand Up @@ -466,14 +466,37 @@ messageFailure[ args___ ] :=
quiet = If[ TrueQ @ $failed, Quiet, Identity ];
message = messageFailure0;
WithCleanup[
StackInhibit @ quiet @ message @ args,
StackInhibit @ convertCloudFailure @ quiet @ message @ args,
If[ TrueQ @ $catching, $failed = True ]
]
];

(* https://resources.wolframcloud.com/FunctionRepository/resources/MessageFailure *)
importResourceFunction[ messageFailure0, "MessageFailure" ];

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

convertCloudFailure[ Failure[
"Chatbook::Internal",
as: KeyValuePattern @ { "MessageParameters" :> { Hyperlink[ _, url_ ], params___ } }
] ] /; $CloudEvaluation :=
Failure[
"Chatbook::Internal",
Association[
as,
"MessageParameters" -> { "", params },
"Link" -> Hyperlink[ "Report this issue \[RightGuillemet]", url ]
]
];

convertCloudFailure[ failure_ ] :=
failure;

convertCloudFailure // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsection::Closed:: *)
(*messagePrint*)
Expand Down
103 changes: 41 additions & 62 deletions Source/Chatbook/PreferencesContent.wl
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Needs[ "Wolfram`Chatbook`UI`" ];
(* ::**************************************************************************************************************:: *)
(* ::Section::Closed:: *)
(*Configuration*)
$preferencesWidth = 640;
$cloudEvaluationNotebook = None;

$preferencesPages = { "Notebooks", "Services", "Personas", "Tools" };
Expand All @@ -38,6 +39,15 @@ $$preferencesPage = Alternatives @@ $preferencesPages;
$preferencesScope := $FrontEnd;
$inFrontEndScope := MatchQ[ OwnValues @ $preferencesScope, { _ :> $FrontEnd|_FrontEndObject } ];

(* ::**************************************************************************************************************:: *)
(* ::Subsection::Closed:: *)
(*Cloud Overrides*)
$displayedPreferencesPages :=
If[ $CloudEvaluation,
{ "Notebooks", "Services"(*, "Personas", "Tools"*) },
$preferencesPages
];

(* ::**************************************************************************************************************:: *)
(* ::Section::Closed:: *)
(*Scope Utilities*)
Expand Down Expand Up @@ -131,32 +141,23 @@ currentTabPage // endDefinition;
createPreferencesContent // beginDefinition;

createPreferencesContent[ ] := Enclose[
Module[ { notebookSettings, serviceSettings, personaSettings, toolSettings, tabView, reset },

Module[ { tabs, tabView, reset },
(* Retrieve the dynamic content for each preferences tab, confirming that it matches the expected types: *)
notebookSettings = ConfirmMatch[ preferencesContent[ "Notebooks" ], _Dynamic | _DynamicModule, "Notebooks" ];
serviceSettings = ConfirmMatch[ preferencesContent[ "Services" ], _Dynamic | _DynamicModule, "Services" ];
personaSettings = ConfirmMatch[ preferencesContent[ "Personas" ], _Dynamic | _DynamicModule, "Personas" ];
toolSettings = ConfirmMatch[ preferencesContent[ "Tools" ], _Dynamic | _DynamicModule, "Tools" ];
tabs = ConfirmMatch[ createTabViewTabs[ ], { { _String, _String -> _Dynamic|_DynamicModule }.. }, "Tabs" ];

(* Create a TabView for the preferences content, with the tab state stored in the FE's private options: *)
tabView = TabView[
{
{ "Notebooks", "Notebooks" -> notebookSettings },
{ "Services" , "Services" -> serviceSettings },
{ "Personas" , "Personas" -> personaSettings },
{ "Tools" , "Tools" -> toolSettings }
},
tabs,
currentTabPageDynamic @ $preferencesScope,
Background -> None,
FrameMargins -> { { 2, 2 }, { 2, 3 } },
ImageMargins -> { { 10, 10 }, { 2, 2 } },
ImageSize -> { 640, Automatic },
ImageSize -> { $preferencesWidth, Automatic },
LabelStyle -> "feTabView" (* Defined in the SystemDialog stylesheet: *)
];

(* Create a reset button that will reset preferences to default settings: *)
reset = Pane[ $resetButton, ImageMargins -> { { 20, 0 }, { 0, 10 } }, ImageSize -> 640 ];
reset = Pane[ $resetButton, ImageMargins -> { { 20, 0 }, { 0, 10 } }, ImageSize -> $preferencesWidth ];

(* Arrange the TabView and reset button in a Grid layout with vertical spacers: *)
Grid[
Expand All @@ -180,6 +181,21 @@ createPreferencesContent[ ] := Enclose[

createPreferencesContent // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsection::Closed:: *)
(*createTabViewTabs*)
createTabViewTabs // beginDefinition;
createTabViewTabs[ ] := createTabViewTabs @ $displayedPreferencesPages;
createTabViewTabs[ pages: { __String } ] := createTabViewTab /@ pages;
createTabViewTabs // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsection::Closed:: *)
(*createTabViewTab*)
createTabViewTab // beginDefinition;
createTabViewTab[ name_String ] := { name, name -> preferencesContent @ name };
createTabViewTab // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsection::Closed:: *)
(*preferencesContent*)
Expand Down Expand Up @@ -359,49 +375,14 @@ makePersonaSelector0[ personas: { (_String -> _).. } ] :=
Row @ {
"Persona:",
Spacer[ 3 ],
PopupMenu[
scopedDynamic[
CurrentChatSettings[ $preferencesScope, "LLMEvaluator" ],
Function[
CurrentChatSettings[ $preferencesScope, "LLMEvaluator" ] = #;
refreshCloudToolbar[ ]
]
],
personas
]
PopupMenu[ scopedDynamic @ CurrentChatSettings[ $preferencesScope, "LLMEvaluator" ], personas ]
},
"Notebooks",
"LLMEvaluator"
];

makePersonaSelector0 // endDefinition;

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

refreshCloudToolbar[ ] :=
If[ TrueQ @ $CloudEvaluation,
refreshCloudToolbar[ EvaluationNotebook[ ], $cloudEvaluationNotebook ]
];

refreshCloudToolbar[ nbo_NotebookObject, nbo_NotebookObject ] :=
Null;

refreshCloudToolbar[ _, None ] :=
Null;

refreshCloudToolbar[ _NotebookObject, nbo_NotebookObject ] :=
Module[ { dc },
dc = Replace[ CurrentValue[ nbo, DockedCells ], Except[ Inherited | _List ] :> Inherited ];
SetOptions[ nbo, DockedCells -> { } ];
Pause[ 1 ];
SetOptions[ nbo, DockedCells -> dc ];
];

refreshCloudToolbar // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsubsection::Closed:: *)
(*personaPopupLabel*)
Expand Down Expand Up @@ -463,7 +444,7 @@ makeModelSelector0[ services_Association? AssociationQ ] := Enclose[
highlight[
"Model:",
Dynamic[
If[ state === "Loading", $loadingPopupMenu, modelSelector ],
If[ state === "Loading" || MatchQ[ modelSelector, _Symbol ], $loadingPopupMenu, modelSelector ],
TrackedSymbols :> { state, modelSelector }
],
"ModelName"
Expand Down Expand Up @@ -559,9 +540,7 @@ serviceSelectCallback[
Dynamic @ modelSelector,
Dynamic @ state
]
];

refreshCloudToolbar[ ]
]
];

serviceSelectCallback // endDefinition;
Expand Down Expand Up @@ -642,7 +621,7 @@ modelNameInputField // beginDefinition;

modelNameInputField[ Dynamic[ service_ ], Dynamic[ model_ ], Dynamic[ modelSelector_ ], Dynamic[ state_ ] ] :=
prefsInputField[
"Model:",
None,
scopedDynamic[
Replace[
extractModelName @ CurrentChatSettings[ $preferencesScope, "Model" ],
Expand Down Expand Up @@ -707,18 +686,15 @@ modelSelectCallback[
ensureServiceName @ service;
ConfirmAssert[ StringQ @ service, "ServiceName" ];

(* Store the service/model in FE settings: *)
CurrentChatSettings[ $preferencesScope, "Model" ] = <| "Service" -> service, "Name" -> model |>;

(* Remember the selected model for the given service, so it will be automatically chosen
when choosing this service again: *)
CurrentChatSettings[ $preferencesScope, "ServiceDefaultModel" ] = Append[
CurrentChatSettings[ $preferencesScope, "ServiceDefaultModel" ],
service -> model
];

(* Store the service/model in FE settings: *)
CurrentChatSettings[ $preferencesScope, "Model" ] = <| "Service" -> service, "Name" -> model |>;

updateDynamics[ "Models" ];
refreshCloudToolbar[ ]
]
,
throwInternalFailure
];
Expand Down Expand Up @@ -1338,6 +1314,9 @@ subsectionText // endDefinition;
prefsInputField // beginDefinition;

(* cSpell: ignore leadin *)
prefsInputField[ None, value_Dynamic, type_, opts: OptionsPattern[ ] ] :=
prefsInputField0[ value, type, opts ];

prefsInputField[ label_, value_Dynamic, type_, opts: OptionsPattern[ ] ] := Grid[
{ { label, prefsInputField0[ value, type, opts ] } },
Alignment -> { Automatic, Baseline },
Expand Down
19 changes: 12 additions & 7 deletions Source/Chatbook/Services.wl
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ HoldComplete[

Begin[ "`Private`" ];

Needs[ "Wolfram`Chatbook`" ];
Needs[ "Wolfram`Chatbook`Common`" ];
Needs[ "Wolfram`Chatbook`Models`" ];
Needs[ "Wolfram`Chatbook`UI`" ];
Needs[ "Wolfram`Chatbook`" ];
Needs[ "Wolfram`Chatbook`Common`" ];
Needs[ "Wolfram`Chatbook`Dynamics`" ];
Needs[ "Wolfram`Chatbook`Models`" ];
Needs[ "Wolfram`Chatbook`UI`" ];

$ContextAliases[ "llm`" ] = "LLMServices`";

Expand All @@ -46,7 +47,7 @@ $llmServicesAvailable := $llmServicesAvailable = (
(* ::Section::Closed:: *)
(*InvalidateServiceCache*)
InvalidateServiceCache // beginDefinition;
InvalidateServiceCache[ ] := ($serviceCache = None; Null);
InvalidateServiceCache[ ] := catchAlways[ $serviceCache = None; updateDynamics[ { "Models", "Services" } ]; ];
InvalidateServiceCache // endDefinition;

(* ::**************************************************************************************************************:: *)
Expand Down Expand Up @@ -91,9 +92,13 @@ getServiceModelList[ KeyValuePattern[ "Service" -> service_String ] ] :=

getServiceModelList[ service_String ] :=
With[ { models = $availableServices[ service, "CachedModels" ] },
If[ ListQ @ models,
Replace[
models,
getServiceModelList[ service, $availableServices[ service ] ]
{
{ } | None :> Missing[ "NoModelList" ],
list_List :> list,
_ :> getServiceModelList[ service, $availableServices[ service ] ]
}
]
];

Expand Down