fix(Git): 处理大模型返回结果之后 电脑崩溃了
This commit is contained in:
parent
ca645d4c24
commit
4576f28b22
24
assistant.py
24
assistant.py
|
|
@ -242,11 +242,15 @@ 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():
|
||||
if not line:
|
||||
continue
|
||||
|
||||
try:
|
||||
body = json.loads(line)
|
||||
token = body.get('response', '')
|
||||
full_response += token
|
||||
|
|
@ -259,15 +263,29 @@ class Assistant:
|
|||
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
|
||||
|
||||
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:
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user