Skip to content

Commit

Permalink
Merge pull request #26 from RockChinQ/fix/gpt4free-long-response-trun…
Browse files Browse the repository at this point in the history
…cated

Fix: gpt4free msg truncated
  • Loading branch information
RockChinQ authored Oct 9, 2023
2 parents e1c9f54 + 6361500 commit 0d00b2b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 6 deletions.
6 changes: 4 additions & 2 deletions free_one_api/impls/adapter/gpt4free.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ async def _select_provider(self):
"role": "user",
"content": "Hi, My name is Rock."
}
]
],
timeout=120
)
async for _ in resp:
pass
Expand Down Expand Up @@ -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):
Expand Down
15 changes: 12 additions & 3 deletions free_one_api/impls/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,23 @@ class Application:

watchdog: wdmgr.AbsWatchDog

logging_level: int = logging.INFO

def __init__(
self,
dbmgr: db.DatabaseInterface,
router: routermgr.RouterManager,
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."""
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.")
Expand Down
3 changes: 3 additions & 0 deletions free_one_api/impls/forward/mgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion free_one_api/models/forward/mgr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down

0 comments on commit 0d00b2b

Please sign in to comment.