fix(Git): 处理大模型返回结果之后 电脑崩溃了
This commit is contained in:
parent
ca645d4c24
commit
4576f28b22
46
assistant.py
46
assistant.py
|
|
@ -242,32 +242,50 @@ class Assistant:
|
||||||
json=jsonParam,
|
json=jsonParam,
|
||||||
headers=OLLAMA_REST_HEADERS,
|
headers=OLLAMA_REST_HEADERS,
|
||||||
stream=True,
|
stream=True,
|
||||||
timeout=30) # Increase the timeout value
|
timeout=30)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
|
|
||||||
full_response = ""
|
full_response = ""
|
||||||
for line in response.iter_lines():
|
for line in response.iter_lines():
|
||||||
body = json.loads(line)
|
if not line:
|
||||||
token = body.get('response', '')
|
continue
|
||||||
full_response += token
|
|
||||||
|
try:
|
||||||
|
body = json.loads(line)
|
||||||
|
token = body.get('response', '')
|
||||||
|
full_response += token
|
||||||
|
|
||||||
if 'error' in body:
|
if 'error' in body:
|
||||||
logging.error(f"Error from OLLaMa: {body['error']}")
|
logging.error(f"Error from OLLaMa: {body['error']}")
|
||||||
responseCallback("Error: " + body['error'])
|
responseCallback("Error: " + body['error'])
|
||||||
return
|
return
|
||||||
|
|
||||||
if body.get('done', False) and 'context' in body:
|
if body.get('done', False) and 'context' in body:
|
||||||
self.context = body['context']
|
self.context = body['context']
|
||||||
break
|
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:
|
except requests.exceptions.ReadTimeout as e:
|
||||||
logging.error(f"ReadTimeout occurred while asking OLLaMa: {str(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:
|
except requests.exceptions.RequestException as e:
|
||||||
logging.error(f"An error occurred while asking OLLaMa: {str(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):
|
async def edge_tts_speak(self, text):
|
||||||
try:
|
try:
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user