Skip to content

Commit 15c6381

Browse files
authored
Merge pull request #992 from WolframResearch/experimental/web-search-rag
Experimental: Added opt-in web search RAG prototype
2 parents f721c82 + 58baf34 commit 15c6381

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@
7878
"Subsubsubsubsection",
7979
"Subsubtitle",
8080
"tabletags",
81+
"tavily",
8182
"textbf",
8283
"textcontent",
8384
"textit",

LLMConfiguration/Personas/NotebookAssistant/LLMConfiguration.wl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
"DisplayName" -> Dynamic @ FEPrivate`FrontEndResource[ "ChatbookStrings", "PersonaNameNotebookAssistant" ],
55
"Hidden" -> True,
66
"Icon" -> RawBoxes @ TemplateBox[ { }, "ChatIconNotebookAssistant" ],
7-
"PromptGenerators" -> { "RelatedDocumentation" },
7+
"PromptGenerators" -> { "RelatedDocumentation", "WebSearch" },
88
"Tools" -> { "WolframAlpha", "WolframLanguageEvaluator", ParentList }
99
|>

PacletInfo.wl

Lines changed: 1 addition & 1 deletion
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.5",
4+
"Version" -> "2.0.6",
55
"WolframVersion" -> "14.1+",
66
"Description" -> "Wolfram Notebooks + LLMs",
77
"License" -> "MIT",

Source/Chatbook/PromptGenerators/DefaultPromptGenerators.wl

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,78 @@ HoldComplete[
1515
(* ::Section::Closed:: *)
1616
(*DefaultPromptGenerators*)
1717
$defaultPromptGenerators := $defaultPromptGenerators = <|
18-
"RelatedDocumentation" -> LLMPromptGenerator[ relatedDocumentationGenerator, "Messages" ](*,
19-
"RelatedWolframAlphaQueries" -> LLMPromptGenerator[ relatedWolframAlphaQueriesGenerator, "Messages" ]*)
18+
"RelatedDocumentation" -> LLMPromptGenerator[ relatedDocumentationGenerator, "Messages" ],
19+
"WebSearch" -> LLMPromptGenerator[ webSearchGenerator , "Messages" ]
2020
|>;
2121

2222
(* TODO: update RelatedWolframAlphaQueries to support same argument types as RelatedDocumentation *)
2323

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+
2490
(* ::**************************************************************************************************************:: *)
2591
(* ::Subsection::Closed:: *)
2692
(*relatedDocumentationGenerator*)

0 commit comments

Comments
 (0)