@@ -15,12 +15,78 @@ HoldComplete[
15
15
(* ::Section::Closed:: *)
16
16
(*DefaultPromptGenerators*)
17
17
$defaultPromptGenerators := $defaultPromptGenerators = < |
18
- "RelatedDocumentation" -> LLMPromptGenerator [ relatedDocumentationGenerator , "Messages" ](* ,
19
- "RelatedWolframAlphaQueries" -> LLMPromptGenerator[ relatedWolframAlphaQueriesGenerator , "Messages" ]*)
18
+ "RelatedDocumentation" -> LLMPromptGenerator [ relatedDocumentationGenerator , "Messages" ],
19
+ "WebSearch" -> LLMPromptGenerator [ webSearchGenerator , "Messages" ]
20
20
|> ;
21
21
22
22
(* TODO: update RelatedWolframAlphaQueries to support same argument types as RelatedDocumentation *)
23
23
24
+ (* ::**************************************************************************************************************:: *)
25
+ (* ::Subsection::Closed:: *)
26
+ (*webSearchGenerator*)
27
+ webSearchGenerator // beginDefinition ;
28
+
29
+ webSearchGenerator [ messages : $$chatMessages ] /; CurrentChatSettings [ "WebSearchRAGMethod" ] === "Tavily" := Enclose [
30
+ Catch @ Module [ { key , string , request , response , data , results , snippets },
31
+
32
+ key = SystemCredential [ "TAVILY_API_KEY" ];
33
+ If [ ! StringQ @ key , Throw [ "" ] ];
34
+
35
+ string = StringDelete [
36
+ ConfirmBy [ getSmallContextString @ messages , StringQ , "String" ],
37
+ Shortest [ ("/wl" |"/wa" ) ~~ __ ~~ "ENDRESULT\n " ]
38
+ ];
39
+
40
+ If [ StringLength @ string > 200 , string = "..." <> StringTake [ string , { - 197 , - 1 } ] ];
41
+
42
+ request = HTTPRequest [
43
+ "https://api.tavily.com/search" ,
44
+ < |
45
+ "Method" -> "POST" ,
46
+ "ContentType" -> "application/json" ,
47
+ "Body" -> Developer ` WriteRawJSONString @ < | "query" -> string , "api_key" -> key |>
48
+ |>
49
+ ];
50
+
51
+ response = URLRead @ request ;
52
+
53
+ If [ response [ "StatusCode" ] =!= 200 , Throw [ "" ] ];
54
+
55
+ data = Developer ` ReadRawJSONString @ ByteArrayToString @ response [ "BodyByteArray" ];
56
+ If [ ! AssociationQ @ data , Throw [ "" ] ];
57
+
58
+ results = Select [
59
+ ConfirmMatch [ data [ "results" ], { KeyValuePattern [ "score" -> $$size ]... }, "Results" ],
60
+ # score > 0.1 &
61
+ ];
62
+
63
+ If [ results === { }, Throw [ "" ] ];
64
+
65
+ snippets = ConfirmMatch [ formatWebSearchResult /@ results , { __ String }, "Snippets" ];
66
+
67
+ "# Web Search Results\n\n " <> StringRiffle [ snippets , "\n\n ======\n\n " ] <> "\n\n ======\n\n "
68
+ ],
69
+ throwInternalFailure
70
+ ];
71
+
72
+ webSearchGenerator [ messages_ ] :=
73
+ "" ;
74
+
75
+ webSearchGenerator // endDefinition ;
76
+
77
+ (* ::**************************************************************************************************************:: *)
78
+ (* ::Subsubsection::Closed:: *)
79
+ (*formatWebSearchResult*)
80
+ formatWebSearchResult // beginDefinition ;
81
+
82
+ formatWebSearchResult [ KeyValuePattern @ {
83
+ "title" -> title_ String ,
84
+ "url" -> url_ String ,
85
+ "content" -> content_ String
86
+ } ] := "## [" <> title <> "](" <> url <> ")\n\n " <> content ;
87
+
88
+ formatWebSearchResult // endDefinition ;
89
+
24
90
(* ::**************************************************************************************************************:: *)
25
91
(* ::Subsection::Closed:: *)
26
92
(*relatedDocumentationGenerator*)
0 commit comments