Skip to content

Commit ce102e7

Browse files
authored
fix(nc_texttotext): retries and graceful error handling for schedule (#179)
fix: richer error message for embedding server start fail --------- Signed-off-by: Anupam Kumar <kyteinsky@gmail.com>
1 parent 006755f commit ce102e7

File tree

2 files changed

+38
-9
lines changed

2 files changed

+38
-9
lines changed

context_chat_backend/dyn_loader.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def load(self):
7171
)
7272
pid.value = proc.pid
7373

74+
last_resp, last_exc = None, None
7475
# poll for heartbeat
7576
try_ = 0
7677
while try_ < 20:
@@ -84,12 +85,17 @@ def load(self):
8485
)
8586
if response.status_code == 200:
8687
return
87-
except Exception:
88+
last_resp = response
89+
except Exception as e:
90+
last_exc = e
8891
logger.debug(f'Try {try_} failed in exception')
8992
try_ += 1
9093
sleep(3)
9194

92-
raise EmbeddingException('Error: the embedding server is not responding')
95+
raise EmbeddingException(
96+
'Error: the embedding server is not responding or could not be started. '
97+
+ (f'\nLast error: {last_resp.status_code} {last_resp.text}' if last_resp else '')
98+
) from last_exc
9399

94100
def offload(self):
95101
global pid

context_chat_backend/models/nc_texttotext.py

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,34 @@ def _call(
6969
else:
7070
logger.warning('No user ID provided for Nextcloud TextToText provider')
7171

72-
response = nc.ocs(
73-
'POST',
74-
'/ocs/v1.php/taskprocessing/schedule',
75-
json={'type': 'core:text2text', 'appId': 'context_chat_backend', 'input': {'input': prompt}},
76-
)
72+
sched_tries = 3
73+
while True:
74+
try:
75+
sched_tries -= 1
76+
if sched_tries <= 0:
77+
raise LlmException('Failed to schedule Nextcloud TaskProcessing task, tried 3 times')
78+
79+
response = nc.ocs(
80+
'POST',
81+
'/ocs/v1.php/taskprocessing/schedule',
82+
json={'type': 'core:text2text', 'appId': 'context_chat_backend', 'input': {'input': prompt}},
83+
)
84+
break
85+
except NextcloudException as e:
86+
if e.status_code == httpx.codes.PRECONDITION_FAILED:
87+
raise LlmException(
88+
'Failed to schedule Nextcloud TaskProcessing task: '
89+
'This app is setup to use a text generation provider in Nextcloud. '
90+
'No such provider is installed on Nextcloud instance. '
91+
'Please install integration_openai, llm2 or any other text2text provider.'
92+
) from e
93+
94+
if e.status_code == httpx.codes.TOO_MANY_REQUESTS:
95+
logger.warning('Rate limited during task scheduling, waiting 10s before retrying')
96+
time.sleep(10)
97+
continue
98+
99+
raise LlmException('Failed to schedule Nextcloud TaskProcessing task') from e
77100

78101
try:
79102
task = Response.model_validate(response).task
@@ -103,8 +126,8 @@ def _call(
103126
i += 1
104127
continue
105128
except NextcloudException as e:
106-
if e.status_code == 429:
107-
logger.warning('Rate limited during task polling, waiting 10s more')
129+
if e.status_code == httpx.codes.TOO_MANY_REQUESTS:
130+
logger.warning('Rate limited during task polling, waiting 10s before retrying')
108131
time.sleep(10)
109132
i += 2
110133
continue

0 commit comments

Comments
 (0)