-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#35 added safe asking mode that wraps the query into try-catch block.
- Loading branch information
Showing
2 changed files
with
27 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,35 @@ | ||
import logging | ||
import time | ||
|
||
from bulk_chain.core.utils import format_model_name | ||
|
||
|
||
class BaseLM(object): | ||
|
||
def __init__(self, name): | ||
def __init__(self, name, attempts=None, delay_sec=1, enable_log=True): | ||
self.__name = name | ||
self.__attempts = 1 if attempts is None else attempts | ||
self.__delay_sec = delay_sec | ||
|
||
if enable_log: | ||
self.__logger = logging.getLogger(__name__) | ||
logging.basicConfig(level=logging.INFO) | ||
|
||
def ask_safe(self, prompt): | ||
|
||
for i in range(self.__attempts): | ||
try: | ||
response = self.ask(prompt) | ||
return response | ||
except: | ||
if self.__logger is not None: | ||
self.__logger.info("Unable to infer the result. Try {} out of {}.".format(i, self.__attempts)) | ||
time.sleep(self.__delay_sec) | ||
|
||
raise Exception("Can't infer") | ||
|
||
def ask(self, prompt): | ||
raise NotImplemented() | ||
|
||
def name(self): | ||
return format_model_name(self.__name) | ||
return format_model_name(self.__name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters