From 46b7da6b5d02a9291da42c887d45a8f5475fa10e Mon Sep 17 00:00:00 2001 From: Anupam Kumar Date: Mon, 5 May 2025 17:38:07 +0530 Subject: [PATCH 1/2] fix(nc_texttotext): retries and grace error handling for text2text task scheduling Signed-off-by: Anupam Kumar --- context_chat_backend/models/nc_texttotext.py | 37 ++++++++++++++++---- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/context_chat_backend/models/nc_texttotext.py b/context_chat_backend/models/nc_texttotext.py index 4babd21..6321517 100644 --- a/context_chat_backend/models/nc_texttotext.py +++ b/context_chat_backend/models/nc_texttotext.py @@ -69,11 +69,34 @@ def _call( else: logger.warning('No user ID provided for Nextcloud TextToText provider') - response = nc.ocs( - 'POST', - '/ocs/v1.php/taskprocessing/schedule', - json={'type': 'core:text2text', 'appId': 'context_chat_backend', 'input': {'input': prompt}}, - ) + sched_tries = 3 + while True: + try: + sched_tries -= 1 + if sched_tries <= 0: + raise LlmException('Failed to schedule Nextcloud TaskProcessing task, tried 3 times') + + response = nc.ocs( + 'POST', + '/ocs/v1.php/taskprocessing/schedule', + json={'type': 'core:text2text', 'appId': 'context_chat_backend', 'input': {'input': prompt}}, + ) + break + except NextcloudException as e: + if e.status_code == httpx.codes.PRECONDITION_FAILED: + raise LlmException( + 'Failed to schedule Nextcloud TaskProcessing task: ' + 'This app is setup to use a text generation provider in Nextcloud. ' + 'No such provider is installed on Nextcloud instance. ' + 'Please install integration_openai, llm2 or any other text2text provider.' + ) from e + + if e.status_code == httpx.codes.TOO_MANY_REQUESTS: + logger.warning('Rate limited during task scheduling, waiting 10s before retrying') + time.sleep(10) + continue + + raise LlmException('Failed to schedule Nextcloud TaskProcessing task') from e try: task = Response.model_validate(response).task @@ -103,8 +126,8 @@ def _call( i += 1 continue except NextcloudException as e: - if e.status_code == 429: - logger.warning('Rate limited during task polling, waiting 10s more') + if e.status_code == httpx.codes.TOO_MANY_REQUESTS: + logger.warning('Rate limited during task polling, waiting 10s before retrying') time.sleep(10) i += 2 continue From ea49e7bddea6dee3aa1b84c40e223ed1ba78cf27 Mon Sep 17 00:00:00 2001 From: Anupam Kumar Date: Thu, 8 May 2025 01:42:29 +0530 Subject: [PATCH 2/2] fix: richer error message for embedding server start fail Signed-off-by: Anupam Kumar --- context_chat_backend/dyn_loader.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/context_chat_backend/dyn_loader.py b/context_chat_backend/dyn_loader.py index f4d3833..274a402 100644 --- a/context_chat_backend/dyn_loader.py +++ b/context_chat_backend/dyn_loader.py @@ -71,6 +71,7 @@ def load(self): ) pid.value = proc.pid + last_resp, last_exc = None, None # poll for heartbeat try_ = 0 while try_ < 20: @@ -84,12 +85,17 @@ def load(self): ) if response.status_code == 200: return - except Exception: + last_resp = response + except Exception as e: + last_exc = e logger.debug(f'Try {try_} failed in exception') try_ += 1 sleep(3) - raise EmbeddingException('Error: the embedding server is not responding') + raise EmbeddingException( + 'Error: the embedding server is not responding or could not be started. ' + + (f'\nLast error: {last_resp.status_code} {last_resp.text}' if last_resp else '') + ) from last_exc def offload(self): global pid