Skip to content

Commit de513bf

Browse files
committed
Merge remote-tracking branch 'origin/main' into feature/LLM-713_NACloudDockedCell
2 parents e80c74a + 59a2cd7 commit de513bf

File tree

14 files changed

+562
-109
lines changed

14 files changed

+562
-109
lines changed

.vscode/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"Deinitialization",
2121
"Demi",
2222
"Dirtiable",
23+
"Docu",
2324
"dpkg",
2425
"EFAIL",
2526
"ENDARGUMENTS",
@@ -79,6 +80,7 @@
7980
"Subsubsubsection",
8081
"Subsubsubsubsection",
8182
"Subsubtitle",
83+
"Subsuperscript",
8284
"tabletags",
8385
"tavily",
8486
"textbf",

Assets/TemplateBoxOptions.wxf

131 KB
Binary file not shown.

PacletInfo.wl

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PacletObject[ <|
22
"Name" -> "Wolfram/Chatbook",
33
"PublisherID" -> "Wolfram",
4-
"Version" -> "2.0.20",
4+
"Version" -> "2.1.6",
55
"WolframVersion" -> "14.1+",
66
"Description" -> "Wolfram Notebooks + LLMs",
77
"License" -> "MIT",
@@ -34,11 +34,12 @@ PacletObject[ <|
3434
{ "Asset",
3535
"Root" -> "Assets",
3636
"Assets" -> {
37-
{ "AIAssistant" , "AIAssistant" },
38-
{ "DisplayFunctions", "DisplayFunctions.wxf" },
39-
{ "Icons" , "Icons.wxf" },
40-
{ "SandboxMessages" , "SandboxMessages.wl" },
41-
{ "Tokenizers" , "Tokenizers" }
37+
{ "AIAssistant" , "AIAssistant" },
38+
{ "DisplayFunctions" , "DisplayFunctions.wxf" },
39+
{ "Icons" , "Icons.wxf" },
40+
{ "SandboxMessages" , "SandboxMessages.wl" },
41+
{ "TemplateBoxOptions", "TemplateBoxOptions.wxf" },
42+
{ "Tokenizers" , "Tokenizers" }
4243
}
4344
},
4445
{ "LLMConfiguration",

Source/Chatbook/Actions.wl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,8 +739,8 @@ StopChat[ cell0_CellObject ] := Enclose[
739739
settings = ConfirmMatch[ currentChatSettings @ cell, _Association|_Missing, "ChatNotebookSettings" ];
740740
If[ MissingQ @ settings, finish[ ] ];
741741
removeTask @ Lookup[ settings, "Task", None ];
742-
container = ConfirmMatch[ Lookup[ settings, "Container", None ], _Association|None, "Container" ];
743-
If[ container === None, finish[ ] ];
742+
container = ConfirmMatch[ Lookup[ settings, "Container", None ], _Association|_Symbol, "Container" ];
743+
If[ MatchQ[ container, _Symbol ], finish[ ] ];
744744
content = ConfirmMatch[ Lookup[ container, "FullContent" ], _String|$$progressIndicator, "Content" ];
745745
FinishDynamic[ ];
746746
Block[ { createFETask = # & }, writeReformattedCell[ settings, content, cell ] ]

Source/Chatbook/Common.wl

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ catchMine // endDefinition;
631631
(* ::Subsection::Closed:: *)
632632
(*throwTop*)
633633
throwTop // beginDefinition;
634-
throwTop[ expr_ ] /; $catching := Throw[ Unevaluated @ expr, $catchTopTag ];
634+
throwTop[ expr_ ] := If[ TrueQ @ $catching, Throw[ Unevaluated @ expr, $catchTopTag ], expr ];
635635
throwTop // endDefinition;
636636

637637
(* ::**************************************************************************************************************:: *)
@@ -1340,9 +1340,33 @@ tr // endDefinition;
13401340
(*trRaw*)
13411341
trRaw // beginDefinition;
13421342
trRaw[ name_? StringQ ] /; $CloudEvaluation := cloudTextResource @ name;
1343-
trRaw[ name_? StringQ ] := FrontEndResource[ "ChatbookStrings", name ];
1343+
trRaw[ name_? StringQ ] := chatbookString @ name;
13441344
trRaw // endDefinition;
13451345

1346+
(* ::**************************************************************************************************************:: *)
1347+
(* ::Subsubsection::Closed:: *)
1348+
(*chatbookString*)
1349+
chatbookString // beginDefinition;
1350+
1351+
chatbookString[ name_String ] := Enclose[
1352+
Catch @ Module[ { string },
1353+
string = ConfirmMatch[ usingFrontEnd @ FrontEndResource[ "ChatbookStrings", name ], _String|$Failed, "String" ];
1354+
If[ StringQ @ string, Throw @ string ];
1355+
pacletDataRebuild[ ];
1356+
ConfirmBy[ usingFrontEnd @ FrontEndResource[ "ChatbookStrings", name ], StringQ, "Retry" ]
1357+
],
1358+
throwInternalFailure
1359+
];
1360+
1361+
chatbookString // endDefinition;
1362+
1363+
(* ::**************************************************************************************************************:: *)
1364+
(* ::Subsubsubsection::Closed:: *)
1365+
(*pacletDataRebuild*)
1366+
pacletDataRebuild // beginDefinition;
1367+
pacletDataRebuild[ ] := pacletDataRebuild[ ] = PacletDataRebuild[ ];
1368+
pacletDataRebuild // endDefinition;
1369+
13461370
(* ::**************************************************************************************************************:: *)
13471371
(* ::Subsection::Closed:: *)
13481372
(*trStringTemplate*)

Source/Chatbook/CommonSymbols.wl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ BeginPackage[ "Wolfram`Chatbook`Common`" ];
8181
`$simpleToolMethod;
8282
`$statelessProgressIndicator;
8383
`$suppressButtonAppearance;
84+
`$templateBoxOptionsCache;
8485
`$timingLog;
8586
`$tinyHashLength;
8687
`$tokenBudget;

Source/Chatbook/FrontEnd.wl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1328,7 +1328,7 @@ rasterize // endDefinition;
13281328
(*replaceCellContext*)
13291329
replaceCellContext // beginDefinition;
13301330

1331-
replaceCellContext[ expr_ ] := replaceCellContext[ expr ] = ReplaceAll[
1331+
replaceCellContext[ expr_ ] := replaceCellContext[ Verbatim[ expr ] ] = ReplaceAll[
13321332
expr,
13331333
s_Symbol /; AtomQ @ Unevaluated @ s && Context @ Unevaluated @ s === "$CellContext`" :>
13341334
With[ { new = ToExpression[ $Context <> SymbolName @ Unevaluated @ s, InputForm, $ConditionHold ] },

Source/Chatbook/Handlers.wl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ applyHandlerFunction[ settings_Association, name_String, args0_ ] := Enclose[
6767
args = ConfirmBy[
6868
<|
6969
"EventName" -> name,
70+
(* FIXME: Add settings at start of evaluation instead of each function call *)
7071
"ChatNotebookSettings" -> KeyDrop[ settings, $settingsDroppedKeys ],
7172
args0
7273
|>,
@@ -178,6 +179,7 @@ applyProcessingFunction[ settings_Association, name_String, args_HoldComplete, p
178179
addProcessingArguments[
179180
name,
180181
<|
182+
(* FIXME: Add settings at start of evaluation instead of each function call *)
181183
"ChatNotebookSettings" -> KeyDrop[ settings, $settingsDroppedKeys ],
182184
"DefaultProcessingFunction" -> default,
183185
params

Source/Chatbook/LLMUtilities.wl

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,13 @@ llmSynthesize0[ prompt: $$llmPrompt, evaluator_Association, attempt_ ] := Enclos
5454
task = llmSynthesizeSubmit[ prompt, evaluator, callback ];
5555
If[ FailureQ @ task, throwFailureToChatOutput @ task ];
5656
TaskWait @ ConfirmMatch[ task, _TaskObject, "Task" ];
57-
If[ MatchQ[ result, Failure[ "InvalidResponse", _ ] ] && attempt <= 3,
57+
58+
If[ MatchQ[ result, Failure[ "InvalidResponse", _ ] ]
59+
,
60+
If[ attempt > 3, throwFailureToChatOutput @ result ];
5861
Pause[ Exp @ attempt / E ];
59-
llmSynthesize0[ prompt, evaluator, attempt + 1 ],
62+
llmSynthesize0[ prompt, evaluator, attempt + 1 ]
63+
,
6064
result
6165
]
6266
],
@@ -108,7 +112,7 @@ llmSynthesizeSubmit[ prompt0: $$llmPrompt, evaluator0_Association, callback_ ] :
108112
FailureQ @ strings,
109113
callback[ strings, #1 ],
110114
True,
111-
callback[ Failure[ "InvalidResponse", <| "Data" -> data |> ], #1 ]
115+
callback[ makeInvalidResponseFailure @ data, #1 ]
112116
]
113117
]
114118
]
@@ -132,13 +136,39 @@ llmSynthesizeSubmit[ prompt0: $$llmPrompt, evaluator0_Association, callback_ ] :
132136

133137
llmSynthesizeSubmit // endDefinition;
134138

139+
(* ::**************************************************************************************************************:: *)
140+
(* ::Subsubsection::Closed:: *)
141+
(*makeInvalidResponseFailure*)
142+
makeInvalidResponseFailure // beginDefinition;
143+
144+
makeInvalidResponseFailure[ data_List ] /; MemberQ[ data, KeyValuePattern[ "StatusCode" -> 504 ] ] := Failure[
145+
"InvalidResponse",
146+
<|
147+
"Message" -> "The server is currently unavailable (504 Gateway Time-out). Please try again later.",
148+
"Data" -> data
149+
|>
150+
];
151+
152+
makeInvalidResponseFailure[ data_List ] := Failure[
153+
"InvalidResponse",
154+
<|
155+
"Message" -> "The server returned an invalid response. Please try again later.",
156+
"Data" -> data
157+
|>
158+
];
159+
160+
makeInvalidResponseFailure // endDefinition;
161+
135162
(* ::**************************************************************************************************************:: *)
136163
(* ::Subsubsection::Closed:: *)
137164
(*truncatePrompt*)
138165
truncatePrompt // beginDefinition;
139166

140-
truncatePrompt[ string_String, evaluator_ ] := stringTrimMiddle[ string, modelContextLimit @ evaluator ];
141-
truncatePrompt[ { strings___String }, evaluator_ ] := truncatePrompt[ StringJoin @ strings, evaluator ];
167+
truncatePrompt[ string_String, evaluator_ ] :=
168+
stringTrimMiddle[ string, modelContextLimit @ evaluator ];
169+
170+
truncatePrompt[ { strings___String }, evaluator_ ] :=
171+
truncatePrompt[ StringJoin @ strings, evaluator ];
142172

143173
truncatePrompt[ prompts: { ($$string|$$graphics).. }, evaluator_ ] := Enclose[
144174
Module[ { stringCount, images, imageCount, budget, imageBudget, resized, imageTokens, stringBudget },

Source/Chatbook/PromptGenerators/RelatedDocumentation.wl

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ Needs[ "Wolfram`Chatbook`PromptGenerators`Common`" ];
1010
(* ::**************************************************************************************************************:: *)
1111
(* ::Section::Closed:: *)
1212
(*Configuration*)
13-
$snippetType = "Text";
14-
$documentationSnippetVersion = "14-1-0-10549042";
15-
$baseURL = "https://www.wolframcloud.com/obj/wolframai-content/DocumentationSnippets";
16-
$documentationSnippetBaseURL = URLBuild @ { $baseURL, $documentationSnippetVersion, $snippetType };
17-
$resourceSnippetBaseURL = URLBuild @ { $baseURL, "Resources", $snippetType };
13+
$snippetType = "Text";
14+
$documentationSnippetVersion := $snippetVersion;
15+
$baseURL = "https://www.wolframcloud.com/obj/wolframai-content/DocumentationSnippets";
16+
$documentationSnippetBaseURL := URLBuild @ { $baseURL, $documentationSnippetVersion, $snippetType };
17+
$resourceSnippetBaseURL = URLBuild @ { $baseURL, "Resources", $snippetType };
1818

1919
$documentationSnippetsCacheDirectory := $documentationSnippetsCacheDirectory =
2020
ChatbookFilesDirectory @ { "DocumentationSnippets", "Documentation", $documentationSnippetVersion };
@@ -46,6 +46,11 @@ $unfilteredItemsPerSource = 10;
4646

4747
$filteringLLMConfig = <| "StopTokens" -> { "CasualChat" } |>;
4848

49+
(* ::**************************************************************************************************************:: *)
50+
(* ::Subsection::Closed:: *)
51+
(*$snippetVersion*)
52+
$snippetVersion := $snippetVersion = If[ $VersionNumber >= 14.2, "14-2-0-11168610", "14-1-0-10549042" ];
53+
4954
(* ::**************************************************************************************************************:: *)
5055
(* ::Section::Closed:: *)
5156
(*Messages*)
@@ -914,14 +919,17 @@ fetchDocumentationSnippets[ uris: { __String } ] := Enclose[
914919
Module[ { count, text, $results, tasks },
915920
count = Length @ uris;
916921

917-
text = ConfirmBy[
918-
If[ count === 1,
919-
trStringTemplate[ "ProgressTextDownloadingSnippet" ][ count ],
920-
trStringTemplate[ "ProgressTextDownloadingSnippets" ][ count ]
921-
],
922-
StringQ,
923-
"Text"
924-
];
922+
text = If[ $EvaluationEnvironment === "Session",
923+
ConfirmBy[
924+
If[ count === 1,
925+
trStringTemplate[ "ProgressTextDownloadingSnippet" ][ count ],
926+
trStringTemplate[ "ProgressTextDownloadingSnippets" ][ count ]
927+
],
928+
StringQ,
929+
"Text"
930+
],
931+
""
932+
];
925933

926934
withApproximateProgress[
927935
$results = AssociationMap[ <| "URI" -> #1 |> &, uris ];
@@ -1010,7 +1018,9 @@ processDocumentationSnippetResult[ base_String, as_Association ] :=
10101018
processDocumentationSnippetResult[ base_String, as_, bytes_ByteArray, 200 ] :=
10111019
processDocumentationSnippetResult[ base, as, Quiet @ Developer`ReadWXFByteArray @ bytes ];
10121020

1013-
processDocumentationSnippetResult[ base_String, as_Association, bytes_, code: Except[ 200, _Integer ] ] :=
1021+
(* A 401/403 means we're missing a file in the snippet deployment or it has the wrong permissions,
1022+
so it should trigger an internal failure, otherwise just issue a generic cloud download failure. *)
1023+
processDocumentationSnippetResult[ base_String, as_Association, bytes_, code: Except[ 401|403 ] ] :=
10141024
throwFailureToChatOutput @ Failure[
10151025
"CloudDownloadError",
10161026
<|

0 commit comments

Comments
 (0)