@@ -19,6 +19,8 @@ $multimodal = True;
1919$keepAllImages = False ;
2020$wolframAlphaAppID = None ;
2121$instructions = None ;
22+ $reinterpret = False ;
23+ $includeWLResults := TrueQ @ toolSelectedQ [ "WolframLanguageEvaluator" ];
2224
2325$timeouts = < |
2426 "URLSubmit" -> 25 ,
@@ -30,6 +32,13 @@ $timeouts = <|
3032 "AlphaTotal" -> 20
3133|> ;
3234
35+ $podStates = Flatten @ { "Step-by-step solution" , "Show all steps" , ConstantArray [ "Result__More" , 3 ] };
36+
37+ $formats := If [ TrueQ @ $includeWLResults ,
38+ { "image" , "plaintext" , "minput" },
39+ { "image" , "plaintext" }
40+ ];
41+
3342(* ::**************************************************************************************************************:: *)
3443(* ::Section::Closed:: *)
3544(*Argument Patterns*)
@@ -71,11 +80,13 @@ RelatedWolframAlphaResults // Options = {
7180 "AppID" -> Automatic ,
7281 "CacheResults" -> False ,
7382 "Debug" -> False ,
83+ "IncludeWLResults" -> Automatic ,
7484 "Instructions" -> None ,
7585 "LLMEvaluator" -> Automatic ,
7686 "MaxItems" -> Automatic ,
7787 "PromptHeader" -> Automatic ,
7888 "RandomQueryCount" -> Automatic ,
89+ "Reinterpret" -> False ,
7990 "RelatedQueryCount" -> Automatic ,
8091 "SampleQueryCount" -> Automatic
8192};
@@ -126,24 +137,17 @@ RelatedWolframAlphaResults[ prompt_, property_, Automatic, opts: OptionsPattern[
126137RelatedWolframAlphaResults [ prompt_ , "Prompt" , n_ Integer , opts : OptionsPattern [ ] ] :=
127138 catchMine @ withChatState @ Block [
128139 {
129- $cacheResults = TrueQ @ OptionValue [ "CacheResults" ],
130- $wolframAlphaDebug = TrueQ @ OptionValue [ "Debug" ],
131- $wolframAlphaAppID = Replace [
132- OptionValue [ "AppID" ],
133- $$unspecified :> $wolframAlphaAppID
134- ],
135- $generatorLLMConfig = Replace [
136- OptionValue [ "LLMEvaluator" ],
137- $$unspecified :> $generatorLLMConfig
138- ],
139- $usePromptHeader = Replace [
140- OptionValue [ "PromptHeader" ],
141- $$unspecified :> $usePromptHeader
142- ],
143- $instructions = OptionValue [ "Instructions" ],
144- $maxItems = n ,
145- $sampleQueries = None ,
146- $sampleQueryCount = Replace [ OptionValue [ "SampleQueryCount" ], $$unspecified |None :> 0 ]
140+ $cacheResults = TrueQ @ OptionValue [ "CacheResults" ],
141+ $wolframAlphaDebug = TrueQ @ OptionValue [ "Debug" ],
142+ $wolframAlphaAppID = Replace [ OptionValue [ "AppID" ], $$unspecified :> $wolframAlphaAppID ],
143+ $generatorLLMConfig = Replace [ OptionValue [ "LLMEvaluator" ], $$unspecified :> $generatorLLMConfig ],
144+ $usePromptHeader = Replace [ OptionValue [ "PromptHeader" ], $$unspecified :> $usePromptHeader ],
145+ $includeWLResults = Replace [ OptionValue [ "IncludeWLResults" ], $$unspecified :> $includeWLResults ],
146+ $instructions = OptionValue [ "Instructions" ],
147+ $maxItems = n ,
148+ $sampleQueries = None ,
149+ $sampleQueryCount = Replace [ OptionValue [ "SampleQueryCount" ], $$unspecified |None :> 0 ],
150+ $reinterpret = Replace [ OptionValue [ "Reinterpret" ], $$unspecified :> $reinterpret ]
147151 },
148152 relatedWolframAlphaResultsPrompt [
149153 ensureChatMessages @ prompt ,
@@ -174,24 +178,17 @@ RelatedWolframAlphaResults[ prompt_, "Content", n_Integer, opts: OptionsPattern[
174178RelatedWolframAlphaResults [ prompt_ , "FullData" , n_ Integer , opts : OptionsPattern [ ] ] :=
175179 catchMine @ withChatState @ Block [
176180 {
177- $cacheResults = TrueQ @ OptionValue [ "CacheResults" ],
178- $wolframAlphaDebug = TrueQ @ OptionValue [ "Debug" ],
179- $wolframAlphaAppID = Replace [
180- OptionValue [ "AppID" ],
181- $$unspecified :> $wolframAlphaAppID
182- ],
183- $generatorLLMConfig = Replace [
184- OptionValue [ "LLMEvaluator" ],
185- $$unspecified :> $generatorLLMConfig
186- ],
187- $usePromptHeader = Replace [
188- OptionValue [ "PromptHeader" ],
189- $$unspecified :> $usePromptHeader
190- ],
191- $instructions = OptionValue [ "Instructions" ],
192- $maxItems = n ,
193- $sampleQueries = None ,
194- $sampleQueryCount = Replace [ OptionValue [ "SampleQueryCount" ], $$unspecified |None :> All ]
181+ $cacheResults = TrueQ @ OptionValue [ "CacheResults" ],
182+ $wolframAlphaDebug = TrueQ @ OptionValue [ "Debug" ],
183+ $wolframAlphaAppID = Replace [ OptionValue [ "AppID" ], $$unspecified :> $wolframAlphaAppID ],
184+ $generatorLLMConfig = Replace [ OptionValue [ "LLMEvaluator" ], $$unspecified :> $generatorLLMConfig ],
185+ $usePromptHeader = Replace [ OptionValue [ "PromptHeader" ], $$unspecified :> $usePromptHeader ],
186+ $includeWLResults = Replace [ OptionValue [ "IncludeWLResults" ], $$unspecified :> $includeWLResults ],
187+ $instructions = OptionValue [ "Instructions" ],
188+ $maxItems = n ,
189+ $sampleQueries = None ,
190+ $sampleQueryCount = Replace [ OptionValue [ "SampleQueryCount" ], $$unspecified |None :> All ],
191+ $reinterpret = Replace [ OptionValue [ "Reinterpret" ], $$unspecified :> $reinterpret ]
195192 },
196193 relatedWolframAlphaResultsFullData [
197194 ensureChatMessages @ prompt ,
@@ -905,7 +902,7 @@ submitXMLRequest // Attributes = { HoldFirst };
905902submitXMLRequest [ assoc_ ] :=
906903 submitXMLRequest [ assoc , # ] & ;
907904
908- (* cSpell: ignore sbsmode, podstate, scantimeout, podtimeout, formattimeout, parsetimeout, totaltimeout *)
905+ (* cSpell: ignore minput, sbsmode, podstate, scantimeout, podtimeout, formattimeout, parsetimeout, totaltimeout *)
909906submitXMLRequest [ assoc_ , query_ String ] /; StringQ @ $wolframAlphaAppID := Enclose [
910907 Module [ { request , task },
911908
@@ -920,12 +917,12 @@ submitXMLRequest[ assoc_, query_String ] /; StringQ @ $wolframAlphaAppID := Encl
920917 < |
921918 "Query" -> {
922919 "input" -> query ,
923- "reinterpret" -> False ,
920+ "reinterpret" -> $reinterpret ,
924921 "appid" -> $wolframAlphaAppID ,
925- "format" -> "image,plaintext" ,
922+ "format" -> StringRiffle [ $formats , "," ] ,
926923 "output" -> "xml" ,
927924 "sbsmode" -> "StepByStepDetails" ,
928- "podstate" -> "Step-by-step solution,Show all steps" ,
925+ "podstate" -> StringRiffle [ $podStates , "," ] ,
929926 "scantimeout" -> $timeouts [ "AlphaScan" ],
930927 "podtimeout" -> $timeouts [ "AlphaPod" ],
931928 "formattimeout" -> $timeouts [ "AlphaFormat" ],
@@ -952,15 +949,16 @@ submitXMLRequest[ assoc_, query_String ] := Enclose[
952949
953950 If [ ! AssociationQ @ assoc , assoc = < | |> ];
954951 If [ ! AssociationQ @ assoc @ query , assoc @ query = < | |> ];
955- assoc [ query , "Query" ] = query ;
956- assoc [ query , "URL" ] = makeWolframAlphaURL @ query ;
952+ assoc [ query , "Query" ] = query ;
953+ assoc [ query , "URL" ] = makeWolframAlphaURL @ query ;
954+ assoc [ query , "Content" ] = $TimedOut ;
957955
958956 url = ConfirmBy [
959957 WolframAlpha [
960958 query ,
961959 "URL" ,
962- PodStates -> { "Step-by-step solution" , "Show all steps" } ,
963- Method -> { "Formats" -> { "image" , "plaintext" } }
960+ PodStates -> $podStates ,
961+ Method -> { "Formats" -> $formats }
964962 ],
965963 StringQ ,
966964 "URL"
@@ -992,7 +990,7 @@ setXMLResult[ result_ ] :=
992990 setXMLResult [ result , # ] & ;
993991
994992setXMLResult [ result_ , as : KeyValuePattern @ { "StatusCode" -> 200 , "BodyByteArray" -> bytes_ ByteArray } ] := Enclose [
995- Module [ { xmlString , xml , warnings , processed , query , input },
993+ Module [ { xmlString , xml , warnings , processed , query , input , content },
996994
997995 xmlString = ConfirmBy [ ByteArrayToString @ bytes , StringQ , "XMLString" ];
998996
@@ -1005,14 +1003,18 @@ setXMLResult[ result_, as: KeyValuePattern @ { "StatusCode" -> 200, "BodyByteArr
10051003 warnings = Internal ` Bag [ ];
10061004 processed = Block [ { $warnings = warnings }, processXML @ xml ];
10071005 query = ConfirmBy [ result [ "Query" ], StringQ , "Query" ];
1008- input = ConfirmMatch [ inputInterpretation [ query , processed ], _ String |_ Missing , "Input" ];
1006+ input = ConfirmMatch [ inputInterpretation [ query , xml ], _ String |_ Missing , "Input" ];
1007+ content = If [ ListQ @ processed && TrueQ @ $includeWLResults ,
1008+ ConfirmBy [ formatNaturalLanguageInput @ processed , ListQ , "Content" ],
1009+ processed
1010+ ];
10091011
10101012 If [ ! AssociationQ @ result , result = < | |> ];
10111013 result = < |
10121014 result ,
10131015 as ,
10141016 "InputInterpretation" -> input ,
1015- "Content" -> processed ,
1017+ "Content" -> content ,
10161018 "Warnings" -> Internal ` BagPart [ warnings , All ]
10171019 |>
10181020 ],
@@ -1024,24 +1026,71 @@ setXMLResult[ result_, as: KeyValuePattern @ { "StatusCode" -> _Missing, "BodyBy
10241026
10251027setXMLResult // endDefinition ;
10261028
1029+ (* ::**************************************************************************************************************:: *)
1030+ (* ::Subsubsection::Closed:: *)
1031+ (*formatNaturalLanguageInput*)
1032+ formatNaturalLanguageInput // beginDefinition ;
1033+
1034+ formatNaturalLanguageInput [ processed_ List ] := ReplaceAll [
1035+ processed ,
1036+ string_ String :>
1037+ RuleCondition @ StringReplace [
1038+ string ,
1039+ "# Input interpretation\n\n " ~~ query__ ~~ "\n\n ```wl\n " ~~ code__ ~~ "\n ```" /;
1040+ StringFreeQ [ query , "\n\n " ] :> makeInterpretationCode [ query , code ]
1041+ ]
1042+ ];
1043+
1044+ formatNaturalLanguageInput // endDefinition ;
1045+
1046+ (* ::**************************************************************************************************************:: *)
1047+ (* ::Subsubsection::Closed:: *)
1048+ (*makeInterpretationCode*)
1049+ makeInterpretationCode // beginDefinition ;
1050+
1051+ makeInterpretationCode [ query0_ String , code_ String ] := Enclose [
1052+ Module [ { query , input },
1053+
1054+ query = StringRiffle @ DeleteCases [ StringTrim @ StringSplit [ query0 , { "\\ |" , "\n " } ], "" ];
1055+
1056+ input = If [ StringMatchQ [ code , "EntityClass[" ~~ __ ~~ "]" ],
1057+ "EntityList[\[ FreeformPrompt][\" " <> query <> "\" ]]" ,
1058+ "\[ FreeformPrompt][\" " <> query <> "\" ]"
1059+ ];
1060+
1061+ ConfirmBy [
1062+ TemplateApply [ $interpretationTemplate , < | "Input" -> input , "Query" -> query , "Code" -> code |> ],
1063+ StringQ ,
1064+ "Result"
1065+ ]
1066+ ],
1067+ throwInternalFailure
1068+ ];
1069+
1070+ makeInterpretationCode // endDefinition ;
1071+
1072+
1073+ $interpretationTemplate = StringTemplate [ "\
1074+ # Input interpretation
1075+
1076+ ```wl
1077+ In[1]:= %%Input%%
1078+
1079+ During evaluation of In[1]:= [INFO] Interpreted \" %%Query%%\" as: %%Code%%
1080+ ```" , Delimiters -> "%%" ];
1081+
10271082(* ::**************************************************************************************************************:: *)
10281083(* ::Subsubsection::Closed:: *)
10291084(*inputInterpretation*)
10301085inputInterpretation // beginDefinition ;
10311086
1032- inputInterpretation [ query_ String , content_ ] := FirstCase [
1033- content ,
1034- s_ String :> With [
1035- {
1036- int = StringCases [
1037- s ,
1038- "# Input interpretation\n " ~~ Shortest [ i__ ] ~~ (("\n " ~~ "#" .. ~~ " " ) | EndOfString ) :> i ,
1039- 1
1040- ]
1041- },
1042- StringTrim @ First @ int /; MatchQ [ int , { _ String } ]
1043- ],
1044- Missing [ "NotFound" , query ],
1087+ inputInterpretation [ query_ String , xml_ ] := FirstCase [
1088+ xml ,
1089+ XMLElement [ "pod" , KeyValuePattern [ "title" -> "Input interpretation" ], content_ ] :>
1090+ With [ { s = StringTrim @ StringJoin @ Select [ parsePodContent @ content , StringQ ] },
1091+ s /; s =!= ""
1092+ ],
1093+ Missing [ "NotFound" ],
10451094 Infinity
10461095];
10471096
@@ -1057,6 +1106,9 @@ processXML[ xml_ ] := Enclose[
10571106
10581107 parsed = parseXML @ xml ;
10591108 If [ MatchQ [ parsed , KeyValuePattern [ "Type" -> "Error" ] ], Throw @ parsed ];
1109+ If [ MatchQ [ parsed , { KeyValuePattern [ "Title" -> "Input interpretation" ] } ],
1110+ Throw @ < | "Type" -> "Error" , "Data" -> Missing [ "NoContent" ] |>
1111+ ];
10601112 pods = SortBy [ Cases [ parsed , KeyValuePattern [ "Type" -> "Pod" ] ], Lookup [ "Position" ] ];
10611113 markdown = toMarkdownData @ pods ;
10621114
@@ -1159,6 +1211,9 @@ parsePodContent[ XMLElement[ "plaintext", _, text: _String | { __String } ] ] :=
11591211parsePodContent [ XMLElement [ "plaintext" , _ , { } ] ] :=
11601212 { };
11611213
1214+ parsePodContent [ xml : XMLElement [ "minput" , _ , _ ] ] :=
1215+ parseWLCode @ xml ;
1216+
11621217parsePodContent [ XMLElement [ "mathml" , _ , xml_ ] ] :=
11631218 parseMathML @ xml ;
11641219
@@ -1170,6 +1225,30 @@ parsePodContent[ _String ] :=
11701225
11711226parsePodContent // endDefinition ;
11721227
1228+ (* ::**************************************************************************************************************:: *)
1229+ (* ::Subsubsection::Closed:: *)
1230+ (*parseWLCode*)
1231+ parseWLCode // beginDefinition ;
1232+
1233+ parseWLCode [ XMLElement [ "minput" , _ , xml_ ] ] :=
1234+ parseWLCode @ xml ;
1235+
1236+ parseWLCode [ code_ String ] :=
1237+ code ;
1238+
1239+ parseWLCode [ content_ List ] := Enclose [
1240+ Catch @ Module [ { parsed , strings , code },
1241+ parsed = ConfirmMatch [ Flatten [ parseWLCode /@ content ], { ___ String }, "Parsed" ];
1242+ strings = DeleteCases [ StringTrim @ parsed , "" ];
1243+ If [ strings === { }, Throw @ { } ];
1244+ code = StringRiffle [ strings , "\n\n " ];
1245+ { "```wl\n " <> code <> "\n ```" }
1246+ ],
1247+ throwInternalFailure
1248+ ];
1249+
1250+ parseWLCode // endDefinition ;
1251+
11731252(* ::**************************************************************************************************************:: *)
11741253(* ::Subsubsection::Closed:: *)
11751254(*parseAssumptions*)
0 commit comments