Skip to content

Commit e19386b

Browse files
committed
Bugfix: Added handling and messages for gateway timeout errors
1 parent cf1d065 commit e19386b

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

Source/Chatbook/LLMUtilities.wl

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,13 @@ llmSynthesize0[ prompt: $$llmPrompt, evaluator_Association, attempt_ ] := Enclos
5454
task = llmSynthesizeSubmit[ prompt, evaluator, callback ];
5555
If[ FailureQ @ task, throwFailureToChatOutput @ task ];
5656
TaskWait @ ConfirmMatch[ task, _TaskObject, "Task" ];
57-
If[ MatchQ[ result, Failure[ "InvalidResponse", _ ] ] && attempt <= 3,
57+
58+
If[ MatchQ[ result, Failure[ "InvalidResponse", _ ] ]
59+
,
60+
If[ attempt > 3, throwFailureToChatOutput @ result ];
5861
Pause[ Exp @ attempt / E ];
59-
llmSynthesize0[ prompt, evaluator, attempt + 1 ],
62+
llmSynthesize0[ prompt, evaluator, attempt + 1 ]
63+
,
6064
result
6165
]
6266
],
@@ -108,7 +112,7 @@ llmSynthesizeSubmit[ prompt0: $$llmPrompt, evaluator0_Association, callback_ ] :
108112
FailureQ @ strings,
109113
callback[ strings, #1 ],
110114
True,
111-
callback[ Failure[ "InvalidResponse", <| "Data" -> data |> ], #1 ]
115+
callback[ makeInvalidResponseFailure @ data, #1 ]
112116
]
113117
]
114118
]
@@ -132,13 +136,39 @@ llmSynthesizeSubmit[ prompt0: $$llmPrompt, evaluator0_Association, callback_ ] :
132136

133137
llmSynthesizeSubmit // endDefinition;
134138

139+
(* ::**************************************************************************************************************:: *)
140+
(* ::Subsubsection::Closed:: *)
141+
(*makeInvalidResponseFailure*)
142+
makeInvalidResponseFailure // beginDefinition;
143+
144+
makeInvalidResponseFailure[ data_List ] /; MemberQ[ data, KeyValuePattern[ "StatusCode" -> 504 ] ] := Failure[
145+
"InvalidResponse",
146+
<|
147+
"Message" -> "The server is currently unavailable (504 Gateway Time-out). Please try again later.",
148+
"Data" -> data
149+
|>
150+
];
151+
152+
makeInvalidResponseFailure[ data_List ] := Failure[
153+
"InvalidResponse",
154+
<|
155+
"Message" -> "The server returned an invalid response. Please try again later.",
156+
"Data" -> data
157+
|>
158+
];
159+
160+
makeInvalidResponseFailure // endDefinition;
161+
135162
(* ::**************************************************************************************************************:: *)
136163
(* ::Subsubsection::Closed:: *)
137164
(*truncatePrompt*)
138165
truncatePrompt // beginDefinition;
139166

140-
truncatePrompt[ string_String, evaluator_ ] := stringTrimMiddle[ string, modelContextLimit @ evaluator ];
141-
truncatePrompt[ { strings___String }, evaluator_ ] := truncatePrompt[ StringJoin @ strings, evaluator ];
167+
truncatePrompt[ string_String, evaluator_ ] :=
168+
stringTrimMiddle[ string, modelContextLimit @ evaluator ];
169+
170+
truncatePrompt[ { strings___String }, evaluator_ ] :=
171+
truncatePrompt[ StringJoin @ strings, evaluator ];
142172

143173
truncatePrompt[ prompts: { ($$string|$$graphics).. }, evaluator_ ] := Enclose[
144174
Module[ { stringCount, images, imageCount, budget, imageBudget, resized, imageTokens, stringBudget },

0 commit comments

Comments
 (0)