diff --git a/free_one_api/impls/adapter/gpt4free.py b/free_one_api/impls/adapter/gpt4free.py index c9bd6c4..55754e7 100644 --- a/free_one_api/impls/adapter/gpt4free.py +++ b/free_one_api/impls/adapter/gpt4free.py @@ -145,7 +145,8 @@ async def _select_provider(self): "role": "user", "content": "Hi, My name is Rock." } - ] + ], + timeout=120 ) async for _ in resp: pass @@ -194,7 +195,8 @@ async def query(self, req: request.Request) -> typing.AsyncGenerator[response.Re else: resp = provider.create_async_generator( model=req.model, - messages=req.messages + messages=req.messages, + timeout=120 ) if isinstance(resp, typing.Generator): diff --git a/free_one_api/impls/app.py b/free_one_api/impls/app.py index 0aad112..f4f9836 100644 --- a/free_one_api/impls/app.py +++ b/free_one_api/impls/app.py @@ -43,6 +43,8 @@ class Application: watchdog: wdmgr.AbsWatchDog + logging_level: int = logging.INFO + def __init__( self, dbmgr: db.DatabaseInterface, @@ -50,12 +52,14 @@ def __init__( channel: chanmgr.AbsChannelManager, key: keymgr.AbsAPIKeyManager, watchdog: wdmgr.AbsWatchDog, + logging_level: int = logging.INFO, ): self.dbmgr = dbmgr self.router = router self.channel = channel self.key = key self.watchdog = watchdog + self.logging_level = logging_level async def run(self): """Run application.""" @@ -124,12 +128,15 @@ async def make_application(config_path: str) -> Application: if 'DEBUG' in os.environ and os.environ['DEBUG'] == 'true': logging_level = logging.DEBUG - + + print("Logging level:", logging_level) + logging.debug("Debug mode enabled.") + terminal_out = logging.StreamHandler() terminal_out.setLevel(logging_level) terminal_out.setFormatter(colorlog.ColoredFormatter( - "[%(asctime)s.%(msecs)03d] %(log_color)s%(filename)s (%(lineno)d) - [%(levelname)s] : " + "[%(asctime)s.%(msecs)03d] %(log_color)s%(pathname)s (%(lineno)d) - [%(levelname)s] :\n" "%(message)s", datefmt="%Y-%m-%d %H:%M:%S", log_colors=log_colors_config, @@ -163,7 +170,8 @@ async def make_application(config_path: str) -> Application: # database handler dblogger = log.SQLiteHandler(dbmgr) - dblogger.setLevel(logging_level) + # failed to set debug level for db handler + dblogger.setLevel(logging.INFO if logging_level <= logging.INFO else logging_level) dblogger.setFormatter(logging.Formatter("[%(asctime)s.%(msecs)03d] %(pathname)s (%(lineno)d) - [%(levelname)s] :\n%(message)s")) logging.getLogger().addHandler(dblogger) @@ -233,6 +241,7 @@ async def make_application(config_path: str) -> Application: channel=channelmgr, key=apikeymgr, watchdog=wdmgr, + logging_level=logging_level, ) logging.info("Application initialized.") diff --git a/free_one_api/impls/forward/mgr.py b/free_one_api/impls/forward/mgr.py index 2b09f95..b3f2f9e 100644 --- a/free_one_api/impls/forward/mgr.py +++ b/free_one_api/impls/forward/mgr.py @@ -76,6 +76,9 @@ async def _gen(): "param": e.param, } })) + except Exception as e: + logging.warning("Exception should be processed by adapter but caught by forward manager:") + logging.error(e) spent_ms = int((time.time() - before)*1000) diff --git a/free_one_api/models/forward/mgr.py b/free_one_api/models/forward/mgr.py index 4b7f553..a5ee589 100644 --- a/free_one_api/models/forward/mgr.py +++ b/free_one_api/models/forward/mgr.py @@ -29,7 +29,7 @@ class AbsForwardManager(metaclass=abc.ABCMeta): """API key manager.""" @abc.abstractmethod - def query( + async def query( self, path: str, req: request.Request, diff --git a/main.py b/main.py index c1e5dae..66a415e 100644 --- a/main.py +++ b/main.py @@ -15,6 +15,7 @@ def main(): application = loop.run_until_complete(app.make_application("./data/config.yaml")) + logging.getLogger().setLevel(application.logging_level) loop.run_until_complete(application.run()) if __name__ == "__main__":