Skip to content

Commit

Permalink
Merge pull request #1039 from WolframResearch/989-gateway-timeout-err…
Browse files Browse the repository at this point in the history
…ors-504-need-error-handling

Bugfix: Added handling and messages for gateway timeout errors
  • Loading branch information
rhennigan authored Jan 21, 2025
2 parents cf1d065 + e19386b commit 003c68a
Showing 1 changed file with 35 additions and 5 deletions.
40 changes: 35 additions & 5 deletions Source/Chatbook/LLMUtilities.wl
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,13 @@ llmSynthesize0[ prompt: $$llmPrompt, evaluator_Association, attempt_ ] := Enclos
task = llmSynthesizeSubmit[ prompt, evaluator, callback ];
If[ FailureQ @ task, throwFailureToChatOutput @ task ];
TaskWait @ ConfirmMatch[ task, _TaskObject, "Task" ];
If[ MatchQ[ result, Failure[ "InvalidResponse", _ ] ] && attempt <= 3,

If[ MatchQ[ result, Failure[ "InvalidResponse", _ ] ]
,
If[ attempt > 3, throwFailureToChatOutput @ result ];
Pause[ Exp @ attempt / E ];
llmSynthesize0[ prompt, evaluator, attempt + 1 ],
llmSynthesize0[ prompt, evaluator, attempt + 1 ]
,
result
]
],
Expand Down Expand Up @@ -108,7 +112,7 @@ llmSynthesizeSubmit[ prompt0: $$llmPrompt, evaluator0_Association, callback_ ] :
FailureQ @ strings,
callback[ strings, #1 ],
True,
callback[ Failure[ "InvalidResponse", <| "Data" -> data |> ], #1 ]
callback[ makeInvalidResponseFailure @ data, #1 ]
]
]
]
Expand All @@ -132,13 +136,39 @@ llmSynthesizeSubmit[ prompt0: $$llmPrompt, evaluator0_Association, callback_ ] :

llmSynthesizeSubmit // endDefinition;

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

makeInvalidResponseFailure[ data_List ] /; MemberQ[ data, KeyValuePattern[ "StatusCode" -> 504 ] ] := Failure[
"InvalidResponse",
<|
"Message" -> "The server is currently unavailable (504 Gateway Time-out). Please try again later.",
"Data" -> data
|>
];

makeInvalidResponseFailure[ data_List ] := Failure[
"InvalidResponse",
<|
"Message" -> "The server returned an invalid response. Please try again later.",
"Data" -> data
|>
];

makeInvalidResponseFailure // endDefinition;

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

truncatePrompt[ string_String, evaluator_ ] := stringTrimMiddle[ string, modelContextLimit @ evaluator ];
truncatePrompt[ { strings___String }, evaluator_ ] := truncatePrompt[ StringJoin @ strings, evaluator ];
truncatePrompt[ string_String, evaluator_ ] :=
stringTrimMiddle[ string, modelContextLimit @ evaluator ];

truncatePrompt[ { strings___String }, evaluator_ ] :=
truncatePrompt[ StringJoin @ strings, evaluator ];

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

0 comments on commit 003c68a

Please sign in to comment.