From c517df1cee33cd65eca717819e37222600349a7e Mon Sep 17 00:00:00 2001 From: Rick Hennigan Date: Fri, 15 Nov 2024 13:53:50 -0500 Subject: [PATCH] Bugfix: Fix several regressions due to bad merge --- .../VectorDatabases/VectorDatabaseBuilder.wl | 34 +++++++++++++++---- Source/Chatbook/ChatMessageToCell.wl | 5 +-- Source/Chatbook/ChatModes/UI.wl | 24 ++++--------- Source/Chatbook/Formatting.wl | 2 ++ .../PromptGenerators/VectorDatabases.wl | 5 ++- Source/Chatbook/Search.wl | 2 +- Source/Chatbook/SendChat.wl | 27 +++++++-------- Source/Chatbook/UI.wl | 18 ++++++++++ 8 files changed, 73 insertions(+), 44 deletions(-) diff --git a/Developer/VectorDatabases/VectorDatabaseBuilder.wl b/Developer/VectorDatabases/VectorDatabaseBuilder.wl index f4ff6f90..8a134b83 100644 --- a/Developer/VectorDatabases/VectorDatabaseBuilder.wl +++ b/Developer/VectorDatabases/VectorDatabaseBuilder.wl @@ -536,14 +536,36 @@ createEmbeddings[ string_String ] := (* ::**************************************************************************************************************:: *) (* ::Subsubsection::Closed:: *) (*sentenceBERTEmbedding*) -sentenceBERTEmbedding // beginDefinition; +sentenceBERTEmbedding := getSentenceBERTEmbeddingFunction[ ]; -sentenceBERTEmbedding[ args___ ] := ( - Needs[ "SemanticSearch`" -> None ]; - SemanticSearch`SemanticSearch`Private`SentenceBERTEmbedding @ args -); +(* ::**************************************************************************************************************:: *) +(* ::Subsubsubsection::Closed:: *) +(*getSentenceBERTEmbeddingFunction*) +getSentenceBERTEmbeddingFunction // beginDefinition; + +getSentenceBERTEmbeddingFunction[ ] := Enclose[ + Module[ { name }, + + Needs[ "SemanticSearch`" -> None ]; + + name = ConfirmBy[ + SelectFirst[ + { + "SemanticSearch`SentenceBERTEmbedding", + "SemanticSearch`SemanticSearch`Private`SentenceBERTEmbedding" + }, + NameQ @ # && ToExpression[ #, InputForm, System`Private`HasAnyEvaluationsQ ] & + ], + StringQ, + "SymbolName" + ]; + + getSentenceBERTEmbeddingFunction[ ] = Symbol @ name + ], + throwInternalFailure +]; -sentenceBERTEmbedding // endDefinition; +getSentenceBERTEmbeddingFunction // endDefinition; (* ::**************************************************************************************************************:: *) (* ::Subsection::Closed:: *) diff --git a/Source/Chatbook/ChatMessageToCell.wl b/Source/Chatbook/ChatMessageToCell.wl index a9213928..26f2ebfe 100644 --- a/Source/Chatbook/ChatMessageToCell.wl +++ b/Source/Chatbook/ChatMessageToCell.wl @@ -101,10 +101,7 @@ workspaceOutput // beginDefinition; workspaceOutput[ text_TextData ] := wrapCellContent[ - TextData @ Cell[ - BoxData @ TemplateBox[ { Cell[ text, $selectableOptions ] }, "AssistantMessageBox" ], - Background -> None - ], + BoxData @ TemplateBox[ { Cell[ text, $selectableOptions ] }, "AssistantMessageBox" ], "Assistant", "Default" ]; diff --git a/Source/Chatbook/ChatModes/UI.wl b/Source/Chatbook/ChatModes/UI.wl index bcd37ac8..0874e178 100644 --- a/Source/Chatbook/ChatModes/UI.wl +++ b/Source/Chatbook/ChatModes/UI.wl @@ -1133,19 +1133,16 @@ $inlineToWorkspaceConversionRules := $inlineToWorkspaceConversionRules = Dispatc ] , Cell[ - TextData @ { Cell[ BoxData @ TemplateBox[ { Cell[ text_, ___ ] }, "AssistantMessageBox", ___ ], ___ ] }, + BoxData @ TemplateBox[ { Cell[ text_, ___ ] }, "AssistantMessageBox", ___ ], "ChatOutput", ___, TaggingRules -> tags_, ___ ] :> Cell[ - TextData @ Cell[ - BoxData @ TemplateBox[ - { Cell[ Flatten @ TextData @ text, Background -> None, Editable -> True, Selectable -> True ] }, - "AssistantMessageBox" - ], - Background -> None + BoxData @ TemplateBox[ + { Cell[ Flatten @ TextData @ text, Background -> None, Editable -> True, Selectable -> True ] }, + "AssistantMessageBox" ], "ChatOutput", TaggingRules -> tags, @@ -1200,17 +1197,8 @@ $fromWorkspaceChatConversionRules := $fromWorkspaceChatConversionRules = Dispatc Cell[ BoxData @ TemplateBox[ { text_ }, "UserMessageBox", ___ ], "ChatInput", ___ ] :> Cell[ Flatten @ TextData @ text, "ChatInput" ] , - Cell[ - TextData @ Cell[ BoxData @ TemplateBox[ { Cell[ text_, ___ ] }, "AssistantMessageBox", ___ ], ___ ], - "ChatOutput", - ___ - ] :> Cell[ Flatten @ TextData @ text, "ChatOutput" ] - , - Cell[ - TextData @ { Cell[ BoxData @ TemplateBox[ { Cell[ text_, ___ ] }, "AssistantMessageBox", ___ ], ___ ] }, - "ChatOutput", - ___ - ] :> Cell[ Flatten @ TextData @ text, "ChatOutput" ] + Cell[ BoxData @ TemplateBox[ { Cell[ text_, ___ ] }, "AssistantMessageBox", ___ ], "ChatOutput", ___ ] :> + Cell[ Flatten @ TextData @ text, "ChatOutput" ] }; (* ::**************************************************************************************************************:: *) diff --git a/Source/Chatbook/Formatting.wl b/Source/Chatbook/Formatting.wl index fddc0a62..6e1a1c90 100644 --- a/Source/Chatbook/Formatting.wl +++ b/Source/Chatbook/Formatting.wl @@ -1173,6 +1173,8 @@ $dynamicSplitRules = { (* ::Subsection::Closed:: *) (*$stringFormatRules*) $stringFormatRules = { + "```" ~~ code: Except[ "\n" ].. ~~ "```" :> inlineCodeCell @ code, + "***" ~~ text: Except[ "*" ].. ~~ "***" /; StringFreeQ[ text, "\n" ] :> styleBox[ text, FontWeight -> Bold, FontSlant -> Italic ], diff --git a/Source/Chatbook/PromptGenerators/VectorDatabases.wl b/Source/Chatbook/PromptGenerators/VectorDatabases.wl index 32a440d1..2047e8a8 100644 --- a/Source/Chatbook/PromptGenerators/VectorDatabases.wl +++ b/Source/Chatbook/PromptGenerators/VectorDatabases.wl @@ -603,7 +603,10 @@ getAndCacheEmbeddings[ { } ] := getAndCacheEmbeddings[ strings: { __String } ] /; $embeddingModel === "SentenceBERT" := Enclose[ Module[ { vectors }, vectors = ConfirmBy[ - Developer`ToPackedArray @ sentenceBERTEmbedding @ strings, + If[ AllTrue[ strings, StringMatchQ[ WhitespaceCharacter... ] ], + Developer`ToPackedArray @ Rest @ sentenceBERTEmbedding @ Prepend[ strings, "hello" ], + Developer`ToPackedArray @ sentenceBERTEmbedding @ strings + ], Developer`PackedArrayQ, "PackedArray" ]; diff --git a/Source/Chatbook/Search.wl b/Source/Chatbook/Search.wl index 742904fd..1b6676b8 100644 --- a/Source/Chatbook/Search.wl +++ b/Source/Chatbook/Search.wl @@ -99,7 +99,7 @@ addChatToSearchIndex[ spec_ ] := Enclose[ Catch @ Module[ { data, appName, uuid, vectors, metadata }, If[ $noSemanticSearch, Throw @ Missing[ "NoSemanticSearch" ] ]; data = ConfirmMatch[ getChatConversationData @ spec, _Association|_Missing, "Data" ]; - If[ MissingQ @ data, Throw @ data ]; (* TODO: auto-save here? *) + If[ MissingQ @ data, Throw @ Missing[ "NoVectors" ] ]; (* TODO: auto-save here? *) appName = ConfirmBy[ data[ "AppName" ], StringQ, "AppName" ]; uuid = ConfirmBy[ data[ "ConversationUUID" ], StringQ, "ConversationUUID" ]; vectors = ConfirmMatch[ data[ "Vectors" ], { ___NumericArray }, "Vectors" ]; diff --git a/Source/Chatbook/SendChat.wl b/Source/Chatbook/SendChat.wl index 0d1362b4..cd3a90bf 100644 --- a/Source/Chatbook/SendChat.wl +++ b/Source/Chatbook/SendChat.wl @@ -110,7 +110,7 @@ sendChat[ evalCell_, nbo_, settings0_ ] /; $useLLMServices := catchTopAs[ Chatbo ]; AppendTo[ settings, "Data" -> data ]; - CurrentValue[ cellObject, { TaggingRules, "ChatNotebookSettings", "Data" } ] = data; + CurrentChatSettings[ cellObject, "Data" ] = data; $resultCellCache = <| |>; $debugLog = Internal`Bag[ ]; @@ -145,8 +145,8 @@ sendChat[ evalCell_, nbo_, settings0_ ] /; $useLLMServices := catchTopAs[ Chatbo addHandlerArguments[ "Task" -> task ]; - CurrentValue[ cellObject, { TaggingRules, "ChatNotebookSettings", "CellObject" } ] = cellObject; - CurrentValue[ cellObject, { TaggingRules, "ChatNotebookSettings", "Task" } ] = task; + CurrentChatSettings[ cellObject, "CellObject" ] = cellObject; + CurrentChatSettings[ cellObject, "Task" ] = task; If[ FailureQ @ task, throwTop @ writeErrorCell[ cellObject, task ] ]; setProgressDisplay[ "Waiting for response", 1.0 ]; @@ -895,6 +895,7 @@ autoCorrect[ string_String ] := StringReplace[ string, $llmAutoCorrectRules ]; autoCorrect // endDefinition; $llmAutoCorrectRules := $llmAutoCorrectRules = Flatten @ { + "```" ~~ code: Except[ "\n" ].. ~~ "```" :> "``"<>code<>"``", "wolfram_language_evaliator" -> "wolfram_language_evaluator", "\\!\\(\\*MarkdownImageBox[\"" ~~ Shortest[ uri__ ] ~~ "\"]\\)" :> uri, "\\!\\(MarkdownImageBox[\"" ~~ Shortest[ uri__ ] ~~ "\"]\\)" :> uri, @@ -2264,15 +2265,10 @@ reformatCell[ settings_, string_, tag_, open_, label_, pageData_, cellTags_, uui dingbat = makeOutputDingbat @ settings; outer = If[ TrueQ @ $WorkspaceChat, - TextData @ { - Cell[ - BoxData @ TemplateBox[ - { Cell[ #, Background -> None, Editable -> True, Selectable -> True ] }, - "AssistantMessageBox" - ], - Background -> None - ] - } &, + BoxData @ TemplateBox[ + { Cell[ #, Background -> None, Editable -> True, Selectable -> True ] }, + "AssistantMessageBox" + ] &, # & ]; @@ -2524,11 +2520,14 @@ restoreLastPage // endDefinition; (*attachChatOutputMenu*) attachChatOutputMenu // beginDefinition; -attachChatOutputMenu[ cell_CellObject ] /; $cloudNotebooks := Null; +attachChatOutputMenu[ cell_CellObject ] /; $cloudNotebooks || $WorkspaceChat || $InlineChat := Null; attachChatOutputMenu[ cell_CellObject ] := ( $lastChatOutput = cell; - Lookup[ Options[ cell, Initialization ] /. HoldPattern @ EvaluationCell[ ] -> cell, Initialization ] + Quiet[ + Lookup[ Options[ cell, Initialization ] /. HoldPattern @ EvaluationCell[ ] -> cell, Initialization ], + Lookup::invrl + ] ); attachChatOutputMenu // endDefinition; diff --git a/Source/Chatbook/UI.wl b/Source/Chatbook/UI.wl index 5612af30..36e99c36 100644 --- a/Source/Chatbook/UI.wl +++ b/Source/Chatbook/UI.wl @@ -1039,6 +1039,24 @@ makeServiceModelMenu[ Dynamic[ display_ ], obj_, root_, currentModel_, service_S 200 ]; +makeServiceModelMenu[ Dynamic[ display_ ], obj_, root_, currentModel_, service_String, Missing[ "NoModelList" ] ] := + display = MakeMenu[ + { + { service }, + { + Spacer[ 0 ], + Automatic, + Hold[ + removeChatMenus @ EvaluationCell[ ]; + (* TODO: this could probably prompt the user with an InputField to enter a name: *) + setModel[ obj, <| "Service" -> service, "Name" -> Automatic |> ] + ] + } + }, + GrayLevel[ 0.85 ], + 200 + ]; + makeServiceModelMenu // endDefinition; (* ::**************************************************************************************************************:: *)