From 4576f28b221b26101ab8a5c6257d67f0b0b9a541 Mon Sep 17 00:00:00 2001 From: liubing1 Date: Wed, 22 Jan 2025 13:05:54 +0800 Subject: [PATCH] =?UTF-8?q?fix(Git):=20=E5=A4=84=E7=90=86=E5=A4=A7?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E8=BF=94=E5=9B=9E=E7=BB=93=E6=9E=9C=E4=B9=8B?= =?UTF-8?q?=E5=90=8E=20=E7=94=B5=E8=84=91=E5=B4=A9=E6=BA=83=E4=BA=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assistant.py | 46 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/assistant.py b/assistant.py index 607f650..bd0decc 100644 --- a/assistant.py +++ b/assistant.py @@ -242,32 +242,50 @@ class Assistant: json=jsonParam, headers=OLLAMA_REST_HEADERS, stream=True, - timeout=30) # Increase the timeout value + timeout=30) response.raise_for_status() full_response = "" for line in response.iter_lines(): - body = json.loads(line) - token = body.get('response', '') - full_response += token + if not line: + continue + + try: + body = json.loads(line) + token = body.get('response', '') + full_response += token - if 'error' in body: - logging.error(f"Error from OLLaMa: {body['error']}") - responseCallback("Error: " + body['error']) - return + if 'error' in body: + logging.error(f"Error from OLLaMa: {body['error']}") + responseCallback("Error: " + body['error']) + return - if body.get('done', False) and 'context' in body: - self.context = body['context'] - break + if body.get('done', False) and 'context' in body: + self.context = body['context'] + break + except json.JSONDecodeError as e: + logging.error(f"Failed to decode JSON response: {str(e)}") + continue - responseCallback(full_response.strip()) + if full_response.strip(): + try: + responseCallback(full_response.strip()) + except Exception as e: + logging.error(f"Error in response callback: {str(e)}") + self.display_message("Error processing response") + else: + logging.warning("Received empty response from OLLaMa") + self.display_message("Received empty response") except requests.exceptions.ReadTimeout as e: logging.error(f"ReadTimeout occurred while asking OLLaMa: {str(e)}") - responseCallback("Sorry, the request timed out. Please try again.") + self.display_message("Request timed out. Please try again.") except requests.exceptions.RequestException as e: logging.error(f"An error occurred while asking OLLaMa: {str(e)}") - responseCallback("Sorry, an error occurred. Please try again.") + self.display_message("Connection error. Please try again.") + except Exception as e: + logging.error(f"Unexpected error in ask_ollama: {str(e)}") + self.display_message("An unexpected error occurred") async def edge_tts_speak(self, text): try: