Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search Functionality #799

Merged
merged 10 commits into from
Oct 2, 2024
2 changes: 2 additions & 0 deletions Source/Chatbook/ChatModes/ShowCodeAssistance.wl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ $workspaceChatWidth = 325;

$codeAssistanceBaseSettings = <|
"AppName" -> "CodeAssistance",
"Authentication" -> Automatic, (* TODO *)
"Model" -> <| "Service" -> "OpenAI", "Name" -> "gpt-4o" |>,
"PromptGenerators" -> { "RelatedDocumentation" },
"ServiceCaller" -> "CodeAssistance",
"ToolOptions" -> <| "WolframLanguageEvaluator" -> <| "AppendURIPrompt" -> True, "Method" -> "Session" |> |>,
Expand Down
22 changes: 13 additions & 9 deletions Source/Chatbook/ChatModes/UI.wl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ $inputFieldFrameOptions = Sequence[
FrameStyle -> Directive[ AbsoluteThickness[ 2 ], RGBColor[ "#a3c9f2" ] ]
];

$userImageParams = <| "size" -> 50, "default" -> "identicon", "rating" -> "G" |>;
$userImageParams = <| "size" -> 40, "default" -> "404", "rating" -> "G" |>;

$defaultUserImage = Graphics[
{
Expand All @@ -41,7 +41,7 @@ $defaultUserImage = Graphics[
Disk[ { 0, 1 }, 1 ],
Disk[ { 0, -1.8 }, { 1.65, 2 } ]
},
ImageSize -> 25,
ImageSize -> 20,
PlotRange -> { { -2.4, 2.4 }, { -2.0, 2.8 } }
];

Expand All @@ -51,7 +51,7 @@ $inputFieldBox = None;
$inlineChatScrollPosition = 0.0;
$lastScrollPosition = 0.0;

$maxHistoryItems = 50;
$maxHistoryItems = 25;

(* ::**************************************************************************************************************:: *)
(* ::Section::Closed:: *)
Expand Down Expand Up @@ -717,12 +717,12 @@ userImage[ ] := userImage[ $CloudUserID ];

userImage[ user_String ] := Enclose[
Module[ { hash, url, image },
hash = Hash[ ToLowerCase @ StringTrim @ user, "MD5", "HexString" ];
url = ConfirmBy[ URLBuild[ { "https://www.gravatar.com/avatar/", hash }, $userImageParams ], StringQ, "URL" ];
image = ConfirmBy[ Import @ url, ImageQ, "Image" ];
userImage[ user ] = Show[ image, ImageSize -> 25 ]
hash = Hash[ ToLowerCase @ StringTrim @ user, "MD5", "HexString" ];
url = ConfirmBy[ URLBuild[ { "https://www.gravatar.com/avatar/", hash }, $userImageParams ], StringQ, "URL" ];
image = ConfirmBy[ Quiet @ Import @ url, ImageQ, "Image" ];
userImage[ user ] = Show[ image, ImageSize -> 20 ]
],
$defaultUserImage &
(userImage[ user ] = $defaultUserImage) &
];

userImage[ other_ ] :=
Expand Down Expand Up @@ -858,7 +858,11 @@ createHistoryMenu[ nbo_NotebookObject ] := Enclose[
appName = ConfirmBy[ CurrentChatSettings[ nbo, "AppName" ], StringQ, "AppName" ];
chats = ConfirmMatch[ ListSavedChats @ appName, { ___Association }, "Chats" ];
If[ chats === { }, Throw @ ActionMenu[ "History", { "Nothing here yet" :> Null } ] ];
ActionMenu[ "History", makeHistoryMenuItem[ nbo ] /@ Take[ chats, UpTo @ $maxHistoryItems ] ]
ActionMenu[
"History",
makeHistoryMenuItem[ nbo ] /@ Take[ chats, UpTo @ $maxHistoryItems ],
Method -> "Queued"
]
],
throwInternalFailure
];
Expand Down
7 changes: 5 additions & 2 deletions Source/Chatbook/Common.wl
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ Needs[ "Wolfram`Chatbook`" ];
$cloudNotebooks := TrueQ @ CloudSystem`$CloudNotebooks;

$chatIndicatorSymbol = "\|01f4ac";
$defaultAppName = "Default";

$chatDelimiterStyles = { "ChatBlockDivider", "ChatDelimiter", "ExcludedChatDelimiter" };
$chatIgnoredStyles = { "ChatExcluded" };
Expand Down Expand Up @@ -127,7 +128,9 @@ $resourceVersions = <|
"ClickToCopy" -> "1.0.0",
"GPTTokenizer" -> "1.1.0",
"MessageFailure" -> "1.0.0",
"ReplaceContext" -> "1.0.0"
"RelativeTimeString" -> "1.0.0",
"ReplaceContext" -> "1.0.0",
"SelectByCurrentValue" -> "1.0.1"
|>;

(* ::**************************************************************************************************************:: *)
Expand Down Expand Up @@ -541,7 +544,7 @@ importResourceFunction::failure = "[ERROR] Failed to import resource function `1
importResourceFunction // Attributes = { HoldFirst };

importResourceFunction[ symbol_Symbol, name_String ] :=
importResourceFunction[ symbol, name, Lookup[ $resourceVersions, name, "Latest" ] ];
importResourceFunction[ symbol, name, Lookup[ $resourceVersions, name ] ];

importResourceFunction[ symbol_Symbol, name_String, version_String ] /; $mxFlag := Enclose[
Block[ { PrintTemporary },
Expand Down
5 changes: 5 additions & 0 deletions Source/Chatbook/CommonSymbols.wl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ BeginPackage[ "Wolfram`Chatbook`Common`" ];
`$currentChatSettings;
`$currentSettingsCache;
`$customToolFormatter;
`$defaultAppName;
`$defaultChatSettings;
`$defaultChatTools;
`$defaultMaxCellStringLength;
Expand Down Expand Up @@ -151,7 +152,11 @@ BeginPackage[ "Wolfram`Chatbook`Common`" ];
`functionTemplateBoxes;
`getAvailableServiceNames;
`getBoxObjectFromBoxID;
`getChatConversationData;
`getChatGroupSettings;
`getChatMetadata;
`getEmbedding;
`getEmbeddings;
`getHandlerFunctions;
`getInlineChatPrompt;
`getModelList;
Expand Down
7 changes: 7 additions & 0 deletions Source/Chatbook/Main.wl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ BeginPackage[ "Wolfram`Chatbook`" ];
`$ToolFunctions;
`$WorkspaceChat;
`AbsoluteCurrentChatSettings;
`AddChatToSearchIndex;
`AppendURIInstructions;
`BasePrompt;
`CachedBoxes;
Expand Down Expand Up @@ -65,10 +66,12 @@ BeginPackage[ "Wolfram`Chatbook`" ];
`LoadChat;
`LogChatTiming;
`MakeExpressionURI;
`RebuildChatSearchIndex;
`RelatedDocumentation;
`RelatedWolframAlphaQueries;
`SandboxLinguisticAssistantData;
`SaveChat;
`SearchChats;
`SetModel;
`SetToolOptions;
`ShowCodeAssistance;
Expand Down Expand Up @@ -143,6 +146,7 @@ $ChatbookContexts = {
"Wolfram`Chatbook`Prompting`",
"Wolfram`Chatbook`ResourceInstaller`",
"Wolfram`Chatbook`Sandbox`",
"Wolfram`Chatbook`Search`",
"Wolfram`Chatbook`SendChat`",
"Wolfram`Chatbook`Serialization`",
"Wolfram`Chatbook`Services`",
Expand Down Expand Up @@ -184,6 +188,7 @@ $ChatbookProtectedNames = "Wolfram`Chatbook`" <> # & /@ {
"$ToolFunctions",
"$WorkspaceChat",
"AbsoluteCurrentChatSettings",
"AddChatToSearchIndex",
"AppendURIInstructions",
"BasePrompt",
"CachedBoxes",
Expand Down Expand Up @@ -216,10 +221,12 @@ $ChatbookProtectedNames = "Wolfram`Chatbook`" <> # & /@ {
"LoadChat",
"LogChatTiming",
"MakeExpressionURI",
"RebuildChatSearchIndex",
"RelatedDocumentation",
"RelatedWolframAlphaQueries",
"SandboxLinguisticAssistantData",
"SaveChat",
"SearchChats",
"SetModel",
"SetToolOptions",
"ShowCodeAssistance",
Expand Down
52 changes: 34 additions & 18 deletions Source/Chatbook/PromptGenerators/VectorDatabases.wl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ HoldComplete[
(* ::**************************************************************************************************************:: *)
(* ::Section::Closed:: *)
(*Configuration*)
$vectorDBNames = { "DocumentationURIs", "WolframAlphaQueries" };
$dbVersion = "1.1.0";
$allowDownload = True;
$vectorDBNames = { "DocumentationURIs", "WolframAlphaQueries" };
$dbVersion = "1.1.0";
$allowDownload = True;
$cacheEmbeddings = True;

$embeddingDimension = 384;
$maxNeighbors = 50;
Expand Down Expand Up @@ -693,14 +694,15 @@ cacheVectorDBResult // endDefinition;
(* ::Subsection::Closed:: *)
(*getEmbedding*)
getEmbedding // beginDefinition;
getEmbedding // Options = { "CacheEmbeddings" -> $cacheEmbeddings };

getEmbedding[ string_String ] :=
getEmbedding[ string_String, opts: OptionsPattern[ ] ] :=
With[ { embedding = $embeddingCache[ string ] },
embedding /; NumericArrayQ @ embedding
];

getEmbedding[ string_String ] := Enclose[
First @ ConfirmMatch[ getEmbeddings @ { string }, { _NumericArray }, "Embedding" ],
getEmbedding[ string_String, opts: OptionsPattern[ ] ] := Enclose[
First @ ConfirmMatch[ getEmbeddings[ { string }, opts ], { _NumericArray }, "Embedding" ],
throwInternalFailure
];

Expand All @@ -710,19 +712,31 @@ getEmbedding // endDefinition;
(* ::Subsection::Closed:: *)
(*getEmbeddings*)
getEmbeddings // beginDefinition;
getEmbeddings // Options = { "CacheEmbeddings" -> $cacheEmbeddings };

getEmbeddings[ { } ] := { };
getEmbeddings[ { }, opts: OptionsPattern[ ] ] := { };

getEmbeddings[ strings: { __String } ] := Enclose[
getEmbeddings[ strings: { __String }, opts: OptionsPattern[ ] ] :=
If[ TrueQ @ OptionValue[ "CacheEmbeddings" ],
getEmbeddings0 @ strings,
Block[ { $cacheEmbeddings = False }, getAndCacheEmbeddings @ strings ]
] // LogChatTiming[ "GetEmbeddings" ];

getEmbeddings // endDefinition;


getEmbeddings0 // beginDefinition;

getEmbeddings0[ strings: { __String } ] := Enclose[
Module[ { notCached },
notCached = Select[ strings, ! KeyExistsQ[ $embeddingCache, # ] & ];
ConfirmMatch[ getAndCacheEmbeddings @ notCached, { ___NumericArray }, "CacheEmbeddings" ];
ConfirmMatch[ Lookup[ $embeddingCache, strings ], { __NumericArray }, "Result" ]
] // LogChatTiming[ "GetEmbeddings" ],
],
throwInternalFailure
];

getEmbeddings // endDefinition;
getEmbeddings0 // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsection::Closed:: *)
Expand All @@ -742,10 +756,7 @@ getAndCacheEmbeddings[ strings: { __String } ] /; $embeddingModel === "SentenceB

ConfirmAssert[ Length @ strings === Length @ vectors, "LengthCheck" ];

MapThread[
($embeddingCache[ #1 ] = toTinyVector @ #2) &,
{ strings, vectors }
]
MapThread[ cacheEmbedding, { strings, vectors } ]
],
throwInternalFailure
];
Expand All @@ -771,16 +782,21 @@ getAndCacheEmbeddings[ strings: { __String } ] := Enclose[

ConfirmAssert[ Length @ strings === Length @ vectors, "LengthCheck" ];

MapThread[
($embeddingCache[ #1 ] = toTinyVector @ #2) &,
{ strings, vectors }
]
MapThread[ cacheEmbedding, { strings, vectors } ]
],
throwInternalFailure
];

getAndCacheEmbeddings // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsection::Closed:: *)
(*cacheEmbedding*)
cacheEmbedding // beginDefinition;
cacheEmbedding[ key_String, vector_ ] /; ! $cacheEmbeddings := toTinyVector @ vector;
cacheEmbedding[ key_String, vector_ ] := $embeddingCache[ key ] = toTinyVector @ vector;
cacheEmbedding // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsection::Closed:: *)
(*sentenceBERTEmbedding*)
Expand Down
Loading
Loading