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

Append URI instructions to evaluator outputs #618

Merged
merged 14 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions Assets/SandboxMessages.wl
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
(* cSpell: ignore ntype *)
(* cSpell: ignore ntype, usenl, notent *)

ExampleData::notent = "`1` is not a known entity for the collection `2`. Try using natural language input (\[FreeformPrompt][\"query\"]) to get the correct expression instead."
General::messages = "Messages were generated which may indicate errors. Use the documentation_searcher tool to find solutions."
General::usenl = "Messages involving `1` were generated. Try using natural language input (\[FreeformPrompt][\"query\"]) to get the correct expression instead."
Needs::nocont = "Context `1` was not created when Needs was evaluated. Use the documentation_searcher tool to find alternatives."
RandomEntity::ntype = "`1` is not a valid type of Entity or EntityClass. Use the documentation_searcher tool to find alternatives."
ResourceObject::notfname = "The ResourceObject `1` does not exist. Use the documentation_searcher tool to find alternatives."
Symbol::undefined = "Warning: Global symbol `1` is undefined. Use the documentation_searcher tool to find alternatives."
Symbol::undefined2 = "Warning: Global symbols `1` are undefined. Use the documentation_searcher tool to find alternatives."
ResourceObject::notfname = "The ResourceObject `1` does not exist. Use the documentation_searcher tool to find alternatives."
Symbol::undefined2 = "Warning: Global symbols `1` are undefined. Use the documentation_searcher tool to find alternatives."
68 changes: 66 additions & 2 deletions Source/Chatbook/ChatMessages.wl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Wolfram`Chatbook`CellToChatMessage;
`expandMultimodalString;
`getTokenizer;
`getTokenizerName;
`logUsage;
`resizeMultimodalImage;

Begin[ "`Private`" ];
Expand Down Expand Up @@ -132,7 +133,15 @@ constructMessages[ settings_Association? AssociationQ, messages0: { __Associatio
prompted = addPrompts[ settings, messages0 ];

messages = prompted /.
s_String :> RuleCondition @ StringTrim @ StringReplace[ s, "%%BASE_PROMPT%%" :> $basePrompt ];
s_String :> RuleCondition @ StringTrim @ StringReplace[
s,
{
"%%BASE_PROMPT%%" :> $basePrompt,
(* cSpell: ignore ENDRESULT *)
"\nENDRESULT(" ~~ Repeated[ LetterCharacter|DigitCharacter, $tinyHashLength ] ~~ ")\n" :>
"\nENDRESULT\n"
}
];

processed = applyProcessingFunction[ settings, "ChatMessages", HoldComplete[ messages, $ChatHandlerData ] ];

Expand Down Expand Up @@ -429,17 +438,71 @@ cutMessageContent // endDefinition;
(*tokenCount*)
tokenCount // beginDefinition;

tokenCount[ as_, KeyValuePattern[ "TokenCount" -> n_Integer ] ] := n;

tokenCount[ as_, messages_List ] :=
With[ { counts = tokenCount[ as, # ] & /@ messages },
Total @ counts /; MatchQ[ counts, { ___Integer } ]
];

tokenCount[ as_Association, message_ ] := Enclose[
Module[ { tokenizer, content },
tokenizer = getTokenizer @ as;
content = ConfirmBy[ messageContent @ message, validContentQ, "Content" ];
Length @ ConfirmMatch[ applyTokenizer[ tokenizer, content ], _List, "TokenCount" ]
],
throwInternalFailure[ tokenCount[ as, message ], ## ] &
throwInternalFailure
];

tokenCount // endDefinition;

(* ::**************************************************************************************************************:: *)
(* ::Subsubsection::Closed:: *)
(*logUsage*)
logUsage // beginDefinition;

logUsage[ KeyValuePattern[ "FullContent" -> response_String ] ] :=
logUsage @ response;

logUsage[ response_String ] := logUsage[
$ChatHandlerData[ "ChatNotebookSettings" ],
$ChatHandlerData[ "Messages" ],
response
];

logUsage[ settings_, messages_List, response_String ] :=
logUsage[ settings, messages, response, $ChatHandlerData[ "Usage" ] ];

logUsage[ settings_, messages_List, response_String, usage_Association ] := Enclose[
Module[ { prompt1, completion1, prompt2, completion2, prompt, completion, requests },

prompt1 = ConfirmMatch[ usage[ "Prompt" ] , $$size, "Prompt1" ];
completion1 = ConfirmMatch[ usage[ "Completion" ] , $$size, "Completion1" ];
prompt2 = ConfirmMatch[ tokenCount[ settings, messages ], $$size, "Prompt2" ];
completion2 = ConfirmMatch[ tokenCount[ settings, response ], $$size, "Completion2" ];

prompt = prompt1 + prompt2;
completion = completion1 + completion2;
requests = Replace[ usage[ "Requests" ], Except[ $$size ] :> 0 ] + 1;

addHandlerArguments[ "Usage" -> <| "Prompt" -> prompt, "Completion" -> completion, "Requests" -> requests |> ]
],
throwInternalFailure
];

logUsage[ settings_, messages_List, response_String, $$unspecified ] :=
addHandlerArguments[
"Usage" -> <|
"Prompt" -> tokenCount[ settings, messages ],
"Completion" -> tokenCount[ settings, response ],
"Requests" -> 1
|>
];

logUsage // endDefinition;

(* TODO: this could log usage by model since the model could technically change during a chat evaluation *)

(* ::**************************************************************************************************************:: *)
(* ::Subsubsection::Closed:: *)
(*applyTokenizer*)
Expand Down Expand Up @@ -760,6 +823,7 @@ inferMultimodalTypes0 // endDefinition;
(* ::Subsubsubsection::Closed:: *)
(*ensureCompatibleImage*)
ensureCompatibleImage // beginDefinition;
ensureCompatibleImage[ img_RawBoxes ] := rasterize @ img; (* Workaround for 446030 *)
ensureCompatibleImage[ img_ ] /; $useRasterizationCompatibility && ! Image`PossibleImageQ @ img := rasterize @ img;
ensureCompatibleImage[ img_ ] := img;
ensureCompatibleImage // endDefinition;
Expand Down
2 changes: 1 addition & 1 deletion Source/Chatbook/Chatbook.wl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Quiet[
Quiet[
Unprotect[ "Wolfram`Chatbook`*" ];
ClearAll[ "Wolfram`Chatbook`*" ];
ClearAll[ "Wolfram`Chatbook`*`*" ],
Remove[ "Wolfram`Chatbook`*`*" ],
{ Remove::rmnsm }
];
Get[ "Wolfram`Chatbook`Main`" ]
Expand Down
3 changes: 2 additions & 1 deletion Source/Chatbook/Common.wl
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,8 @@ KeyValueMap[ Function[ MessageName[ Chatbook, #1 ] = #2 ], <|
"ServerOverloaded" -> "The server is currently overloaded with other requests. Please try again later.",
"ToolNotFound" -> "Tool `1` not found.",
"UnknownResponse" -> "Unexpected response from server",
"UnknownStatusCode" -> "Unexpected response from server with status code `StatusCode`"
"UnknownStatusCode" -> "Unexpected response from server with status code `StatusCode`",
"URIUnavailable" -> "The URI `1` is from a previous kernel session and no longer available."
|> ];

(* ::**************************************************************************************************************:: *)
Expand Down
15 changes: 13 additions & 2 deletions Source/Chatbook/Formatting.wl
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,14 @@ $autoOperatorRenderings = <|
"|>" -> "\[RightAssociation]"
|>;

$expressionURIPlaceholder = "\[LeftSkeleton]\[Ellipsis]\[RightSkeleton]";

(* ::**************************************************************************************************************:: *)
(* ::Section::Closed:: *)
(*StringToBoxes*)
StringToBoxes // beginDefinition;
StringToBoxes[ string_String? StringQ ] := catchAlways[ stringToBoxes @ string, StringToBoxes ];
StringToBoxes[ string_String? StringQ, "WL" ] := catchAlways[ wlStringToBoxes @ string, StringToBoxes ];
StringToBoxes // endExportedDefinition;

(* ::**************************************************************************************************************:: *)
Expand Down Expand Up @@ -1920,8 +1923,16 @@ unescapeInlineMarkdown // endDefinition;
(* ::Subsection::Closed:: *)
(*stringToBoxes*)
stringToBoxes // beginDefinition;
stringToBoxes[ s_String ] /; $dynamicText := s;
stringToBoxes[ s_String ] := adjustBoxSpacing @ MathLink`CallFrontEnd @ FrontEnd`ReparseBoxStructurePacket @ s;

stringToBoxes[ s_String ] /; $dynamicText := StringReplace[
s,
"InlinedExpression[\"" ~~ LetterCharacter.. ~~ "://" ~~ (LetterCharacter|DigitCharacter|"-").. ~~ "\"]" :>
$expressionURIPlaceholder
];

stringToBoxes[ s_String ] :=
adjustBoxSpacing @ MathLink`CallFrontEnd @ FrontEnd`ReparseBoxStructurePacket @ s;

stringToBoxes // endDefinition;

(* ::**************************************************************************************************************:: *)
Expand Down
4 changes: 4 additions & 0 deletions Source/Chatbook/Main.wl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ BeginPackage[ "Wolfram`Chatbook`" ];
`$InstalledTools;
`$SandboxKernel;
`$ToolFunctions;
`AbsoluteCurrentChatSettings;
`AppendURIInstructions;
`CellToChatMessage;
`Chatbook;
`ChatbookAction;
Expand Down Expand Up @@ -121,6 +123,8 @@ Protect[
$DefaultTools,
$InstalledTools,
$ToolFunctions,
AbsoluteCurrentChatSettings,
AppendURIInstructions,
CellToChatMessage,
Chatbook,
ChatbookAction,
Expand Down
Loading
Loading