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

Feature: MaxContextTokens can be set to Infinity to avoid truncation #508

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
14 changes: 9 additions & 5 deletions Source/Chatbook/ChatMessages.wl
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,14 @@ makeChatMessages[ settings_, cells_ ] :=
{
$multimodalMessages = TrueQ @ settings[ "Multimodal" ],
$tokenBudget = settings[ "MaxContextTokens" ],
$tokenPressure = 0.0
$tokenPressure = 0.0,
$initialCellStringBudget = Replace[
settings[ "MaxCellStringLength" ],
Except[ $$size ] -> $defaultMaxCellStringLength
],
$cellStringBudget
},
$cellStringBudget = $initialCellStringBudget;
If[ settings[ "BasePrompt" ] =!= None, tokenCheckedMessage[ settings, $fullBasePrompt ] ];
(* FIXME: need to account for persona/tool prompting as well *)
makeChatMessages0[ settings, cells ]
Expand All @@ -241,10 +247,6 @@ makeChatMessages0[ settings0_, cells_List ] := Enclose[
toMessage0 = Confirm[ getCellMessageFunction @ settings, "CellMessageFunction" ];

$tokenBudgetLog = Internal`Bag[ ];
$initialCellStringBudget = Replace[
settings[ "MaxCellStringLength" ],
Except[ $$size ] -> $defaultMaxCellStringLength
];

toMessage = Function @ With[
{ msg = toMessage0[ #1, <| #2, "TokenBudget" -> $tokenBudget, "TokenPressure" -> $tokenPressure |> ] },
Expand Down Expand Up @@ -304,6 +306,8 @@ combineExcisedMessages // endDefinition;
(*tokenCheckedMessage*)
tokenCheckedMessage // beginDefinition;

tokenCheckedMessage[ as_Association, message_ ] /; $cellStringBudget === Infinity := message;

tokenCheckedMessage[ as_Association, message_ ] := Enclose[
Catch @ Module[ { count, budget, resized },

Expand Down
1 change: 1 addition & 0 deletions Source/Chatbook/SendChat.wl
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,7 @@ $autoSettingKeyPriority := Enclose[
(* FIXME: need to hook into token pressure to gradually decrease limits *)
chooseMaxCellStringLength // beginDefinition;
chooseMaxCellStringLength[ as_Association ] := chooseMaxCellStringLength[ as, as[ "MaxContextTokens" ] ];
chooseMaxCellStringLength[ as_, Infinity ] := Infinity;
chooseMaxCellStringLength[ as_, tokens: $$size ] := Ceiling[ $defaultMaxCellStringLength * tokens / 2^13 ];
chooseMaxCellStringLength // endDefinition;

Expand Down