From a2c7fe2d1bc84c19fde6794bd4e74164e179ad7c Mon Sep 17 00:00:00 2001 From: Rick Hennigan Date: Thu, 14 Dec 2023 19:15:15 -0500 Subject: [PATCH] Update the cloud toolbar to handle multiple services --- PacletInfo.wl | 2 +- Source/Chatbook/CloudToolbar.wl | 111 +++++++++++++++++++++++++- Source/Chatbook/PreferencesContent.wl | 10 ++- Source/Chatbook/Prompting.wl | 3 +- Source/Chatbook/Sandbox.wl | 2 +- 5 files changed, 123 insertions(+), 5 deletions(-) diff --git a/PacletInfo.wl b/PacletInfo.wl index 304048e8..eed7161c 100644 --- a/PacletInfo.wl +++ b/PacletInfo.wl @@ -2,7 +2,7 @@ PacletObject[ <| "Name" -> "Wolfram/Chatbook", "PublisherID" -> "Wolfram", "Version" -> "1.3.4", - "WolframVersion" -> "13.3+", + "WolframVersion" -> "14.0+", "Description" -> "Wolfram Notebooks + LLMs", "License" -> "MIT", "Creator" -> "Connor Gray, Theodore Gray, Richard Hennigan", diff --git a/Source/Chatbook/CloudToolbar.wl b/Source/Chatbook/CloudToolbar.wl index d6705bce..b33a91ab 100644 --- a/Source/Chatbook/CloudToolbar.wl +++ b/Source/Chatbook/CloudToolbar.wl @@ -13,6 +13,7 @@ Needs[ "Wolfram`Chatbook`Common`" ]; Needs[ "Wolfram`Chatbook`Dialogs`" ]; Needs[ "Wolfram`Chatbook`Dynamics`" ]; Needs[ "Wolfram`Chatbook`PreferencesContent`" ]; +Needs[ "Wolfram`Chatbook`Services`" ]; (* ::**************************************************************************************************************:: *) (* ::Section::Closed:: *) @@ -42,7 +43,7 @@ makeChatCloudDockedCellContents[ ] := Item[ $cloudChatBanner, Alignment -> Left ], Item[ "", ItemSize -> Fit ], makePersonaSelector[ ], - makeModelSelector[ ] + cloudModelSelector[ ] } }, Alignment -> { Left, Baseline }, @@ -62,6 +63,114 @@ makeChatCloudDockedCellContents[ ] := makeChatCloudDockedCellContents // endDefinition; +(* ::**************************************************************************************************************:: *) +(* ::Subsubsection::Closed:: *) +(*cloudModelSelector*) +cloudModelSelector // beginDefinition; + +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 + ] + ]; + + 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" ] + } + ] + ]; + + Row @ { + "LLM Service: ", serviceSelector, + Spacer[ 5 ], + "Model: ", Dynamic @ modelSelector + } + ]; + +cloudModelSelector // 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; + (* ::**************************************************************************************************************:: *) (* ::Section::Closed:: *) (*Notebook Type Label*) diff --git a/Source/Chatbook/PreferencesContent.wl b/Source/Chatbook/PreferencesContent.wl index 0a87d21e..82602f51 100644 --- a/Source/Chatbook/PreferencesContent.wl +++ b/Source/Chatbook/PreferencesContent.wl @@ -363,7 +363,15 @@ makePersonaSelector0[ personas: { (_String -> _).. } ] := "Persona:", Spacer[ 3 ], PopupMenu[ - scopedDynamic @ CurrentChatSettings[ $preferencesScope, "LLMEvaluator" ], + scopedDynamic[ + CurrentChatSettings[ $preferencesScope, "LLMEvaluator" ], + Function[ + CurrentValue[ + $preferencesScope, + { TaggingRules, "ChatNotebookSettings", "LLMEvaluator" } + ] = #1 + ] + ], personas ] }, diff --git a/Source/Chatbook/Prompting.wl b/Source/Chatbook/Prompting.wl index 7b81f476..f25f4ec6 100644 --- a/Source/Chatbook/Prompting.wl +++ b/Source/Chatbook/Prompting.wl @@ -170,7 +170,8 @@ becomes ``Styled message``."; $basePromptComponents[ "SpecialURI" ] = "\ * You will occasionally see markdown links with special URI schemes, e.g. ![label](scheme://content-id) that represent \ -interactive interface elements. You can use these in your responses to display the same elements to the user."; +interactive interface elements. You can use these in your responses to display the same elements to the user, but they \ +must be formatted as image links (include the '!' at the beginning). If you do not include the '!', the link will fail."; $basePromptComponents[ "SpecialURIAudio" ] = "\ * ![label](audio://content-id) represents an interactive audio player."; diff --git a/Source/Chatbook/Sandbox.wl b/Source/Chatbook/Sandbox.wl index 954bc983..3fa1f771 100644 --- a/Source/Chatbook/Sandbox.wl +++ b/Source/Chatbook/Sandbox.wl @@ -432,7 +432,7 @@ initializeExpressions[ flat: HoldComplete @ Association @ OrderlessPatternSequen ReplacePart[ flat, Thread[ pos -> Extract[ flat, pos ] ] ] ]; -initializeExpressions[ failed: HoldComplete[ _Failure ] ] := +initializeExpressions[ failed: HoldComplete[ _Failure|$Failed|$Aborted ] ] := failed; initializeExpressions // endDefinition;