Skip to content

Commit

Permalink
Add some additional error handling to the `"ForceSynchronous" -> True…
Browse files Browse the repository at this point in the history
…` scenario
  • Loading branch information
rhennigan committed Sep 30, 2024
1 parent 09b353c commit 31e205e
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions Source/Chatbook/SendChat.wl
Original file line number Diff line number Diff line change
Expand Up @@ -547,24 +547,38 @@ chatSubmit0 // beginDefinition;
chatSubmit0 // Attributes = { HoldFirst };

(* Currently used for o1 models since they don't support streaming: *)
chatSubmit0[ container_, messages: { __Association }, cellObject_, settings_ ] /; settings[ "ForceSynchronous" ] :=
chatSubmit0[
container_,
messages: { __Association },
cellObject_,
settings_
] /; settings[ "ForceSynchronous" ] := Enclose[
Module[ { auth, stop, result },
auth = settings[ "Authentication" ];
stop = makeStopTokens @ settings;

result = LLMServices`Chat[
standardizeMessageKeys @ messages,
makeLLMConfiguration @ settings,
Authentication -> auth
result = ConfirmMatch[
LLMServices`Chat[
standardizeMessageKeys @ messages,
makeLLMConfiguration @ settings,
Authentication -> auth
],
_Association | _Failure,
"ChatResult"
];

If[ FailureQ @ result, throwTop @ writeErrorCell[ cellObject, result ] ];

writeChunk[ Dynamic @ container, cellObject, <| "BodyChunkProcessed" -> result[ "Content" ] |> ];
logUsage @ container;
trimStopTokens[ container, stop ];
checkResponse[ settings, Unevaluated @ container, cellObject, <| |> ];

(* This cannot return a TaskObject: *)
None
];
],
throwInternalFailure
];

chatSubmit0[ container_, messages: { __Association }, cellObject_, settings_ ] := Quiet[
Needs[ "LLMServices`" -> None ];
Expand Down

0 comments on commit 31e205e

Please sign in to comment.