From 9199fee21df3bae396b7487199f9c747254dea6a Mon Sep 17 00:00:00 2001 From: Abi Raja Date: Tue, 17 Sep 2024 15:56:35 +0200 Subject: [PATCH] keep generating even if one of the models fails --- backend/routes/generate_code.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/backend/routes/generate_code.py b/backend/routes/generate_code.py index 1d9364a..e454ad0 100644 --- a/backend/routes/generate_code.py +++ b/backend/routes/generate_code.py @@ -312,7 +312,23 @@ async def stream_code(websocket: WebSocket): ) ) - completions = await asyncio.gather(*tasks) + # Run the models in parallel and capture exceptions if any + completions = await asyncio.gather(*tasks, return_exceptions=True) + + # If all generations failed, throw an error + all_generations_failed = all( + isinstance(completion, Exception) for completion in completions + ) + if all_generations_failed: + await throw_error("Error generating code. Please contact support.") + raise Exception("All generations failed") + + # If some completions failed, replace them with empty strings + for index, completion in enumerate(completions): + if isinstance(completion, Exception): + completions[index] = "" + print("Generation failed for variant", index) + print("Models used for generation: ", variant_models) except openai.AuthenticationError as e: