Skip to content

Commit

Permalink
Bugfix: Need to allow empty responses when starting with a stop token
Browse files Browse the repository at this point in the history
  • Loading branch information
rhennigan committed Jan 13, 2025
1 parent 0d74442 commit 138b766
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions Source/Chatbook/LLMUtilities.wl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ llmSynthesize[ prompt: $$llmPrompt ] :=
llmSynthesize[ prompt, <| |> ];

llmSynthesize[ prompt: $$llmPrompt, evaluator_Association ] := Enclose[
ConfirmMatch[ llmSynthesize0[ prompt, evaluator, 1 ], Except[ "", _String ], "Result" ],
ConfirmMatch[
llmSynthesize0[ prompt, evaluator, 1 ],
If[ MatchQ[ Flatten @ { evaluator[ "StopTokens" ] }, { __String } ], _String, Except[ "", _String ] ],
"Result"
],
throwInternalFailure
];

Expand Down Expand Up @@ -70,7 +74,7 @@ llmSynthesizeSubmit[ prompt: $$llmPrompt, callback_ ] :=
llmSynthesizeSubmit[ prompt, <| |>, callback ];

llmSynthesizeSubmit[ prompt0: $$llmPrompt, evaluator0_Association, callback_ ] := Enclose[
Module[ { evaluator, prompt, messages, config, chunks, handlers, keys, auth },
Module[ { evaluator, prompt, messages, config, chunks, allowEmpty, handlers, keys, auth },

evaluator = Replace[
ConfirmBy[
Expand All @@ -87,6 +91,8 @@ llmSynthesizeSubmit[ prompt0: $$llmPrompt, evaluator0_Association, callback_ ] :
config = LLMConfiguration @ evaluator;
chunks = Internal`Bag[ ];

allowEmpty = MatchQ[ Flatten @ { evaluator[ "StopTokens" ] }, { __String } ];

handlers = <|
"BodyChunkReceived" -> Function[
Internal`StuffBag[ chunks, # ]
Expand All @@ -97,7 +103,7 @@ llmSynthesizeSubmit[ prompt0: $$llmPrompt, evaluator0_Association, callback_ ] :
$lastSynthesizeSubmitLog = data;
strings = extractBodyChunks @ data;
Which[
MatchQ[ strings, { __String } ],
MatchQ[ strings, { __String } ] || (allowEmpty && strings === { }),
With[ { s = StringJoin @ strings }, callback[ s, #1 ] ],
FailureQ @ strings,
callback[ strings, #1 ],
Expand Down

0 comments on commit 138b766

Please sign in to comment.