From ea4dfebb8bf2d028220ce30de548a3cc0c5679f3 Mon Sep 17 00:00:00 2001 From: fjganan14 Date: Sat, 9 Mar 2024 11:09:45 +0100 Subject: [PATCH 1/3] pinned openai version 0.28 for ChatCompletion function usage --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index f0dd0ae..360d2ed 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1 @@ -openai \ No newline at end of file +openai==0.28 \ No newline at end of file From 9e08c395f7a0bfc27d9a1872d138f6042d9347dd Mon Sep 17 00:00:00 2001 From: fjganan14 Date: Sat, 9 Mar 2024 11:11:17 +0100 Subject: [PATCH 2/3] Modified timeout execution to be perform in a thread rather than in a process. --- LLMCode/utils/completion.py | 59 +++++++++++++++++++------------------ 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/LLMCode/utils/completion.py b/LLMCode/utils/completion.py index 55664fe..6165120 100644 --- a/LLMCode/utils/completion.py +++ b/LLMCode/utils/completion.py @@ -1,39 +1,40 @@ -import multiprocessing +import threading import openai import LLMCode.cfg.completion_params as completion_params from . import ANSI_CODE from .logger import LOGGER -def run_with_timeout(target_function, args=None, timeout=30): - pool = multiprocessing.Pool(processes=1) - if args is None: - result = pool.apply_async(target_function) - else: - result = pool.apply_async(target_function, args) - try: - result_value = result.get(timeout=timeout) - return result_value - except Exception as e: - if isinstance(e, multiprocessing.TimeoutError): - LOGGER.info( - "%s\r⚠ The completion could not be done. %s response lasted more than %s seconds, which is the limit.", - ANSI_CODE["yellow"], - target_function, - timeout, - ) - else: - LOGGER.info( - "%s\r⚠ The completion could not be done. %s raised the exception %s", - ANSI_CODE["yellow"], - target_function, - e, - ) - pool.terminate() +def run_with_timeout(target_function, args=(), kwargs={}, timeout=30): + result = [None] # A mutable container to store the function result + exception = [None] # A container to capture exceptions + + def target_wrapper(): + try: + result[0] = target_function(*args, **kwargs) + except Exception as e: + exception[0] = e + + thread = threading.Thread(target=target_wrapper) + thread.start() + thread.join(timeout) + if thread.is_alive(): + LOGGER.info( + "%s⚠ The completion could not be done. %s response lasted more than %s seconds, which is the limit.", + ANSI_CODE["yellow"], + target_function.__name__, + timeout, + ) + return None + if exception[0]: + LOGGER.info( + "%s⚠ The completion could not be done. %s raised the exception %s", + ANSI_CODE["yellow"], + target_function.__name__, + exception[0], + ) return None - finally: - pool.close() - pool.join() + return result[0] # Completion using openai API From e2bf4effb86df87ed204fcee985c7f372041451d Mon Sep 17 00:00:00 2001 From: fjganan14 Date: Sat, 9 Mar 2024 11:11:49 +0100 Subject: [PATCH 3/3] Added utf-8 encoding to write the python file for windows compatibility --- LLMCode/utils/document.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LLMCode/utils/document.py b/LLMCode/utils/document.py index 18dbeec..f0d33fe 100644 --- a/LLMCode/utils/document.py +++ b/LLMCode/utils/document.py @@ -137,5 +137,5 @@ def add_msg(msg, where, element, script_content): script_content, ) pass - with open(script, "w") as python_file: + with open(script, "w", encoding="utf-8") as python_file: python_file.write(script_content)