@@ -39,13 +39,15 @@ $unfilteredItemsPerSource = 10;
39
39
(* ::**************************************************************************************************************:: *)
40
40
(* ::Section::Closed:: *)
41
41
(*Messages*)
42
- Chatbook ::CloudDownloadError = "Unable to download required data from the cloud. Please try again later." ;
43
- Chatbook ::InvalidSources = "Invalid value for the \" Sources\" option: `1`." ;
42
+ Chatbook ::CloudDownloadError = "Unable to download required data from the cloud. Please try again later." ;
43
+ Chatbook ::InvalidSources = "Invalid value for the \" Sources\" option: `1`." ;
44
+ Chatbook ::SnippetFunctionOutputFailure = "The snippet function `1` returned a list of length `2` for `3` values." ;
45
+ Chatbook ::SnippetFunctionLengthFailure = "The snippet function `1` returned a list of length `2` for `3` values." ;
44
46
45
47
(* ::**************************************************************************************************************:: *)
46
48
(* ::Section::Closed:: *)
47
49
(*$RelatedDocumentationSources*)
48
- $RelatedDocumentationSources = $defaultSources ;
50
+ $RelatedDocumentationSources : = $defaultSources ;
49
51
50
52
(* ::**************************************************************************************************************:: *)
51
53
(* ::Section::Closed:: *)
@@ -94,20 +96,28 @@ RelatedDocumentation[ prompt_, Automatic, count_, opts: OptionsPattern[ ] ] :=
94
96
95
97
RelatedDocumentation [ prompt : $$prompt , "URIs" , Automatic , opts : OptionsPattern [ ] ] := catchMine @ Enclose [
96
98
(* TODO: filter results *)
97
- ConfirmMatch [ vectorDBSearch [ getSources @ OptionValue [ "Sources" ], prompt , "Values" ], { ___ String }, "Queries" ],
99
+ URL /@ ConfirmMatch [
100
+ vectorDBSearch [ getSources @ OptionValue [ "Sources" ], prompt , "Values" ],
101
+ { ___ String },
102
+ "Values"
103
+ ],
98
104
throwInternalFailure
99
105
];
100
106
101
107
RelatedDocumentation [ All , "URIs" , Automatic , opts : OptionsPattern [ ] ] := catchMine @ Enclose [
102
108
(* TODO: filter results *)
103
- Union @ ConfirmMatch [ vectorDBSearch [ getSources @ OptionValue [ "Sources" ], All ], { __ String }, "QueryList" ],
109
+ URL /@ Union @ ConfirmMatch [
110
+ vectorDBSearch [ getSources @ OptionValue [ "Sources" ], All ],
111
+ { ___ String },
112
+ "Values"
113
+ ],
104
114
throwInternalFailure
105
115
];
106
116
107
117
RelatedDocumentation [ prompt : $$prompt , "Snippets" , Automatic , opts : OptionsPattern [ ] ] := catchMine @ Enclose [
108
118
ConfirmMatch [
109
119
(* TODO: filter results *)
110
- DeleteMissing [ makeDocSnippets @ vectorDBSearch [ getSources @ OptionValue [ "Sources" ], prompt , "Values " ] ],
120
+ DeleteMissing [ makeDocSnippets @ vectorDBSearch [ getSources @ OptionValue [ "Sources" ], prompt , "Results " ] ],
111
121
{ ___ String },
112
122
"Snippets"
113
123
],
@@ -118,7 +128,7 @@ RelatedDocumentation[ prompt_, property_, UpTo[ n_Integer ], opts: OptionsPatter
118
128
catchMine @ RelatedDocumentation [ prompt , property , n , opts ];
119
129
120
130
RelatedDocumentation [ prompt_ , property_ , n_ Integer , opts : OptionsPattern [ ] ] := catchMine @ Enclose [
121
- Take [ ConfirmMatch [ RelatedDocumentation [ prompt , property , Automatic , opts ], { ___ String } ], UpTo @ n ],
131
+ Take [ ConfirmBy [ RelatedDocumentation [ prompt , property , Automatic , opts ], ListQ ], UpTo @ n ],
122
132
throwInternalFailure
123
133
];
124
134
@@ -163,7 +173,8 @@ RelatedDocumentation[ prompt_, "Prompt", n_Integer, opts: OptionsPattern[ ] ] :=
163
173
$rerankMethod = Replace [
164
174
OptionValue [ "RerankMethod" ],
165
175
$$unspecified :> CurrentChatSettings [ "DocumentationRerankMethod" ]
166
- ]
176
+ ],
177
+ $RelatedDocumentationSources = getSources @ OptionValue [ "Sources" ]
167
178
},
168
179
relatedDocumentationPrompt [
169
180
ensureChatMessages @ prompt ,
@@ -223,23 +234,25 @@ ensureChatMessages // endDefinition;
223
234
relatedDocumentationPrompt // beginDefinition ;
224
235
225
236
relatedDocumentationPrompt [ messages : $$chatMessages , count_ , filter_ , filterCount_ ] := Enclose [
226
- Catch @ Module [ { uris , filtered , string },
237
+ Catch @ Module [ { results , filtered , string },
227
238
228
- uris = ConfirmMatch [
229
- RelatedDocumentation [ messages , "URIs" , count ],
230
- { ___ String },
231
- "URIs"
232
- ] // LogChatTiming [ "RelatedDocumentationURIs" ] // withApproximateProgress [ "CheckingDocumentation" , 0.2 ];
239
+ results = ConfirmMatch [
240
+ RelatedDocumentation [ messages , "Results" , count ],
241
+ { ___ Association },
242
+ "Results"
243
+ ] // LogChatTiming [ "RelatedDocumentationResults" ] // withApproximateProgress [ "CheckingDocumentation" , 0.2 ];
244
+
245
+ If [ results === { }, Throw [ "" ] ];
233
246
234
- If [ uris === { }, Throw [ "" ] ];
247
+ results = DeleteDuplicatesBy [ results , Lookup [ "Value " ] ];
235
248
236
249
filtered = ConfirmMatch [
237
- filterSnippets [ messages , uris , filter , filterCount ] // LogChatTiming [ "FilterSnippets" ],
250
+ filterSnippets [ messages , results , filter , filterCount ] // LogChatTiming [ "FilterSnippets" ],
238
251
{ ___ String },
239
252
"Filtered"
240
253
];
241
254
242
- string = StringTrim @ StringRiffle [ "# " <> # & /@ DeleteCases [ filtered , "" ], "\n\n ======\n\n " ];
255
+ string = StringTrim @ StringRiffle [ DeleteCases [ filtered , "" ], "\n\n ======\n\n " ];
243
256
244
257
If [ string === "" ,
245
258
"" ,
@@ -272,20 +285,20 @@ $relatedDocsStringUnfilteredHeader =
272
285
filterSnippets // beginDefinition ;
273
286
274
287
275
- filterSnippets [ messages_ , uris : { __ String } , Except [ True ], filterCount_ ] := Enclose [
276
- ConfirmMatch [ makeDocSnippets @ uris , { ___ String }, "Snippets" ],
288
+ filterSnippets [ messages_ , results_ List , Except [ True ], filterCount_ ] := Enclose [
289
+ ConfirmMatch [ makeDocSnippets @ results , { ___ String }, "Snippets" ],
277
290
throwInternalFailure
278
291
];
279
292
280
293
281
294
filterSnippets [
282
295
messages_ ,
283
- uris : { __ String } ,
296
+ results_ List ,
284
297
True ,
285
298
filterCount_ Integer ? Positive
286
299
] /; $rerankMethod === None := Enclose [
287
300
Catch @ Module [ { snippets },
288
- snippets = ConfirmMatch [ makeDocSnippets @ uris , { ___ String }, "Snippets" ];
301
+ snippets = ConfirmMatch [ makeDocSnippets @ results , { ___ String }, "Snippets" ];
289
302
Take [ snippets , UpTo [ filterCount ] ]
290
303
],
291
304
throwInternalFailure
@@ -294,13 +307,13 @@ filterSnippets[
294
307
295
308
filterSnippets [
296
309
messages_ ,
297
- uris : { __ String } ,
310
+ results_ List ,
298
311
True ,
299
312
filterCount_ Integer ? Positive
300
313
] /; $rerankMethod === "rerank-english-v3.0" (* EXPERIMENTAL *) := Enclose [
301
- Catch @ Module [ { snippets , inserted , transcript , instructions , resp , results , idx , ranked },
314
+ Catch @ Module [ { snippets , inserted , transcript , instructions , resp , respResults , idx , ranked },
302
315
303
- snippets = ConfirmMatch [ makeDocSnippets @ uris , { ___ String }, "Snippets" ];
316
+ snippets = ConfirmMatch [ makeDocSnippets @ results , { ___ String }, "Snippets" ];
304
317
setProgressDisplay [ "ProgressTextChoosingDocumentation" ];
305
318
inserted = insertContextPrompt @ messages ;
306
319
transcript = ConfirmBy [ getSmallContextString @ inserted , StringQ , "Transcript" ];
@@ -323,26 +336,33 @@ filterSnippets[
323
336
324
337
If [ FailureQ @ resp , throwTop @ resp ];
325
338
326
- results = ConfirmMatch [ resp [ "results" ], { __ Association }, "Results" ];
339
+ respResults = ConfirmMatch [ resp [ "results" ], { __ Association }, "Results" ];
327
340
328
341
idx = ConfirmMatch [
329
- Select [ results , # [ "relevance_score" ] > 0.01 & ][[ All , "index" ]] + 1 ,
342
+ Select [ respResults , # [ "relevance_score" ] > 0.01 & ][[ All , "index" ]] + 1 ,
330
343
{ ___ Integer },
331
344
"Indices"
332
345
];
333
346
334
347
ranked = ConfirmMatch [ snippets [[ idx ]], { ___ String }, "Ranked" ];
335
348
349
+ (* FIXME: need to add handler data here *)
350
+
336
351
Take [ ranked , UpTo [ filterCount ] ]
337
352
],
338
353
throwInternalFailure
339
354
];
340
355
341
356
342
- filterSnippets [ messages_ , uris : { __ String }, True , filterCount_ Integer ? Positive ] := Enclose [
343
- Catch @ Module [ { snippets , inserted , transcript , xml , instructions , response , pages },
357
+ filterSnippets [ messages_ , results0_ List , True , filterCount_ Integer ? Positive ] := Enclose [
358
+ Catch @ Module [
359
+ {
360
+ results , snippets , inserted , transcript , xml ,
361
+ instructions , response , uriToSnippet , uris , selected , pages
362
+ },
344
363
345
- snippets = ConfirmMatch [ makeDocSnippets @ uris , { ___ String }, "Snippets" ];
364
+ results = ConfirmMatch [ addDocSnippets @ results0 , { ___ Association }, "Results" ];
365
+ snippets = ConfirmMatch [ Lookup [ results , "Snippet" ], { ___ String }, "Snippets" ];
346
366
setProgressDisplay [ "ChoosingDocumentation" ];
347
367
inserted = insertContextPrompt @ messages ;
348
368
transcript = ConfirmBy [ getSmallContextString @ inserted , StringQ , "Transcript" ];
@@ -370,7 +390,15 @@ filterSnippets[ messages_, uris: { __String }, True, filterCount_Integer? Positi
370
390
"Response"
371
391
];
372
392
373
- pages = ConfirmMatch [ makeDocSnippets @ selectSnippetsFromJSON [ response , uris ], { ___ String }, "Pages" ];
393
+ $lastFilterInstructions = instructions ;
394
+ $lastFilterResponse = response ;
395
+
396
+ uriToSnippet = < | # Value -> # Snippet & /@ results |> ;
397
+ uris = ConfirmMatch [ Keys @ uriToSnippet , { ___ String }, "URIs" ];
398
+ selected = ConfirmMatch [ selectSnippetsFromJSON [ response , uris ], { ___ String }, "Pages" ];
399
+ pages = ConfirmMatch [ Lookup [ uriToSnippet , selected ], { ___ String }, "Pages" ];
400
+
401
+ addHandlerArguments [ "RelatedDocumentation" -> < | "Results" -> uris , "Filtered" -> selected |> ];
374
402
375
403
pages
376
404
],
@@ -387,6 +415,7 @@ Your task is to read a chat transcript between a user and assistant, and then se
387
415
documentation snippets that could help the assistant answer the user's latest message.
388
416
389
417
Each snippet is uniquely identified by a URI (always starts with 'paclet:' or 'https://*.wolframcloud.com').
418
+ You must also include the fragment appearing after the '#' in the URI.
390
419
391
420
Choose up to %%FilteredCount%% documentation snippets that would help answer the user's MOST RECENT message.
392
421
@@ -486,26 +515,100 @@ snippetXML // endDefinition;
486
515
(*Documentation Snippets*)
487
516
$documentationSnippets = < | |> ;
488
517
518
+ (* ::**************************************************************************************************************:: *)
519
+ (* ::Subsection::Closed:: *)
520
+ (*addDocSnippets*)
521
+ addDocSnippets // beginDefinition ;
522
+
523
+ addDocSnippets [ results : { ___ Association } ] := Enclose [
524
+ Module [ { withOrdering , grouped , withSnippets , sorted },
525
+
526
+ withOrdering = MapIndexed [ < | "Position" -> First [ #2 ], #1 |> & , results ];
527
+ grouped = GroupBy [ withOrdering , Lookup [ "SnippetFunction" ] ];
528
+
529
+ withSnippets = ConfirmMatch [
530
+ Flatten @ KeyValueMap [ applySnippetFunction , grouped ],
531
+ { ___ Association },
532
+ "WithSnippets"
533
+ ];
534
+
535
+ sorted = ConfirmMatch [ SortBy [ withSnippets , Lookup [ "Position" ] ], { ___ Association }, "Sorted" ];
536
+
537
+ ConfirmAssert [ Length @ sorted === Length @ results , "LengthCheck" ];
538
+
539
+ sorted
540
+ ],
541
+ throwInternalFailure
542
+ ];
543
+
544
+ addDocSnippets // endDefinition ;
545
+
489
546
(* ::**************************************************************************************************************:: *)
490
547
(* ::Subsection::Closed:: *)
491
548
(*makeDocSnippets*)
492
549
makeDocSnippets // beginDefinition ;
493
550
494
- makeDocSnippets [ uris0 : { ___ String } ] := Enclose [
495
- Module [ { uris , data , snippets , strings },
496
- uris = DeleteDuplicates @ uris0 ;
551
+ makeDocSnippets [ results : { ___ Association } ] := Enclose [
552
+ Module [ { sorted , snippets },
553
+ sorted = ConfirmMatch [ addDocSnippets @ results , { ___ Association }, "Sorted" ];
554
+ snippets = ConfirmMatch [ Lookup [ sorted , "Snippet" ], { ___ String }, "Snippets" ];
555
+ ConfirmAssert [ Length @ snippets === Length @ results , "LengthCheck" ];
556
+ DeleteDuplicates @ snippets
557
+ ],
558
+ throwInternalFailure
559
+ ];
560
+
561
+ makeDocSnippets // endDefinition ;
562
+
563
+ (* ::**************************************************************************************************************:: *)
564
+ (* ::Subsubsection::Closed:: *)
565
+ (*applySnippetFunction*)
566
+ applySnippetFunction // beginDefinition ;
567
+
568
+ applySnippetFunction [ f_ , { } ] := { };
569
+
570
+ applySnippetFunction [ f_ , data : { ___ Association } ] := Enclose [
571
+ Module [ { values , snippets , snippetLen , valuesLen },
572
+
573
+ values = ConfirmMatch [ Lookup [ data , "Value" ], { ___ String }, "Values" ];
574
+ snippets = f @ values ;
575
+ snippetLen = Length @ snippets ;
576
+ valuesLen = Length @ values ;
577
+
578
+ If [ ! MatchQ [ snippets , { ___ String } ], throwFailure [ "SnippetFunctionOutputFailure" , f , snippets ] ];
579
+ If [ snippetLen =!= valuesLen , throwFailure [ "SnippetFunctionLengthFailure" , f , snippetLen , valuesLen ] ];
580
+
581
+ ConfirmBy [
582
+ Association /@ Transpose @ { data , Thread [ "Snippet" -> snippets ] },
583
+ AllTrue @ AssociationQ ,
584
+ "Result"
585
+ ]
586
+ ] // LogChatTiming @ { "ApplySnippetFunction" , f },
587
+ throwInternalFailure
588
+ ];
589
+
590
+ applySnippetFunction // endDefinition ;
591
+
592
+ (* ::**************************************************************************************************************:: *)
593
+ (* ::Subsection::Closed:: *)
594
+ (*getSnippets*)
595
+ getSnippets // beginDefinition ;
596
+
597
+ getSnippets [ uris : { ___ String } ] := Enclose [
598
+ Module [ { data , snippets , strings },
497
599
data = ConfirmBy [ getDocumentationSnippetData @ uris , AssociationQ , "Data" ];
498
- snippets = ConfirmMatch [ Values @ data , { ___ Association }, "Snippets" ];
600
+ snippets = ConfirmMatch [ Lookup [ data , uris ] , { ___ Association }, "Snippets" ];
499
601
strings = ConfirmMatch [ Lookup [ "String" ] /@ snippets , { ___ String }, "Strings" ];
500
- strings
602
+ ConfirmAssert [ Length @ strings === Length @ uris , "LengthCheck" ];
603
+ "# " <> # & /@ strings
501
604
],
502
605
throwInternalFailure
503
606
];
504
607
505
- makeDocSnippets [ uri_ String ] :=
506
- First @ makeDocSnippets @ { uri };
608
+ getSnippets [ uri_ String ] :=
609
+ First @ getSnippets @ { uri };
507
610
508
- makeDocSnippets // endDefinition ;
611
+ getSnippets // endDefinition ;
509
612
510
613
(* ::**************************************************************************************************************:: *)
511
614
(* ::Subsection::Closed:: *)
0 commit comments