From 96c4a13aa58944bc8b39b1a3717f9f17c303f85f Mon Sep 17 00:00:00 2001 From: lasseedfast <> Date: Thu, 17 Oct 2024 14:41:56 +0200 Subject: [PATCH] Refactor _llm.py to handle UnicodeDecodeError when decoding chunks and add timeout to LLM API request --- _llm.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/_llm.py b/_llm.py index 42108f4..a698f71 100644 --- a/_llm.py +++ b/_llm.py @@ -37,7 +37,10 @@ class LLM: buffer = "" for chunk in response.iter_content(chunk_size=64): if chunk: - buffer += chunk.decode('utf-8') + try: + buffer += chunk.decode('utf-8') + except UnicodeDecodeError: + continue while "\n" in buffer: line, buffer = buffer.split("\n", 1) if line: @@ -74,6 +77,7 @@ class LLM: json=data, auth=HTTPBasicAuth(os.getenv('LLM_API_USER'), os.getenv('LLM_API_PWD_LASSE')), stream=stream, + timeout= 3600, ) if response.status_code == 404: